-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFogNode.cc
More file actions
154 lines (107 loc) · 3.56 KB
/
FogNode.cc
File metadata and controls
154 lines (107 loc) · 3.56 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
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
#include <iomanip>
#include "FogNode.h"
#include "functions.h"
using namespace omnetpp;
Define_Module(FogNode);
void FogNode::initialize()
{
processing_delay=par("processingDelay").doubleValue();
queue_size=par("queueSize").intValue();
scheduleEvent = new cMessage("scheduleEvent");
}
void FogNode::handleMessage(cMessage *msg)
{
if (!msg->isSelfMessage()){
addToQueue(msg);
if (!scheduleEvent->isScheduled()){
scheduleAt(simTime()+ processing_delay, scheduleEvent); //wait until get last message in queue
}
}
else if (msg == scheduleEvent){
if (!waitingMessagePool.empty()) {
msg = waitingMessagePool.front();
waitingMessagePool.pop(); //take the message
forwardMessage(msg);
scheduleAt(simTime()+ processing_delay, scheduleEvent);
}
}
}
void FogNode::addToQueue(cMessage *msg)
{
if (waitingMessagePool.size() < queue_size)
{
sum_utilize_time+=processing_delay;
waitingMessagePool.push(msg);
}
else if (strcmp(msg->getArrivalGate()->getName(),"in3") == 3)
{
cMessage* lastMessage = waitingMessagePool.back();
waitingMessagePool.pop();
delete lastMessage;
waitingMessagePool.push(msg);
}
else
{
forwardMessage(msg, true);
}
}
void FogNode::forwardMessage(cMessage *msg, BOOLEAN queue_full){
if (!queue_full)
{
std::string outputGateName = functions.getDestGate(std::string(this->getName()), std::string(msg->getArrivalGate()->getName()), msg);
send(msg, outputGateName.c_str());
return;
}
std::string nextgate = getBestFogGate(msg);
if (nextgate != "")
{
incrementHopCounter(msg);
send(msg, nextgate.c_str());
}
}
std::string FogNode::getBestFogGate(cMessage *msg) //Neighbor
{
int fog_node_number = std::stoi(std::string(msg->getArrivalModule()->getName()).substr(7));
std:: string arrivall_gate=msg->getArrivalGate()->getName();
bubble(("fog" + std::to_string(fog_node_number) + " queue full !!! hops: " + std::to_string(getHopCounter(msg))).c_str());
if (getHopCounter(msg) == 0 ){
return "out4"; //send all full messages to right edge
}
switch(fog_node_number)
{
case 1:
return "out4";
case 5:
bubble("Deleting image queues full !!!");
delete msg;
break;
default:
return (arrivall_gate=="in5") ? "out4" : "out5";
}
return ""; // Return empty string for undefined behavior.
}
void FogNode::incrementHopCounter(cMessage* msg) {
hopCounter[msg]++;
}
int FogNode::getHopCounter(cMessage* msg) const {
auto it = hopCounter.find(msg);
if (it != hopCounter.end()) {
return it->second;
}
return 0; // Default hop count is 0 if not found
}
FogNode :: ~FogNode(){
while(!waitingMessagePool.empty()) {
cMessage* msg = waitingMessagePool.front();
waitingMessagePool.pop();
delete msg;
}
if (scheduleEvent) {
cancelAndDelete(scheduleEvent);
scheduleEvent = nullptr;
}
}
void FogNode::finish() {
double utlztime=measurments.getServerUtilizationPrecent(sum_utilize_time.dbl(),waitingMessagePool.size(),processing_delay);
EV << "FogNode ulitization: " << std::fixed << std::setprecision(2) << utlztime << "%\n";
}