-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdated.cpp
More file actions
2830 lines (2378 loc) · 87.3 KB
/
updated.cpp
File metadata and controls
2830 lines (2378 loc) · 87.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
/*
author: Hannaneh Barahouei (barahoueipash@vcu.edu)
file: updated.cpp
*/
using namespace std;
#include <iostream>
#include <fstream>
#include <deque>
#include <queue>
#include <string>
#include <vector>
#include <fstream>
#include <cmath>
#include <time.h>
#include <random>
#include <string>
#include <sstream>
#include <thread>
#include <mutex>
#include "updated.h"
double next_slot =0.000;
double interval;
double prev_slot=0.000;
double total_t_=0;
int ret_threshold= 8;
int num_packets=0;
double slt_duration=0.00002;
ofstream myfile;
ofstream myfile2;
ofstream myfiledis;
ofstream nodefile;
ofstream nodefile1;
ofstream nodefile2;
ofstream nodefile3;
ofstream nodefile4;
ofstream nodefile5;
ifstream infile;
ifstream infile1;
ifstream infile2;
ifstream infile3;
ifstream infile4;
int rew[200];
int num_nodes;
safeChannel channel;
std::mutex m;
int channelBusy[MAXNODES];
int accessCount;
long double prop[MAXNODES][MAXNODES];
long double dist0[MAXNODES][MAXNODES];
FILE * fEvent; /* Pointer to file of events */
int
isDeferring[MAXNODES], /* is the node deferring? */
/* Is the channel busy? */
DEBUGGING,
done; /* Are we done with the simulation yet? */
int numNodes, /* number of nodes in the system */
NUM_REARRIVAL=0,
channelLength, /* length of the channel */
bitsPerPac, /* number of bits / packet */
bitsPerAck, /* number of bits / ack */
buffPerNode, /* Maximum size of buffer for each node */
maxCWindow, /* Maximum contention window size */
/* Minimum contention window size */
curCWindow, /* The current contention window size */
numCollisions, /* The total number of collisions */
numPacketsLost, /* the number of packets which overflow the nodes' buffers */
//numInQ[MAXNODES], /* The number of packets in queue for a node */
numDeferring, /* Number of nodes deferring */
totalPackets, /* Total number of packets sent so far.. */
loopcounter, /* Count the total number of events */
iseed; /* Seed of random number generator */
long double propDelay, /* End-to-end propagation delay */
channelCapacity, /* Capacity of the channel */
probError, /* Probability[no error] when transmitting a packet */
SIFS, /* SIFS interval */
DIFS, /* DIFS interval */
T3, /* Time out interval */
slotTime, /* Approximation of the slot time */
packetDelay, /* The delay a packet has incurred in the system */
totalPacketDelay, /* The total delay of all the packets incur in the system */
timeNextEvent, /* Time of next event occurance */
lambdaN, /* Node arrival rate */
lambdaS, /* System arrival rate */
sysTime, /* Global clock */
timeXAck, /* time to transmit an ACK */
timeXPac, /* time to transmit a packet */
I, /* interrupt time + service delay */
P1,
minCWindow,
P2;
//long double timeNextEvent;
bool isBusy[MAXNODES]; /* is the node busy */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int initParams(int argc, char * argv[])
{
/* This function read in all command line parameters and assigns
* the proper values to the corresponding variables
*/
int counter;
num_nodes=5; /* set default values - in case not */
channelLength=100; /* all command line args are present */
propDelay=3.3e-07;
if(trans_r){
channelCapacity=5242880;
}
else
{
channelCapacity=10242880;
}
bitsPerPac=120000;
bitsPerAck=3680;
buffPerNode=30;
maxCWindow=1024;
SIFS=(2.5)*(3.3e-07);
DIFS=(4.0)*(3.3e-07);
T3=(3.0)*(3.3e-07);
lambdaS=1.0;
if(prob)
probError=0.0;
else
probError=0.2;
DEBUGGING=1;
P1=0.1;
P2=0.0;
//interval = 2* propDelay;
interval = 0.0001000;
/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> command line options */
if (argc==1)
{ printf ("\nUsing the default parameters...\n"); return (1); }
for (counter=1; counter<argc; counter++) /* Process args */
if (argv[counter][0] == '-') /* If it's a flag */
{
switch (argv[counter][1])
{
case 'n': numNodes=atoi(argv[counter+1]); break;
case 'l': channelLength=atoi(argv[counter+1]); break;
case 'T': propDelay=atof(argv[counter+1]); break;
case 'c': channelCapacity=atof(argv[counter+1]); break;
case 'x': bitsPerPac=atoi(argv[counter+1]); break;
case 'a': bitsPerAck=atoi(argv[counter+1]); break;
case 'b': buffPerNode=atoi(argv[counter+1]); break;
case 'u': maxCWindow=atoi(argv[counter+1]); break;
case 'v': minCWindow=atoi(argv[counter+1]); break;
case 's': SIFS=atof(argv[counter+1]); break;
case 'd': DIFS=atof(argv[counter+1]); break;
case 't': T3=atof(argv[counter+1]); break;
case 'p': probError=atof(argv[counter+1]); break;
case 'S': lambdaS=atof(argv[counter+1]); break;
case 'D': DEBUGGING=atoi(argv[counter+1]); break;
default :
{ printf ("\nUnrecognized command line option...(%s)(%c).\n\n",
argv[counter], argv[counter][1]);
exit(-1); }
} /* End if switch() */
} /* End of for loop */
} /* End if initParams() */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void initAll(void)
{
/* This function goes through and calculates all of the necesary
* parameters based on the command line arguments and the default
* settings - it also initializes all variables to NULL/zero
*/
int i;
/*_______________General stuff to set to _________________*/
timeNextEvent=0.00; /* Reset time of next event */
sysTime=0; /* Reset system timer */
timeXPac=0; /* Reset time to transmit a packet */
timeXAck=0; /* Reset time to transmit an Ack */
iseed=1837; /* Seed the random # generator */
loopcounter=0; /* Number of events */
done=FALSE; /* Done with simulation */
totalPackets=0; /* total packets sent is zero */
numCollisions=0; /* number of collisions */
curCWindow=minCWindow; /* Start with the smallest C win. */
numPacketsLost=0; /* No packets lost due to Q overflow */
for (i=0; i<num_nodes; i++){ /* None of the nodes is */
isDeferring[i]=FALSE; /* deferring */
isBusy[i]=false;
channelBusy[i]=0;
}
numDeferring=0; /* nobody is deferring */
//channelBusy=FALSE; /* the channel is assumed idle */
packetDelay=0; /* No packet delay */
totalPacketDelay=0; /* no total packet delay */
//I= (double)(channelLength*bitsPerPac)/channelCapacity;
I=0.0001;
/*_____________General stuff to calculate_______________*/
lambdaN= lambdaS/((double)num_nodes); /* Nodal arrival rate */
timeXPac = (double)bitsPerPac/channelCapacity;
timeXAck = (double)bitsPerAck/channelCapacity;
slotTime=100000*(propDelay+timeXPac);
/*____________________Open files_________________________*/
if (DEBUGGING) fEvent=fopen("logfile.txt", "w");
if (DEBUGGING) if (!fEvent) fatal("initAll", "Can't open event file");
} /* End of initAll;
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void showParams(FILE * fp)
{
/* This function shows all of the values of the command line
* argument variables after they have been entered and processed
*/
if (fp!=NULL)
{
/* end of going through all of the command line args */
fprintf (fp, "\n\nNumber of Nodes............: %d", num_nodes);
fprintf (fp, "\nChannel length.............: %d ", channelLength);
fprintf (fp, "\nPropagation Delay..........: %1.2e", propDelay);
fprintf (fp, "\nChannel capacity...........: %1.3e", channelCapacity);
fprintf (fp, "\nBits/packet................: %d", bitsPerPac);
fprintf (fp, "\nBits/ACK...................: %d ", bitsPerAck);
fprintf (fp, "\nMax number of buffers/node.: %d", buffPerNode);
fprintf (fp, "\nMax contention window......: %d", maxCWindow);
fprintf (fp, "\nMin contention window......: %d", minCWindow);
fprintf (fp, "\nSIFS.......................: %1.2e", SIFS);
fprintf (fp, "\nDIFS.......................: %1.2e", DIFS);
fprintf (fp, "\nT3.........................: %1.2e", T3);
fprintf (fp, "\nProb[unsuccessful Packet]..: %1.1lf", probError);
fprintf (fp, "\nSystem packet arrival rate.: %lf", lambdaS);
fprintf (fp, "\nNodal packet arrival rate..: %lf", lambdaN);
fprintf (fp, "\nTime to transmit an ack....: %lf", timeXAck);
fprintf (fp, "\nTime to transmit a packet..: %lf\n", timeXPac);
}
} /* showParams */
//if (DEBUGGING) if (fEvent) fprintf (fEvent, "\nMedia free? %s", (channelBusy==FALSE)?"YES":"NO");
vector<Wireless_node> list_nodes;
//vector<Wireless_node> node1;
vector<Packet> buffer1;
vector<Packet> buffer_received;
Wireless_node node1[60];
void Packet::sent(long double current_time1)
{
//event *ev=NULL;
if (first_send_attempt_ == -1)
{
first_send_attempt_ = current_time1;
}
num_retransmission_attempts_++;
sent_time_ = current_time1;
status_ = Packet::SENDING();
}
void Packet::received(long double current_time)
{
receive_time_ = current_time;
status_ = Packet::RECEIVED();
}
long double Packet::get_effective_transmission_time() const {
return receive_time_ - sent_time_;
}
long double Packet::get_total_transmission_time() const {
return receive_time_ - first_send_attempt_;
}
long double Packet::get_total_delay() const {
return receive_time_ - first_schedule_time_;
}
void Packet::output() const{
cout << "PACKET: " << id_ << " from NODE: " << from_ << " [SCH]:" << schedule_time_ << " [FIRS]:" << first_send_attempt_ << " [SENT]:" << sent_time_
<< " [XP_RCV]:" << expected_receive_time_ << " [RCV]:" << receive_time_ << " [ATMPT]" << num_retransmission_attempts_ <<endl;
}
Packet Wireless_node::create_new_packet(char type, int from, int to, double current_time)
{
Packet pkt;
Random_Number_Generator r;
pkt.set_type(type);
pkt.set_status(Packet::SCHEDULED());
pkt.set_from(from);
pkt.set_to(to);
if(pkt.get_type()==Packet::ACK())
{
node1[from].set_generated_ack_packets(1);
pkt.set_id(get_generated_ack_packets());
}
else
{
++num_packets;
node1[from].set_generated_packets(1);
//pkt.set_id(get_generated_packets());
pkt.set_id(num_packets);
}
cout << "Packet's type: " << pkt.get_type() << "with id: "<< pkt.get_id()<<endl;
pkt.set_first_schedule_time(current_time);
int id = pkt.get_id();
//pkt.set_schedule_time(pkt.get_first_schedule_time());
// cw=false;
if (cw){
minCWindow=rand_CW(7)* slt_duration;
//cout <<"cw is"<< endl;
}
else{
minCWindow=0;
}
// myfile2 <<"cw: "<<minCWindow<<'\n';
Packet *pack= NULL;
pack = &pkt;
if(!Ack && pkt.get_type()== Packet::MSG()){
cout << " Packet: "<< pack ->get_id() << " is from node: " << pack ->get_from() << " to node: "<< pack ->get_to()<<endl;
node1[from].get_startTransmission_buff().insert(node1[from].get_startTransmission_buff().begin()+id,
pkt.get_first_schedule_time()+ minCWindow);
}
else if(Ack && pkt.get_type()== Packet::MSG()){
if(pack->get_id()==1 ){
node1[from].get_startTransmission_buff().insert(node1[from].get_startTransmission_buff().begin()+id,
pkt.get_first_schedule_time()+minCWindow);
}
else{
cout << "This is after Packet's type: " <<endl;
node1[from].get_startTransmission_buff().insert(node1[from].get_startTransmission_buff().begin()+id,
pkt.get_first_schedule_time()+minCWindow+ 2 * timeXPac);
}
} /*else if Ack and msg*/
if ( pkt.get_type()== Packet::ACK())
{
cout << "This is if we DO NOT have ack mechanism or its an Ack packet" << endl;
//node1[j].set_start_transmission( current_time_ + pkt.get_schedule_time());
node1[from].get_startTransmission_buff().insert(node1[from].get_startTransmission_buff().begin()+id,
pkt.get_first_schedule_time()+minCWindow);
}
long double thisSchedule = node1[from].start_transmission[id];
cout << "packet with id: " << pkt.get_id() <<" is created from: " << from << " to: " << to << "at time: " << thisSchedule << '\n';
if (protocol){
pkt.set_schedule_time(thisSchedule);
if (pkt.get_type()==Packet::ACK())
{
pkt.set_expected_receive_time(pkt.get_schedule_time() + timeXAck);
}
else
{
pkt.set_expected_receive_time(pkt.get_schedule_time() + timeXPac);
}
theSimulation.Event_Scheduler (new sendEvent (thisSchedule));
}
else if(!protocol)
{
if (thisSchedule <= next_slot){
cout<<"less than next slot"<<endl;
pkt.set_schedule_time(next_slot);
next_slot += interval;
}
else{
next_slot += interval;
pkt.set_schedule_time(thisSchedule);
}
if (pkt.get_type()==Packet::ACK())
{
pkt.set_expected_receive_time(pkt.get_schedule_time() + timeXAck + prop[from][to]);
}
else
{
pkt.set_expected_receive_time(pkt.get_schedule_time() + timeXPac + prop[from][to]);
}
theSimulation.Event_Scheduler (new slotted_sendEvent (pkt.get_schedule_time()));
}
return pkt;
}
Packet Wireless_node::broadcast(char type, int from, int to, long double current_time){
}
Packet Wireless_node::create_new_msg_packet(int from, int to, double current_time)
{
return create_new_packet(Packet::MSG(), from, to, current_time);
}
Packet Wireless_node::create_new_ack_packet(int from, int to, double current_time, int pkt_id)
{
Packet p = create_new_packet(Packet::ACK(), from, to, current_time);
p.set_payload(pkt_id);
return p;
}
/*
Sets the seed of the random variables
*/
void Wireless_node::set_seed(long double trans)
{
rnd_transmission.set_seed(trans);
}
/*
Creates instances of the Random Variate Generator and
also the first event...
*/
void Wireless_node::initialize()
{
if (!initialized_) {
//rnd_transmission.set_rate(transmission_rate_);
initialized_ = true;
}
}
void Wireless_node::output() const{
for (int j=0; j< num_nodes; ++j)
cout << "NODE: " << j << " [COMP]" << node1[j].get_completed_transmissions() << " [ATMPT]"
<< node1[j].get_attempted_transmissions()<< " [Total-GEN-MSG]" << node1[j].get_generated_packets() << " [GEN-ACK]" << node1[j].get_generated_ack_packets() -600 << " [Average Delay]" << abs (node1[j].get_average_delay()/ num_nodes)<< endl;
// << " [GEN]" << generated_packets_ << " [EFF]" << efficiency_ << "( " << transmission_rate_ * propagation_time_ << ") [DELAY]" << delay_/generated_packets_ << endl;
//cout << "NODE: " << id_ << " [Completed transmissions]" << completed_transmissions_ << " [# Transmission attempts]" << attempted_transmissions_
//<< " [Throughput]" << efficiency_ << " [G]" << transmission_rate_ /* * propagation_time_ << " [Average Delay]" */ << delay_/generated_packets_ << endl;
}
void Wireless_node::attempt_transmission(Packet *p, long double current_time1) {
attempted_transmissions_++;
p->sent(current_time1);
}
void Wireless_node::completed_transmission(Packet *p, long double current_time) {
completed_transmissions_++;
p->received(current_time);
efficiency_ += p->get_effective_transmission_time()/p->get_total_transmission_time();
efficiency_ = efficiency_ / 2;
delay_ += p->get_total_delay();
}
Packet Wireless_node::retransmit(Packet *pack, double current_time) {
pack->set_first_schedule_time(current_time);
int id = pack ->get_id();
int from = pack -> get_from();
int to = pack->get_to();
if (cw){
minCWindow=rand_CW(7)* slt_duration;
cout <<"cw is"<< endl;
}
else{
minCWindow=0;
}
if(!Ack && pack->get_type()== Packet::MSG()){
cout << " Packet: "<< pack ->get_id() << " is from node: " << pack ->get_from() << " to node: "<< pack ->get_to()<<endl;
node1[from].get_startTransmission_buff().insert(node1[from].get_startTransmission_buff().begin()+id,
pack->get_first_schedule_time()+ minCWindow);
}
else if(Ack && pack->get_type()== Packet::MSG()){
if(pack->get_id()==1 ){
node1[from].get_startTransmission_buff().insert(node1[from].get_startTransmission_buff().begin()+id,
pack->get_first_schedule_time()+minCWindow);
}
else{
node1[from].get_startTransmission_buff().insert(node1[from].get_startTransmission_buff().begin()+id,
pack->get_first_schedule_time()+minCWindow+ 2 * timeXPac);
}
} /*else if Ack and msg*/
if ( pack->get_type()== Packet::ACK())
{
cout << "This is if we DO NOT have ack mechanism or its an Ack packet" << endl;
//node1[j].set_start_transmission( current_time_ + pkt.get_schedule_time());
node1[from].get_startTransmission_buff().insert(node1[from].get_startTransmission_buff().begin()+id,
pack->get_first_schedule_time()+minCWindow);
}
long double thisSchedule = node1[from].start_transmission[id];
pack->set_schedule_time(thisSchedule);
Packet pkt = *pack;
cout << "packet with id: " <<pack->get_id() <<" is retransmitted from: " << from << " to: " << to << "at time: " << thisSchedule << '\n';
if (protocol){
theSimulation.Event_Scheduler (new sendEvent (thisSchedule));
}
else if(!protocol)
{
if (thisSchedule <= next_slot){
cout<<"less than next slot"<<endl;
pkt.set_schedule_time(next_slot);
next_slot += interval;
}
else{
pkt.set_schedule_time(thisSchedule);
cout<<"NOw next slot"<<endl;
next_slot += interval;
}
theSimulation.Event_Scheduler (new slotted_sendEvent (pkt.get_schedule_time()));
}
return pkt;
}
void Wireless_node::reschedule_packet(Packet *p, long double time1)
{
p->set_status(Packet::RESCHEDULED());
if (p->get_first_schedule_time()==-1)
{
p->set_first_schedule_time(p->get_schedule_time());
}
p->set_schedule_time(time1);
p->set_expected_receive_time(p->get_schedule_time()+ prop [p->get_from()] [p->get_to()]);
int from = p->get_from();
Packet pk = *p;
//cout << "packet: " <<pk.get_id() << " from node: "<< from << " is rescheduled" << endl;
node1[from].getMyVec().push_back(pk);
}
void Wireless_node::collision(Packet *p, long double current) {
Random_Number_Generator r;
failed_transmissions_++;
numCollisions ++;
//completed_transmissions_ --;
int from = p->get_from();
int to = p->get_to();
int id = p->get_id();
//reschedule_packet(p, current + rnd_transmission.rand_exponential());
if (backoff){
if (p->get_retransmit_attempt() < ret_threshold){
cout<< "retransmit attempt of packet "<< id << "is " << p->get_retransmit_attempt() <<endl;
p->set_retransmit_attempt(1);
gen_rearrival(p, (rand_CW(p->get_retransmit_attempt()))* slt_duration );
}
}
else{
if (p->get_retransmit_attempt() < ret_threshold){
cout<< "retransmit attempt of packet "<< id << "is " << p->get_retransmit_attempt() <<endl;
p->set_retransmit_attempt(1);
gen_rearrival(p , 0 );
}
}
}
/* this next method returns TRUE if the simulation is within the desired error range */
bool Wireless_node::is_within_error_range(long double err) const {
return (efficiency_ > 0) && (abs(efficiency_ - get_expected_efficiency()) < err);
}
long double Wireless_node::get_expected_efficiency() const {
long double G = transmission_rate_ ;//* propagation_time_;
return G*exp((-1)*G);
}
long double Wireless_node::distance (long double x1, long double y1, long double x2, long double y2)
{
int distancex = abs(x2 - x1) * abs(x2 - x1);
int distancey = abs(y2 - y1) * abs(y2 - y1);
long double distance = sqrt(abs(distancex - distancey));
return distance;
}
long double Wireless_node::propagation_delay (long double dist)
{
dist= dist/300000000;
return dist;
}
long double Wireless_node::range (int from, int to)
{
Wireless_node dis;
long double distance_two;
long double x1 = list_nodes[from].x;
long double y1 = list_nodes[from].x;
long double x2 = list_nodes[to].y;
long double y2 = list_nodes[to].y;
distance_two = dis.distance(x1,y1,x2,y2);
if (distance_two - trans_range_ < 0)
return true;
else
return false;
}
void simulator::run(){
while (!eventQueue.empty()){
event * nextEvent = eventQueue.top();
eventQueue.pop();
current_time = nextEvent ->current_time_;
sysTime=current_time;
if (current_time < 30){
cout<< ">>>>>next event happens at: " << current_time << endl;
nextEvent ->processEvent ();
delete nextEvent;
//nextEvent=NULL;
}
}
}
void Wireless_node::BubbleSort(vector <Packet> num)
{
int i, j, flag = 1; // set flag to 1 to start first pass
Packet temp; // holding variable
Wireless_node node;
Packet pkt;
double numLength = node.buff.size();
for(i = 1; (i <= numLength) && flag; i++)
{
flag = 0;
for (j=0; j < (numLength -1); j++)
{
if (node.buff[j+1].get_schedule_time() < node.buff[j].get_schedule_time()) // ascending order simply changes to <
{
temp = node.buff[j]; // swap elements
node.buff[j] = node.buff[j+1];
node.buff[j+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
return; //arrays are passed to functions by address; nothing is returned
}
void transmitEvent::processEvent() {
Wireless_node node_;
Packet *pack=NULL;
Packet pkt;
cout << "- - - - - - - transmitEvent Happened- - - - - - - - "<<endl;
long double b;
for (int j = 0; j < num_nodes; ++j)
{
//for (int j = i; node1[j].getMyVec().size(); ++j)
if(!(node1[j].getMyVec().empty()))
{
//node1[j].BubbleSort(node1[j].buff);
int i, x, flag = 1; // set flag to 1 to start first pass
Packet temp; // holding variable
Packet pkt,pkt2,pkt3;
double numLength = node1[j].buff.size();
for(i = 1; (i <= numLength) && flag; i++)
{
flag = 0;
for (x=0; x < (numLength -1); x++)
{
if (node1[j].buff[x+1].get_schedule_time() < node1[j].buff[x].get_schedule_time()) // ascending order simply changes to <
{
temp = node1[j].buff[x]; // swap elements
node1[j].buff[x] = node1[j].buff[x+1];
node1[j].buff[x+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
//for (int n1=0; n1<node1[j].buff.size(); ++n1)
//{
pkt= node1[0].buff[0];
pkt2=node1[j].buff[0];
pkt3=node1[j].buff[0];
pack= &pkt;
b= current_time_ + pkt.get_schedule_time();
//cout << "min(buff[0],buff[1])==" << std::min(pkt.get_schedule_time(), pkt3.get_schedule_time()) <<endl;
//cout << "transmitEvent time is: " << T->current_time << endl;
//int t = pack ->get_to();
//node1[t].set_start_reception( pack->get_expected_receive_time());
//t.current_time= pkt.get_schedule_time();
cout << "sendEvent happens at time: " << b + pkt.get_schedule_time() << endl;
cout << "current_time_ is: " << current_time_ << endl;
//current_time =current_time + pkt.get_schedule_time();
//}
}
}
theSimulation.Event_Scheduler (new sendEvent (b));
}
void sendEvent::processEvent()
{
Wireless_node node_;
Packet *pack=NULL;
Packet pkt;
cout << "- - - - - - - sendEvent Happened- - - - - - - - "<<endl;
for (int j = 0; j < num_nodes; ++j)
{
/*check the buffer of each node*/
if (! node1[j].getMyVec().empty())
{
long double b;
int i, x, flag = 1; //set flag to 1 to start first pass
Packet temp; //holding variable
double temp_id;
Packet pkt,pkt2,pkt3;
double numLength = node1[j].getMyVec().size();
cout<< "node buff size: "<< numLength << endl;
for(i = 1; (i <= numLength) && flag; i++)
{
flag = 0;
for (x=0; x < (numLength -1); x++)
{
if (node1[j].buff[x+1].get_schedule_time() < node1[j].buff[x].get_schedule_time())
{
temp_id = node1[j].buff[x].get_schedule_time(); //swap elements
node1[j].buff[x].set_schedule_time(node1[j].buff[x+1].get_schedule_time());
node1[j].buff[x+1].set_schedule_time(temp_id);
flag = 1; //indicates that a swap occurred.
}
}
}
for(int k= 0 ; k < node1[j].buff.size() ; ++k )
{
cout<< "node " << j << "size: " << node1[j].buff.size()<< endl;
pkt= node1[j].buff[k];
pack= &pkt;
int to = pack->get_to();
int from= pack->get_from();
int id =pack->get_id();
cout<< "packet with id: "<< id << "schedule time is "<<pack->get_schedule_time()<< endl;
cout <<"current_time_ is "<< current_time_ <<endl;
/*This is only to test*/
if(current_time_ == pack->get_schedule_time())
cout <<"its equal"<<endl;
if((node1[j].get_status()==Wireless_node::IDLE()) && channelBusy[j]==0 && current_time_ == pack->get_schedule_time())
{
cout<< "i entered" <<endl;
if (Ack && pkt.get_type()== Packet::MSG())
{
cout << " Packet: "<< pack ->get_id() << " is from node: " << pack ->get_from() << " to node: "<< pack ->get_to()<<endl;
cout << "\n With start transmission time of the packet: "<< id << " "<< node1[j].start_transmission[id] << endl;
node1[j].attempt_transmission(pack, node1[j].start_transmission[id]);
//node1[j].completed_transmission(pack, current_time_);
node1[j].set_status(Wireless_node::SENDING());
isBusy[from]=true;
/*Check if its wired/wireless and assign channel as busy accordingly */
for(int i=0; i< num_nodes; ++i){
if (wire){
m.lock();
++channelBusy[i];
cout<< "send ev: channel busy for node "<<i<<"with value "<< channelBusy[i]<<endl;
accessCount++;
m.unlock();
}
else{
if (dist0[from][i]<70){
//cout << "dist from "<< from <<" to node "<< i << dist0[from][i]<<endl;
m.lock();
++channelBusy[i];
cout<< "send ev: channel busy for node "<<i<<"with value "<< channelBusy[i]<<endl;
accessCount++;
m.unlock();
}
}
}
node1[from].get_send_buff().push_back(pkt);
cout<< "node "<< from<< "buffer send size: "<< node1[j].get_send_buff().size()<<endl;
node1[to].get_receipt_buff().push_back(pkt);
node1[j].get_endTransmission_buff().insert(node1[j].get_endTransmission_buff().begin()+id,
node1[j].start_transmission[id]+ timeXPac);
cout << "\n With end transmission time of first packet: "<< node1[j].end_transmission[id] << endl;
//node1[to].get_startReception_buff().insert(node1[to].get_startReception_buff().begin()+id, node1[j].start_transmission[id] + timeXPac + I );
node1[to].get_startReception_buff().insert(node1[to].get_startReception_buff().begin()+id, node1[j].end_transmission[id]+ I);
//cout << "node: " << to << " start_reception is: " << node1[j].end_transmission[id] <<endl;
//pack->set_timer(pack->get_expected_receive_time() * 2);
//cout <<"\n timer is set to " << pack->get_timer() << endl;
theSimulation.Event_Scheduler (new end_sendEvent ( node1[j].end_transmission[id]));
cout << "with ReceptionEvent at node: "<< to << "at time: "<< node1[to].start_reception[id] << endl;
theSimulation.Event_Scheduler (new receptionEvent (node1[to].start_reception[id]));
node1[j].getMyVec().erase(node1[j].getMyVec().begin());
}//if msg
else if(!Ack && pkt.get_type()== Packet::MSG()){
cout << " Packet: "<< pack ->get_id() << " is from node: " << pack ->get_from() << " to node: "<< pack ->get_to()<<endl;
cout << "\n With start transmission time of the packet: "<< id << " "<< node1[j].start_transmission[id] << endl;
node1[j].attempt_transmission(pack, node1[j].start_transmission[id]);
//node1[j].completed_transmission(pack, current_time_);
//pack->set_timer(pack->get_expected_receive_time() * 2);
//cout <<"\n timer is set to " << pack->get_timer() << endl;
node1[j].set_status(Wireless_node::SENDING());
isBusy[from]=true;
/*Check if its wired/wireless and assign channel as busy accordingly */
for(int i=0; i< num_nodes; ++i){
if (wire){
m.lock();
++channelBusy[i];
cout<< "send ev: channel busy for node "<<i<<"with value "<< channelBusy[i]<<endl;
accessCount++;
m.unlock();
}
else{
if (dist0[from][i]<70){
//cout << "dist from "<< from <<" to node "<< i << dist0[from][i]<<endl;
m.lock();
++channelBusy[i];
cout<< "send ev: channel busy for node "<<i<<"with value "<< channelBusy[i]<<endl;
accessCount++;
m.unlock();
}
}
}
node1[from].get_send_buff().push_back(pkt);
cout<< "node "<< from<< "buffer send size: "<< node1[j].get_send_buff().size()<<endl;
node1[to].get_receipt_buff().push_back(pkt);
node1[j].get_endTransmission_buff().insert(node1[j].get_endTransmission_buff().begin()+id,
node1[j].start_transmission[id]+ timeXPac);
cout << "\n With end transmission time of first packet: "<< node1[j].end_transmission[id] << endl;
cout<< "the I value is: "<< I <<endl;
//node1[to].get_startReception_buff().insert(node1[to].get_startReception_buff().begin()+id, node1[j].start_transmission[id] + timeXPac + I );
node1[to].get_startReception_buff().insert(node1[to].get_startReception_buff().begin()+id, node1[j].end_transmission[id] + I );
cout << "node: " << to << " start_reception is: " << node1[to].start_reception[id] <<endl;
theSimulation.Event_Scheduler (new end_sendEvent ( node1[j].end_transmission[id]));
cout << "with ReceptionEvent at node: "<< to << "at time: "<< node1[to].start_reception[id] << endl;
theSimulation.Event_Scheduler (new receptionEvent (node1[to].start_reception[id]));
node1[j].getMyVec().erase(node1[j].getMyVec().begin());
}
if ( pkt.get_type()== Packet::ACK() && current_time_ == pack->get_schedule_time())
{
cout << "This is if we DO NOT have ack mechanism or its an Ack packet" << endl;
node1[j].attempt_transmission(pack,node1[j].start_transmission[id]);
node1[j].get_send_buff().push_back(pkt);
node1[j].set_status(Wireless_node::SENDING());
isBusy[from]=true;
/*Check if its wired/wireless and assign channel as busy accordingly */
for(int i=0; i< num_nodes; ++i){
if (wire){
m.lock();
++channelBusy[i];
cout<< "send ev: channel busy for node "<<i<<"with value "<< channelBusy[i]<<endl;
accessCount++;
m.unlock();
}
else{
if (dist0[from][i]<70){
//cout << "dist from "<< from <<" to node "<< i << dist0[from][i]<<endl;
m.lock();
++channelBusy[i];
cout<< "send ev: channel busy for node "<<i<<"with value "<< channelBusy[i]<<endl;
accessCount++;
m.unlock();
}
}
}
node1[j].get_endTransmission_buff().insert(node1[j].get_endTransmission_buff().begin()+id,
node1[j].start_transmission[id]+ timeXAck );
node1[to].get_startReception_buff().insert(node1[to].get_startReception_buff().begin()+id, node1[j].start_transmission[id] + timeXAck + I );
cout << "End_sendEvent happens at time: " << node1[j].start_transmission[id]+ timeXAck << endl;
theSimulation.Event_Scheduler (new end_sendEvent ( node1[j].start_transmission[id]+ timeXAck));
//node1[to].set_start_reception(node1[j].get_start_transmission() + pack->get_expected_receive_time());
cout << "with ReceptionEvent at node: "<< to << "at time: "<< node1[to].start_reception[id] << endl;
pack->set_timer(pack->get_expected_receive_time() + I);
cout <<"\n timer is set to " << pack->get_timer() << endl;
node1[to].get_receipt_buff().push_back(pkt);
theSimulation.Event_Scheduler (new receptionEvent (node1[to].start_reception[id]));
node1[j].getMyVec().erase(node1[j].getMyVec().begin());
}
} /*This is if the channel is busy and we try to send*/
else if (node1[j].get_status() ==Wireless_node::IDLE() && channelBusy[j]!= 0 && current_time_ == pack->get_schedule_time())
{