-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.cc
103 lines (78 loc) · 3.54 KB
/
source.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
/*
* source.cc
*
* Created on: 22 June 2024
* Author: MONDALS
*/
#include <string.h>
#include <math.h>
#include <omnetpp.h>
#include "ponPacket_m.h"
#include "ping_m.h"
#include "sim_params.h"
using namespace std;
using namespace omnetpp;
/*
* Each source generates packets following some probability distribution.
* The average inter-arrival time is derived using the maximum datarate of the
* SourceApp and average packet size.
* The generateEvent is a self-message which is scheduled back-to-back to create
* a new packet. Immediately the packet is transmitted to the corresponding ONU.
*/
class SourceApp : public cSimpleModule
{
private:
double Load;
double ArrivalRate;
double pkt_interval; // inter-packet generation interval
cMessage *generateEvent = nullptr; // holds pointer to the self-timeout message
//simsignal_t arrivalSignal; // to send signals for statistics collection
public:
virtual ~SourceApp();
protected:
// The following redefined virtual function holds the algorithm.
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
virtual ponPacket *generateNewPacket();
};
// The module class needs to be registered with OMNeT++
Define_Module(SourceApp);
SourceApp::~SourceApp()
{
cancelAndDelete(generateEvent);
}
void SourceApp::initialize()
{
//arrivalSignal = registerSignal("generation"); // registering the signal
Load = par("load"); // get the load factor from NED file
double R_o = par("dataRate"); // get the max ONU datarate from NED file
ArrivalRate = Load*R_o/(8*pkt_sz_avg); // average packet arrival rate with datarate in bytes
// Initialize variables
pkt_interval = exponential(1/ArrivalRate); // packet inter-arrival times are generated following exponential distribution
generateEvent = new cMessage("generateEvent"); // self-message is generated for next packet generation
//emit(arrivalSignal,pkt_interval);
ponPacket *pkt = generateNewPacket(); // generating the first packet at T = 0
send(pkt,"out");
//EV << "[src] pkt_interval = " << pkt_interval << " and current time = " << simTime() << endl;
scheduleAt(simTime()+pkt_interval, generateEvent); // scheduling the next packet generation
}
void SourceApp::handleMessage(cMessage *msg)
{
if(strcmp(msg->getName(),"generateEvent") == 0) {
cPacket *pkt = generateNewPacket(); // generating a new packet at current time
send(pkt,"out");
pkt_interval = exponential(1/ArrivalRate); // packet inter-arrival time generation
scheduleAt(simTime()+pkt_interval, generateEvent); // scheduling the next packet generation
//emit(arrivalSignal,pkt_interval);
//EV << "[src] pkt_interval = " << pkt_interval << " and current time = " << simTime() << endl;
}
}
ponPacket *SourceApp::generateNewPacket()
{
int pkt_size = intuniform(64,1542);
ponPacket *pkt = new ponPacket("app_data");
pkt->setByteLength(pkt_size); // adding a random size payload to the packet
pkt->setGenerationTime(simTime());
//EV << "[src] New packet generated with size (bytes): " << pkt_size << endl;
return pkt;
}