-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.cc
executable file
·121 lines (105 loc) · 3.51 KB
/
App.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
#ifndef APP
#define APP
#include <string.h>
#include <omnetpp.h>
#include <packet_m.h>
using namespace omnetpp;
class App: public cSimpleModule {
private:
cMessage *sendMsgEvent;
cStdDev delayStats;
cOutVector delayVector;
cOutVector hopsCount;
cStdDev ** receivedFromStdDevArray;
public:
App();
virtual ~App();
protected:
virtual void initialize();
virtual void finish();
virtual void handleMessage(cMessage *msg);
};
Define_Module(App);
#endif /* APP */
App::App() {
}
App::~App() {
for (int i=0; i<par("amountHosts").intValue(); ++i) {
cStdDev * receivedFrom = (cStdDev *)(receivedFromStdDevArray[i]);
delete(receivedFrom);
receivedFromStdDevArray[i] = NULL;
}
delete(receivedFromStdDevArray);
}
void App::initialize() {
// If interArrivalTime for this node is higher than 0
// initialize packet generator by scheduling sendMsgEvent
if (par("interArrivalTime").doubleValue() != 0) {
sendMsgEvent = new cMessage("sendEvent");
scheduleAt(par("interArrivalTime"), sendMsgEvent);
}
// Initialize statistics
delayStats.setName("TotalDelay");
delayStats.collect(0);
delayVector.setName("Delay");
delayVector.record(0);
hopsCount.setName("Hops");
hopsCount.record(0);
int amountHosts = par("amountHosts").intValue();
receivedFromStdDevArray = new cStdDev*[amountHosts];
for (int i=0; i<amountHosts; ++i) {
cStdDev* receivedFrom = new cStdDev();
std::string name =
std::to_string(i) +
"->" +
std::to_string(this->getParentModule()->getIndex());
std::cout << "??? " << name << "\n";
receivedFrom->setName(name.c_str());
receivedFrom->collect(0);
receivedFromStdDevArray[i] = receivedFrom;
}
}
void App::finish() {
// Record statistics
recordScalar("Average delay", delayStats.getMean());
recordScalar("Number of packets", delayStats.getCount());
for (int i=0; i<par("amountHosts").intValue(); ++i) {
std::string scalarName =
"Packets from " +
std::to_string(i) +
" to " +
std::to_string(this->getParentModule()->getIndex());
std::cout << scalarName << "\n";
cStdDev* receivedFrom = receivedFromStdDevArray[i];
recordScalar(scalarName.c_str(), receivedFrom->getCount());
}
}
void App::handleMessage(cMessage *msg) {
// if msg is a sendMsgEvent, create and send new packet
if (msg == sendMsgEvent) {
// create new packet
Packet *pkt = new Packet("packet",this->getParentModule()->getIndex());
pkt->setByteLength(par("packetByteSize"));
pkt->setSource(this->getParentModule()->getIndex());
pkt->setHopCount(0);
pkt->setDestination(par("destination"));
// send to net layer
send(pkt, "toNet$o");
// compute the new departure time and schedule next sendMsgEvent
simtime_t departureTime = simTime() + par("interArrivalTime");
scheduleAt(departureTime, sendMsgEvent);
}
// else, msg is a packet from net layer
else {
// compute delay and record statistics
simtime_t delay = simTime() - msg->getCreationTime();
cStdDev* receivedFrom = receivedFromStdDevArray[((Packet*)msg)->getSource()];
receivedFrom->collect(1);
delayStats.collect(delay);
delayVector.record(delay);
Packet *pkt = (Packet *) msg;
hopsCount.record(pkt->getHopCount());
// delete msg
delete (msg);
}
}