-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAodv.groovy
1409 lines (1165 loc) · 61.2 KB
/
Aodv.groovy
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
import org.arl.fjage.*
import org.arl.unet.*
import org.arl.unet.phy.*
import org.arl.unet.net.*
import org.arl.unet.mac.*
import org.arl.unet.nodeinfo.*
"""
The class implements the Ad hoc On-demand Distance Vector-based routing
protocol (designed for MANETs) for a Shallow Water Ad Hoc Network.
Reference:
=========
C. Perkins, E. Belding-Royer, and S. Das, "Ad-hoc On-Demand Distance Vector
(AODV) Routing," RFC 3561, 2003.
"""
class Aodv extends UnetAgent
{
private AgentID node, phy, rtr, mac
private int myAddr
private int temp = 0 // Initial Broadcast request ID number (temp) will be zero.
private int seqn = 0 // Initial sequential number would be zero.
private int rreqcount = 0 // Monitors the RREQ rate per second.
private int rerrcount = 0 // Monitors the RERR rate per second.
private int firstActiveRouteFlag = 0 // Monitors the FIRST ACTIVE ROUTE instance to initiate HELLO PACKET CHECK.
/* CONSTANTS */
// Types of packet:
private final static int RERR = 0x01
private final static int HELLO = 0x02
// Sequence number status:
private final static int VALID_DSN = 1
private final static int INVALID_DSN = 0
private final static int UNKNOWN_DSN = -1
// Important parameters:
private final static int MAX_NUMBER_OF_TXS = 1
private final static int ALLOWED_HELLO_LOSS = 2
private final static int NET_DIAMETER = 6
// Various timeout values:
private final static long ACTIVE_ROUTE_TIMEOUT = 60000
private final static long HELLO_INTERVAL = 30000
private final static long NODE_TRAVERSAL_TIME = 5000
private final static long NET_TRAVERSAL_TIME = 2*NODE_TRAVERSAL_TIME*NET_DIAMETER
// Different Protocols used:
private final static int ROUTING_PROTOCOL = Protocol.ROUTING
private final static int RM_PROTOCOL = Protocol.ROUTE_MAINTENANCE
private final static int DATA_PROTOCOL = Protocol.DATA
private final static int HOP_ZERO = 0
private final static int HOP_ONE = 1
// Active status of a route:
private final static boolean ACTIVE = true
private final static boolean INACTIVE = false
// HELLO or NON HELLO packet:
private final static boolean NON_HELLO_PACKET = true
private final static boolean HELLO_PACKET = false
// RREQ or Non RREQ packet:
private final static boolean RREQ_PKT = true
private final static boolean NON_RREQ = false
private final static double inf = Double.POSITIVE_INFINITY // Infinity
private final static PDU rreqpacket = PDU.withFormat
{
uint32('sourceAddr')
uint32('sourceSeqNum')
uint32('broadcastId')
uint32('destAddr')
uint32('destSeqNum')
uint32('hopCount')
}
private final static PDU rreppacket = PDU.withFormat
{
uint32('sourceAddr')
uint32('destAddr')
uint32('destSeqNum')
uint32('hopCount')
uint32('expirationTime')
}
private final static PDU rmpacket = PDU.withFormat
{
uint32('type')
uint32('destAddr')
uint32('destSeqNum')
uint32('hopCount')
uint32('expirationTime')
}
private final static PDU dataMsg = PDU.withFormat
{
uint16('source')
uint16('destination')
}
// Routing Table of the nodes.
private class RoutingInfo
{
private int destinationAddress
private int validdsn
private int dsn
private int nexHop
private int numHops
private long expTime
private boolean active
}
// Number of retransmissions for every destination.
private class AttemptingHistory
{
private int destinationAddr
private int num
}
// Packet history table.
private class PacketHistory
{
private int osna
private int ridn
private int hoco
}
// To maintain precursor list.
private class Precursor
{
private int finalnode
private int neighbournode
}
// Reservations for MAC.
private class TxReserve
{
private TxFrameReq txreq
private ReservationReq resreq
}
// Route error packet table.
private class PacketError
{
private int errnode // faulty route to this DESTINATION.
private int errnum // Destination sequence number.
}
ArrayList<Aodv.RoutingInfo> myroutingtable = new ArrayList<Aodv.RoutingInfo>() // routing table.
ArrayList<Aodv.AttemptingHistory> attemptHistory = new ArrayList<Aodv.AttemptingHistory>() // route discovery attempts.
ArrayList<Aodv.PacketHistory> myPacketHistory = new ArrayList<Aodv.PacketHistory>() // packet history.
ArrayList<Aodv.Precursor> precursorList = new ArrayList<Aodv.Precursor>() // precursor list for destination nodes.
ArrayList<Aodv.TxReserve> reservationTable = new ArrayList<Aodv.TxReserve>() // packet reservation table.
ArrayList<Aodv.PacketError> errorTable = new ArrayList<Aodv.PacketError>() // rerr packet history.
@Override
void setup()
{
register Services.ROUTE_MAINTENANCE
}
@Override
void startup()
{
node = agentForService(Services.NODE_INFO) // Node information
myAddr = node.Address
mac = agentForService(Services.MAC) // MAC services
rtr = agentForService(Services.ROUTING) // Routing services
phy = agentForService(Services.PHYSICAL) // Physical layer services
subscribe phy // subscribe to phy for Notifications
}
private void rxDisable()
{
// Disable Receiver.
ParameterReq req = new ParameterReq(agentForService(Services.PHYSICAL))
req.get(PhysicalParam.rxEnable)
ParameterRsp rsp = (ParameterRsp) request(req, 1000)
rsp.set(PhysicalParam.rxEnable,false)
}
private void rxEnable()
{
// Enable Receiver.
ParameterReq req = new ParameterReq(agentForService(Services.PHYSICAL))
req.get(PhysicalParam.rxEnable)
ParameterRsp rsp = (ParameterRsp)request(req, 1000)
rsp.set(PhysicalParam.rxEnable,true)
}
// Broadcast RREQ for Route Discovery.
private void sendRreqBroadcast(int destination)
{
int dsnvalue = getDsn(destination) // Either some (known) value or UNKNOWN DSN.
// Before broadcasting the RREQ, save the details of this packet.
PacketHistory ph = new PacketHistory(osna: myAddr, ridn: temp, hoco: HOP_ZERO)
myPacketHistory.add(ph)
packetHistoryDeletion(myAddr, temp, HOP_ZERO) // Packet History shall be deleted after a certain time.
// Preparing the RREQ-BROADCAST packet.
def bytes = rreqpacket.encode(sourceAddr: myAddr, sourceSeqNum: seqn, broadcastId: temp, destAddr: destination, destSeqNum: dsnvalue, hopCount: HOP_ZERO)
TxFrameReq tx = new TxFrameReq(to: Address.BROADCAST, type: Physical.CONTROL, protocol: ROUTING_PROTOCOL, data: bytes)
sendMessage(tx)
// Page 15: Binary exponential backoff-based timeout for ROUTE DISCOVERY CHECK.
/*add new WakerBehavior(routeDiscTimeout(destination)*NET_TRAVERSAL_TIME, {
//routeDiscoveryCheck(destination)
})*/
}
// Binary exponential backoff for retransmissions.
private double routeDiscTimeout(int destination)
{
for (int i = 0; i < attemptHistory.size(); i++)
{
if (attemptHistory.get(i).destinationAddr == destination)
{
return Math.pow(2, attemptHistory.get(i).num - 1)
}
}
}
// Check for Route discovery success.
private void routeDiscoveryCheck(int dest)
{
// Destination node found in the RT with an ACTIVE route.
if (nodePresent(dest) == true && getActiveStatus(dest) == ACTIVE && getExpirationTime(dest) >= currentTimeMillis())
{
return
}
// Do another Route Discovery.
else
{
def rdp = agentForService(Services.ROUTE_MAINTENANCE)
rdp << new RouteDiscoveryReq(to: dest, maxHops: 50, count: 1)
}
}
// Sending Reservation Requests for exponential backoff-based carrier sensing.
private void sendMessage(TxFrameReq txReq)
{
if (txReq.type == Physical.CONTROL) // CONTROL packets.
{
if (txReq.protocol == ROUTING_PROTOCOL) // RREQ and RREP packets.
{
ReservationReq rs = new ReservationReq(to: txReq.to, duration: controlMsgDuration/1000)
TxReserve tr = new TxReserve(txreq: txReq, resreq: rs)
reservationTable.add(tr)
mac << rs // send ReservationReq.
}
if (txReq.protocol == RM_PROTOCOL) // RERR and HELLO packets.
{
ReservationReq rs = new ReservationReq(to: txReq.to, duration: 1.62/1000)
TxReserve tr = new TxReserve(txreq: txReq, resreq: rs)
reservationTable.add(tr)
mac << rs // send ReservationReq.
}
}
if (txReq.type == Physical.DATA) // DATA packets.
{
ReservationReq rs = new ReservationReq(to: txReq.to, duration: dataMsgDuration/1000)
TxReserve tr = new TxReserve(txreq: txReq, resreq: rs)
reservationTable.add(tr)
mac << rs // send ReservationReq.
}
}
// As mentioned on Page 22, 6.9., after every HELLO_INTERVAL,
// I check the LAST BROADCAST of a node if there is a single ACTIVE route present in the RT.
private void localConnectivityManagement()
{
add new TickerBehavior(HELLO_INTERVAL, {
int checkActive = 0 // After every HELLO_INTERVAL, this should become 1 if there's a single ACTIVE NEIGHBOUR ROUTE present.
for (int i = 0; i < myroutingtable.size(); i++)
{
// If there is any ACTIVE NEIGHBOUR route, I check whether I BROADCASTED any packet in the last HELLO_INTERVAL.
if (myroutingtable.get(i).destinationAddress == myroutingtable.get(i).nexHop &&
myroutingtable.get(i).active == ACTIVE && (myroutingtable.get(i).expTime - currentTimeMillis()) < 5000)
{
checkActive = 1 // An ACTIVE NEIGHBOUR ROUTE is present.
long expiry = currentTimeMillis() + ALLOWED_HELLO_LOSS*HELLO_INTERVAL // See Page 22.
def bytes = rmpacket.encode(type: HELLO, destAddr: myAddr, destSeqNum: seqn, hopCount: HOP_ZERO, expirationTime: expiry)
TxFrameReq tx = new TxFrameReq(to: Address.BROADCAST, type: Physical.CONTROL, protocol: RM_PROTOCOL, data: bytes)
sendMessage(tx)
break
}
}
if (checkActive == 0) // There are no ACTIVE NEIGHBOUR ROUTES in this node's RT.
{
firstActiveRouteFlag = 0 // Turn OFF local connectivity management as there's no ACTIVE NEIGHBOUR ROUTE.
stop()
return
}
})
}
// Sending RERR to PRECURSORS (if any) of the affected destination.
private void routeErrorPacket(int target)
{
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == target)
{
myroutingtable.get(i).expTime = 0 // EXPIRATION time as zero.
myroutingtable.get(i).active = INACTIVE // DEACTIVATING the route.
int so = ++myroutingtable.get(i).dsn // Incrementing the DSN.
// Send a RERR packet if there are any PRECURSORS for TARGET.
if (getPrecursorCount(target) == 0)
{
// No need for RERR unicast or broadcast.
}
// There is only ONE PRECURSOR.
if (getPrecursorCount(target) == 1)
{
PacketError pe = new PacketError(errnode: target, errnum: so)
errorTable.add(pe) // Add this RERR packet in Error Table history.
// Preparing the RERR packet for UNICAST.
def bytes = rmpacket.encode(type: RERR, destAddr: target, destSeqNum: so, hopCount: inf, expirationTime: 0)
TxFrameReq tx = new TxFrameReq(to: getPrecursor(target), type: Physical.CONTROL, protocol: RM_PROTOCOL, data: bytes)
sendMessage(tx)
}
// There are MORE THAN ONE PRECURSORS.
if (getPrecursorCount(target) > 1)
{
PacketError pe = new PacketError(errnode: target, errnum: so)
errorTable.add(pe) // Add this RERR packet in Error Table history.
// Preparing the RERR packet for BROADCAST.
def bytes = rmpacket.encode(type: RERR, destAddr: target, destSeqNum: so, hopCount: inf, expirationTime: 0)
TxFrameReq tx = new TxFrameReq(to: Address.BROADCAST, type: Physical.CONTROL, protocol: RM_PROTOCOL, data: bytes)
sendMessage(tx)
}
// Wait for this long, then DELETE the route if still INACTIVE.
add new WakerBehavior(ALLOWED_HELLO_LOSS*HELLO_INTERVAL,
{
if (getActiveStatus(target) == INACTIVE || getExpirationTime(target) < currentTimeMillis())
{
removeRouteEntry(target)
}
else
{
// The route was ACTIVATED.
}
})
// If it is a faulty Neighbour Node, also send a RERR packet for all the destinations reached using this as the NEXT HOP.
if (myroutingtable.get(i).nexHop == target)
{
for (int j = 0; j < myroutingtable.size(); j++)
{
// DEACTIVATE all the ACTIVE routes using TARGET as their next hop.
if (myroutingtable.get(j).destinationAddress != target &&
(myroutingtable.get(j).active == ACTIVE || myroutingtable.get(j).expTime < currentTimeMillis()))
{
int finaldest = myroutingtable.get(j).destinationAddress
int incdsn = ++myroutingtable.get(j).dsn // Incrementing the DSN.
myroutingtable.get(j).expTime = 0 // EXPIRATION time as zero.
myroutingtable.get(j).active = INACTIVE // DEACTIVATING the route.
if (getPrecursorCount(finaldest) == 0)
{
// No need for RERR unicast or broadcast.
}
// There is only ONE PRECURSOR.
if (getPrecursorCount(finaldest) == 1)
{
PacketError pe = new PacketError(errnode: finaldest, errnum: incdsn)
errorTable.add(pe) // Add this RERR packet in Error Table history.
// Preparing the RERR packet for UNICAST.
def bytes = rmpacket.encode(type: RERR, destAddr: finaldest, destSeqNum: incdsn, hopCount: inf, expirationTime: 0)
TxFrameReq tx = new TxFrameReq(to: getPrecursor(finaldest), type: Physical.CONTROL, protocol: RM_PROTOCOL, data: bytes)
sendMessage(tx)
}
// There are MORE THAN ONE PRECURSORS.
if (getPrecursorCount(finaldest) > 1)
{
PacketError pe = new PacketError(errnode: finaldest, errnum: incdsn)
errorTable.add(pe) // Add this RERR packet in Error Table history.
// Preparing the RERR packet for BROADCAST.
def bytes = rmpacket.encode(type: RERR, destAddr: finaldest, destSeqNum: incdsn, hopCount: inf, expirationTime: 0)
TxFrameReq tx = new TxFrameReq(to: Address.BROADCAST, type: Physical.CONTROL, protocol: RM_PROTOCOL, data: bytes)
sendMessage(tx)
}
// Wait for this long, then DELETE the route if still INACTIVE.
add new WakerBehavior(ALLOWED_HELLO_LOSS*HELLO_INTERVAL,
{
if (getActiveStatus(finaldest) == INACTIVE || getExpirationTime(finaldest) < currentTimeMillis())
{
removeRouteEntry(finaldest)
}
else
{
// The route was ACTIVATED.
}
})
}
}
}
break
}
}
}
// Remove a route entry along with its precursor list.
private void removeRouteEntry(int dest)
{
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == dest)
{
myroutingtable.remove(i) // remove route entry.
break
}
}
for (int i = 0; i < precursorList.size(); i++)
{
if (precursorList.get(i).finalnode == dest)
{
precursorList.remove(i) // remove corresponding precursor entry.
break
}
}
}
// When a route is used to transmit DATA/RREP packets, its life is extended by max of current expiration buffer and ACTIVE_ROUTE_TIMEOUT.
private void extendRouteLife(int node)
{
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == node)
{
myroutingtable.get(i).active = ACTIVE
myroutingtable.get(i).expTime = Math.max(getExpirationTime(node), currentTimeMillis() + ACTIVE_ROUTE_TIMEOUT) // Page 21.
break
}
}
}
/* If the OS is already there in the RT, update its details in case
* the seq number of packet < the sequence number in the RT, or
* the seq numbers of both the packet and the RT are equal, but the packet has a smaller hop count than that in the RT, or
* the seq number in the RT is UNKNOWN.
*/
private void routingDetailsUpdate( int origin, int from, int seqnum, int hcv, long life, int statusone, boolean statustwo,
int statusthree, boolean statusfour, boolean nonhello, boolean rreq)
{
if (origin == from) // 1) If the OS directly sent me this message.
{
int flag = 0
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == origin)
{
flag = 1 // The node is already there in the RT.
if (rreq) // RREQ packet. For these conditions, see page 16: 6.5.
{
myroutingtable.get(i).validdsn = statusone // always VALID.
if (myroutingtable.get(i).dsn < seqnum)
{
myroutingtable.get(i).dsn = seqnum
}
myroutingtable.get(i).nexHop = from
myroutingtable.get(i).numHops = hcv
myroutingtable.get(i).expTime = life
myroutingtable.get(i).active = statustwo
}
// RREP and HELLO packets.
if (!rreq && (myroutingtable.get(i).dsn < seqnum || myroutingtable.get(i).validdsn == INVALID_DSN ||
(myroutingtable.get(i).dsn == seqnum && (hcv < myroutingtable.get(i).numHops || myroutingtable.get(i).active == INACTIVE))))
{
// For these conditions, see page 20: 6.7.
myroutingtable.get(i).validdsn = statusone // always VALID.
myroutingtable.get(i).dsn = seqnum
myroutingtable.get(i).nexHop = from
myroutingtable.get(i).numHops = hcv
myroutingtable.get(i).expTime = life
myroutingtable.get(i).active = statustwo
}
break
}
}
// If this node was never there in my RT, I add its details.
if (flag == 0)
{
RoutingInfo ri = new RoutingInfo(
destinationAddress: origin,
validdsn: statusone,
dsn: seqnum,
nexHop: from,
numHops: hcv,
expTime: life,
active: statustwo
)
myroutingtable.add(ri)
if (rreq) // To purge any unwanted route entries created during a Route Discovery.
{ // Only RREQ-generated routes.
routeValidityCheck(life - currentTimeMillis(), origin)
}
}
/*if (nonhello && statustwo) // The Non HELLO PACKET-generated ROUTE is ACTIVE.
{ // HELLO Packet Check has not yet been initiated.
// Start HELLO PACKET check as I have an ACTIVE NEIGHBOUR node.
}*/
// THIS IS FOR NEIGHBOUR NODES
if (statustwo && firstActiveRouteFlag == 0) // It's a HELLO packet-generated route. Monitor its status regularly.
{
firstActiveRouteFlag = 1 // LOCAL CONNECTIVITY MANAGEMENT BEGINS
localConnectivityManagement() // Page 22, 6.9.
}
}
else // 2) The OS did not send this message directly, it came from an intermediate node.
{
int flag = 0
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == origin)
{
flag = 1 // The node is already there in the RT.
if (rreq) // RREQ packet. For these conditions, see page 16: 6.5.
{
myroutingtable.get(i).validdsn = statusone // always VALID.
if (myroutingtable.get(i).dsn < seqnum) // Updated if only the seqnum > RT seqn.
{
myroutingtable.get(i).dsn = seqnum
}
myroutingtable.get(i).nexHop = from
myroutingtable.get(i).numHops = hcv
myroutingtable.get(i).expTime = life
myroutingtable.get(i).active = statustwo
}
// RREP packets.
if (!rreq && (myroutingtable.get(i).dsn < seqnum || myroutingtable.get(i).validdsn == INVALID_DSN ||
(myroutingtable.get(i).dsn == seqnum && (hcv < myroutingtable.get(i).numHops || myroutingtable.get(i).active == INACTIVE))))
{
// For these conditions, see page 20: 6.7.
myroutingtable.get(i).validdsn = statusone // always VALID.
myroutingtable.get(i).dsn = seqnum
myroutingtable.get(i).nexHop = from
myroutingtable.get(i).numHops = hcv
myroutingtable.get(i).expTime = life
myroutingtable.get(i).active = statustwo
}
break
}
}
// If this node was never there in my RT, I add its details.
if (flag == 0)
{
RoutingInfo ri = new RoutingInfo(
destinationAddress: origin,
validdsn: statusone,
dsn: seqnum,
nexHop: from,
numHops: hcv,
expTime: life,
active: statustwo
)
myroutingtable.add(ri)
if (rreq) // To purge any unwanted route entries created during a Route Discovery.
{
routeValidityCheck(life - currentTimeMillis(), origin)
}
}
if (statustwo) // The Non HELLO PACKET-generated ROUTE is ACTIVE.
{
activeStatusCheck(origin) // The route is ACTIVE, keep a regular check on its ACTIVE STATUS.
}
int verification = 0
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == from) // The next hop towards the origin is already there in the RT.
{
verification = 1 // The node is already there in the RT.
if (getDsnStatus(from) == INVALID_DSN) // The DSN status shall be changed only when it is INVALID.
{
myroutingtable.get(i).validdsn = statusthree
}
myroutingtable.get(i).dsn = getDsn(from) // Either some value or UNKNOWN.
myroutingtable.get(i).nexHop = from
myroutingtable.get(i).numHops = HOP_ONE
// If this node was already there, it's TIMEOUT should be the max of current expiration buffer and life.
myroutingtable.get(i).expTime = Math.max(getExpirationTime(from), life)
myroutingtable.get(i).active = statusfour
break
}
}
// This neighbour node was never there in the RT.
if (verification == 0)
{
RoutingInfo ri = new RoutingInfo(
destinationAddress: from,
validdsn: statusthree,
dsn: UNKNOWN_DSN, // Keep the DSN of this guy as UNKNOWN for now.
nexHop: from,
numHops: HOP_ONE,
expTime: life,
active: statusfour
)
myroutingtable.add(ri)
if (rreq) // To purge any unwanted route entries created during a Route Discovery.
{
routeValidityCheck(life - currentTimeMillis(), from)
}
}
if (statusfour && firstActiveRouteFlag == 0) // The Non HELLO PACKET-generated ROUTE is ACTIVE.
{ // HELLO Packet check has not yet been initiated.
firstActiveRouteFlag = 1
localConnectivityManagement() // Start HELLO PACKET check, as I have an ACTIVE NEIGHBOUR node.
}
}
}
// To purge any unwanted REVERSE ROUTE entries added during Route discovery.
private void routeValidityCheck(long timegap, int node)
{
add new WakerBehavior(timegap,
{
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == node)
{
if (myroutingtable.get(i).expTime < currentTimeMillis() || myroutingtable.get(i).active == INACTIVE)
{
myroutingtable.remove(i)
return
}
}
}
})
}
// After every ACTIVE_ROUTE_TIMEOUT seconds, the ACTIVE STATUS of the route is CHECKED.
// If the ROUTE is found as INACTIVE, a RERR packet is prepared.
private void activeStatusCheck(int node)
{
add new TickerBehavior(ACTIVE_ROUTE_TIMEOUT,
{
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == node)
{
if (myroutingtable.get(i).expTime < currentTimeMillis() || myroutingtable.get(i).active == INACTIVE)
{
routeErrorPacket(node) // Do a Route Error analysis on this node.
stop()
return
}
break
}
}
})
}
// Delete PH after twice of the NET_TRAVERSAL_TIME period.
private void packetHistoryDeletion(int sender, int requestId, int hopcount)
{
add new WakerBehavior(2*NET_TRAVERSAL_TIME,
{
for (int i = 0; i < myPacketHistory.size(); i++)
{
if (myPacketHistory.get(i).osna == sender && myPacketHistory.get(i).ridn == requestId && myPacketHistory.get(i).hoco == hopcount)
{
myPacketHistory.remove(i)
return
}
}
})
}
private boolean nodePresent(int node)
{
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == node)
{
return true
}
}
return false
}
private int getDsn(int destination)
{
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == destination)
{
return myroutingtable.get(i).dsn
}
}
return UNKNOWN_DSN
}
private int getNextHop(int destination)
{
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == destination)
{
return myroutingtable.get(i).nexHop
}
}
}
private int getHopCount(int destination)
{
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == destination)
{
return myroutingtable.get(i).numHops
}
}
return inf
}
private int getDsnStatus(int destination)
{
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == destination)
{
return myroutingtable.get(i).validdsn
}
}
return INVALID_DSN
}
private boolean getActiveStatus(int destination)
{
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == destination)
{
return myroutingtable.get(i).active
}
}
return INACTIVE
}
private long getExpirationTime(int destination)
{
for (int i = 0; i < myroutingtable.size(); i++)
{
if (myroutingtable.get(i).destinationAddress == destination)
{
return myroutingtable.get(i).expTime
}
}
return 0
}
private int getPrecursor(int dest)
{
for (int i = 0; i < precursorList.size(); i++)
{
if (precursorList.get(i).finalnode == dest)
{
return precursorList.get(i).neighbournode
}
}
return 0
}
private int getPrecursorCount(int destination)
{
int count = 0
for (int i = 0; i < precursorList.size(); i++)
{
if (precursorList.get(i).finalnode == destination)
{
count++
}
}
return count
}
private void precursorAddition(int fn, int nn)
{
for (int i = 0; i < precursorList.size(); i++)
{
if (precursorList.get(i).finalnode == fn && precursorList.get(i).neighbournode == nn)
{
return // If this precursor pair is already there, do not add it.
}
}
Precursor pc = new Precursor(finalnode: fn, neighbournode: nn)
precursorList.add(pc)
}
/* 0: Max no. of transmissions not yet reached, but the node has been searched before.
* 1: Node is searching for the first time. Do a route discovery.
* 2: Maximum number of transmissions reached for this node. Refuse the request.
*/
private int retransmissionCheck(int destination)
{
for (int i = 0; i < attemptHistory.size(); i++)
{
if (attemptHistory.get(i).destinationAddr == destination)
{
int txs = attemptHistory.get(i).num // Number of transmissions for this destination.
if (txs == MAX_NUMBER_OF_TXS)
{
return 2
}
if (txs < MAX_NUMBER_OF_TXS)
{
return 0
}
}
}
// Either this destination was never searched before or this is the first ever search by this node.
return 1
}
@Override
Message processRequest(Message msg)
{
if (msg instanceof RouteDiscoveryReq)
{
int fd = msg.to // The Final Destination.
if (myroutingtable.size() == 0) // 1) My RT is empty, check for how many times the destination has already been searched.
{
int reTxCheck = retransmissionCheck(fd)
if (reTxCheck == 0) // 1.1) This dest has been searched before, but it hasn't reached its max txs yet.
{
for (int i = 0; i < attemptHistory.size(); i++)
{
if (attemptHistory.get(i).destinationAddr == fd)
{
rreqcount++ // Incrementing the RREQ count.
attemptHistory.get(i).num++ // Increment the value of attempts for this destination; a new Route Discovery is starting.
temp++ // Increase the request ID.
seqn++ // Incrementing the sequence number for a Route Discovery.
sendRreqBroadcast(fd) // Send an RREQ Broadcast.
return new Message(msg, Performative.AGREE)
}
}
}
else if (reTxCheck == 1) // 1.2) This dest is being searched for the first time.
{
rreqcount++ // Incrementing the RREQ count.
AttemptingHistory tr = new AttemptingHistory(destinationAddr: fd, num: 1)
attemptHistory.add(tr) // Added this destination in the attempting table.
temp++ // Incrementing the request ID.
seqn++ // Incrementing the sequence number for a Route Discovery.
sendRreqBroadcast(fd) // Send an RREQ Broadcast.
return new Message(msg, Performative.AGREE)
}
else if (reTxCheck == 2) // 1.3) Max txs limit reached. The node is unreachable, so refuse.
{
return new Message(msg, Performative.REFUSE)
}
}
else // 2) In case my RT has some entries, search for the Desired destination first.
{
/* If my RT has some entries, check for destination first:
* 1) If found, it should be active. Send a notification;
* 2) If not, do a Route discovery if the number of re-transmissions permits.
*/
if (nodePresent(fd) && getActiveStatus(fd) == ACTIVE && getExpirationTime(fd) >= currentTimeMillis())
{
int ko = getNextHop(fd) // Destination found in the RT and the route is active.
int jo = getHopCount(fd) // Number of hops.
rtr << new RouteDiscoveryNtf(to: fd, nextHop: ko, hops: jo, reliability: true) // Sending Route discovery notification.
// Extend the route life of FINAL DESTINATION and NEXT HOP routes.
extendRouteLife(fd)
extendRouteLife(ko)
return new Message(msg, Performative.AGREE)
}
// Destination was not found in the RT. Do a check on the number of re-transmissions.
int reTxCheck = retransmissionCheck(fd)
if (reTxCheck == 0) // 2.1) This dest has been searched before, but it hasn't reached its max txs yet.
{
for (int i = 0; i < attemptHistory.size(); i++)
{
if (attemptHistory.get(i).destinationAddr == fd)
{
rreqcount++ // Incrementing the RREQ count.
attemptHistory.get(i).num++ // Increment the value of transmissions for this destination.
temp++ // Increase temp (request ID number), as a new route discovery is gonna start.
seqn++ // Incrementing the sequence number for a Route Discovery.
sendRreqBroadcast(fd) // Send an RREQ Broadcast.
return new Message(msg, Performative.AGREE)
}
}
}
else if (reTxCheck == 1) // 2.2) This dest is being searched for the first time.
{