-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEEBTProtocol.cc
1689 lines (1458 loc) · 68.7 KB
/
EEBTProtocol.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SimpleBroadcastProtocol.cc
*
* Created on: 02.06.2020
* Author: krassus
*/
#include <math.h>
#include "ns3/EEBTPTag.h"
#include "SeqNoCache.h"
#include "SendEvent.h"
#include "AD_SendEvent.h"
#include "CC_SendEvent.h"
#include "EEBTProtocol.h"
#include "EEBTPDataHeader.h"
#include "EEBTPQueueDiscItem.h"
#include "CustomWifiTxCurrentModel.h"
#include "float.h"
#include "ns3/nstime.h"
#include "ns3/mac-low.h"
#include "ns3/log.h"
#include "ns3/wifi-utils.h"
#include "ns3/integer.h"
#include "ns3/callback.h"
#include "ns3/core-module.h"
#include "ns3/wifi-mac.h"
#include "ns3/wifi-phy.h"
#include "ns3/llc-snap-header.h"
#include "ns3/mobility-module.h"
#include "ns3/fifo-queue-disc.h"
#include "ns3/traffic-control-helper.h"
namespace ns3
{
NS_LOG_COMPONENT_DEFINE("EEBTProtocol");
NS_OBJECT_ENSURE_REGISTERED(EEBTProtocol);
const uint16_t EEBTProtocol::PROT_NUMBER = 153;
const uint32_t EEBTProtocol::MAX_UNCHANGED_ROUNDS = 10;
EEBTProtocol::EEBTProtocol()
{
this->sendCounter = 0;
this->maxPackets = 1000;
this->dataLength = 1000;
this->ndInterval = 0;
this->cache = SeqNoCache();
this->maxAllowedTxPower = 23;
}
EEBTProtocol::~EEBTProtocol()
{
this->cache.~SeqNoCache();
this->cycleWatchDog->~CycleWatchDog();
this->cycleWatchDog = 0;
this->games.clear();
this->packetManager->~EEBTPPacketManager();
this->packetManager = 0;
}
TypeId EEBTProtocol::GetTypeId()
{
static TypeId tid = TypeId("ns3::EEBTPProtocol").SetParent<Object>().AddConstructor<EEBTProtocol>();
return tid;
}
TypeId EEBTProtocol::GetInstanceTypeId() const
{
return GetTypeId();
}
std::ostream &operator<<(std::ostream &os, EEBTProtocol &prot)
{
prot.Print(os);
return os;
}
void EEBTProtocol::Print(std::ostream &os) const
{
Ptr<GameState> gs;
uint64_t gid = 0;
bool found = false;
for (uint i = 0; i < this->games.size(); i++)
{
gs = this->games[i];
if (gs->getGameID() == gid)
{
found = true;
break;
}
}
os << "<===================== Node " << this->device->GetNode()->GetId() << " =====================>\n";
if (!found)
{
os << "NO INFORMATION AVAILABLE\n";
return;
}
else
gs->findHighestTxPowers();
Vector pos = this->device->GetNode()->GetObject<MobilityModel>()->GetPosition();
os << "MAC ADDR:\t\t" << this->myAddress << "\n";
os << "POSITION:\t\tX " << pos.x << ", Y " << pos.y << "\n";
os << "TX POWER:\t\t" << gs->getHighestTxPower() << "dBm\n";
os << "FINISH TIME:\t" << gs->getTimeFinished() << "\n";
Ptr<EEBTPNode> parent = gs->getParent();
if (parent != 0)
os << "PARENT:\t\t\t" << parent->getAddress() << "\n";
else
os << "PARENT:\t\t\tff:ff:ff:ff:ff:ff\n";
os << "\n";
os << "FINISHED:\t\t" << (gs->gameFinished() ? "TRUE" : "FALSE") << "\n";
os << "WAITING FOR:\t";
if (gs->allChildsFinished())
os << "NONE";
else
{
for (int i = 0; i < gs->getNChilds(); i++)
{
os << "\n";
Ptr<EEBTPNode> child = gs->getChild(i);
if (!child->hasFinished())
os << "\t" << child->getAddress();
}
}
os << "\n\n";
os << "LOCKED:\t\t" << (gs->isLocked() ? "TRUE" : "FALSE") << "\n";
os << "LOCKED BY:\t" << (gs->isLocked() ? gs->getLockedBy() : Mac48Address::GetBroadcast()) << "\n";
os << "\n";
os << "HIGHEST TX POWER:\t\t\t" << gs->getHighestTxPower() << "\n";
os << "SECOND HIGHEST TX POWER:\t" << gs->getSecondHighestTxPower() << "\n";
os << "\n";
os << "APP DATA PACKETS:\t\t\t" << this->maxPackets << "\n";
os << "MISSING APP DATA PACKETS:\t" << (this->maxPackets - gs->getApplicationDataHandler()->getPacketCount()) << "\n";
os << "\n";
os << "CYCLES DURING CONSTRUCTION PHASE:\n";
int numCyc = 0;
std::vector<Ptr<CycleInfo>> cycles = this->cycleWatchDog->getCycles(gid, this->device->GetNode()->GetId());
for (Ptr<CycleInfo> ci : cycles)
{
if (ci->isRealCycle())
{
numCyc++;
os << "\t\tTIME: start = " << ci->getStartTime() << ", end = " << ci->getEndTime() << ", duration = " << ci->getDuration() << " | ";
ci->Print(os);
os << "\n";
}
}
os << "TOTAL NUMBER OF CYCLES:\t" << numCyc << "\n";
os << "\n";
os << "MY CHILD NODES:\n";
for (int i = 0; i < gs->getNChilds(); i++)
{
Ptr<EEBTPNode> child = gs->getChild(i);
os << "\tNODE " << child->getAddress() << " => " << child->getReachPower() << "dBm | " << child->getNoise() << "dBm\n";
}
os << "\n";
os << "REACH POWER FOR NODES:\n";
for (uint i = 0; i < gs->getNNeighbors(); i++)
{
Ptr<EEBTPNode> neighbor = gs->getNeighbor(i);
os << "\tNODE " << neighbor->getAddress() << " => " << neighbor->getReachPower() << "dBm | " << neighbor->getNoise() << "dBm\n";
os << "\t\thTx = " << neighbor->getHighestMaxTxPower() << "dBm\n";
os << "\t\tshTx = " << neighbor->getSecondHighestMaxTxPower() << "dBm\n";
}
os << "\n";
os << "ENERGY BY FRAME TYPE:\tRECV | SENT\n";
os << "\tCYCLE_CHECK:\t\t" << this->packetManager->getEnergyByRecvFrame(gid, 0) << "J | " << this->packetManager->getEnergyBySentFrame(gid, 0) << "J\n";
os << "\tNEIGHBOR_DISCOVERY:\t" << this->packetManager->getEnergyByRecvFrame(gid, 1) << "J | " << this->packetManager->getEnergyBySentFrame(gid, 1) << "J\n";
os << "\tCHILD_REQUEST:\t\t" << this->packetManager->getEnergyByRecvFrame(gid, 2) << "J | " << this->packetManager->getEnergyBySentFrame(gid, 2) << "J\n";
os << "\tCHILD_CONFIRMATION:\t" << this->packetManager->getEnergyByRecvFrame(gid, 3) << "J | " << this->packetManager->getEnergyBySentFrame(gid, 3) << "J\n";
os << "\tCHILD_REJECTION:\t" << this->packetManager->getEnergyByRecvFrame(gid, 4) << "J | " << this->packetManager->getEnergyBySentFrame(gid, 4) << "J\n";
os << "\tPARENT_REVOCATION:\t" << this->packetManager->getEnergyByRecvFrame(gid, 5) << "J | " << this->packetManager->getEnergyBySentFrame(gid, 5) << "J\n";
os << "\tEND_OF_GAME:\t\t" << this->packetManager->getEnergyByRecvFrame(gid, 6) << "J | " << this->packetManager->getEnergyBySentFrame(gid, 6) << "J\n";
os << "\tAPPLICATION_DATA:\t" << this->packetManager->getEnergyByRecvFrame(gid, 7) << "J | " << this->packetManager->getEnergyBySentFrame(gid, 7) << "J\n";
os << "TOTAL ENERGY:\t\t\t" << this->packetManager->getTotalEnergyConsumed(gid) << "J\n\n";
uint32_t allFrameTypesSent = 0;
for (int i = 0; i < 8; i++)
allFrameTypesSent += this->packetManager->getFrameTypeSent(gid, i);
os << "DATA SENT BY FRAME TYPE:\tCOUNT\t | DATA\n";
os << "\tCYCLE_CHECK:\t\t\t" << this->packetManager->getFrameTypeSent(gid, 0) << "\t | " << this->packetManager->getDataSentByFrame(gid, 0) << "B\n";
os << "\tNEIGHBOR_DISCOVERY:\t\t" << this->packetManager->getFrameTypeSent(gid, 1) << "\t | " << this->packetManager->getDataSentByFrame(gid, 1) << "B\n";
os << "\tCHILD_REQUEST:\t\t\t" << this->packetManager->getFrameTypeSent(gid, 2) << "\t | " << this->packetManager->getDataSentByFrame(gid, 2) << "B\n";
os << "\tCHILD_CONFIRMATION:\t\t" << this->packetManager->getFrameTypeSent(gid, 3) << "\t | " << this->packetManager->getDataSentByFrame(gid, 3) << "B\n";
os << "\tCHILD_REJECTION:\t\t" << this->packetManager->getFrameTypeSent(gid, 4) << "\t | " << this->packetManager->getDataSentByFrame(gid, 4) << "B\n";
os << "\tPARENT_REVOCATION:\t\t" << this->packetManager->getFrameTypeSent(gid, 5) << "\t | " << this->packetManager->getDataSentByFrame(gid, 5) << "B\n";
os << "\tEND_OF_GAME:\t\t\t" << this->packetManager->getFrameTypeSent(gid, 6) << "\t | " << this->packetManager->getDataSentByFrame(gid, 6) << "B\n";
os << "\tAPPLICATION_DATA:\t\t" << this->packetManager->getFrameTypeSent(gid, 7) << "\t | " << this->packetManager->getDataSentByFrame(gid, 7) << "B\n";
os << "DATA SENT TOTAL:\t\t\t" << allFrameTypesSent << "\t | " << this->packetManager->getDataSent(gid) << "B\n\n";
uint32_t allFrameTypesRecv = 0;
for (int i = 0; i < 8; i++)
allFrameTypesRecv += this->packetManager->getFrameTypeRecv(gid, i);
os << "DATA RECEIVED BY FRAME TYPE:\tCOUNT\t | DATA\n";
os << "\tCYCLE_CHECK:\t\t\t" << this->packetManager->getFrameTypeRecv(gid, 0) << "\t | " << this->packetManager->getDataRecvByFrame(gid, 0) << "B\n";
os << "\tNEIGHBOR_DISCOVERY:\t\t" << this->packetManager->getFrameTypeRecv(gid, 1) << "\t | " << this->packetManager->getDataRecvByFrame(gid, 1) << "B\n";
os << "\tCHILD_REQUEST:\t\t\t" << this->packetManager->getFrameTypeRecv(gid, 2) << "\t | " << this->packetManager->getDataRecvByFrame(gid, 2) << "B\n";
os << "\tCHILD_CONFIRMATION:\t\t" << this->packetManager->getFrameTypeRecv(gid, 3) << "\t | " << this->packetManager->getDataRecvByFrame(gid, 3) << "B\n";
os << "\tCHILD_REJECTION:\t\t" << this->packetManager->getFrameTypeRecv(gid, 4) << "\t | " << this->packetManager->getDataRecvByFrame(gid, 4) << "B\n";
os << "\tPARENT_REVOCATION:\t\t" << this->packetManager->getFrameTypeRecv(gid, 5) << "\t | " << this->packetManager->getDataRecvByFrame(gid, 5) << "B\n";
os << "\tEND_OF_GAME:\t\t\t" << this->packetManager->getFrameTypeRecv(gid, 6) << "\t | " << this->packetManager->getDataRecvByFrame(gid, 6) << "B\n";
os << "\tAPPLICATION_DATA:\t\t" << this->packetManager->getFrameTypeRecv(gid, 7) << "\t | " << this->packetManager->getDataRecvByFrame(gid, 7) << "B\n";
os << "DATA RECEIVED TOTAL:\t\t\t" << allFrameTypesRecv << "\t | " << this->packetManager->getDataRecv(gid) << "B\n";
os << "<==================================================>\n";
}
/*
* Statistics Getter
*/
double EEBTProtocol::getEnergyByFrameType(uint64_t gid, FRAME_TYPE ft)
{
double energy = 0;
energy += this->packetManager->getEnergyByRecvFrame(gid, ft);
energy += this->packetManager->getEnergyBySentFrame(gid, ft);
return energy;
}
double EEBTProtocol::getEnergyForConstruction(uint64_t gid)
{
double energy = 0;
for (uint8_t i = 0; i < 7; i++)
{
energy += this->packetManager->getEnergyByRecvFrame(gid, i);
energy += this->packetManager->getEnergyBySentFrame(gid, i);
}
return energy;
}
double EEBTProtocol::getEnergyForApplicationData(uint64_t gid)
{
double energy = 0;
//energy += this->packetManager->getEnergyByRecvFrame(gid, 7);
energy += this->packetManager->getEnergyBySentFrame(gid, 7);
return energy;
}
Ptr<CycleWatchDog> EEBTProtocol::getCycleWatchDog()
{
return this->cycleWatchDog;
}
Ptr<EEBTPPacketManager> EEBTProtocol::getPacketManager()
{
return this->packetManager;
}
Ptr<NetDevice> EEBTProtocol::GetDevice()
{
return this->device;
}
/*
* This method searches for the GameState with the gameID `gid`
* If there is no such GameState, it creates a new one and stores
* it in the games list
*/
Ptr<GameState> EEBTProtocol::getGameState(uint64_t gid)
{
for (uint i = 0; i < this->games.size(); i++)
{
Ptr<GameState> gs = this->games[i];
if (gs->getGameID() == gid)
return gs;
}
Ptr<GameState> gs = Create<GameState>(false, gid);
gs->setMyAddress(this->myAddress);
this->games.push_back(gs);
return this->games[this->games.size() - 1];
}
Ptr<GameState> EEBTProtocol::initGameState(uint64_t gid)
{
for (uint i = 0; i < this->games.size(); i++)
{
Ptr<GameState> gs = this->games[i];
if (gs->getGameID() == gid)
return gs;
}
Ptr<GameState> gs = Create<GameState>(true, gid);
gs->setMyAddress(this->myAddress);
this->games.push_back(gs);
return this->games[this->games.size() - 1];
}
void EEBTProtocol::removeGameState(uint64_t gid)
{
uint i = 0;
for (; i < this->games.size(); i++)
{
if (this->games[i]->getGameID() == gid)
break;
}
if (i < this->games.size())
games.erase(this->games.begin() + i, this->games.begin() + (i + 1));
}
/*
* Install method to install this protocol on the stack of a node
*/
void EEBTProtocol::Install(Ptr<WifiNetDevice> netDevice, Ptr<CycleWatchDog> cwd)
{
this->device = netDevice;
this->cycleWatchDog = cwd;
this->random = CreateObject<UniformRandomVariable>();
this->wifiPhy = this->device->GetMac()->GetWifiPhy();
this->myAddress = Mac48Address::ConvertFrom(this->device->GetAddress());
//Set the number of power levels to one to ensure the node sends with a constant power
this->wifiPhy->SetNTxPower(1);
//Set the max allowed transmission power according to the selected standard
switch (this->wifiPhy->GetStandard())
{
case WIFI_PHY_STANDARD_80211a:
case WIFI_PHY_STANDARD_80211n_5GHZ:
this->maxAllowedTxPower = 23.0;
break;
case WIFI_PHY_STANDARD_80211b:
case WIFI_PHY_STANDARD_80211g:
case WIFI_PHY_STANDARD_80211n_2_4GHZ:
default:
this->maxAllowedTxPower = 20.0;
break;
}
this->wifiPhy->SetTxPowerEnd(this->maxAllowedTxPower);
this->wifiPhy->SetTxPowerStart(this->maxAllowedTxPower);
//Register a callback for incoming packets to read the rxPower, SNR and noise levels
this->packetManager = Create<EEBTPPacketManager>();
this->packetManager->setDevice(this->device);
this->wifiPhy->TraceConnectWithoutContext("PhyRxBegin", MakeCallback(&EEBTPPacketManager::onRxStart, this->packetManager));
this->wifiPhy->TraceConnectWithoutContext("PhyRxEnd", MakeCallback(&EEBTPPacketManager::onRxEnd, this->packetManager));
this->wifiPhy->TraceConnectWithoutContext("PhyTxBegin", MakeCallback(&EEBTPPacketManager::onTxStart, this->packetManager));
this->wifiPhy->TraceConnectWithoutContext("PhyTxDrop", MakeCallback(&EEBTPPacketManager::onTxDrop, this->packetManager));
this->wifiPhy->TraceConnectWithoutContext("PhyTxEnd", MakeCallback(&EEBTPPacketManager::onTxEnd, this->packetManager));
this->wifiPhy->TraceConnectWithoutContext("MonitorSnifferRx", MakeCallback(&EEBTPPacketManager::onPacketRx, this->packetManager));
this->wifiPhy->TraceConnectWithoutContext("MonitorSnifferTx", MakeCallback(&EEBTPPacketManager::onPacketTx, this->packetManager));
this->device->GetMac()->TraceConnectWithoutContext("TxOkHeader", MakeCallback(&EEBTPPacketManager::onTxSuccessful, this->packetManager));
this->device->GetMac()->TraceConnectWithoutContext("MacTxDrop", MakeCallback(&EEBTPPacketManager::onTxDropped, this->packetManager));
this->device->GetMac()->TraceConnectWithoutContext("MacTx", MakeCallback(&EEBTPPacketManager::onTx, this->packetManager));
this->device->GetMac()->TraceConnectWithoutContext("TxErrHeader", MakeCallback(&EEBTPPacketManager::onTxFailed, this->packetManager));
this->device->GetMac()->GetWifiRemoteStationManager()->TraceConnectWithoutContext("MacTxFinalRtsFailed", MakeCallback(&EEBTPPacketManager::onTxFinalRtsFailed, this->packetManager));
this->device->GetMac()->GetWifiRemoteStationManager()->TraceConnectWithoutContext("MacTxFinalDataFailed", MakeCallback(&EEBTPPacketManager::onTxFinalDataFailed, this->packetManager));
this->ndInterval = this->device->GetMac()->GetSlot().GetMicroSeconds() * 2000;
TrafficControlHelper tch = TrafficControlHelper();
tch.Install(this->device);
this->tcl = this->device->GetNode()->GetObject<TrafficControlLayer>();
this->device->GetNode()->RegisterProtocolHandler(MakeCallback(&TrafficControlLayer::Receive, this->tcl), EEBTProtocol::PROT_NUMBER, this->device);
this->tcl->RegisterProtocolHandler(MakeCallback(&EEBTProtocol::Receive, this), EEBTProtocol::PROT_NUMBER, this->device);
//this->device->GetNode()->RegisterProtocolHandler(MakeCallback(&EEBTProtocol::Receive, this), EEBTProtocol::PROT_NUMBER, this->device);
}
/*
* TxPower calculation
* Calculates the required transmission power to reach a given node (reachPower)
* rxPower (dBm)
* txPower (dBm)
* noise (dBm)
* minSNR (dB)
*/
double EEBTProtocol::calculateTxPower(double rxPower, double txPower, double noise, double minSNR)
{
double snr = rxPower - noise;
double neededPower = (txPower - (snr - minSNR));
if (neededPower <= this->maxAllowedTxPower)
neededPower = std::min(neededPower + 5, this->maxAllowedTxPower);
//NS_LOG_DEBUG("rxPower = " << rxPower << "dBm, txPower = " << txPower << "dBm, noise = " << noise << "dBm, SNR = " << (rxPower - noise) << "dB, minSNR = " << minSNR << "dB, neededPower = " << neededPower);
return neededPower;
}
/*
* Receive method
* Every packet with the protocol ID 'PROT_NUMBER' will be redirected to this method
* This methods takes
* - device: the NetDevice on which the packet arrived
* - packet: the actual Packet
* - pID: the protocol ID
* - sender: the sender of this packet
* - receiver: the receiver of this packet
* - ptype: the type of this packet
*/
void EEBTProtocol::Receive(Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t pID, const Address &sender, const Address &receiver, NetDevice::PacketType pType)
{
EEBTPTag tag;
EEBTPHeader header;
Ptr<WifiNetDevice> dev = DynamicCast<WifiNetDevice>(device);
Mac48Address sender_addr = Mac48Address::ConvertFrom(sender);
Mac48Address receiver_addr = Mac48Address::ConvertFrom(receiver);
//Get packet header
packet->PeekHeader(header);
//Get packet tag
tag = this->packetManager->getPacketTag(header.GetSequenceNumber());
NS_LOG_DEBUG("[Node " << dev->GetNode()->GetId() << " / " << Now() << "]: [" << this->myAddress << "] received packet from " << sender_addr << " / SeqNo: " << header.GetSequenceNumber() << " / FRAME_TYPE: " << (int)header.GetFrameType() << " / txPower: " << header.GetTxPower() << "dBm / noise: " << tag.getNoise() << "dBm");
NS_LOG_DEBUG("\thTx: " << header.GetHighestMaxTxPower() << ", shTx: " << header.GetSecondHighestMaxTxPower());
//Check for duplicated sequence number
if (this->cache.checkForDuplicate(sender_addr, header.GetSequenceNumber()))
{
NS_LOG_DEBUG("Received duplicated frame from '" << sender_addr << "' with GID " << header.GetGameId() << " and SeqNo " << header.GetSequenceNumber());
return;
}
//Get the GameState
Ptr<GameState> gs = this->getGameState(header.GetGameId());
//Update frame type seq no
if (gs->checkLastFrameType(sender_addr, header.GetFrameType(), header.GetSequenceNumber()))
gs->updateLastFrameType(sender_addr, header.GetFrameType(), header.GetSequenceNumber());
else if (header.GetFrameType() != CYCLE_CHECK)
{
NS_LOG_DEBUG("\tReceived a frame with the same type and a higher sequence number earlier. Ignoring this packet");
return;
}
//Update frame type seq no
gs->updateLastFrameType(sender_addr, header.GetFrameType(), header.GetSequenceNumber());
//Check if node is in our neighbor list
if (!gs->isNeighbor(sender_addr))
gs->addNeighbor(sender_addr);
//Get the EEBTPNode
Ptr<EEBTPNode> node = gs->getNeighbor(sender_addr);
if (header.GetFrameType() != APPLICATION_DATA)
{
node->setParentAddress(header.GetParent());
node->setReachPower(this->calculateTxPower(tag.getSignal(), header.GetTxPower(), tag.getNoise(), tag.getMinSNR()));
node->updateRxInfo(tag.getSignal(), tag.getNoise());
node->setHighestMaxTxPower(header.GetHighestMaxTxPower());
node->setSecondHighestMaxTxPower(header.GetSecondHighestMaxTxPower());
node->setFinished(header.getGameFinishedFlag());
gs->findHighestTxPowers();
//If the node has a reachpower that is higher than our maximum allowed txPower
if (node->getReachPower() > this->maxAllowedTxPower)
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "/" << Now() << "]: ReachPower for node [" << node->getAddress() << "] is too high: " << node->getReachPower() << "dBm > " << this->maxAllowedTxPower << "dBm; FRAME_TYPE: " << (uint)header.GetFrameType());
//Mark node with the reach problem flag
node->hasReachPowerProblem(true);
//If this node is a child of us
if (gs->isChild(node) && header.GetFrameType() != PARENT_REVOCATION)
{
gs->updateLastFrameType(sender_addr, PARENT_REVOCATION, header.GetSequenceNumber());
//Handle as parent revocation
this->handleParentRevocation(gs, node);
//And reject the connection
this->Send(gs, CHILD_REJECTION, node->getAddress(), node->getReachPower());
return;
}
}
else
{
node->hasReachPowerProblem(false);
//NS_LOG_DEBUG("ReachPower for node [" << node->getAddress() << "]: " << node->getReachPower() << "dBm | hTx: " << node->getHighestMaxTxPower() << "dBm, shTx: " << node->getSecondHighestMaxTxPower() << "dBm");
}
//If node had receiving problems
if (header.hadReceivingProblems())
{
node->hasReachPowerProblem(true);
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "/" << Now() << "]: Recipient had receive problems. New frame type is " << (uint)(header.GetFrameType() & 0b01111111));
//and node is a child of us
if (gs->isChild(node) && header.GetFrameType() != PARENT_REVOCATION)
{
//and its reachpower is too high
if (node->getReachPower() > this->maxAllowedTxPower)
{
gs->updateLastFrameType(sender_addr, PARENT_REVOCATION, header.GetSequenceNumber());
//Handle as parent revocation
this->handleParentRevocation(gs, node);
//And reject the connection
this->Send(gs, CHILD_REJECTION, node->getAddress(), node->getReachPower());
return;
}
else
{
//send out updated information
gs->resetUnchangedCounter();
this->Send(gs, NEIGHBOR_DISCOVERY, this->maxAllowedTxPower);
}
}
}
}
switch (header.GetFrameType())
{
case CYCLE_CHECK:
//If we are the recipient, we need some information from our neighbor list which intermediate nodes could not have since they don't have these neighbors
if (receiver_addr == header.GetOriginator())
this->handleCycleCheck(gs, node, Create<EEBTPNode>(header.GetOriginator(), this->maxAllowedTxPower), gs->getNeighbor(header.GetNewParent()), gs->getNeighbor(header.GetOldParent()));
else
{
Ptr<EEBTPNode> newParent = gs->getNeighbor(header.GetNewParent());
if (newParent == 0)
newParent = Create<EEBTPNode>(header.GetNewParent(), this->maxAllowedTxPower);
Ptr<EEBTPNode> oldParent = gs->getNeighbor(header.GetOldParent());
if (oldParent == 0)
oldParent = Create<EEBTPNode>(header.GetOldParent(), this->maxAllowedTxPower);
if (gs->isChild(header.GetOriginator()))
this->handleCycleCheck(gs, node, gs->getNeighbor(header.GetOriginator()), newParent, oldParent);
else
this->handleCycleCheck(gs, node, Create<EEBTPNode>(header.GetOriginator(), this->maxAllowedTxPower), newParent, oldParent);
}
break;
case NEIGHBOR_DISCOVERY:
this->handleNeighborDiscovery(gs, node);
break;
case CHILD_REQUEST:
this->handleChildRequest(gs, node);
break;
case CHILD_CONFIRMATION:
this->handleChildConfirmation(gs, node);
break;
case CHILD_REJECTION:
this->handleChildRejection(gs, node);
break;
case PARENT_REVOCATION:
this->handleParentRevocation(gs, node);
break;
case END_OF_GAME:
this->handleEndOfGame(gs, node);
break;
case APPLICATION_DATA:
this->handleApplicationData(gs, node, packet->Copy());
break;
default:
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Invalid frame type: " << (int)header.GetFrameType());
}
node->resetReachPowerChanged();
//Print new line to separate events
NS_LOG_DEBUG("\n");
}
/*
* Send methods
*
* The regular send method just takes the frame type, recipient and transmission power,
* build the EEBTPHeader with information from the GameState and hand it over to the final send method.
*
* There is also a send method for quick broadcast. It just takes the frame type and txPower and adds
* the broadcast address as recipient.
*
* To initialize a cycle check there is a send method which takes the originator, newParent and oldParent
* to build the EEBTPHeader and send it to the parent node
*
* The final send method takes care of repeating events (SendEvent), the sequence number and transmission power
*/
void EEBTProtocol::Send(Ptr<GameState> gs, FRAME_TYPE ft, Mac48Address recipient, double txPower)
{
EEBTPHeader header = EEBTPHeader();
header.SetFrameType(ft);
if (ft == CHILD_REJECTION && gs->isChild(recipient))
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Node [" << recipient << "] is a child of mine, but it is marked as a child...");
gs->removeChild(gs->getNeighbor(recipient));
}
this->Send(gs, header, recipient, txPower);
}
//Send method for Sendevent to check if a packet needs retransmission or not
void EEBTProtocol::Send(Ptr<GameState> gs, FRAME_TYPE ft, Mac48Address recipient, uint16_t seqNo, double txPower, Ptr<SendEvent> event)
{
if (ft == CHILD_REQUEST || ft == CHILD_CONFIRMATION || ft == CHILD_REJECTION || ft == PARENT_REVOCATION || ft == END_OF_GAME)
{
Ptr<EEBTPNode> node = gs->getNeighbor(recipient);
if (node == 0)
node = Create<EEBTPNode>(recipient, this->maxAllowedTxPower);
if (ft == CHILD_REQUEST && gs->getContactedParent() != node)
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Packet with seqNo " << seqNo << " will not be retransmitted since the recipient is not our contacted parent");
return;
}
if (this->packetManager->isPacketAcked(seqNo))
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Packet with seqNo " << seqNo << " has been acked. No retransmission, time = " << Now());
this->packetManager->deleteSeqNoEntry(seqNo);
return;
}
else if (this->packetManager->isPacketLost(seqNo) || event->getNTimes() > 20)
{
if (event->getNTimes() > 20)
{
if (ft == CHILD_REQUEST && txPower >= this->maxAllowedTxPower)
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Packet with seqNo " << seqNo << " has not been acked yet. Cancle..., time = " << Now());
this->handleChildRejection(gs, gs->getNeighbor(recipient));
}
else
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Packet with seqNo " << seqNo << " has not been acked yet. Retransmitting..., time = " << Now());
}
else
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Packet with seqNo " << seqNo << " has been lost. Retransmitting..., time = " << Now());
this->packetManager->deleteSeqNoEntry(seqNo);
EEBTPHeader header;
header.SetFrameType(ft);
header.SetSequenceNumber(seqNo);
this->Send(gs, header, recipient, txPower + 1, true);
}
else
{
Time ttw = this->device->GetMac()->GetAckTimeout() * 200;
Simulator::Schedule(ttw, event);
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Rescheduled SendEvent(" << seqNo << ") for " << (Now() + ttw) << ", now = " << Now());
}
}
else
{
//NS_FATAL_ERROR("[Node " << this->device->GetNode()->GetId() << "]: Invalid frametype to send: " << (uint)ft);
this->Send(gs, ft, recipient, txPower);
}
}
//Send method for quick broadcasts
void EEBTProtocol::Send(Ptr<GameState> gs, FRAME_TYPE ft, double txPower)
{
this->Send(gs, ft, Mac48Address::GetBroadcast(), txPower);
}
//Send method for initializing cycle checks
void EEBTProtocol::Send(Ptr<GameState> gs, Mac48Address originator, Mac48Address newParent, Mac48Address oldParent)
{
EEBTPHeader header = EEBTPHeader();
header.SetFrameType(CYCLE_CHECK);
header.SetOriginator(originator);
header.SetNewParent(newParent);
header.SetOldParent(oldParent);
//If we have a parent, send cycle check to our parent
//If we are currently switching our parent, it is not necessary since we will send a cycle check after connection successfully
if (gs->getParent() != 0)
this->Send(gs, header, gs->getParent()->getAddress(), gs->getParent()->getReachPower());
else
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Cannot send cycle check since we have no parent");
}
//Send method for CCSendevent to check if a packet needs retransmission or not
void EEBTProtocol::Send(Ptr<GameState> gs, Mac48Address originator, Mac48Address newParent, Mac48Address oldParent, uint16_t seqNo, double txPower, Ptr<CCSendEvent> event)
{
if (this->packetManager->isPacketAcked(seqNo))
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Packet with seqNo " << seqNo << " has been acked. No retransmission, time = " << Now());
this->packetManager->deleteSeqNoEntry(seqNo);
return;
}
else if (this->packetManager->isPacketLost(seqNo) || event->getNTimes() > 20)
{
if (event->getNTimes() > 20)
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Packet with seqNo " << seqNo << " has not been acked yet. Retransmitting..., time = " << Now());
else
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Packet with seqNo " << seqNo << " has been lost. Retransmitting..., time = " << Now());
this->packetManager->deleteSeqNoEntry(seqNo);
EEBTPHeader header = EEBTPHeader();
header.SetFrameType(CYCLE_CHECK);
header.SetOriginator(originator);
header.SetNewParent(newParent);
header.SetOldParent(oldParent);
header.SetSequenceNumber(seqNo);
//If we have a parent, send cycle check to our parent
//If we are currently switching our parent, it is not necessary since we will send a cycle check after connection successfully
if (gs->getParent() != 0)
this->Send(gs, header, gs->getParent()->getAddress(), gs->getParent()->getReachPower() + 1, true);
else
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Cannot send cycle check since we have no parent");
}
else
{
Time ttw = this->device->GetMac()->GetAckTimeout() * 200;
//Simulator::Schedule(ttw, Create<CCSendEvent>(gs, this, originator, newParent, oldParent, txPower, seqNo));
Simulator::Schedule(ttw, event);
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Rescheduled CCSendEvent(" << seqNo << ") for " << (Now() + ttw) << ", now = " << Now());
}
}
void EEBTProtocol::Send(Ptr<GameState> gs, EEBTPHeader header, Mac48Address recipient, double txPower)
{
this->Send(gs, header, recipient, txPower, false);
}
//Final send method
void EEBTProtocol::Send(Ptr<GameState> gs, EEBTPHeader header, Mac48Address recipient, double txPower, bool isRetransmission)
{
//Adjust the transmission power
header.setReceivingProblems(false);
if (txPower > this->maxAllowedTxPower)
{
txPower = this->maxAllowedTxPower;
header.setReceivingProblems(true);
}
gs->findHighestTxPowers();
//Set sequence number
if (!isRetransmission)
this->cache.injectSeqNo(&header);
if (header.GetFrameType() == NEIGHBOR_DISCOVERY) //Check the neighbor discovery event handler (SendEvent)
{
if (!this->checkNeigborDiscoverySendEvent(gs))
return;
}
//Packet size must be greater than 0 to prevent this error in visualized mode:
// assert failed. cond="m_current >= m_dataStart && m_current < m_dataEnd"
Ptr<Packet> packet = Create<Packet>(1);
header.SetGameId(gs->getGameID());
header.SetTxPower(txPower);
if (gs->getParent() == 0)
header.SetParent(Mac48Address::GetBroadcast());
else
header.SetParent(gs->getParent()->getAddress());
header.SetHighestMaxTxPower(gs->getHighestTxPower());
header.SetSecondHighestMaxTxPower(gs->getSecondHighestTxPower());
header.setGameFinishedFlag(gs->gameFinished());
packet->AddHeader(header);
if (header.GetFrameType() == CYCLE_CHECK)
{
Time ttw = this->device->GetMac()->GetAckTimeout() * 100;
Simulator::Schedule(ttw, Create<CCSendEvent>(gs, this, header.GetOriginator(), header.GetNewParent(), header.GetOldParent(), txPower, header.GetSequenceNumber()));
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Scheduled CCSendEvent(" << header.GetSequenceNumber() << ") for " << (Now() + ttw));
}
else if (header.GetFrameType() == CHILD_REQUEST || header.GetFrameType() == CHILD_CONFIRMATION || header.GetFrameType() == CHILD_REJECTION || header.GetFrameType() == PARENT_REVOCATION || header.GetFrameType() == END_OF_GAME)
{
Time ttw = this->device->GetMac()->GetAckTimeout() * 100;
EventId id = Simulator::Schedule(ttw, Create<SendEvent>(gs, this, (FRAME_TYPE)header.GetFrameType(), recipient, txPower, header.GetSequenceNumber()));
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Scheduled SendEvent(" << (uint)header.GetFrameType() << ") for " << (Now() + ttw) << ". EventID = " << id.GetUid());
}
/*Ptr<EEBTPQueueDiscItem> qdi = Create<EEBTPQueueDiscItem>(packet, recipient, EEBTProtocol::PROT_NUMBER);
this->tcl->Send(this->device, qdi);*/
EEBTPTag tag;
tag.setGameID(gs->getGameID());
tag.setFrameType(header.GetFrameType());
tag.setSequenceNumber(header.GetSequenceNumber());
tag.setTxPower(header.GetTxPower());
packet->AddPacketTag(tag);
this->packetManager->sendPacket(packet, recipient);
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: EEBTProtocol::Send(): " << this->myAddress << " => " << recipient << " / SeqNo: " << header.GetSequenceNumber() << " / FRAME_TYPE: " << (uint)header.GetFrameType() << " / txPower: " << header.GetTxPower() << "/" << this->wifiPhy->GetTxPowerStart() << "|" << this->wifiPhy->GetTxPowerEnd() << "dBm");
NS_LOG_DEBUG("\thTx: " << header.GetHighestMaxTxPower() << ", shTx: " << header.GetSecondHighestMaxTxPower() << ", rounds: " << gs->getUnchangedCounter() << "/" << ((gs->getNNeighbors() * 0.5) + 2));
}
/*
* Handle the cycle check (FrameType 0)
*/
void EEBTProtocol::handleCycleCheck(Ptr<GameState> gs, Ptr<EEBTPNode> node, Ptr<EEBTPNode> originator, Ptr<EEBTPNode> newParent, Ptr<EEBTPNode> oldParent)
{
NS_ASSERT_MSG(originator != 0, "originator is null");
NS_ASSERT_MSG(newParent != 0, "newParent is null");
NS_ASSERT_MSG(oldParent != 0, "oldParent is null");
NS_LOG_DEBUG(this->device->GetNode()->GetId() << " => EEBTProtocol::handleCycleCheck() | FROM: " << node->getAddress() << " | ORIG: " << originator->getAddress() << " | nP: " << newParent->getAddress() << " | oP: " << oldParent->getAddress());
if (gs->isInitiator())
{
//NS_LOG_DEBUG("Ignoring cycle check from " << node->getAddress() << " because I am the initiator");
}
else if (originator->getAddress() == this->myAddress)
{
if (gs->getParent() != 0 && newParent->getAddress() == gs->getParent()->getAddress())
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Cycle detected! Connecting to last parent (" << oldParent->getAddress() << ") and blacklisting (" << newParent->getAddress() << "," << newParent->getParentAddress() << ")");
//Set my parent with its parent on the blacklist
gs->updateBlacklist(gs->getParent());
this->disconnectOldParent(gs);
//Remove all my previous parents from the list until the oldParent occurs
if (gs->hasLastParents())
{
Ptr<EEBTPNode> p = gs->popLastParent();
while (p != oldParent && gs->hasLastParents())
p = gs->popLastParent();
}
//Try to connect to one of our last parents
//We get automatically disconnected from our current parent
Ptr<EEBTPNode> lastParent = gs->popLastParent();
while (lastParent != 0)
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Last parent is also blacklisted, contacting another last parent [" << lastParent->getAddress() << "]");
this->contactNode(gs, lastParent);
//If we cannot contact lastParent, get next lastParent from stack, else break the loop
if (gs->getContactedParent() == 0)
lastParent = gs->popLastParent();
else
break;
}
//If we could not contact a lastParent, search for cheapest neighbor
if (gs->getContactedParent() == 0)
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: None of our last parents is an option, searching cheapest neighbor...");
this->contactCheapestNeighbor(gs);
//If we are not able to find a valid cheapest neighbor, disconnect child nodes and try again
if (gs->getContactedParent() == 0)
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Unable to connect to any other node. Disconnecting children...");
this->disconnectAllChildNodes(gs);
this->contactCheapestNeighbor(gs);
}
}
}
else
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Cycle detected! newParent: " << newParent->getAddress() << ", currentParent: " << ((gs->getParent() != 0) ? gs->getParent()->getAddress() : "ff:ff:ff:ff:ff:ff"));
//Blacklist newParent, since this route creates a cycle
gs->updateBlacklist(newParent);
}
}
else
{
//We are not the originator of that packet nor the initiator of the game. Sending this packet to our parent
this->Send(gs, originator->getAddress(), newParent->getAddress(), oldParent->getAddress());
}
}
/*
* Handle neighbor discovery (FrameType 1)
*/
void EEBTProtocol::handleNeighborDiscovery(Ptr<GameState> gs, Ptr<EEBTPNode> node)
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: EEBTProtocol::handleNeighborDiscovery()");
if (gs->isInitiator()) //Check if I am the initiator. If yes, we can ignore this packet
{
//NS_LOG_DEBUG("Ignoring neighbor discovery from " << node->getAddress() << " because I am the initiator");
}
else if (gs->isBlacklisted(node)) //If the sender is on our blacklist due to a cycle, ignore
{
//NS_LOG_DEBUG("Ignoring neighbor discovery from " << node->getAddress() << " because it and its parent (" << node->getParentAddress() << ") are blacklisted.");
}
else if (gs->isChild(node)) //Check if sender is child of me
{
//NS_LOG_DEBUG("Ignoring neighbor discovery from " << node->getAddress() << " because it is a child of mine");
//If the reachpower of this child changed, inform neighbors
if (node->reachPowerChanged() && gs->gameFinished())
this->Send(gs, NEIGHBOR_DISCOVERY, this->maxAllowedTxPower);
}
else if (node->getReachPower() > this->maxAllowedTxPower)
{
//NS_LOG_DEBUG("Ignoring neighbor discovery from " << node->getAddress() << " because I cannot reach it (" << node->getReachPower() << " dBm)");
}
else if (node->getConnCounter() > 5)
{
//NS_LOG_DEBUG("Ignoring neighbor discovery from " << node->getAddress() << " because connection counter exceeds the maximum");
}
else if (node == gs->getLastParent())
{
//NS_LOG_DEBUG("Ignoring neighbor discovery from " << node->getAddress() << " because it is my last parent");
}
else if (gs->getContactedParent() == 0)
{
//If we have no parent and are not connecting to on
if (gs->getParent() == 0)
{
gs->resetRejectionCounter();
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Switching to [" << node->getAddress() << "] because of neighbor discovery");
return this->contactNode(gs, node);
}
else if (gs->getParent() != 0 && node != gs->getParent())
{
//Cost of current connection (connCost) is 0 if we are (one of) the nodes that are the farthest away
Ptr<EEBTPNode> parent = gs->getParent();
double connCost = (parent->getHighestMaxTxPower() - parent->getReachPower());
//Only if we are (one of) the nodes that are the farthest away, it is useful to switch (else no savings)
if (connCost <= 0.00001 && connCost >= -0.00001)
{
//TX power our parent can save, if we leave
double saving = DbmToW(gs->getParent()->getHighestMaxTxPower()) - DbmToW(gs->getParent()->getSecondHighestMaxTxPower());
//Cost of the new connection is the difference between the node's highest tx power and the reach power to this node
double costOfNewConn = DbmToW(node->getReachPower()) - DbmToW(node->getHighestMaxTxPower());
//If we are actually saving tx power, switch
if (costOfNewConn <= saving)
{
if (costOfNewConn < saving + 0.0001 && costOfNewConn > saving - 0.0001 && (gs->gameFinished() || gs->getUnchangedCounter() >= EEBTProtocol::MAX_UNCHANGED_ROUNDS))
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Ignoring neighbor discovery from [" << node->getAddress() << "] since we cannot realy save energy an we had too many unchanged roundes");
else
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Switching to [" << node->getAddress() << "] because of neighbor discovery");
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: "
<< "rp(" << node->getAddress() << ") = " << node->getReachPower() << ", "
<< "hTx(" << node->getAddress() << ") = " << node->getHighestMaxTxPower() << ", "
<< "shTx(" << node->getAddress() << ") = " << node->getSecondHighestMaxTxPower());
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: "
<< "rp(" << gs->getParent()->getAddress() << ") = " << gs->getParent()->getReachPower() << ", "
<< "hTx(" << gs->getParent()->getAddress() << ") = " << gs->getParent()->getHighestMaxTxPower() << ", "
<< "shTx(" << gs->getParent()->getAddress() << ") = " << gs->getParent()->getSecondHighestMaxTxPower());
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: saving = " << saving << "W, costOfNewConn = " << costOfNewConn << "W");
this->contactNode(gs, node);
}
}
else
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << " / " << Now() << "]: [" << node->getAddress() << "] is NOT a better choice than our current parent");
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: "
<< "rp(" << node->getAddress() << ") = " << node->getReachPower() << ", "
<< "hTx(" << node->getAddress() << ") = " << node->getHighestMaxTxPower() << ", "
<< "shTx(" << node->getAddress() << ") = " << node->getSecondHighestMaxTxPower());
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: "
<< "rp(" << gs->getParent()->getAddress() << ") = " << gs->getParent()->getReachPower() << ", "
<< "hTx(" << gs->getParent()->getAddress() << ") = " << gs->getParent()->getHighestMaxTxPower() << ", "
<< "shTx(" << gs->getParent()->getAddress() << ") = " << gs->getParent()->getSecondHighestMaxTxPower());
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: saving = " << saving << ", costOfNewConn = " << costOfNewConn);
}
}
else
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << " / " << Now() << "]: Ignoring neighbor discovery from [" << node->getAddress() << "] since we cannot save energy by leaving our parent: "
<< DbmToW(gs->getParent()->getHighestMaxTxPower()) << " - " << DbmToW(gs->getParent()->getSecondHighestMaxTxPower()) << " = " << gs->getCostOfCurrentConn());
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: "
<< "rp(" << gs->getParent()->getAddress() << ") = " << gs->getParent()->getReachPower() << ", "
<< "hTx(" << gs->getParent()->getAddress() << ") = " << gs->getParent()->getHighestMaxTxPower() << ", "
<< "shTx(" << gs->getParent()->getAddress() << ") = " << gs->getParent()->getSecondHighestMaxTxPower());
}
}
}
}
/*
* Handle child request (FrameType 2)
* Sent by child, received by parent
* if a child wants to connect to the parent node
*/
void EEBTProtocol::handleChildRequest(Ptr<GameState> gs, Ptr<EEBTPNode> node)
{
//NS_LOG_DEBUG(this->device->GetNode()->GetId() << " => EEBTProtocol::handleChildRequest()");
//Check if we received a parent revocation after this child request
if (gs->checkLastFrameType(node->getAddress(), CHILD_REQUEST, gs->getLastSeqNo(node->getAddress(), PARENT_REVOCATION)))
{
NS_LOG_DEBUG("[Node " << this->device->GetNode()->GetId() << "]: Child request from [" << node->getAddress() << "] dismissed because we received a PARENT_REVOCATION with a higher sequence number earlier.");
return;
}
//Reject child request if the reach power for this node exceeds our maxAllowedTxPower