-
Notifications
You must be signed in to change notification settings - Fork 8
/
Router.cpp
executable file
·2049 lines (1950 loc) · 61.6 KB
/
Router.cpp
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
// Router.cpp : implementation file
//
#include "stdafx.h"
#include "MANET.h"
#include "Router.h"
/////////////////////////////
#include "RouterDialog.h"
#include "Link.h"
#include "Traffic.h"
#include "Packet.h"
/////////////////////////////
/////////////////////////////
#include <math.h>
/////////////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRouter
//Selfishness Bound
#define MAX_POWER 200000 //2000000000
#define MIN1_POWER MAX_POWER/4 //1000
#define MIN2_POWER MAX_POWER/2//2000
#define TimeOutFactor 3
CRouter::CRouter(CString Name,int X_axis,int Y_axis,int Rect_Length,int Rect_Width,int COVERAGE,int DELAY,int Buffer_Size,CString Image_Path,int type)
{
this->Caption =Name;
this->X =X_axis;
this->Y =Y_axis;
this->RectLength =Rect_Length;
this->RectWidth =Rect_Width;
this->Rectangle.top =Y;
this->Rectangle.bottom =Y+RectLength;
this->Rectangle.left =X;
this->Rectangle.right =X+RectWidth;
this->Coverage =COVERAGE;
this->Delay =DELAY;
this->BufferSize =Buffer_Size;
this->ImagePath =Image_Path;
this->Type =0;//0=Adhoc
///////////////////////////////////
this->Created =0;//not created
this->RReqSeqNum =0;
this->TCR =0;
this->RRepSeqNum =0;
this->DataSeqNum =0;
this->RReqExp =-1;
this->RRepExp =-1;
this->DataExp =-1;
this->ProcessDelay =-1;//it means infinite!
this->SendData =FALSE;
this->Clock =0;
this->HelloPeriod =100;
this->TCRcredit =500;
this->GenDataCounter =0;
this->GenAckCounter =0;
this->GenSelDataCounter =0;
this->GenSelfishAckCounter=0;
this->GenRReqCounter =0;
this->GenRRepCounter =0;
this->EstablishedConnectionsCounter=0;
DataTransCounter =0;
AckTransCounter =0;
RReqTransCounter =0;
RRepTransCounter =0;
//PacketUploaders.Add("");
Power =MAX_POWER;
Game_Strategy =1;
//pre_Type =0; //Shows what was previous type of router? if selfish3
///////////////////////////////////
}
CRouter::~CRouter()
{
int rreqp;
for(rreqp=0;rreqp<GenRReqPackets.GetSize();rreqp++)
if( ((CPacket*)(GenRReqPackets[rreqp]))!=NULL )
delete ((CPacket*)(GenRReqPackets[rreqp]));
GenRReqPackets.RemoveAll();
int rrepp;
for(rrepp=0;rrepp<GenRRepPackets.GetSize();rrepp++)
if( ((CPacket*)(GenRRepPackets[rrepp]))!=NULL )
delete ((CPacket*)(GenRRepPackets[rrepp]));
GenRRepPackets.RemoveAll();
int datap;
for(datap=0;datap<GenDataPackets.GetSize();datap++)
if( ((CPacket*)(GenDataPackets[datap]))!=NULL )
delete ((CPacket*)(GenDataPackets[datap]));
GenDataPackets.RemoveAll();
int ackp;
for(ackp=0;ackp<GenAckPackets.GetSize();ackp++)
if( ((CPacket*)(GenAckPackets[ackp]))!=NULL )
delete ((CPacket*)(GenAckPackets[ackp]));
GenAckPackets.RemoveAll();
int il;
for(il=0;il<Links.GetSize();il++)
if( ((CLink*)(Links[il]))!=NULL )
delete ((CLink*)(Links[il]));
Links.RemoveAll();
int it;
for(it=0;it<Traffics.GetSize();it++)
if( ((CTraffic*)(Traffics[it]))!=NULL )
delete ((CTraffic*)(Traffics[it]));
Traffics.RemoveAll();
int ig;
for(ig=0;ig<Games.GetSize();ig++)
if( ((CGame*)(Games[ig]))!=NULL )
delete ((CGame*)(Games[ig]));
Games.RemoveAll();
}
BEGIN_MESSAGE_MAP(CRouter, CButton)
//{{AFX_MSG_MAP(CRouter)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRouter message handlers
void CRouter::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetCapture();
Moved=0;
LeftPreviousPosition=point;
CButton::OnLButtonDown(nFlags, point);
}
void CRouter::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(GetCapture()==this)
{
ReleaseCapture();
if(Moved)
{
int dx=point.x-LeftPreviousPosition.x;
int dy=point.y-LeftPreviousPosition.y;
Rectangle.bottom+=dy;
Rectangle.top+=dy;
Rectangle.left+=dx;
Rectangle.right+=dx;
this->RECT2XY(Rectangle);
MoveWindow(&Rectangle);
GetParent()->Invalidate();
}
else
{
this->OnClicked();
//CRouterDialog rd;
//rd.GetRouterData(this);
//if(rd.DoModal()==IDOK)
//rd.PushRouterData(this);
}
}
CButton::OnLButtonUp(nFlags, point);
}
void CRouter::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if((GetCapture()==this)&&(nFlags==MK_LBUTTON))
{
Moved=1;
int dx=point.x-LeftPreviousPosition.x;
int dy=point.y-LeftPreviousPosition.y;
Rectangle.bottom+=dy;
Rectangle.top+=dy;
Rectangle.left+=dx;
Rectangle.right+=dx;
this->RECT2XY(Rectangle);
MoveWindow(&Rectangle);
//GetParent()->Invalidate();
}
CButton::OnMouseMove(nFlags, point);
}
void CRouter::OnClicked()
{
// TODO: Add your control notification handler code here
CRouterDialog rd;
rd.GetRouterData(this);
if(rd.DoModal()==IDOK)
{
rd.PushRouterData(this);
//HINSTANCE hinst;
//if(this->Type==1)
// this->SetBitmap(::LoadBitmap(NULL/*hinst*/, MAKEINTRESOURCE(IDB_Selfish1)));//OBM_CLOSE)));
}
}
void CRouter::Update(CString Name,int X_axis,int Y_axis,int Rect_Length,int Rect_Width,int COVERAGE,int DELAY,int Buffer_Size,CString Image_Path,int type)
{
this->Caption =Name;
this->X =X_axis;
this->Y =Y_axis;
this->RectLength =Rect_Length;
this->RectWidth =Rect_Width;
this->Rectangle.top =Y;
this->Rectangle.bottom =Y+RectLength;
this->Rectangle.left =X;
this->Rectangle.right =X+RectWidth;
this->Coverage =COVERAGE;
this->Delay =DELAY;
this->BufferSize =Buffer_Size;
this->ImagePath =Image_Path;
this->Type =type;
}
void CRouter::RECT2XY(RECT rectangle)
{
this->X =rectangle.left;
this->Y =rectangle.top;
this->RectLength=rectangle.bottom-Y;
this->RectWidth =rectangle.right-X;
}
void CRouter::XY2RECT(long x,long y)
{
this->Y =y;
this->X =x;
this->Rectangle.top =y;
this->Rectangle.left =x;
this->Rectangle.bottom =Y+RectLength;
this->Rectangle.right =X+RectWidth;
}
void CRouter::UpdateRect()
{
this->Rectangle.top =Y;
this->Rectangle.left =X;
this->Rectangle.bottom =Y+RectLength;
this->Rectangle.right =X+RectWidth;
}
void CRouter::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{ // storing code
ar<<Caption<<Type<<Rectangle<<Coverage<<Delay<<BufferSize<<ImagePath;
//for(int i=0;i<links.GetSize();i++)
// ((CLink*)(links[i]))->Serialize(ar);
}
else
{ // loading code
ar>>Caption>>Type>>Rectangle>>Coverage>>Delay>>BufferSize>>ImagePath;
this->RECT2XY(Rectangle);
}
}
void CRouter::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//CMANETView* pParent=
// this->GetParent()->OnRButtonDown(nFlags,point);
//pParent->PreviousRouter=this;
CButton::OnRButtonDown(nFlags, point);
}
void CRouter::OnRButtonUp(UINT nFlags, CPoint point)
{
CButton::OnRButtonUp(nFlags, point);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
CPacket* CRouter::GenRReq(int protocol, CRouter *destination)//CTraffic* tr)
{
Packet=new CPacket(rand(),0/*type=RReq*/,protocol,Caption,destination->Caption,++RReqSeqNum,RReqExp,TCR);
return Packet;
}
CPacket* CRouter::GenRRep(CPacket* packet)//int protocol, CRouter *source, CRouter *destination)
{
Packet=new CPacket(packet->ID,1/*type=RRep*/,packet->Protocol,Caption,packet->Source,++RRepSeqNum,RRepExp);
return Packet;
}
CPacket* CRouter::GenDataPacket(CPacket *packet)
{
Packet=new CPacket(packet->ID,2/*type=Data*/,packet->Protocol,Caption,packet->Source,++DataSeqNum,DataExp);
return Packet;
}
CPacket* CRouter::GenAckPacket(CPacket* packet)
{
Packet=new CPacket(packet->ID,3/*type=Ack*/,packet->Protocol,packet->Destination,packet->Source,packet->SeqNum,packet->Expiration);
return Packet;
}
void CRouter::Broadcast(CPacket* packet)
{
if(LinkSize=Links.GetSize())
{
int uis=UploadersIndexes.GetSize();
if((Caption==packet->Source)||(Caption==packet->Destination))
{
for(int i=0;i<LinkSize;i++)
{
((CLink*)(Links[i]))->Upload(packet,this);
packet->HopCount++;
//DIMAN
if(Power>0)Power-=4;
if(packet->Type)
RRepTransCounter++;
else
RReqTransCounter++;
}
}
else if(uis>0)
{
for(int i=0;i<LinkSize;i++)
{
if(i!=UploadersIndexes[0])
{
((CLink*)(Links[i]))->Upload(packet,this);
packet->HopCount++;
if(Power>0)Power-=4;
if(packet->Type)
RRepTransCounter++;
else
RReqTransCounter++;
}
}
}
}
}
void CRouter::ProcessCredit()
{
if(LinkSize=Links.GetSize())
for(int i=0;i<LinkSize;i++)
///if(!(Clock%HelloPeriod))
///credits[i]+=70;
/// credits[i]+=HelloPeriod/2;
//else
if (Credits[i]!=0)
Credits[i]--;
}
void CRouter::ProcessTimers()
{
int OutQueueSize=OutQueue.GetSize();
int i=0;
//for(int i=0;i<queueoutsize;i++)
while(i<OutQueueSize)
{
if(Timers[i]!=0)
{
Timers[i]--;
if (Timers[i]==0)
{ //find the next hop to which packet was given
//////////////////////////
Credits[NextHops[i]]=0; //and remove its credit
((CGame*)(Games[NextHops[i]]))->AddneighborAction(0); //NextHops[i] = Neighbor who discarded our packet
//////////////////////////
OutQueue.RemoveAt(i);
Timers.RemoveAt(i);
NextHops.RemoveAt(i);
OutQueueSize--;
//////////////////////////
}
}
i++;
}
}
void CRouter::Receive()
{
if(LinkSize=Links.GetSize())
for(int l=0;l<LinkSize;l++)
if(((CLink*)(Links[l]))->Transmit(this))
if(((Packet=((CLink*)(Links[l]))->Download(this))!=NULL)
&&((Packet->Destination==this->Caption)||(this->Type!=1)))
{
//DIMAN
//Power Management to be selfish!
if(Power<MIN1_POWER)
{
this->Type=1;//Selfish1
}
else if(Power<MIN2_POWER)
{
this->Type=2;//Selfish2
}
//else if(Power<0)
// Power=MAX_POWER;
///if (credits[l]!= 0) //ignore the packet if sender has no credit
///{
///GiveCredit() if neighbor forward packet
int q=0;
int OutQueueSize=0;
if (Packet->Type==2) //for data packets
{
if(Power>0)Power-=10;//DIMAN// ghablan 4 bood man data ro 10 barabare RREQ gereftam
OutQueueSize=OutQueue.GetSize();
while((q<OutQueueSize)&&(Packet!=(CPacket*)(OutQueue[q])))
q++;
if(q<OutQueueSize)
{
if (Timers[q]!=0)
{
((CGame*)(Games[l]))->AddneighborAction(1); //Forwarding Data = Receiving Confirmation
Credits[l]+=Packet->Credit; //"l" is index of the uploader
Timers.RemoveAt(q); //give credit and empty the queueout of that packet
OutQueue.RemoveAt(q);
NextHops.RemoveAt(q);
}
}
}
else //for control packets
{
if(Power>0)Power--;
if ( Packet->Source != (((CRouter*)(Neighbors[l]))->Caption) ) //if sender is not the packet source
Credits[l]+=Packet->Credit; //give credit
if ( (Credits[l]==0) && (Packet->Type==0) && (TCRcounters[l]!=0) && (Packet->Uploader==Packet->Source) )
///if ( (credits[l]==0) && (TCRcounters[l]!=0)) ///&& (Packet->Uploader==Packet->Source) )
{
TCRcounters[l]--;
Credits[1]+=TCRcredit;
}
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
///////////We can add here GAME theoretic decisions//////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//int action;
if( (((CGame*)(Games[l]))->main_strategy()) == 1) //Forward
//if ((Credits[l]!= 0))///||(Packet->Type==3)) //ignore the packet if sender has no credit
{ //forward ack packets anyway!
((CGame*)(Games[l]))->AddmyAction(1);
if(q==OutQueueSize)//not for credit
{
if(PacketRepetition=IsProcessed((CRouter*)(Neighbors[l]),Packet))
//Already Processed? if yes,do nothing
{
if(ProcessedPackets.GetSize()>20000)
{// Also ProcessedNeighbors.GetSize()>20000
int pps;
for(pps=0;pps<10000;pps++)
{
ProcessedPackets.RemoveAt(0);
ProcessedNeighbors.RemoveAt(0);
}
}
ProcessedPackets.Add(Packet); //processedpackets ~ processedneighbors
ProcessedNeighbors.Add((CRouter*)(Neighbors[l]));
/////////////////////////
PacketUploaders.Add(((CRouter*)(Neighbors[l]))->Caption);
UploadersIndexes.Add(l);
PacketRepetitions.Add(PacketRepetition);
if(!ReceivedPackets.Add((CPacket*)Packet))//if Packet is not the first
ProcessDelay=GetProcessDelay(Packet);
//ReceivePackets ~ packetrepetitions ~ PacketUploaders ~ uploaderindexes
/////////////////////////
GetParent()->Invalidate();
/////////////////////////
}
else
{
//if(Packet->Type==2)
//{//Source must delete the path
//}
}
}
}
else
((CGame*)(Games[l]))->AddmyAction(0);
}
}
BYTE CRouter::IsProcessed(CRouter* neighbor, CPacket* packet)
{//0=own packet or same packet same neighbor, 1=same packet, 2=new packet
if(packet->Source!=Caption) //if we didnt originate the packet
{
if(ProcessedPacketSize=ProcessedPackets.GetSize()) //if there is any processedpacket
{ //look up the processed packets to see if
//the received one was processed before
int ppc=0;
ReturnValue=2;//Default=new packet
//int SamePacketCounter=0;
while( (ppc<ProcessedPacketSize) &&
((packet!=(CPacket*)(ProcessedPackets[ppc])) ||
(neighbor!=(CRouter*)(ProcessedNeighbors[ppc]))) )//same packet and same neighbor?
{
if(packet==(CPacket*)(ProcessedPackets[ppc]))
ReturnValue=1; //same packet
//SamePacketCounter++;
ppc++;
}
if(ppc<ProcessedPacketSize) //if received packet was found in previously processed ones
{//same packet from the same neighbor
if(packet->Type<2) //rreq or rrep -> not a data or ack packet
return 0;//same packet same neighbor
else
{// data or ack packet
int sti=GetTableIndexToSource(packet);
if((sti!=-1)&&(PathSize=((CTable*)(Tables[sti]))->Paths.GetSize()))
{//Get the Destine-Table Matched With Packet!
int psc=0;
while((psc<PathSize)&&(((CPath*)(((CTable*)(Tables[sti]))->Paths[psc]))->FirstHop!=neighbor->Caption))
psc++;
if(psc<PathSize)//Get the FirstHop Matched
{
if(packet->SeqNum>((CPath*)(((CTable*)(Tables[sti]))->Paths[psc]))->SeqNum)
return 1;//same packet same neighbor BUT new SeqNum
else
return 0;//same packet same neighbor OLD SeqNum
}
else
return 2;//no path to neighbor to reach the source
}
else
return 2;//no table to the source of data or ack packet
}
}
else
return ReturnValue; //new packet || same packet but not from the same neighbor
// if(ReceivedPacketSize=ReceivedPackets.GetSize()) //search previously received packets
// {
// int rpc=0;
// while((rpc<ReceivedPacketSize)&&(packet!=(CPacket*)(ReceivedPackets[rpc])))
// rpc++;
// if(rpc<ReceivedPacketSize) //packet received before
// {
// if(neighbor->Caption==PacketUploaders[rpc])
// return 0; //same neighbor,same packet,received before
// else
// return 1; //received before but from another neighbor
// }
// }
}
else
return 2;//new packet, not originated by us
}
else
return 3;//own packet
}
int CRouter::GetProcessDelay(CPacket* packet)
{
if(packet->Type==0) //Route Request Packet *3
{
return 3*Delay;
}
else if(packet->Type==1) //Route Reply Packet *3
{
return 3*Delay;
}
else if(packet->Type==2) //Data Packet *1
{
return Delay;
}
else if(packet->Type==3) //Ack Packet *1
{
return Delay;
}
else
return 0;
}
void CRouter::SparkTraffic()
{
if(TrafficSize=Traffics.GetSize())
{
int t=0;
while(t<TrafficSize)
{
if( ((CTraffic*)(Traffics[t]))->Start )
{
((CTraffic*)(Traffics[t]))->Start--;
t++;
}
else
{
Packet=GenRReq(((CTraffic*)(Traffics[t]))->Protocol,((CTraffic*)(Traffics[t]))->Destination);
GenRReqPackets.Add((CPacket*)Packet);
Packet->Start=Clock; //Save current clock of router
Broadcast(Packet);
delete ((CTraffic*)(Traffics[t])); //traffic ->cntrl packets.that's why we delete them
Traffics.RemoveAt(t);
TrafficSize--;
}
}
}
if(DataPacketSize=DataPackets.GetSize())
{
int dpsc=0;
while((dpsc<DataPacketSize)&&(((CPacket*)(DataPackets[dpsc]))->SendData))
{
DataPacket=(CPacket*)(DataPackets[dpsc]);
if(!(DataPacket->TimeoutCounter--))
{//When datapacket times out
DataPacket->TimeoutCounter = DataPacket->Timeout;
int sti=GetTableIndexToDest(DataPacket);
if(sti!=-1)
{
//DataPacket->ChangePath=TRUE;
//int n=GetNextHop(datapacket,(CTable*)(Tables[sti]));
int n=GetNextBenignHop(DataPacket,(CTable*)(Tables[sti]));
if(n!=-1)
{
DataPacket->SeqNum++;
((CLink*)(Links[n]))->Upload(DataPacket,this);
DataPacket->HopCount++;
if(Power>0)Power-=40;//DIMAN// ghablan 16 bood man data ro 10 barabare RREQ gereftam
if (this->Type==0)
GenDataCounter++;
else
GenSelDataCounter++;
if(DataPacket->Destination!=((CRouter*)(Neighbors[n]))->Caption)
{
OutQueue.Add((CPacket*)DataPacket);
Timers.Add(4*((CLink*)(Links[n]))->Delay); //modified/
NextHops.Add(n);
}
}
else
{
DataPacket->Stop=Clock;
DataPacket->SendData=FALSE;
//EstablishedConnectionsCounter++;
//DeadLock Zone
//no benign neighbor to destination
}
}
}
dpsc++;
}
}
}
void CRouter::tick()
{
if(Power>0)
{
Receive();//Receive Packets
Process();//Process Packets
/// ProcessCredit(); //Update Credits as time goes by
ProcessTimers(); //Update Timers as time goes by
SparkTraffic();//Spark Traffics
}
Clock++;
}
void CRouter::Process()
{
if(ReceivedPackets.GetSize())
{
if(ProcessDelay)
ProcessDelay--;//decrease ProcessDelay until it reaches ZERO
else //ProcessDelay==0
{
Packet=(CPacket*)(ReceivedPackets[0]); //Get Current Packet To Be To Process
if(Packet->Type==0) //Route Request Packet Processing
{
ProcessRReqPacket(Packet);
//processedpackets.Add(Packet); //Add Current Packet to processedpackets Array
}
else if(Packet->Type==1) //Route Reply Packet Processing
{
ProcessRRepPacket(Packet);
//processedpackets.Add(Packet); //Add Current Packet to processedpackets Array
}
else if(Packet->Type==2) //Data Packet Processing
{
ProcessDataPacket(Packet);
//processedpackets.Add(Packet); //Add Current Packet to processedpackets Array
}
else if(Packet->Type==3) //Ack Packet Processing
{
ProcessAckPacket(Packet);
//processedpackets.Add(Packet);
}
else
{
//ProcessAnyPacket(Packet);
//processedpackets.Add(Packet);
}
/////////////////Last Modifications////////////////////////////////
ReceivedPackets.RemoveAt(0); //Remove Current Packet from packets Array
PacketUploaders.RemoveAt(0); //Remove Current Packet-Uploader from PacketUploaders Array
UploadersIndexes.RemoveAt(0);
PacketRepetitions.RemoveAt(0); //Remove Current Packet Repetition Type
if(ReceivedPackets.GetSize()) //Set Next Process Delay
ProcessDelay=((CPacket*)(ReceivedPackets[0]))->ProcessDelay;
else
ProcessDelay=-1; //infinite
///////////////////////////////////////////////////////////////////
}
}
}
void CRouter::ProcessRReqPacket(CPacket* packet)
{
if(packet->Protocol==0)
ProcessRReqPacket0(packet);
else if(packet->Protocol==1)
ProcessRReqPacket1(packet);
else if(packet->Protocol==2)
ProcessRReqPacket2(packet);
}
void CRouter::ProcessRRepPacket(CPacket* packet)
{
if(packet->Protocol==0)
ProcessRRepPacket0(packet);
else if(packet->Protocol==1)
ProcessRRepPacket1(packet);
else if(packet->Protocol==2)
ProcessRRepPacket2(packet);
}
void CRouter::ProcessDataPacket(CPacket* packet)
{
if((!this->Type)||(packet->Destination==Caption))
{//Router Type == Ad-hoc
if(packet->Protocol==0)
ProcessDataPacket0(packet);
else if(packet->Protocol==1)
ProcessDataPacket1(packet);
else if(packet->Protocol==2)
ProcessDataPacket2(packet);
}
}
void CRouter::ProcessAckPacket(CPacket* packet)
{
if((this->Type!=1)||(packet->Destination==Caption))
{
if(packet->Protocol==0)
ProcessAckPacket0(packet);
else if(packet->Protocol==1)
ProcessAckPacket1(packet);
else if(packet->Protocol==2)
ProcessAckPacket2(packet);
}
}
/*BYTE CRouter::IsBroadcast(CPacket* packet)
{
if(packet->Source!=Caption)
{
if(processedpacketsize=processedpackets.GetSize())
{
int ppc=0;
while((ppc<processedpacketsize)&&(packet!=(CPacket*)(processedpackets[ppc])))
ppc++;
if(ppc<processedpacketsize)
{
if(neighbor==(CRouter*)(processedneighbors[ppc]))
return 0;//Repeated Packet from Repeated Neighbor
else
return 1;//Repeated Packet Only
}
else
return 2;//new packet
}
else
return 2;//new packet
}
else
return 0;//own packet
}*/
inline int CRouter::GetNextHop(CPacket* packet,CTable *table)
{
if(PathSize=table->Paths.GetSize())
{//Get the Destine-Table Matched With Packet!
if(packet->ChangePath)//Policy to Change Previous Path
{
if(!table->Round)
packet->ChangePath=FALSE;
table->ChangePath();
}
//while((psc<PathSize)&&(!((CPath*)(table->Paths[psc]))->SizeCount))
// psc++;
if(NeighborSize=Neighbors.GetSize())
{//Get the Firsthop-Neighbor Matched With Packet!
int nsc=0;
while((nsc<NeighborSize)&&(((CPath*)(table->Paths[table->Index]))->FirstHop!=((CRouter*)(Neighbors[nsc]))->Caption))
nsc++;
if(nsc<NeighborSize)
{//Send Packet to Firsthop Neighbor = Upload to proper link
//((CPath*)(table->Paths[psc]))->SizeCount--;
return nsc;
}
else
{
if ((table->Index) >= 0)
{
delete (CPath*)(table->Paths[table->Index]);
table->Paths.RemoveAt(table->Index);
table->Index=table->Index%table->Paths.GetSize();
return GetNextHop(packet,table);
}
else
return -1;
}
}
else
return -1;
}
else
return -1;
}
inline int CRouter::GetNextBenignHop(CPacket* packet,CTable *table)
{//Get next benign hop
if(PathSize=table->Paths.GetSize())
{//Get the Destine-Table Matched With Packet!
if(NeighborSize=Neighbors.GetSize())
{//Get the Firsthop-Neighbor Matched With Packet!
int IsNotSource=(packet->Source!=Caption)?1:0;
int nsc;
while(!table->Round)
{
nsc=0;
while((nsc<NeighborSize)&&(((CPath*)(table->Paths[table->Index]))->FirstHop!=((CRouter*)(Neighbors[nsc]))->Caption))
nsc++;
if(nsc<NeighborSize)
{//Send Packet to Firsthop Neighbor = Upload to proper link
if( ((CGame*)(Games[nsc]))->main_strategy() == 1 )
//if(Credits[nsc]>0)
{// Neighbor must have credit
if(IsNotSource)
{//Intermediate-node
if(nsc!=UploadersIndexes[0])
//Non-Uploader
return nsc; //if first hop is a benign node give it the data
else
{//Uploader
table->ChangePath();
packet->ChangePath=FALSE;
}
}
else
{//Source-node
return nsc;
}
}
else //otherwise,change your path to the destination
{
table->ChangePath();
packet->ChangePath=FALSE;
//return GetNextBenignHop(packet,table);
}
}
else
{
delete (CPath*)(table->Paths[table->Index]);
table->Paths.RemoveAt(table->Index);
if(table->Paths.GetSize())
table->Index=table->Index%table->Paths.GetSize();
//else
// delete table;
//return GetNextBenignHop(packet,table);
}
}
//Only 4 Demi
table->Round=0; //zero table->round since each time packet expires we search the whole table only once to find a new path
//by making it zero we finish this round and reset it for next time trials.
if(IsNotSource)
{
packet->ChangePath=TRUE;
return UploadersIndexes[0];
}
else
return -1;
}
else
//return UploadersIndexes[0];
return -1;
}
else
//return UploadersIndexes[0];
return -1;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
void CRouter::ProcessDataPacket0(CPacket* packet)//ELMAR
{
//if(LoopFree(p)){
if(packet->Destination!=Caption)
{
if(TableSize=Tables.GetSize())
{//For Each Destination We have Some Paths
int tdc=0;
while((tdc<TableSize)&&(((CTable*)(Tables[tdc]))->Destination!=packet->Destination))
tdc++;
if(packet->ChangePath)
{
if(tdc<TableSize)
{
Table=(CTable*)(Tables[tdc]);
PathSize=Table->Paths.GetSize();
int pdc=0;
while((pdc<PathSize)&&(((CPath*)(Table->Paths[pdc]))->FirstHop!=PacketUploaders[0]))
pdc++;
if(pdc<PathSize)
{
delete (CPath*)(Table->Paths[pdc]);
Table->Paths.RemoveAt(pdc);
packet->ChangePath=FALSE;
if(Table->Paths.GetSize())
{
if(pdc<Table->Index)
Table->Index--;
Table->Index=Table->Index%Table->Paths.GetSize();
}
else
{
delete Table;
Tables.RemoveAt(tdc);
}
}
}
}
else
{//To set Ack path
int tsc=0;
while((tsc<TableSize)&&(((CTable*)(Tables[tsc]))->Destination!=packet->Source))
tsc++;
if(tsc<TableSize)
{
Table=(CTable*)(Tables[tsc]);
if(PathSize=Table->Paths.GetSize())
{//Get the Destine-Table Matched With Packet!
int psc=0;
while((psc<PathSize)&&(((CPath*)(Table->Paths[psc]))->FirstHop!=PacketUploaders[0]))
psc++;
if(psc<PathSize)
{
Table->Index=psc;
packet->Uploader=PacketUploaders[0];
((CPath*)(Table->Paths[psc]))->Update(packet);
}
}
}
}
TableSize=Tables.GetSize();
if((tdc<TableSize))//&&(PathSize=((CTable*)(Tables[tsc]))->Paths.GetSize()))
{//Get the Destine-Table Matched With Packet!
//int psc=0;
//while((psc<PathSize)&&(!((CPath*)(((CTable*)(Tables[tsc]))->Paths[psc]))->SizeCount))
// psc++;
//if((psc<PathSize)&&(neighborsize=neighbors.GetSize()))
//{//Get the Firsthop-Neighbor Matched With Packet!
//int nsc=0;
int ndc=0;
///if(packet->ChangePath)
ndc=GetNextBenignHop(packet,(CTable*)(Tables[tdc]));
//CString s=((CRouter*)(Neighbors[ndc]))->Caption;
///else
/// nsc=GetNextHop(packet,(CTable*)(Tables[tsc]));
//while((nsc<neighborsize)&&(((CPath*)(((CTable*)(Tables[tsc]))->Paths[psc]))->FirstHop!=((CRouter*)(neighbors[nsc]))->Caption))
// nsc++;