-
Notifications
You must be signed in to change notification settings - Fork 0
/
DevMac.cc
1450 lines (1183 loc) · 51.9 KB
/
DevMac.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
//
// Copyright (C) 2006 Andras Varga and Levente M�sz�ros
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//
#include <algorithm>
#include "DevMac.h"
#include "RadioState.h"
//#include "PhyState.h"
#include "IInterfaceTable.h"
#include "InterfaceTableAccess.h"
#include "PhyControlInfo_m.h"
#include "Ieee802Ctrl_m.h"
#include <math.h>
#include <ctime>
// Uniform from interval [0,1)
Define_Module(DevMac);
// don't forget to keep synchronized the C++ enum and the runtime enum definition
Register_Enum(DevMac,
(DevMac::IDLE,
DevMac::DEFER,
DevMac::WAITDIFS,
DevMac::BACKOFF,
DevMac::WAITACK,
DevMac::WAITBROADCAST,
DevMac::WAITCTS,
DevMac::WAITSIFS,
DevMac::RECEIVE));
// don't forget to keep synchronized the C++ enum and the runtime enum definition
Register_Enum(RadioState,
(RadioState::IDLE,
RadioState::RECV,
RadioState::TRANSMIT,
RadioState::SLEEP));
Register_Enum(PhyState,
(PhyState::IDLE,
PhyState::BUSY));
/****************************************************************
* Construction functions.
*/
DevMac::DevMac()
{
endBackoff = NULL;
endTimeout = NULL;
endReserve = NULL;
mediumStateChange = NULL;
pendingRadioConfigMsg = NULL;
}
DevMac::~DevMac()
{
cancelAndDelete(endTimeout);
cancelAndDelete(mediumStateChange);
if (pendingRadioConfigMsg)
delete pendingRadioConfigMsg;
}
/****************************************************************
* Initialization functions.
*/
void DevMac::initialize(int stage)
{
WirelessMacBase::initialize(stage);
if (stage == 0)
{
EV << "Initializing stage 0\n";
// initialize parameters
maxQueueSize = par("maxQueueSize");
bitrate = par("bitrate");
basicBitrate = 600000; //FIXME make it parameter
const char *addressString = par("address");
if (!strcmp(addressString, "auto")) {
// assign automatic address
address = MACAddress::generateAutoAddress();
// change module parameter from "auto" to concrete address
par("address").setStringValue(address.str().c_str());
}
else
address.setAddress(addressString);
numReceived = 0;
// subscribe for the information of the carrier sense
nb->subscribe(this, NF_RADIOSTATE_CHANGED);
// initalize self messages
endTimeout = new cMessage("Timeout");
mediumStateChange = new cMessage("MediumStateMange");
ackTimeout = new cMessage("ack-not-received-timeout");
delay_timer_30 = new cMessage("delay_timer_30");
backoffTime = new cMessage("backoffTime");
// create a new cMessage here
// interface
registerInterface();
// obtain pointer to external queue
// initializeQueueModule();
// state variables
fsm.setName("DevMac State Machine");
sequenceNumber = 0;
radioState = RadioState::IDLE;
retryCounter = 0;
backoff = false;
// statistics
numRetryNode0 = 0;
numRetryNode1 = 0;
numRetryNode2 = 0;
numRetryNode3 = 0;
numRetryNode4 = 0;
delay=0.02;
numSentWithoutRetry = 0;
numGivenUp = 0;
numCollision = 0;
numSent = 0;
numReceived = 0;
numSentBroadcast = 0;
numReceivedBroadcast = 0;
numRetransmit = 0;
numBits = 0;
numBitsN1= 0;
numBitsN2= 0;
numBitsRB= 0;
numAcks = 0;
numSets = 0;
seed = getParentModule()->getParentModule()->getIndex(); //need different seeds for nodes
srand(time(0));
min_back_off = 0.2;
randvariableb = 1;
currentpacketSequenceNumber_6 = 0;
previouspacketSequenceNumber_6 = -1;
currentpacketSequenceNumber_5 = 0;
previouspacketSequenceNumber_5 = -1;
currentpacketSequenceNumber_4 = 0;
previouspacketSequenceNumber_4 = -1;
currentpacketSequenceNumber_3 = 0;
previouspacketSequenceNumber_3 = -1;
currentpacketSequenceNumber_2 = 0;
previouspacketSequenceNumber_2 = -1;
currentpacketSequenceNumber = 0;
previouspacketSequenceNumber = -1;
iris_hardware_delay = 0;
total_delay=0;
begin_time=0;
end_time=0;
//change the ACK mode ackMode {Explicit, Implicit}
ackMode= Explicit;
// Here the delay to be added in the multi-hop protocol
protocol_delay = 0;
total_packets = 100;
//delays for each test
iris_hardware_delay = 0.008;
iris_hardware_delay_ACK = 0.002;
total_delay = 0.000 + iris_hardware_delay; //delay < timeout (no retransmissions)
macState = DevMac::IDLE;
stateVector.setName("State");
endTimeout = NULL;
stateVector.setEnum("DevMac");
radioStateVector.setName("RadioState");
radioStateVector.setEnum("RadioState");
endToEndDelayVec.setName("End-to-End Delay");
// initialize watches
WATCH(fsm);
WATCH(radioState);
WATCH(phyState);
WATCH(retryCounter);
WATCH(backoff);
WATCH(numRetry);
WATCH(numSentWithoutRetry);
WATCH(numGivenUp);
WATCH(numCollision);
WATCH(numSent);
WATCH(numReceived);
WATCH(numSentBroadcast);
WATCH(numReceivedBroadcast);
WATCH(numRetransmit);
WATCH(min_back_off);
WATCH(begin_time);
WATCH(end_time);
WATCH(numBits);
WATCH(numAcks);
}
}
void DevMac::registerInterface()
{
IInterfaceTable *ift = InterfaceTableAccess().getIfExists();
if (!ift)
return;
InterfaceEntry *e = new InterfaceEntry();
// interface name: NetworkInterface module's name without special characters ([])
char *interfaceName = new char[strlen(getParentModule()->getFullName()) + 1];
char *d = interfaceName;
for (const char *s = getParentModule()->getFullName(); *s; s++)
if (isalnum(*s))
*d++ = *s;
*d = '\0';
e->setName(interfaceName);
delete [] interfaceName;
// address
e->setMACAddress(address);
e->setInterfaceToken(address.formInterfaceIdentifier());
// FIXME: MTU on 802.11 = ?
e->setMtu(par("mtu"));
// capabilities
e->setBroadcast(true);
e->setMulticast(true);
e->setPointToPoint(false);
// add
ift->addInterface(e, this);
}
/****************************************************************
* Message handling functions.
*/
void DevMac::handleSelfMsg(cMessage *msg)
{
//used for retransmissions: called by the scheduledAt function ()
if(msg == ackTimeout)
{
EV << "received self message: " << msg << endl;
ev << "Total packets in the MAC buffer is : " << transmissionQueue.size() <<endl;
sendDownPendingRadioConfigMsg();
macState = DevMac::IDLE; // for all node
if(macState != WAITACK){
if(!transmissionQueue.empty()) {
//min_back_off += (backoff_fun(0)); // all nodes will seed differently because they have different initalization seeds
EV << "INCREASED BACKOFF TIME IN MSG TRANSMIT TO :"<<min_back_off<<endl;
//increment number of retry
if (!strcmp(address.str().c_str(), "0A-AA-00-00-00-01")) {
numRetryNode0++;
}
else if (!strcmp(address.str().c_str() ,"0A-AA-00-00-00-02")) {
numRetryNode1++;
}
else if (!strcmp(address.str().c_str() ,"0A-AA-00-00-00-03")) numRetryNode2++;
else if (!strcmp(address.str().c_str() ,"0A-AA-00-00-00-04")) numRetryNode3++;
else if (!strcmp(address.str().c_str() ,"0A-AA-00-00-00-05")) numRetryNode4++;
macState = DevMac::WAITACK;
cancelEvent(backoffTime); //cancel timer before scheduling
scheduleAt(simTime() + backoff_fun(0), backoffTime);
//sendDataFrame(transmissionQueue.front(), true);
}
else
ev<<"Empty!\n";
}
}
else if(msg == delay_timer_30)
{
ev<<"Timer was delayed by 30 milliseconds"<<endl;
if(!transmissionQueue.empty())
{
sendDataFrame(transmissionQueue.front(), true);
cancelEvent(ackTimeout);
macState = DevMac::WAITACK;
scheduleAt(simTime() + min_back_off, ackTimeout);
}
}
else if(msg == backoffTime)
{
ev<<"Backoff has re-schedule the Transmission "<<endl;
if(!transmissionQueue.empty())
{
sendDataFrame(transmissionQueue.front(), true);
cancelEvent(ackTimeout);
macState = DevMac::WAITACK;
scheduleAt(simTime() + min_back_off, ackTimeout);
}
}
//sendDataFrame((Ieee80211DataOrMgmtFrame *)transmissionQueue.front(), false);
//sendDownDelayed(setBasicBitrate(buildDataFrame(frameToSend)), result); // add hardware delay here
}
float DevMac::backoff_fun(int seed)
{
//float temporary = ((float)(rand()%10))/10.0; //rand(): integer uniform rv 0-RAND_MAX
float temporary = ((float)(rand()%10))*5.0/100.0;
EV <<"We have chosen :"<< temporary <<" as the random backoff time"<<endl;
return temporary;
}
void DevMac::handleUpperMsg(cPacket *msg)
{
macState = DevMac::IDLE;
// check for queue overflow
if (maxQueueSize && ((int)transmissionQueue.size() == maxQueueSize))
{
EV << "message " << msg << " received from higher layer but MAC queue is full, dropping message\n";
delete msg;
return;
}
Ieee80211DataFrame *frame = new Ieee80211DataFrame(msg->getName());
// copy receiver address from the control info (sender address will be set in MAC)
Ieee802Ctrl *ctrl = check_and_cast<Ieee802Ctrl *>(msg->removeControlInfo());
frame->setReceiverAddress(ctrl->getDest());
delete ctrl;
ev << "Frame bit length before encaps "<< frame->getBitLength()<<endl;
frame->encapsulate(msg);
EV << "frame " << frame << " received from higher layer, receiver = " << frame->getReceiverAddress() << endl;
ASSERT(!frame->getReceiverAddress().isUnspecified());
// fill in missing fields (receiver address, seq number), and insert into the queue
frame->setTransmitterAddress(address);
frame->setSequenceNumber(sequenceNumber);
sequenceNumber = (sequenceNumber+1) % 4096; //XXX seqNum must be checked upon reception of frames!
min_back_off = 0.2; //initial ARQ timeout
ev<<"The sequence number of the frame is : " <<frame->getSequenceNumber()<<endl;
transmissionQueue.push_back(frame); //place frame in the queue
if(frame->getSequenceNumber() > 0){
if(!strcmp(address.str().c_str(),"0A-AA-00-00-00-01"))macState = DevMac::WAITACK;
ev << "There is at least one packet waiting in the buffer "<< endl;
}
if (macState == DevMac::WAITACK)
ev << "MAC is in waitack state, queue packet\n";
else
ev << "MAC is in the idle state\n";
ev << "Packets in queue : " << transmissionQueue.size() ;
if(macState != DevMac::WAITACK){
//radio and mac are ready
if(!transmissionQueue.empty()){
previouspacketSequenceNumber_5 = currentpacketSequenceNumber_5;
if(!strcmp(address.str().c_str(),"0A-AA-00-00-00-01") ){ //OK
sendDownPendingRadioConfigMsg();
ev <<"Node 1: RESET BACKOFF TO :" << min_back_off << endl;
/*
cancelEvent(delay_timer_30);
scheduleAt(simTime() + 0.02, delay_timer_30);
*/
sendDataFrame(transmissionQueue.front(), true);
if(frame->getSequenceNumber() == 0){
begin_time = simTime();
}
cancelEvent(ackTimeout);
scheduleAt( simTime()+ min_back_off, ackTimeout);
macState = DevMac::WAITACK;
}
else if (!strcmp(address.str().c_str(),"0A-AA-00-00-00-02")){ //OKAY
numBitsN2+=frame->getBitLength();
sendDownPendingRadioConfigMsg();
ev <<"Node 2: RESET BACKOFF TO :" << min_back_off << endl;
if(transmissionQueue.front()->getSequenceNumber() == 0 || transmissionQueue.size() == 1) {
//check if medium is free!
if(isMediumFree()) {
sendDataFrame(transmissionQueue.front(), false);
ev << "Sequence number is 0 or size of transmission buffer is 1"<<endl;
cancelEvent(ackTimeout);
scheduleAt( simTime()+ min_back_off, ackTimeout);
}
else {
ev <<"MEDIUM IS NOT FREE!!, BACKOFF AND TRY AT AL LATER TIME"<<endl;
cancelEvent(ackTimeout);
scheduleAt( simTime()+ min_back_off, ackTimeout);
}
}
macState = DevMac::WAITACK;
}
else if (!strcmp(address.str().c_str(),"0A-AA-00-00-00-03")){ //OKAY
numBitsN2+=frame->getBitLength();
sendDownPendingRadioConfigMsg();
ev <<"Node 3: RESET BACKOFF TO :" << min_back_off << endl;
sendDataFrame(transmissionQueue.front(), false);
macState = DevMac::WAITACK;
cancelEvent(ackTimeout);
scheduleAt( simTime()+ min_back_off, ackTimeout);
}
else if (!strcmp(address.str().c_str(),"0A-AA-00-00-00-04")){ //OKAY
numBitsN2+=frame->getBitLength();
sendDownPendingRadioConfigMsg();
ev <<"Node 4: RESET BACKOFF TO :" << min_back_off << endl;
sendDataFrame(transmissionQueue.front(), false);
macState = DevMac::WAITACK;
}
else if (!strcmp(address.str().c_str(),"0A-AA-00-00-00-05")){ //OKAY
numBitsN2+=frame->getBitLength();
sendDownPendingRadioConfigMsg();
ev <<"Node 5: RESET BACKOFF TO :" << min_back_off << endl;
sendDataFrame(transmissionQueue.front(), false);
macState = DevMac::WAITACK;
}
else if (!strcmp(address.str().c_str(),"0A-AA-00-00-00-06")){ //OKAY
numBitsN2+=frame->getBitLength();
sendDownPendingRadioConfigMsg();
ev <<"Node 6: RESET BACKOFF TO :" << min_back_off << endl;
sendDataFrame(transmissionQueue.front(), false);
macState = DevMac::WAITACK;
}
//scheduleAt( simTime()+ min_back_off, ackTimeout);
}
else {
//does it ever go aqui ?
scheduleAt(simTime() + min_back_off, ackTimeout);
}
}
}
void DevMac::handleCommand(cMessage *msg)
{
}
void DevMac::handleLowerMsg(cPacket *msg)
{
if(msg == ackTimeout)
EV << "received message from lower layer: " << msg << endl;
Ieee80211Frame *frame = dynamic_cast<Ieee80211Frame *>(msg); //get message
Ieee80211DataOrMgmtFrame *ackframe = dynamic_cast<Ieee80211DataOrMgmtFrame *>(msg); //get message
Ieee80211ACKFrame *ackframe0 = dynamic_cast<Ieee80211ACKFrame *>(msg); //get message
if (!frame) //if not a message, the error message to the screen
error("message from physical layer (%s)%s is not a subclass of Ieee80211Frame",
msg->getClassName(), msg->getName());
EV << "Self address: " << address.str().c_str() //this is the nodes address
<< ", receiver address: " << frame->getReceiverAddress() //this is the receiver address (destination address)
<< ", received frame is for us: " << isForUs(frame) << endl; //check if the data is for us !!
int frameType = frame ? frame->getType() : -1; //this returns either a 0 or 1, letting the user to know if it is
if(ackMode == Explicit)
{
if(isForUs(frame))
{
if (frameType == ST_DATA) { //is for us and data
ev << "RECEIVED DATA for us\n"; //this is at the relay node and last node node
Ieee80211DataFrame *dframe = dynamic_cast<Ieee80211DataFrame *> (frame); //blah, blah ....
currentpacketSequenceNumber=dframe->getSequenceNumber() + 1; //same ol
EV <<"Current sequence number is :" <<dframe->getSequenceNumber()<<endl;
if( currentpacketSequenceNumber > previouspacketSequenceNumber) //data_number is great than the previous value
{
if(!(strcmp(address.str().c_str(),"0A-AA-00-00-00-03"))){
numBits+=frame->getBitLength(); //get the length of the packet in bits
}
cPacket *payload = dframe->decapsulate();
ev << "This is my address: " << address.str().c_str() <<endl; //dislay address of node
Ieee80211DataOrMgmtFrame* ackframe = check_and_cast<Ieee80211DataOrMgmtFrame*> (frame);
ev << "The sequence number of the DATA packet is :" << dframe->getSequenceNumber()<<endl;
ackframe->setSequenceNumber(dframe->getSequenceNumber());
ev << "The sequence number of the ACK packet is :" << ackframe->getSequenceNumber()<<endl;
sendACKFrame(ackframe); //send ack with sequence number
previouspacketSequenceNumber = currentpacketSequenceNumber; //save sequence number as usual
numReceived++; //increment the number of data packets received
if(!strcmp(address.str().c_str() ,"0A-AA-00-00-00-02"))sendUpDelayed(payload,0);
else if(!strcmp(address.str().c_str() ,"0A-AA-00-00-00-03"))sendUpDelayed(payload, 0);
else if(!strcmp(address.str().c_str() ,"0A-AA-00-00-00-04"))sendUpDelayed(payload, 0);
else if(!strcmp(address.str().c_str() ,"0A-AA-00-00-00-05"))sendUpDelayed(payload, 0);
else if (!strcmp(address.str().c_str() ,"0A-AA-00-00-00-06")) {sendUpDelayed(payload, 0);}
EV <<"Current sequence number is :" <<dframe->getSequenceNumber()<<endl;
}
else
{ //if it is old, then do nothing to the data. we can
if(!strcmp(address.str().c_str() ,"0A-AA-00-00-00-02") || !strcmp(address.str().c_str() ,"0A-AA-00-00-00-03") || !strcmp(address.str().c_str() ,"0A-AA-00-00-00-04") || !strcmp(address.str().c_str() ,"0A-AA-00-00-00-05")|| !strcmp(address.str().c_str() ,"0A-AA-00-00-00-06") ){
Ieee80211DataOrMgmtFrame* ackframe = check_and_cast<Ieee80211DataOrMgmtFrame*> (frame);
ackframe->setSequenceNumber(dframe->getSequenceNumber());
sendACKFrame(ackframe);
delete frame;
}
}
// number of packets received in this layer
}
else if (frameType == ST_ACK ) { //if this is an ack packet ..
macState = DevMac::IDLE;
ev << "RECEIVED ACK for us!!\n"; // trigger another transmission after some time? macState = DevMac::WAITMAC
ev << "RESET THE BACKOFF TIMER"<<endl;
if(!transmissionQueue.empty()){
cancelEvent(ackTimeout); //stop timercancelEvent(ackTimeout); //stop timer
ev << "The sequence number for the ACK is" << ackframe0->getSequenceNumber();
if(transmissionQueue.front()->getSequenceNumber() == ackframe0->getSequenceNumber() )
{
transmissionQueue.pop_front(); //pop fix
ev << "DATA HAS BEEN REMOVED-POP!!" <<endl;
numAcks++; //...
if((numAcks%499 == 0) && (numAcks !=0) && !(strcmp(address.str().c_str(),"0A-AA-00-00-00-02")) ){ //change here
end_time = simTime();
//would be nice to store previous_time_in_vector - endtime in the following record statement
endToEndDelayVec.record(end_time);
EV << "The last ACK was received at: " <<end_time<<endl;
numSets++;
}
}
else
ev << "Sequence numbers did not match. DATA WAS NOT REMOVED !" <<endl;
min_back_off = 0.2; //reset the minimum backoff
ev << "Buffer size: " << transmissionQueue.size() <<endl;
if((strcmp(address.str().c_str(),"0A-AA-00-00-00-01")))
{
if(!transmissionQueue.empty())
{
sendDataFrame(transmissionQueue.front(), true);
cancelEvent(ackTimeout);
macState = DevMac::WAITACK;
scheduleAt( simTime()+ min_back_off, ackTimeout);
}
// 3/12
}
else
{
cancelEvent(delay_timer_30);
scheduleAt(simTime()+ protocol_delay , delay_timer_30); // Here we add delay on the protocol
}
}
else{
cancelEvent(ackTimeout); //stop timercancelEvent(ackTimeout); //stop timer
EV << "Cancelled event !!" <<endl;
}
}
else {
ev << "RECEIVED something other than data or ack at DevMac\n";
delete frame;
delete msg;
}
}
else {
ev << "Packet not for us, drop it\n";
delete frame;
}
}
else if(ackMode == Implicit)
{
if(isForUs(frame)) //check if the frame is for us
{
if (frameType == ST_DATA) { //if it is for us and is a data then continue
ev << "RECEIVED DATA for us\n"; //this is at the relay node and last node node
Ieee80211DataFrame *dframe = dynamic_cast<Ieee80211DataFrame*> (frame); //create a frame
if (!strcmp(address.str().c_str() ,"0A-AA-00-00-00-06")) { //middle gets data successfully and then sends implicit ACK (Data+ack)
currentpacketSequenceNumber_6=dframe->getSequenceNumber();
EV << "CurrentSequenceNo:"<<currentpacketSequenceNumber_6<< "/PreviousSequenceNo:" << previouspacketSequenceNumber_6;
if( currentpacketSequenceNumber_6 > previouspacketSequenceNumber_6){
//if we are the second node, the relay node, then ...
numBitsRB+=frame->getBitLength(); //get the length of the packet in bits
cPacket *payload = dframe->decapsulate(); //get the payload , might need to be cleaned up further
previouspacketSequenceNumber_6 = currentpacketSequenceNumber_6;
/*Ieee80211DataOrMgmtFrame* ackframe = check_and_cast<Ieee80211DataOrMgmtFrame*> (frame);
ackframe->setSequenceNumber(dframe->getSequenceNumber());
sendACKFrame(ackframe);
*/
ev << "Received a data packet. Now, the PreviousSeqNumber is:" << previouspacketSequenceNumber_6 ;
numReceived++;
delete dframe;
sendUpDelayed(payload, 0); //send the data packet up to the network layer
}
else
{
//if it is old, then send another ACK
ev << "This is an old frame, sending another acknowledgment !!" <<endl;
Ieee80211DataOrMgmtFrame* ackframe = check_and_cast<Ieee80211DataOrMgmtFrame*> (frame);
ackframe->setSequenceNumber(dframe->getSequenceNumber());
sendACKFrame(ackframe);
delete ackframe;
}
}
else if (!strcmp(address.str().c_str() ,"0A-AA-00-00-00-05")) { //middle gets data successfully and then sends implicit ACK (Data+ack)
currentpacketSequenceNumber_5=dframe->getSequenceNumber();
EV << "CurrentSequenceNo:"<<currentpacketSequenceNumber_5<< "/PreviousSequenceNo:" << previouspacketSequenceNumber_5;
if( currentpacketSequenceNumber_5 > previouspacketSequenceNumber_5){
cPacket *payload = dframe->decapsulate(); //get the payload , might need to be cleaned up further
previouspacketSequenceNumber_5 = currentpacketSequenceNumber_5;
/*Ieee80211DataOrMgmtFrame* ackframe = check_and_cast<Ieee80211DataOrMgmtFrame*> (frame);
ackframe->setSequenceNumber(dframe->getSequenceNumber());
sendACKFrame(ackframe);*/
ev << "previous seq" << previouspacketSequenceNumber_5 ;
numReceived++;
delete dframe;
sendUpDelayed(payload, 0); //send the data packet up to the network layer
}
else
{ //if it is old, then do nothing to the data. we can
ev << "This is an old frame, sending another acknowledgment !!" <<endl;
//if we are the second nod<endl;
Ieee80211DataOrMgmtFrame* ackframe = check_and_cast<Ieee80211DataOrMgmtFrame*> (frame);
ackframe->setSequenceNumber(dframe->getSequenceNumber());
sendACKFrame(ackframe);
delete ackframe;
}
}
else if (!strcmp(address.str().c_str() ,"0A-AA-00-00-00-04")) { //middle gets data successfully and then sends implicit ACK (Data+ack)
currentpacketSequenceNumber_4=dframe->getSequenceNumber();
EV << "CurrentSequenceNo:"<<currentpacketSequenceNumber_4<< "/PreviousSequenceNo:" << previouspacketSequenceNumber_4;
if( currentpacketSequenceNumber_4 > previouspacketSequenceNumber_4){
//if we are the second node, the relay node, then ...
numBitsRB+=frame->getBitLength(); //get the length of the packet in bits
cPacket *payload = dframe->decapsulate(); //get the payload , might need to be cleaned up further
Ieee80211DataOrMgmtFrame* ackframe = check_and_cast<Ieee80211DataOrMgmtFrame*> (frame);
ackframe->setSequenceNumber(dframe->getSequenceNumber());
sendACKFrame(ackframe);
previouspacketSequenceNumber_4 = currentpacketSequenceNumber_4;
ev << "previous seq" << previouspacketSequenceNumber_4 ;
numReceived++;
delete dframe;
sendUpDelayed(payload, 0); //send the data packet up to the network layer
}
else
{ //if it is old, then do nothing to the data. we can
ev << "This is an old frame, sending another acknowledgment !!" <<endl;
//if we are the second nod<endl;
Ieee80211DataOrMgmtFrame* ackframe = check_and_cast<Ieee80211DataOrMgmtFrame*> (frame);
ackframe->setSequenceNumber(dframe->getSequenceNumber());
sendACKFrame(ackframe);
delete ackframe;
}
}
else if (!strcmp(address.str().c_str() ,"0A-AA-00-00-00-03")) { //middle gets data successfully and then sends implicit ACK (Data+ack)
currentpacketSequenceNumber_3=dframe->getSequenceNumber();
EV << "CurrentSequenceNo:"<<currentpacketSequenceNumber_3<< "/PreviousSequenceNo:" << previouspacketSequenceNumber_3;
if( currentpacketSequenceNumber_3 > previouspacketSequenceNumber_3){
/*
//if we are the second node, the relay node, then ...
cPacket *payload = dframe->decapsulate(); //get the payload , might need to be cleaned up further
//Ieee80211DataOrMgmtFrame* ackframe = check_and_cast<Ieee80211DataOrMgmtFrame*> (frame);
//ackframe->setSequenceNumber(dframe->getSequenceNumber());
//sendACKFrame(ackframe);
previouspacketSequenceNumber_3 = currentpacketSequenceNumber_3;
ev << "previous seq" << previouspacketSequenceNumber_3 ;
numReceived++;
delete dframe;
sendUpDelayed(payload, 0); //send the data packet up to the network layer
*/
//if we are the second node, the relay node, then ...
numBitsRB+=frame->getBitLength(); //get the length of the packet in bits
cPacket *payload = dframe->decapsulate(); //get the payload , might need to be cleaned up further
Ieee80211DataOrMgmtFrame* ackframe = check_and_cast<Ieee80211DataOrMgmtFrame*> (frame);
ackframe->setSequenceNumber(dframe->getSequenceNumber());
sendACKFrame(ackframe);
previouspacketSequenceNumber_3 = currentpacketSequenceNumber_3;
ev << "previous seq" << previouspacketSequenceNumber_3 ;
numReceived++;
delete dframe;
sendUpDelayed(payload, 0); //send the data packet up to the network layer
}
else
{ //if it is old, then do nothing to the data. we can
ev << "This is an old frame, sending another acknowledgment !!" <<endl;
//if we are the second nod<endl;
Ieee80211DataOrMgmtFrame* ackframe = check_and_cast<Ieee80211DataOrMgmtFrame*> (frame);
ackframe->setSequenceNumber(dframe->getSequenceNumber());
sendACKFrame(ackframe);
delete ackframe;
}
}
else if (!strcmp(address.str().c_str() ,"0A-AA-00-00-00-02")) { //middle gets data successfully and then sends implicit ACK (Data+ack)
currentpacketSequenceNumber_2=dframe->getSequenceNumber();
EV << "CurrentSequenceNo:"<<currentpacketSequenceNumber_2<< "/PreviousSequenceNo:" << previouspacketSequenceNumber_2;
if( currentpacketSequenceNumber_2 > previouspacketSequenceNumber_2){
//if we are the second node, the relay node, then ...
cPacket *payload = dframe->decapsulate(); //get the payload , might need to be cleaned up further
previouspacketSequenceNumber_2 = currentpacketSequenceNumber_2;
ev << "previous seq" << previouspacketSequenceNumber_2 ;
numReceived++;
delete dframe;
sendUpDelayed(payload, 0); //send the data packet up to the network layer
//and let it check if it is the right data for the node, if it isn't then network will relay the data packet.
//the network layer knows its ip address. the impliciation is hiden from this layer and has been implemented correctly.
}
else
{ //if it is old, then do nothing to the data. we can
ev << "This is an old frame, sending another acknowledgment !!" <<endl;
Ieee80211DataOrMgmtFrame* ackframe = check_and_cast<Ieee80211DataOrMgmtFrame*> (frame);
ackframe->setSequenceNumber(dframe->getSequenceNumber());
sendACKFrame(ackframe);
delete ackframe;
}
}
// number of packets received in this layer
}
else if (frameType == ST_ACK ) { //check if the data received is an ack packet, the second node cares about acks,
//not the first node, in implicit mode
ev << "RECEIVED ACK for us!!\n"; // trigger another transmission after some time? macState = DevMac::WAITMAC
if(!transmissionQueue.empty()){
EV <<"The current sequence number in the buffer is :"<<transmissionQueue.front()->getSequenceNumber()<<"/ The received or ACK packet sequence number is :" <<ackframe0->getSequenceNumber() <<endl;
if(transmissionQueue.front()->getSequenceNumber() == ackframe0->getSequenceNumber())
{
cancelEvent(ackTimeout); //stop timercancelEvent(ackTimeout); //stop timer
ev << "DATA with sequence number"<<transmissionQueue.front()->getSequenceNumber()<<"will has been removed-POPPED!!" <<endl;
transmissionQueue.pop_front(); //pop fix
numAcks++; //...
if((numAcks%499 == 0) && (numAcks !=0) && !(strcmp(address.str().c_str(),"0A-AA-00-00-00-02")) ){ //change here
end_time = simTime();
//would be nice to store previous_time_in_vector - endtime in the following record statement
endToEndDelayVec.record(end_time);
numSets++;
EV << "The last ACK was received at: " <<end_time<<endl;
}
ev << "numAcks is :" <<numAcks <<endl;
ev << "RESET THE BACKOFF TIMER"<<endl;
min_back_off = 0.2; //reset the minimum back off
if(!transmissionQueue.empty()) {
if((!strcmp(address.str().c_str() ,"0A-AA-00-00-00-01")))
{
previouspacketSequenceNumber = currentpacketSequenceNumber; //record //increase the sequence number, since the first one is 0,
sendDataFrame(transmissionQueue.front(), true);
}
else if((!strcmp(address.str().c_str() ,"0A-AA-00-00-00-02")))
{
previouspacketSequenceNumber_2 = currentpacketSequenceNumber_2; //record //increase the sequence number, since the first one is 0,
sendDataFrame(transmissionQueue.front(), true);
}
else if(!strcmp(address.str().c_str() ,"0A-AA-00-00-00-03"))
{
previouspacketSequenceNumber_3 = currentpacketSequenceNumber_3; //increase the sequence number, since the first one is 0,
sendDataFrame(transmissionQueue.front(), true);
}
else if(!strcmp(address.str().c_str() ,"0A-AA-00-00-00-04"))
{
currentpacketSequenceNumber_4=currentpacketSequenceNumber_4; //increase the sequence number, since the first one is 0,
sendDataFrame(transmissionQueue.front(), true);
}
else if((!strcmp(address.str().c_str() ,"0A-AA-00-00-00-05")))
{
currentpacketSequenceNumber_5=currentpacketSequenceNumber_5;; //increase the sequence number, since the first one is 0,
sendDataFrame(transmissionQueue.front(), true);
}
scheduleAt(simTime() + min_back_off, ackTimeout);
} // 3/12
}
else
ev << "Sequence numbers did not match. DATA WAS NOT REMOVED !" <<endl;
}
}
else { //we got shit from the below !! cannot be recognized, then delete!
ev << "RECEIVED something other than data or ack at DevMac\n";
delete frame;
delete msg;
}
}
else if(!isForUs(frame)) //when previous node receives the implicit ACK data packet or a node receives a data packet that is not intended for it
{
if (frameType == ST_DATA) {
Ieee80211DataFrame *dframe = dynamic_cast<Ieee80211DataFrame *> (frame); //create a frame
//new packets
if((!strcmp(address.str().c_str() ,"0A-AA-00-00-00-01")))
{
currentpacketSequenceNumber=dframe->getSequenceNumber(); //increase the sequence number, since the first one is 0,
EV <<"The current sequence(in-coming packet) number is :"<<currentpacketSequenceNumber<<": The previous packet is :" <<previouspacketSequenceNumber <<endl;
}
else if((!strcmp(address.str().c_str() ,"0A-AA-00-00-00-02")))
{
currentpacketSequenceNumber_2=dframe->getSequenceNumber(); //increase the sequence number, since the first one is 0,
EV <<"The current sequence(in-coming packet) number is :"<<currentpacketSequenceNumber_2<<": The previous packet is :" <<previouspacketSequenceNumber_2 <<endl;
}
else if(!strcmp(address.str().c_str() ,"0A-AA-00-00-00-03"))
{
currentpacketSequenceNumber_3=dframe->getSequenceNumber(); //increase the sequence number, since the first one is 0,
EV <<"The current sequence(in-coming packet) number is :"<<currentpacketSequenceNumber_3<<": The previous packet is :" <<previouspacketSequenceNumber_3 <<endl;
}
else if(!strcmp(address.str().c_str() ,"0A-AA-00-00-00-04"))
{
currentpacketSequenceNumber_4=dframe->getSequenceNumber(); //increase the sequence number, since the first one is 0,
EV <<"The current sequence(in-coming packet) number is :"<<currentpacketSequenceNumber_4<<": The previous packet is :" <<previouspacketSequenceNumber_4 <<endl;
}
else if((!strcmp(address.str().c_str() ,"0A-AA-00-00-00-05")))
{
currentpacketSequenceNumber_5=dframe->getSequenceNumber(); //increase the sequence number, since the first one is 0,
EV <<"The current sequence(in-coming packet) number is :"<<currentpacketSequenceNumber_5<<": The previous packet is :" <<previouspacketSequenceNumber_5 <<endl;
}
else if((!strcmp(address.str().c_str() ,"0A-AA-00-00-00-06")))
{
currentpacketSequenceNumber_6=dframe->getSequenceNumber(); //increase the sequence number, since the first one is 0,
EV <<"The current sequence(in-coming packet) number is :"<<currentpacketSequenceNumber_6<<": The previous packet is :" <<previouspacketSequenceNumber_6 <<endl;
}
if( (currentpacketSequenceNumber > previouspacketSequenceNumber) && (!strcmp(address.str().c_str() ,"0A-AA-00-00-00-01")))
{
Ieee80211DataFrame *dframe = dynamic_cast<Ieee80211DataFrame *> (frame); //create a frame
if(!transmissionQueue.empty()){ EV << transmissionQueue.front()->getSequenceNumber() <<" is at the front of the queue!" <<endl; }
ev << "RECEIVED IMPLICIT ACK \n"; // trigger another transmission after some time? macState = DevMac::WAITMAC
ev << "RESET THE BACKOFF TIMER"<<endl;
if( !transmissionQueue.empty() && (currentpacketSequenceNumber == transmissionQueue.front()->getSequenceNumber())) {
ev << "POP DATA : " <<currentpacketSequenceNumber << " FROM THE BUFFER !"<< endl;
transmissionQueue.pop_front(); //pop the front of the transmission que if there is something to pop
cancelEvent(ackTimeout);
min_back_off = 0.2;
previouspacketSequenceNumber = currentpacketSequenceNumber;
if((strcmp(address.str().c_str(),"0A-AA-00-00-00-01")))
{
if(!transmissionQueue.empty())
{
sendDataFrame(transmissionQueue.front(), true);
cancelEvent(ackTimeout);
macState = DevMac::WAITACK;
scheduleAt( simTime()+ min_back_off, ackTimeout);
}
// 3/12
}
else
{
cancelEvent(delay_timer_30);
scheduleAt(simTime()+ protocol_delay , delay_timer_30); // Here the delay to be added in the protocol
}
//if(!transmissionQueue.empty())sendDataFrame(transmissionQueue.front(), true); // 3/12
//previouspacketSequenceNumber = currentpacketSequenceNumber; //record
//macState = DevMac::IDLE; //set the state to idle
//min_back_off = 0.2;
//cancelEvent(ackTimeout);
//scheduleAt(simTime() + min_back_off, ackTimeout);//3/12
}
}