-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.c
394 lines (329 loc) · 14.6 KB
/
receiver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "receiver.h"
#define NDEBUG
#include <assert.h>
void init_receiver(Receiver * receiver, int id)
{
pthread_cond_init(&receiver->buffer_cv, NULL);
pthread_mutex_init(&receiver->buffer_mutex, NULL);
receiver->recv_id = id;
receiver->cur_send_id = 0;
receiver->input_framelist_head = NULL;
receiver->LastFrameReceived = 0 - 1;
receiver->SwpWindow = 0;
memset(receiver->framesInWindow, (unsigned char) 0,
sizeof(Frame) * SWP_WINDOW_SIZE - 1);
receiver->preMsgHasSubsequent = 0;
memset(receiver->hasSavedSwpState, 0, MAX_COM_ID);
receiver->longMsgBufferSize = 0;;
receiver->longMsgBuffer = NULL;
}
void destroy_receiver(Receiver * receiver)
{
int i;
for(i = 0; i < MAX_COM_ID; i++)
if(receiver->hasSavedSwpState[i])
{
//still has unfinished long msg
if(receiver->SavedSwpStates[i]->preMsgHasSubsequent)
free(receiver->SavedSwpStates[i]->longMsgBuffer);
free(receiver->SavedSwpStates[i]);
}
}
/*
struct ReceiverSwpState_t
{
SwpSeqNo LastFrameReceived;
uint8_t SwpWindow;
Frame framesInWindow[SWP_WINDOW_SIZE - 1];
uint8_t preMsgHasSubsequent;
};
*/
//switch the swp state for another sender
void switchSender(Receiver *receiver, uint16_t cur_send_id, uint16_t new_send_id)
{
//save state for current receiver
if(receiver->hasSavedSwpState[cur_send_id] == 0)
receiver->SavedSwpStates[cur_send_id] = (ReceiverSwpState*) malloc(sizeof(ReceiverSwpState));
receiver->SavedSwpStates[cur_send_id]->LastFrameReceived = receiver->LastFrameReceived;
receiver->SavedSwpStates[cur_send_id]->SwpWindow = receiver->SwpWindow;
memcpy(receiver->SavedSwpStates[cur_send_id]->framesInWindow, receiver->framesInWindow,
sizeof(Frame) * (SWP_WINDOW_SIZE - 1));
receiver->SavedSwpStates[cur_send_id]->preMsgHasSubsequent = receiver->preMsgHasSubsequent;
receiver->SavedSwpStates[cur_send_id]->longMsgBufferSize = receiver->longMsgBufferSize;
memcpy(receiver->SavedSwpStates[cur_send_id]->longMsgBuffer,
receiver->longMsgBuffer, receiver->longMsgBufferSize * sizeof(char));
receiver->hasSavedSwpState[cur_send_id] = 1;
//if has saved state
if(receiver->hasSavedSwpState[new_send_id] == 1)
{
receiver->LastFrameReceived = receiver->SavedSwpStates[new_send_id]->LastFrameReceived;
receiver->SwpWindow = receiver->SavedSwpStates[new_send_id]->SwpWindow;
memcpy(receiver->framesInWindow, receiver->SavedSwpStates[new_send_id]->framesInWindow,
sizeof(Frame) * (SWP_WINDOW_SIZE - 1));
receiver->preMsgHasSubsequent = receiver->SavedSwpStates[new_send_id]->preMsgHasSubsequent;
receiver->longMsgBufferSize = receiver->SavedSwpStates[new_send_id]->longMsgBufferSize;
memcpy(receiver->longMsgBuffer, receiver->SavedSwpStates[new_send_id]->longMsgBuffer,
receiver->SavedSwpStates[new_send_id]->longMsgBufferSize);
}
//new init state
else
{
receiver->LastFrameReceived = 0 - 1;
receiver->SwpWindow = 0;
memset(receiver->framesInWindow, (unsigned char) 0, sizeof(Frame) * (SWP_WINDOW_SIZE - 1));
receiver->preMsgHasSubsequent = 0;
receiver->longMsgBufferSize = 0;;
receiver->longMsgBuffer = NULL;
}
receiver->cur_send_id = new_send_id;
}
void print_msg(Receiver *receiver, Frame *inframe)
{
//has subsequent
if(receiver->preMsgHasSubsequent)
{
// in long msg
if((inframe->flag[0] & (1 << 7)) == (1 << 7))
{
//printf("%s", inframe->data);
receiver->longMsgBuffer = (char*) realloc(receiver->longMsgBuffer, receiver->longMsgBufferSize + FRAME_PAYLOAD_SIZE);
memcpy(receiver->longMsgBuffer + receiver->longMsgBufferSize,
inframe->data, sizeof(char) * FRAME_PAYLOAD_SIZE);
receiver->longMsgBufferSize += FRAME_PAYLOAD_SIZE;
receiver->preMsgHasSubsequent = 1;
}
// long msg end
else
{
receiver->longMsgBuffer = (char*) realloc(receiver->longMsgBuffer, receiver->longMsgBufferSize + FRAME_PAYLOAD_SIZE);
memcpy(receiver->longMsgBuffer + receiver->longMsgBufferSize,
inframe->data, sizeof(char) * strlen(inframe->data) + 1);
receiver->longMsgBufferSize += sizeof(char) * strlen(inframe->data);
//print the msg as a single long msg
/*
printf("<RECV_%d>:[%s]\n", receiver->recv_id, receiver->longMsgBuffer);
*/
//Partitioning
int offset = 0;
while(receiver->longMsgBufferSize >= FRAME_PAYLOAD_SIZE)
{
printf("<RECV_%d>:[%.*s]\n", receiver->recv_id,
FRAME_PAYLOAD_SIZE, receiver->longMsgBuffer + offset);
offset += FRAME_PAYLOAD_SIZE;
receiver->longMsgBufferSize -= FRAME_PAYLOAD_SIZE;
}
if(receiver->longMsgBufferSize != 0)
printf("<RECV_%d>:[%s]\n", receiver->recv_id,
receiver->longMsgBuffer + offset);
receiver->preMsgHasSubsequent = 0;
receiver->longMsgBufferSize = 0;
free(receiver->longMsgBuffer);
}
}
// long msg begin
else if((inframe->flag[0] & (1 << 7)) == (1 << 7))
{
//printf("<RECV_%d>:[%s", receiver->recv_id, inframe->data);
receiver->preMsgHasSubsequent = 1;
receiver->longMsgBufferSize = FRAME_PAYLOAD_SIZE;
receiver->longMsgBuffer = (char*) malloc(sizeof(char) * FRAME_PAYLOAD_SIZE);
memcpy(receiver->longMsgBuffer, inframe->data, sizeof(char) * FRAME_PAYLOAD_SIZE);
}
//short msg
else
printf("<RECV_%d>:[%s]\n", receiver->recv_id, inframe->data);
}
void handle_incoming_msgs(Receiver * receiver,
LLnode ** outgoing_frames_head_ptr)
{
//TODO: Suggested steps for handling incoming frames
// 1) Dequeue the Frame from the sender->input_framelist_head
// 2) Convert the char * buffer to a Frame data type
// 3) Check whether the frame is corrupted
// 4) Check whether the frame is for this receiver
// 5) Do sliding window protocol for sender/receiver pair
int incoming_msgs_length = ll_get_length(receiver->input_framelist_head);
while (incoming_msgs_length > 0)
{
//Pop a node off the front of the link list and update the count
LLnode * ll_inmsg_node = ll_pop_node(&receiver->input_framelist_head);
incoming_msgs_length = ll_get_length(receiver->input_framelist_head);
//DUMMY CODE: Print the raw_char_buf
//NOTE: You should not blindly print messages!
// Ask yourself: Is this message really for me?
// Is this message corrupted?
// Is this an old, retransmitted message?
char * raw_char_buf = (char *) ll_inmsg_node->value;
Frame * inframe = convert_char_to_frame(raw_char_buf);
if(frameIsCorrupted(inframe) || inframe->recv_id != receiver->recv_id)
{
free(raw_char_buf);
free(inframe);
free(ll_inmsg_node);
continue;
}
//assert(inframe->send_id == 0);
//changed sender
if(inframe->send_id != receiver->cur_send_id)
{
//fprintf(stderr, "Receiver #%d, curSender = #%d, newSender = #%d, switching sender...\n\n",
// receiver->recv_id, receiver->cur_send_id, inframe->send_id);
switchSender(receiver, receiver->cur_send_id, inframe->send_id);
}
//not for this receiver / corrupted / SeqNo > Acceptable No
if((SwpSeqNo_minus(inframe->swpSeqNo, receiver->LastFrameReceived) > SWP_WINDOW_SIZE
&& (SwpSeqNo_minus(inframe->swpSeqNo, receiver->LastFrameReceived) < 128)))
{
free(raw_char_buf);
free(inframe);
free(ll_inmsg_node);
continue;
}
/*
fprintf(stderr, "Receiver %d receiving a frame: \n\t", receiver->recv_id);
printFrame(inframe);
fprintf(stderr, "\tSending ACK : %d\n", inframe->swpSeqNo);
fprintf(stderr, "\n");
*/
//Free raw_char_buf
free(raw_char_buf);
uint8_t sendAck = 0;
if(SwpSeqNo_minus(inframe->swpSeqNo, receiver->LastFrameReceived) == 0)
{
sendAck = 1;
while(0); // do nothing
}
//continuous seq no
else if(SwpSeqNo_minus(inframe->swpSeqNo, receiver->LastFrameReceived) == 1)
{
sendAck = 1;
receiver->LastFrameReceived += 1;
assert(frameIsCorrupted(inframe) == 0);
print_msg(receiver, inframe);
if((receiver->SwpWindow & (1 << 7)) == (1 << 7))
{
while((receiver->SwpWindow & (1 << 7)) == (1 << 7))
{
//fprintf(stderr, "in while @ receiver\n");
assert(frameIsCorrupted(receiver->framesInWindow) == 0);
//printf("<RECV_%d>:[%s]\n", receiver->recv_id, receiver->framesInWindow->data);
print_msg(receiver, receiver->framesInWindow);
assert(receiver->framesInWindow->swpSeqNo != inframe->swpSeqNo);
//left shift framesInWindow
memmove(receiver->framesInWindow, receiver->framesInWindow + 1,
sizeof(Frame) * (SWP_WINDOW_SIZE - 2));
receiver->LastFrameReceived += 1;
receiver->SwpWindow <<= 1;
}
memmove(receiver->framesInWindow, receiver->framesInWindow + 1,
sizeof(Frame) * (SWP_WINDOW_SIZE - 2));
receiver->SwpWindow <<= 1;
}
else
{
memmove(receiver->framesInWindow, receiver->framesInWindow + 1,
sizeof(Frame) * (SWP_WINDOW_SIZE - 2));
receiver->SwpWindow <<= 1;
}
}
else if(SwpSeqNo_minus(inframe->swpSeqNo, receiver->LastFrameReceived) <= SWP_WINDOW_SIZE
&& SwpSeqNo_minus(inframe->swpSeqNo, receiver->LastFrameReceived) > 0)
{
sendAck = 1;
SwpSeqNo n = SwpSeqNo_minus(inframe->swpSeqNo, receiver->LastFrameReceived) - 1;
assert(n >= 1 && n < 8);
memcpy(receiver->framesInWindow + n - 1, inframe, sizeof(Frame));
//update SWP window status
receiver->SwpWindow |= (1 << (SWP_WINDOW_SIZE - n));
assert((receiver->SwpWindow & 1) == 0);
//fprintf(stderr, "\tSwpSeqNo = %d, new SwpWindow = %X (LFR = %d)\n\n",
// inframe->swpSeqNo, receiver->SwpWindow, receiver->LastFrameReceived);
}
else
{
sendAck = 1;
while(0); // do nothing
}
//send ack
Frame *outframe = NULL;
if(sendAck)
{
outframe = (Frame*) malloc(sizeof(Frame));
outframe->swpSeqNo = inframe->swpSeqNo;
outframe->send_id = inframe->recv_id;
outframe->recv_id = inframe->send_id;
ackFrameAddCRC32(outframe);
char * outgoing_charbuf = convert_frame_to_char(outframe);
ll_append_node(outgoing_frames_head_ptr, outgoing_charbuf);
}
free(inframe);
free(ll_inmsg_node);
if(sendAck)
free(outframe);
}
}
void * run_receiver(void * input_receiver)
{
struct timespec time_spec;
struct timeval curr_timeval;
const int WAIT_SEC_TIME = 0;
const long WAIT_USEC_TIME = 100000;
Receiver * receiver = (Receiver *) input_receiver;
LLnode * outgoing_frames_head;
//This incomplete receiver thread, at a high level, loops as follows:
//1. Determine the next time the thread should wake up if there is nothing in the incoming queue(s)
//2. Grab the mutex protecting the input_msg queue
//3. Dequeues messages from the input_msg queue and prints them
//4. Releases the lock
//5. Sends out any outgoing messages
while(1)
{
//NOTE: Add outgoing messages to the outgoing_frames_head pointer
outgoing_frames_head = NULL;
gettimeofday(&curr_timeval,
NULL);
//Either timeout or get woken up because you've received a datagram
//NOTE: You don't really need to do anything here, but it might be useful for debugging purposes to have the receivers periodically wakeup and print info
time_spec.tv_sec = curr_timeval.tv_sec;
time_spec.tv_nsec = curr_timeval.tv_usec * 1000;
time_spec.tv_sec += WAIT_SEC_TIME;
time_spec.tv_nsec += WAIT_USEC_TIME * 1000;
if (time_spec.tv_nsec >= 1000000000)
{
time_spec.tv_sec++;
time_spec.tv_nsec -= 1000000000;
}
//*****************************************************************************************
//NOTE: Anything that involves dequeing from the input frames should go
// between the mutex lock and unlock, because other threads CAN/WILL access these structures
//*****************************************************************************************
pthread_mutex_lock(&receiver->buffer_mutex);
//Check whether anything arrived
int incoming_msgs_length = ll_get_length(receiver->input_framelist_head);
if (incoming_msgs_length == 0)
{
//Nothing has arrived, do a timed wait on the condition variable (which releases the mutex). Again, you don't really need to do the timed wait.
//A signal on the condition variable will wake up the thread and reacquire the lock
pthread_cond_timedwait(&receiver->buffer_cv,
&receiver->buffer_mutex,
&time_spec);
}
handle_incoming_msgs(receiver,
&outgoing_frames_head);
pthread_mutex_unlock(&receiver->buffer_mutex);
//CHANGE THIS AT YOUR OWN RISK!
//Send out all the frames user has appended to the outgoing_frames list
int ll_outgoing_frame_length = ll_get_length(outgoing_frames_head);
while(ll_outgoing_frame_length > 0)
{
LLnode * ll_outframe_node = ll_pop_node(&outgoing_frames_head);
char * char_buf = (char *) ll_outframe_node->value;
//The following function frees the memory for the char_buf object
send_msg_to_senders(char_buf);
//Free up the ll_outframe_node
free(ll_outframe_node);
ll_outgoing_frame_length = ll_get_length(outgoing_frames_head);
}
}
pthread_exit(NULL);
}