-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_back_n.cpp
71 lines (55 loc) · 1.73 KB
/
go_back_n.cpp
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
#include "trans_protocols.h"
GoBackN::GoBackN (int timeout, int slast_size, int rnext_size, float prob_error, int window): SendRecv(timeout, slast_size, rnext_size, prob_error) {
this->window = window;
}
int GoBackN::sendMsgStream (MSG_TYPE *stream, int size) {
ackbuff ack, temp_ack;
ID_TYPE i;
// the first window is the window 0
i = 0;
while (i<(ID_TYPE)size) {
// send a window
for (ID_TYPE j=i;j-i<window && j<(ID_TYPE)size; j++) {
cout << "Sending package " << j << endl;
sendMsg(stream[j], &j);
}
// wait for the transmition timeout
sleep(timeout);
int ack_count = 0;
// get the first ack and then compare if there is a newer to get
if (msgrcv(outputChannelId, &ack, sizeof(ackbuff), 0, IPC_NOWAIT) >= 0)
ack_count++;
while (msgrcv(outputChannelId, &temp_ack, sizeof(ackbuff), 0, IPC_NOWAIT) >= 0) {
if (crc(temp_ack.ack) == 0)
ack = temp_ack;
ack_count++;
}
// if no ack or nack, poll, to check if the receiver is still there?
if (ack_count == 0)
cout << "No acks nor nacks received" << endl;
else if (crc(ack.ack) != 0)
cout << "None of the acknowledges passed on CRC check" << endl;
else {
cout << "Shifting window" << endl;
cout << "Ack received: " << (EXTRACT_ID_FROM_ACK(ack.ack, slast_size)) << endl;
if (EXTRACT_ID_FROM_ACK(ack.ack, slast_size) < i+window) {
cout << "Nack found: " << (EXTRACT_ID_FROM_ACK(ack.ack, slast_size)) << endl;
i = EXTRACT_ID_FROM_ACK(ack.ack, slast_size);
}
// ack only
else {
cout << "Setting next window" << endl;
i += window;
}
}
}
return 0;
}
int GoBackN::recvMsgStream (MSG_TYPE *stream, int size) {
ACK_TYPE rnext = 0;
for (int i=0; i<size; i++) {
if (!recvMsg(&stream[i], &rnext))
i--;
}
return 0;
}