-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.cc
executable file
·96 lines (78 loc) · 2 KB
/
Queue.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
#ifndef QUEUE
#define QUEUE
#include <string.h>
#include <omnetpp.h>
using namespace omnetpp;
class Queue: public cSimpleModule {
private:
cQueue buffer;
cMessage *endServiceEvent;
cOutVector bufferSizeVector;
cOutVector packetDropVector;
void scheduleSendPacketWithDelay(simtime_t time);
void sendPacket();
void addPacket(cMessage *message);
bool isFullQueue();
void activeQueue();
public:
Queue();
virtual ~Queue();
protected:
virtual void initialize();
virtual void finish();
virtual void handleMessage(cMessage *message);
};
Define_Module(Queue);
Queue::Queue() {
endServiceEvent = NULL;
}
Queue::~Queue() {
cancelAndDelete(endServiceEvent);
}
void Queue::initialize() {
buffer.setName("buffer");
bufferSizeVector.setName("bufferSize");
packetDropVector.setName("packetsDropped");
endServiceEvent = new cMessage("endService");
}
void Queue::finish() {
}
void Queue::scheduleSendPacketWithDelay(simtime_t delay) {
scheduleAt(simTime() + delay, endServiceEvent);
}
void Queue::sendPacket() { // Only if there is any packet
if (!buffer.isEmpty()) {
cPacket *packet = (cPacket*) buffer.pop();
send(packet, "out");
scheduleSendPacketWithDelay(packet->getDuration());
}
}
void Queue::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());
activeQueue();
}
}
bool Queue::isFullQueue() {
return buffer.getLength() >= par("bufferSize").intValue();
}
void Queue::activeQueue() {
if (!endServiceEvent->isScheduled()) { // Queue is inactive
scheduleSendPacketWithDelay(0);
}
}
void Queue::handleMessage(cMessage *message) {
if (message == endServiceEvent) {
sendPacket();
} else {
addPacket(message);
}
}
#endif