-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkLayerRetransmissionProtocol.cpp
More file actions
89 lines (64 loc) · 2.89 KB
/
LinkLayerRetransmissionProtocol.cpp
File metadata and controls
89 lines (64 loc) · 2.89 KB
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
//
// LinkLayerRetransmissionProtocol.cpp
// lab2
//
// Created by Nishad Krishnan on 2015-03-13.
// Copyright (c) 2015 Nish. All rights reserved.
//
#include "LinkLayerRetransmissionProtocol.h"
RetransmissionProtocol::RetransmissionProtocol(int hLength, int pLength, int tR, double bER, int numPacketsForSim, double channDelay, double deltaTaoFact) {
frameHeaderLength = hLength; // Header length
packetLength = pLength; // Payload length
transmissionRate = tR; // Link Rate
packetsDelivered = 0; // Packets delivered Successfully
nextExpectedFrame = 0; // Initialize next expected frame
bitErrorRate = bER; // Bit Error Rate
numPacketsToSend = numPacketsForSim; // Number of packets for simulation
channelDelay = channDelay; // Channel Delay
deltaTaoFactor = deltaTaoFact; // Delta/Tau
packetsLost = 0; // Number of packets lost
srand((uint)time(NULL)); // Seed RNG to current time
}
void RetransmissionProtocol::Simulate(bool nak) {
Sender(nak);
}
int RetransmissionProtocol::Channel(int length, double& propagationDelay) {
int zeroCount = 0;
//Add channel delay to overall delay
propagationDelay += channelDelay;
// Iterate through bits and count zeroes to get number of errors
for (int i = 0; i < length; i++) {
double randomValue = (rand()/((double)RAND_MAX + 1));
if (randomValue < bitErrorRate)
zeroCount++;
}
if (zeroCount > 0 && zeroCount <= 4)
return ERROR;
else if (zeroCount == 0)
return NOERROR;
packetsLost++;
return LOSS;
}
Event* RetransmissionProtocol::Send(double senderTime, int sN, int length) {
if (sN >= 0) {
// Sending frame to channel has delay
double propagationDelay = senderTime + (double)length/(double)transmissionRate;
int rSeqNum = INT_MIN;
int status = 0;
//Forward Channel
status = Channel(length, propagationDelay);
// If loss receiver doesnt get called
if (status != LOSS)
rSeqNum = Receiver(propagationDelay, sN, status);
if (rSeqNum >= 0) {
// Reverse Channel
status = Channel(frameHeaderLength, propagationDelay);
//Sending ack to channel has delay
propagationDelay += (double)frameHeaderLength/(double)transmissionRate;
// If loss no event gets created
if (status != LOSS)
return CreateEvent(ACK, propagationDelay, status, rSeqNum);
}
}
return NULL;
}