-
Notifications
You must be signed in to change notification settings - Fork 0
/
Host.cc
440 lines (370 loc) · 15 KB
/
Host.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//
// This file is part of an OMNeT++/OMNEST simulation example.
//
// Copyright (C) 1992-2015 Andras Varga
//
// This file is distributed WITHOUT ANY WARRANTY. See the file
// `license' for details on this and other legal matters.
//
#include <algorithm>
#include "Host.h"
#include "Server.h"
namespace csma {
Define_Module(Host);
Host::~Host()
{
delete lastPacket;
cancelAndDelete(endTxEvent);
}
void Host::initialize()
{
gate("in")->setDeliverImmediately(true);
stateSignal = registerSignal("state");
server = getModuleByPath("server");
txRate = par("txRate");
iaTime = &par("iaTime");
pkLenBits = &par("pkLenBits");
slotTime = par("slotTime");
WATCH(slotTime);
endTxEvent = new cMessage("send/endTx");
state = IDLE;
emit(stateSignal, state);
pkCounter = 0;
WATCH((int&)state);
WATCH(pkCounter);
x = par("x").doubleValue();
y = par("y").doubleValue();
double serverX = server->par("x").doubleValue();
double serverY = server->par("y").doubleValue();
idleAnimationSpeed = par("idleAnimationSpeed");
transmissionEdgeAnimationSpeed = par("transmissionEdgeAnimationSpeed");
midtransmissionAnimationSpeed = par("midTransmissionAnimationSpeed");
double dist = std::sqrt((x-serverX) * (x-serverX) + (y-serverY) * (y-serverY));
radioDelay = dist / propagationSpeed;
getDisplayString().setTagArg("p", 0, x);
getDisplayString().setTagArg("p", 1, y);
channelBusy = 0;
backoffTime = 0;
maxBackoffs = par("maxBackoffs");
backoffCount = 0;
backoff = new cMessage("backoff");
channelStateSignal = registerSignal("channelState");
server->subscribe("channelState", this);
numOtherHosts = getVectorSize() - 1;
otherHostGate = new cGate *[numOtherHosts];
otherHosts = new cModule *[numOtherHosts];
otherHostDelay = new simtime_t [numOtherHosts];
char hostname[10] = "host[x]";
int index = 0;
for (int i = 0; i < numOtherHosts + 1; i++) {
if (i != getIndex()) {
snprintf(hostname, sizeof(hostname), "host[%d]", i);
otherHosts[index] = getModuleByPath(hostname);
otherHostGate[index] = otherHosts[index]->gate("in");
double dist = std::sqrt((x-otherHosts[index]->par("x").doubleValue()) * \
(x-otherHosts[index]->par("x").doubleValue()) + \
(y-otherHosts[index]->par("y").doubleValue()) * \
(y-otherHosts[index]->par("y").doubleValue()));
otherHostDelay[index] = dist / propagationSpeed;
index++;
}
}
DIFSEvent = new cMessage("DIFSEvent");
DIFS = par("DIFS");
DIFS_FLAG = 0;
RTS_TIME = par("RTS");
SIFS = par("SIFS");
RTSEvent = new cMessage("RTSEvent");
cancleChannelBusy = new cMessage("cancleChannelBusy");
contentFailFlag = false;
scheduleAt(getNextTransmissionTime(), DIFSEvent);
}
void Host::handleMessage(cMessage *msg)
{
// ASSERT(msg == endTxEvent || msg == backoff || endListen);
if (hasGUI() && msg == endTxEvent)
getParentModule()->getCanvas()->setAnimationSpeed(transmissionEdgeAnimationSpeed, this);
if (msg == DIFSEvent) {
if (channelBusy == 0) {
if (state == IDLE) {
// while channel is free and state is idle, wait for DIFS
scheduleAt(simTime() + DIFS, RTSEvent);
} else if (state == FREEZE) {
// maybe this part is not needed, its implementation is below
scheduleAt(simTime() + DIFS + backoffTime, RTSEvent);
DIFS_FLAG = simTime();
}
} else {
scheduleAt(simTime(), backoff);
}
} else if (msg == RTSEvent){
sendRTS();
} else if (msg == endTxEvent) {
if (state == BEFORE_SNED) {
// generate packet
snprintf(pkname, sizeof(pkname), "pk-%d-#%d", getIndex(), pkCounter++);
EV << "generating packet " << pkname << endl;
pk = new cPacket(pkname);
pk->setBitLength(pkLenBits->intValue());
sendPacket(pk);
} else if (state == TRANSMIT) {
// endTxEvent indicates end of transmission
state = IDLE;
emit(stateSignal, state);
// schedule next sending
scheduleAt(getNextTransmissionTime(), DIFSEvent);
// send to other hosts the finish signal
for (int i = 0; i < numOtherHosts; i++) {
endListen = new cMessage("endListen");
sendDirect(endListen, otherHostDelay[i], 0, otherHostGate[i]);
}
} else {
throw cRuntimeError("invalid state");
}
} else if (msg == backoff) {
// only compute backofftime
EV << "host " << getIndex() << " backoff\n";
state = FREEZE;
if (backoffCount < maxBackoffs) {
backoffCount += 1;
}
backoffTime = generateBackofftime();
} else {
if (strcmp(msg->getName(), endListenName) == 0) {
// EV << "finish receive other host\n";
channelBusy = 0;
delete msg;
if (state == FREEZE && channelBusy == 0) {
// At begin, this part is schedule a DIFSEvent, then execute below code
// Now, this part is executed directly
scheduleAt(simTime() + DIFS + backoffTime, RTSEvent);
DIFS_FLAG = simTime();
}
} else if (strcmp(msg->getName(), "CTS_up") == 0) {
if (state == WAIT_CTS) {
// confirm that the CTS is received
cPacket *pkt = check_and_cast<cPacket *>(msg);
state = BEFORE_SNED;
cancelEvent(backoff);
scheduleAt(simTime() + SIFS + pkt->getDuration(), endTxEvent);
delete pkt;
} else {
delete msg;
}
contentFailFlag = false;
} else if (strcmp(msg->getName(), "CTS_down") == 0) {
if (state == WAIT_CTS) {
// backoff as scheduled
delete msg;
} else {
delete msg;
}
contentFailFlag = false;
cancelEvent(cancleChannelBusy);
} else if (strcmp(msg->getName(), "RTS") == 0) {
if (state == FREEZE && !contentFailFlag) {
// contention period
EV << "host " << getIndex() << " content fail and freeze\n";
// while RTS collision, don't receive CTS, then create a new backoffTime below
// then next other RTS arrive, backoffTime is not changed
if (backoffTime - (simTime() - DIFS_FLAG - DIFS) > 0) {
backoffTime = backoffTime - (simTime() - DIFS_FLAG - DIFS);
}
cancelEvent(RTSEvent);
contentFailFlag = true;
}
channelBusy = 1;
if (cancleChannelBusy->isScheduled()) {
cancelEvent(cancleChannelBusy);
scheduleAt(simTime() + DIFS * 10, cancleChannelBusy);
} else {
scheduleAt(simTime() + DIFS * 10, cancleChannelBusy);
}
delete msg;
} else if (msg == cancleChannelBusy) {
channelBusy = 0;
}
else {
cMessage *pkt = check_and_cast<cMessage *>(msg);
ASSERT(pkt->isReceptionStart());
if (state == WAIT_CTS) {
// channel becomes busy while waiting for DIFS
// backoff
EV << "host " << getIndex() << " backoff while waiting for DIFS\n";
scheduleAt(simTime(), backoff);
cancelEvent(endTxEvent);
}
channelBusy++;
delete pkt;
}
}
}
simtime_t Host::generateBackofftime()
{
int CW = pow(2, 2 + backoffCount) - 1;
int slots = intrand(CW + 1);
EV << "slots: " << slots << endl;
return slots * slotTime;
};
simtime_t Host::getNextTransmissionTime()
{
simtime_t t = simTime() + iaTime->doubleValue();
return t;
}
void Host::sendRTS(){
cPacket *RTS = new cPacket("RTS");
sendDirect(RTS, radioDelay, RTS_TIME, server->gate("in"));
for (int i = 0; i < numOtherHosts; i++) {
RTS = new cPacket("RTS");
sendDirect(RTS, otherHostDelay[i], RTS_TIME, otherHostGate[i]);
}
// if don't get CTS, backoff
scheduleAt(simTime() + RTS_TIME + SIFS * 5, backoff);
state = WAIT_CTS;
}
void Host::sendPacket(cPacket *pk) {
EV << "send packet " << pkname << endl;
state = TRANSMIT;
emit(stateSignal, state);
simtime_t duration = pk->getBitLength() / txRate;
sendDirect(pk, radioDelay, duration, server->gate("in"));
for (int i = 0; i < numOtherHosts; i++) {
snprintf(broadcast, sizeof(broadcast), "from-%d, to-%d", getIndex(), otherHosts[i]->getIndex());
// EV << "generating packet " << broadcast << endl;
cMessage *broadcastPacket = new cMessage(broadcast);
sendDirect(broadcastPacket, otherHostDelay[i], \
0, otherHostGate[i]);
}
scheduleAt(simTime()+duration, endTxEvent);
backoffTime = 0;
// let visualization code know about the new packet
if (transmissionRing != nullptr) {
delete lastPacket;
transmissionRing->setVisible(false);
transmissionRing->setAssociatedObject(nullptr);
for (auto c : transmissionCircles) {
c->setVisible(false);
c->setAssociatedObject(nullptr);
}
lastPacket = pk->dup();
}
}
void Host::refreshDisplay() const
{
cCanvas *canvas = getParentModule()->getCanvas();
const int numCircles = 20;
const double circleLineWidth = 10;
// create figures on our first invocation
if (!transmissionRing) {
auto color = cFigure::GOOD_DARK_COLORS[getId() % cFigure::NUM_GOOD_DARK_COLORS];
transmissionRing = new cRingFigure(("Host" + std::to_string(getIndex()) + "Ring").c_str());
transmissionRing->setOutlined(false);
transmissionRing->setFillColor(color);
transmissionRing->setFillOpacity(0.25);
transmissionRing->setFilled(true);
transmissionRing->setVisible(false);
transmissionRing->setZIndex(-1);
canvas->addFigure(transmissionRing);
for (int i = 0; i < numCircles; ++i) {
auto circle = new cOvalFigure(("Host" + std::to_string(getIndex()) + "Circle" + std::to_string(i)).c_str());
circle->setFilled(false);
circle->setLineColor(color);
circle->setLineOpacity(0.75);
circle->setLineWidth(circleLineWidth);
circle->setZoomLineWidth(true);
circle->setVisible(false);
circle->setZIndex(-0.5);
transmissionCircles.push_back(circle);
canvas->addFigure(circle);
}
}
if (lastPacket) {
// update transmission ring and circles
if (transmissionRing->getAssociatedObject() != lastPacket) {
transmissionRing->setAssociatedObject(lastPacket);
for (auto c : transmissionCircles)
c->setAssociatedObject(lastPacket);
}
simtime_t now = simTime();
simtime_t frontTravelTime = now - lastPacket->getSendingTime();
simtime_t backTravelTime = now - (lastPacket->getSendingTime() + lastPacket->getDuration());
// conversion from time to distance in m using speed
double frontRadius = std::min(ringMaxRadius, frontTravelTime.dbl() * propagationSpeed);
double backRadius = backTravelTime.dbl() * propagationSpeed;
double circleRadiusIncrement = circlesMaxRadius / numCircles;
// update transmission ring geometry and visibility/opacity
double opacity = 1.0;
if (backRadius > ringMaxRadius) {
transmissionRing->setVisible(false);
transmissionRing->setAssociatedObject(nullptr);
for (auto c : transmissionCircles) {
c->setVisible(false);
c->setAssociatedObject(nullptr);
}
}
else {
transmissionRing->setVisible(true);
transmissionRing->setBounds(cFigure::Rectangle(x - frontRadius, y - frontRadius, 2*frontRadius, 2*frontRadius));
transmissionRing->setInnerRadius(std::max(0.0, std::min(ringMaxRadius, backRadius)));
if (backRadius > 0)
opacity = std::max(0.0, 1.0 - backRadius / circlesMaxRadius);
}
transmissionRing->setLineOpacity(opacity);
transmissionRing->setFillOpacity(opacity/5);
// update transmission circles geometry and visibility/opacity
double radius0 = std::fmod(frontTravelTime.dbl() * propagationSpeed, circleRadiusIncrement);
for (int i = 0; i < (int)transmissionCircles.size(); ++i) {
double circleRadius = std::min(ringMaxRadius, radius0 + i * circleRadiusIncrement);
if (circleRadius < frontRadius - circleRadiusIncrement/2 && circleRadius > backRadius + circleLineWidth/2) {
transmissionCircles[i]->setVisible(true);
transmissionCircles[i]->setBounds(cFigure::Rectangle(x - circleRadius, y - circleRadius, 2*circleRadius, 2*circleRadius));
transmissionCircles[i]->setLineOpacity(std::max(0.0, 0.2 - 0.2 * (circleRadius / circlesMaxRadius)));
}
else
transmissionCircles[i]->setVisible(false);
}
// compute animation speed
double animSpeed = idleAnimationSpeed;
if ((frontRadius >= 0 && frontRadius < circlesMaxRadius) || (backRadius >= 0 && backRadius < circlesMaxRadius))
animSpeed = transmissionEdgeAnimationSpeed;
if (frontRadius > circlesMaxRadius && backRadius < 0)
animSpeed = midtransmissionAnimationSpeed;
if (hasGUI())
canvas->setAnimationSpeed(animSpeed, this);
}
else {
// hide transmission rings, update animation speed
if (transmissionRing->getAssociatedObject() != nullptr) {
transmissionRing->setVisible(false);
transmissionRing->setAssociatedObject(nullptr);
for (auto c : transmissionCircles) {
c->setVisible(false);
c->setAssociatedObject(nullptr);
}
if (hasGUI())
canvas->setAnimationSpeed(idleAnimationSpeed, this);
}
}
// update host appearance (color and text)
getDisplayString().setTagArg("t", 2, "#808000");
if (state == IDLE) {
getDisplayString().setTagArg("i", 1, "");
getDisplayString().setTagArg("t", 0, "");
}
else if (state == TRANSMIT) {
getDisplayString().setTagArg("i", 1, "yellow");
getDisplayString().setTagArg("t", 0, "TRANSMIT");
}
}
void Host::receiveSignal(cComponent *source, simsignal_t signalID, intval_t i, cObject *details)
{
// EV << "host listen long" << endl;
// if (signalID == channelStateSignal) {
// if (i > 0) {
// channelBusy = true;
// } else {
// channelBusy = false;
// }
// }
}
}; //namespace