-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransportRx.cc
169 lines (131 loc) · 4.28 KB
/
TransportRx.cc
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
#ifndef TRANSPORT_RX
#define TRANSPORT_RX
#include "ControlPacket_m.h"
#include <string.h>
#include <omnetpp.h>
using namespace omnetpp;
class TransportRx: public cSimpleModule {
private:
cQueue buffer, bufferControl;
cMessage *endServiceEvent, *endControlServiceEvent;
cOutVector bufferSizeVector;
cOutVector packetDropVector;
cOutVector controlPacketSentVector;
cOutVector packetReceive;
void sendControlPacket();
void scheduleSendControlPacketWithDelay(simtime_t delay);
void addControlPacket(cMessage *message);
bool isFullControlQueue();
void sendDataPacket();
void scheduleSendPacketWithDelay(simtime_t delay);
void addPacket(cMessage *message);
bool isFullQueue();
bool haveSpaceQueue();
void activeControlQueue();
void activeQueue();
public:
TransportRx();
virtual ~TransportRx();
protected:
virtual void initialize();
virtual void finish();
virtual void handleMessage(cMessage *message);
};
Define_Module(TransportRx);
TransportRx::TransportRx() {
endServiceEvent = NULL;
endControlServiceEvent = NULL;
}
TransportRx::~TransportRx() {
cancelAndDelete(endServiceEvent);
cancelAndDelete(endControlServiceEvent);
}
void TransportRx::initialize() {
buffer.setName("buffer");
bufferControl.setName("bufferControl");
bufferSizeVector.setName("bufferSize");
packetDropVector.setName("packetsDropped");
controlPacketSentVector.setName("controlPacketSent");
packetReceive.setName("packetsReceived");
endServiceEvent = new cMessage("endService");
endControlServiceEvent = new cMessage("endControlService");
}
void TransportRx::finish() {
}
void TransportRx::sendControlPacket() {
if (!bufferControl.isEmpty()) {
ControlPacket *controlPacket = (ControlPacket *) bufferControl.pop();
send(controlPacket, "toOut$o");
controlPacketSentVector.record(1);
scheduleSendControlPacketWithDelay(controlPacket->getDuration());
}
}
void TransportRx::scheduleSendControlPacketWithDelay(simtime_t delay) {
scheduleAt(simTime() + delay, endControlServiceEvent);
}
void TransportRx::addControlPacket(cMessage *message) {
if (!isFullControlQueue()) {
ControlPacket* controlPacket = new ControlPacket("control packet");
controlPacket->setByteLength(20);
controlPacket->setKind(2);
controlPacket->setTotalBuffer(par("bufferSize").intValue());
controlPacket->setRemainingBuffer(par("bufferSize").intValue() - buffer.getLength());
controlPacket->setTimeElapsedToReceivePacket(message->getArrivalTime() - message->getCreationTime());
bufferControl.insert(controlPacket);
activeControlQueue();
}
}
void TransportRx::sendDataPacket() { // Only if there is any packet
if (!buffer.isEmpty()) {
cPacket *packet = (cPacket*) buffer.pop();
send(packet, "toApp");
scheduleSendPacketWithDelay(packet->getDuration());
}
}
void TransportRx::scheduleSendPacketWithDelay(simtime_t delay) {
scheduleAt(simTime() + delay, endServiceEvent);
}
void TransportRx::addPacket(cMessage *message) {
if (isFullQueue()) {
// Drop packet
delete(message);
this->bubble("packet dropped");
packetDropVector.record(1);
} else {
// Add packet
buffer.insert(message);
bufferSizeVector.record(buffer.getLength());
packetReceive.record(1);
addControlPacket(message);
activeQueue();
}
}
bool TransportRx::isFullControlQueue() {
return bufferControl.getLength() >= par("bufferSize").intValue();
}
bool TransportRx::isFullQueue() {
return buffer.getLength() >= par("bufferSize").intValue();
}
bool TransportRx::haveSpaceQueue() {
return buffer.getLength()*2 <= par("bufferSize").intValue();
}
void TransportRx::activeControlQueue() {
if (!endControlServiceEvent->isScheduled()) {
scheduleSendControlPacketWithDelay(0);
}
}
void TransportRx::activeQueue() {
if (!endServiceEvent->isScheduled()) {
scheduleSendPacketWithDelay(0);
}
}
void TransportRx::handleMessage(cMessage *message) {
if (message == endControlServiceEvent) {
sendControlPacket();
} else if (message == endServiceEvent) {
sendDataPacket();
} else {
addPacket(message);
}
}
#endif