-
Notifications
You must be signed in to change notification settings - Fork 0
/
HChord_U.cc
1796 lines (1489 loc) · 50.9 KB
/
HChord_U.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cfloat>
#include <cfloat>
#include <assert.h>
#include <map>
#include <GlobalStatistics.h>
#include <Comparator.h>
#include <BootstrapList.h>
#include <HChordFingerTable.h>
#include <HChordSuccessorList.h>
#include "HChord_U.h"
#include "HChord_L.h"
using namespace std;
Define_Module(HChord_U);
HChord_U::HChord_U()
{
stabilize_timer = stabilize_super_peer_timer = upper_join_timer = migrate_timer = NULL;
}
void HChord_U::initializeOverlay(int stage)
{
// because of IPAddressResolver, we need to wait until interfaces
// are registered, address auto-assignment takes place etc.
if (stage != MIN_STAGE_OVERLAY)
return;
if (iterativeLookupConfig.merge == true) {
throw new cRuntimeError("HChord_U::initializeOverlay(): "
"HChord doesn't work with iterativeLookupConfig.merge = true!");
}
// HChord provides KBR services
kbr = true;
// fetch some parameters
useCommonAPIforward = par("useCommonAPIforward");
successorListSize = par("successorListSize");
joinRetry = par("joinRetry");
stabilizeRetry = par("stabilizeRetry");
joinDelay = par("joinDelay");
stabilizeDelay = par("stabilizeDelay");
fixfingersDelay = par("fixfingersDelay");
aggressiveJoinMode = par("aggressiveJoinMode");
extendedFingerTable = par("extendedFingerTable");
numFingerCandidates = par("numFingerCandidates");
stabilizeSuperPeerDelay = par("stabilizeSuperPeerDelay");
migrateDelay = par("migrateDelay");
strong_peer = par("StrongPeer");
std::stringstream str;
str<<thisNode.ip;
std::string node_ip = str.str();
EV<<"Node ip is "<<node_ip<<endl;
if(node_ip=="1.0.0.1"||node_ip=="1.0.0.8" ||node_ip=="1.0.0.14" ||node_ip=="1.0.0.15" ||node_ip=="1.0.0.22")
strong_peer=1;
// if(node_ip=="1.0.0.1"||node_ip=="1.0.0.3"||node_ip=="1.0.0.27")
// strong_peer=1;
keyLength = OverlayKey::getLength();
missingPredecessorStabRequests = 0;
missingSuccessorStabResponses = 0;
// statistics
joinCount = 0;
stabilizeCount = 0;
fixfingersCount = 0;
notifyCount = 0;
newsuccessorhintCount = 0;
joinBytesSent = 0;
stabilizeBytesSent = 0;
notifyBytesSent = 0;
fixfingersBytesSent = 0;
newsuccessorhintBytesSent = 0;
in_upper=0;
// find friend modules
findFriendModules();
// add some watches
WATCH(predecessorNode);
WATCH(thisNode);
WATCH(bootstrapNode);
WATCH(joinRetry);
WATCH(strong_peer);
WATCH(super_peer);
WATCH(in_upper);
WATCH_MAP(StrongPeerSet);
//WATCH(missingPredecessorStabRequests);
//WATCH(missingSuccessorStabResponses);
// self-messages
join_timer = new cMessage("join_timer");
stabilize_timer= new cMessage("stabilize_timer");
fixfingers_timer = new cMessage("fixfingers_timer");
stabilize_super_peer_timer = new cMessage("stabilize_super_peer_timer");
if(strong_peer)
{
migrate_timer = new cMessage("migrate_timer");
upper_join_timer = new cMessage("upper_join_timer");
}
}
HChord_U::~HChord_U()
{
// destroy self timer messages
cancelAndDelete(join_timer);
cancelAndDelete(stabilize_timer);
cancelAndDelete(fixfingers_timer);
cancelAndDelete(stabilize_super_peer_timer);
if(strong_peer)
cancelAndDelete(migrate_timer);
if(in_upper)
cancelAndDelete(upper_join_timer);
}
void HChord_U::joinOverlay()
{
changeState(INIT);
changeState(BOOTSTRAP);
}
void HChord_U::changeState(int toState)
{
//
// Defines tasks to be executed when a state change occurs.
//
switch (toState) {
case INIT:
state = INIT;
setOverlayReady(false);
if (thisNode.key.isUnspecified()) {
thisNode.key = OverlayKey::random();
callUpdate(thisNode, true);
}
predecessorNode = NodeHandle::UNSPECIFIED_NODE;
// initialize finger table and successor list
initializeFriendModules();
updateTooltip();
// debug message
if (debugOutput) {
EV << "[HChord_U::changeState() @ " << thisNode.ip
<< " (" << thisNode.key.toString(16) << ")]\n"
<< " Entered INIT stage"
<< endl;
}
parentModule()->parentModule()->bubble("Enter INIT state.");
break;
case BOOTSTRAP:
state = BOOTSTRAP;
// initiate bootstrap process
cancelEvent(join_timer);
// workaround: prevent notificationBoard from taking
// ownership of join_timer message
take(join_timer);
scheduleAt(simulation.simTime(), join_timer);
// debug message
if (debugOutput) {
EV << "[HChord_U::changeState() @ " << thisNode.ip
<< " (" << thisNode.key.toString(16) << ")]\n"
<< " Entered BOOTSTRAP stage"
<< endl;
}
parentModule()->parentModule()->bubble("Enter BOOTSTRAP state.");
// find a new bootstrap node and enroll to the bootstrap list
bootstrapNode = bootstrapList->getBootstrapNode();
// is this the first node?
if (bootstrapNode.isUnspecified()) {
// create new cord ring
bootstrapNode = thisNode;
super_peer=1;
lower->SuperPeerNode=thisNode;
LargestNode=thisNode;
LargestNodeSuc=thisNode;
StrongPeerSet.insert(make_pair(thisNode.key,thisNode));
in_upper=1;
changeState(READY);
updateTooltip();
}
break;
case READY:
state = READY;
setOverlayReady(true);
//initiate stabilization protocol
cancelEvent(stabilize_timer);
scheduleAt(simulation.simTime() + stabilizeDelay, stabilize_timer);
// initiate finger repair protocol
cancelEvent(fixfingers_timer);
scheduleAt(simulation.simTime() + fixfingersDelay,fixfingers_timer);
cancelEvent(stabilize_super_peer_timer);
scheduleAt(simulation.simTime() + stabilizeSuperPeerDelay, stabilize_super_peer_timer);
//if strong peer initiate migrate protocol
if(strong_peer&&!super_peer)
{
cancelEvent(migrate_timer);
scheduleAt(simulation.simTime()+migrateDelay,migrate_timer);
}
// debug message
if (debugOutput) {
EV << "[HChord_U::changeState() @ " << thisNode.ip
<< " (" << thisNode.key.toString(16) << ")]\n"
<< " Entered READY stage"
<< endl;
}
parentModule()->parentModule()->bubble("Enter READY state.");
break;
}
}
void HChord_U::handleMessage(cMessage* msg)
{
if(msg->arrivedOn("from_lower"))
{
HChordMessage *m = check_and_cast<HChordMessage*>(msg);
sendMessageToUDP(m->getDest(),m);
}
else
{
BaseOverlay::handleMessage(msg);
}
}
void HChord_U::handleTimerEvent(cMessage* msg)
{
// catch JOIN timer
if (msg->isName("join_timer")) {
handleJoinTimerExpired(msg);
}
// catch STABILIZE timer
else if (msg->isName("stabilize_timer")) {
handleStabilizeTimerExpired(msg);
}
// catch FIX_FINGERS timer
else if (msg->isName("fixfingers_timer")) {
handleFixFingersTimerExpired(msg);
}
else if (msg->isName("migrate_timer")){
handleMigrateTimerExpired(msg);
}
else if( msg->isName("stabilize_super_peer_timer")){
handleStabilizeSuperPeerTimerExpired(msg);
}
else if (msg->isName("upper_join_timer")){
handleUpperJoinTimerExpired(msg);
}
// unknown self message
else {
error("HChord_U::handleTimerEvent(): received self message of "
"unknown type!");
}
}
void HChord_U::handleUDPMessage(BaseOverlayMessage* msg)
{
HChordMessage* hchordMsg = check_and_cast<HChordMessage*>(msg);
switch(hchordMsg->getMesg_type()) {
case JOIN_REQUEST :
{
HJoinCall * hcjoin = check_and_cast<HJoinCall *>(msg);
//UHJoinCall *uj = dynamic_cast<UHJoinCall *>(msg);
//if(uj != NULL )
handleJoinRequest(hcjoin);
//else
//send(hcjoin,"to_lower");
}
break;
case JOIN_RESPONSE :
{
HJoinResponse *hcresp = check_and_cast<HJoinResponse *>(msg);
UHJoinResponse *ur = dynamic_cast<UHJoinResponse *>(msg);
if(ur != NULL)
{
handleJoinResponse(ur);
}
else
{
//checks here
changeState(READY);
bootstrapList->removeBootstrapNode(thisNode);
send(hcresp,"to_lower");
}
}
break;
case NEW_SUCCESSOR_HINT :
{
HNewSuccessorHintMsg *newSucc = check_and_cast<HNewSuccessorHintMsg *>(msg);
UHNewSuccessorHintMsg *ns = dynamic_cast<UHNewSuccessorHintMsg*>(msg);
if(ns!=NULL)
{
handleNewSuccessorHint(newSucc);
}
else
send(newSucc,"to_lower");
}
break;
case STABILIZE_CALL :
{
HStabilizeCall *stab = check_and_cast<HStabilizeCall *>(msg);
UHStabilizeCall *us = dynamic_cast<UHStabilizeCall*>(msg);
if(us != NULL)
handleStabilizeCall(stab);
else
send(stab,"to_lower");
}
break;
case STABILIZE_RESPONSE :
{
HStabilizeResponse *stabr = check_and_cast<HStabilizeResponse *>(msg);
UHStabilizeResponse *sr = dynamic_cast<UHStabilizeResponse *>(msg);
if(sr!=NULL)
handleStabilizeResponse(stabr);
else
send(stabr,"to_lower");
}
break;
case NOTIFY_CALL :
{
HNotifyCall *call = check_and_cast<HNotifyCall *>(msg);
UHNotifyCall *nc = dynamic_cast<UHNotifyCall*>(msg);
if(nc!=NULL)
{
handleNotifyCall(call);
}
else
send(call,"to_lower");
}
break;
case NOTIFY_RESPONSE :
{
HNotifyResponse *notr = check_and_cast<HNotifyResponse *>(msg);
UHNotifyResponse *nr = dynamic_cast<UHNotifyResponse *>(msg);
if(nr !=NULL)
handleNotifyResponse(notr);
else
send(notr,"to_lower");
}
break;
case FIXFINGERS_CALL :
{
HFixfingersCall *fix = check_and_cast<HFixfingersCall *>(msg);
UHFixfingersCall *fc = dynamic_cast<UHFixfingersCall *>(msg);
if(fc!=NULL)
handleFixfingersCall(fix);
else
send(fix,"to_lower");
}
break;
case FIXFINGERS_RESPONSE:
{
HFixfingersResponse *fixr = check_and_cast<HFixfingersResponse *>(msg);
UHFixfingersResponse *fr = dynamic_cast<UHFixfingersResponse *>(msg);
if(fr!=NULL)
handleFixfingersResponse(fixr);
else
send(fixr,"to_lower");
}
break;
case MIGRATE_REQUEST :
{
HMigrateRequest *migr = check_and_cast<HMigrateRequest *>(msg);
handleMigrateRequest(migr);
}
break;
case MIGRATE_RESPONSE :
{
HMigrateResponse *migr = check_and_cast<HMigrateResponse *>(msg);
handleMigrateResponse(migr);
}
break;
case STABILIZE_SUPER_PEER_CALL:
{
HStabilizeSuperPeerCall *spstab = check_and_cast<HStabilizeSuperPeerCall *>(msg);
handleStabilizeSuperPeerCall(spstab);
}
break;
case STABILIZE_SUPER_PEER_RESPONSE:
{
HStabilizeSuperPeerResponse *spstabr = check_and_cast<HStabilizeSuperPeerResponse *>(msg);
handleStabilizeSuperPeerResponse(spstabr);
}
break;
case LARGEST_NODE_REQUEST:
{
HLargestNodeRequest *lnreq = check_and_cast<HLargestNodeRequest *>(msg);
handleLargestNodeRequest(lnreq);
}
break;
case LARGEST_NODE_RESPONSE:
{
HLargestNodeResponse *lnrep = check_and_cast<HLargestNodeResponse *>(msg);
handleLargestNodeResponse(lnrep);
}
break;
case CHANGE_SUC_REQ :
{
HChangeSuccessorRequest *suc = check_and_cast<HChangeSuccessorRequest *>(msg);
handleChangeSuccessorRequest(suc);
}
break;
case CHANGE_SUC_RES :
{
HChangeSuccessorResponse *suc = check_and_cast<HChangeSuccessorResponse *>(msg);
handleChangeSuccessorResponse(suc);
}
break;
case CHANGE_PRE :
{
HChangePredecessor *pre = check_and_cast<HChangePredecessor *>(msg);
handleChangePredecessor(pre);
}
break;
default:
error("handleUDPMessage(): Unknown message type!");
break;
}
//delete chordMsg;
}
int HChord_U::getMaxNumSiblings()
{
return successorListSize;
}
int HChord_U::getMaxNumRedundantNodes()
{
return extendedFingerTable ? numFingerCandidates : 1;
}
bool HChord_U::isSiblingFor(const NodeHandle& node,
const OverlayKey& key,
int numSiblings,
bool* err)
{
EV<<"is sibling for key looking for "<<key<<"num = "<<numSiblings<<"node is "<<node<<endl;
if (key.isUnspecified())
error("HChord_U::isSiblingFor(): key is unspecified!");
if (state != READY)
{
*err = true;
return false;
}
if (numSiblings > getMaxNumSiblings())
{
opp_error("HChord_U::isSiblingFor(): numSiblings too big!");
}
// set default number of siblings to consider
if (numSiblings == -1) numSiblings = getMaxNumSiblings();
// if this is the first and only node on the ring, it is responsible
if ((predecessorNode.isUnspecified()) && (node == thisNode))
{
if(successorList->isEmpty() || (node.key == key))
{
*err = false;
EV<<"issibling pre uns and thisNode and suc empty or key=thiskey ret true "<<endl;
return true;
}
else
{
*err = true;
EV<<"issibling pre uns and thisNode and suc empty or key=thiskey ret false"<<endl;
return false;
}
}
if ((node == thisNode) && (key.isBetweenR(predecessorNode.key, thisNode.key)))
{
*err = false;
EV<<"node is this key btwn pre and this"<<endl;
return true;
}
NodeHandle prevNode = predecessorNode;
NodeHandle curNode;
for (int i = -1; i < (int)successorList->getSize(); i++, prevNode = curNode)
{
if (i < 0)
{
curNode = thisNode;
}
else
{
curNode = successorList->getSuccessor(i);
}
if (node == curNode)
{
// is the message destined for curNode?
if (key.isBetweenR(prevNode.key, curNode.key))
{
if (numSiblings <= ((int)successorList->getSize() - i))
{
*err = false;
EV<<"key btwn suc "<<i<<endl;
return true;
}
else
{
*err = true;
return false;
}
}
else
{
// the key doesn't directly belong to this node, but
// the node could be a sibling for this key
if (numSiblings <= 1)
{
*err = false;
return false;
}
else
{
// In Chord we don't know if we belong to the
// replicaSet of one of our predecessors
*err = true;
return false;
}
}
}
}
// node is not in our neighborSet
*err = true;
return false;
}
NodeHandle HChord_U::findNode_h(const OverlayKey& key,int numSiblings,int numRedundantNodes , BaseOverlayMessage* msg)
{
bool err;
EV<<endl<<"FindNode() called sibligs "<<numSiblings<<" with key "<<key<<endl;
NodeHandle nextHop=NodeHandle::UNSPECIFIED_NODE;
if (state != READY)
return nextHop;
// if key is unspecified, the message is for this node
if (key.isUnspecified())
{
nextHop = thisNode;
}
// the message is destined for this node
else if (isSiblingFor(thisNode, key, 1, &err))
{
nextHop = thisNode;
}
// the message destined for our successor
else if (key.isBetweenR(thisNode.key,successorList->getSuccessor().key))
{
nextHop = successorList->getSuccessor();
}
// find next hop with finger table and/or successor list
else
{
nextHop = closestPreceedingNode(key);
}
return nextHop;
}
NodeHandle HChord_U::closestPreceedingNode(const OverlayKey& key)
{
NodeHandle tempHandle = NodeHandle::UNSPECIFIED_NODE;
EV<<"Closestpreceeding node "<<key<<endl;
// find the closest preceding node in the successor list of upper overlay
for (int j = successorList->getSize() - 1; j >= 0; j--)
{
if(successorList->getSuccessor(j).key.isBetweenR(thisNode.key, key))
{
tempHandle = successorList->getSuccessor(j);
break;
}
}
if(tempHandle.isUnspecified())
{
std::stringstream temp;
temp << "HChord_U::closestPreceedingNode(): Successor list broken "
<< thisNode.key << " " << key;
EV<<"HChord_U::closestPreceedingNode(): Successor list broken "<<thisNode.key<<" "<<key;
throw new cRuntimeError(temp.str().c_str());
}
NodeHandle nextHop;
//look in finger table
for (int i = fingerTable->getSize() - 1; i >= 0; i--)
{
if (fingerTable->getFinger(i).key.isBetweenLR(tempHandle.key, key))
{
if(!extendedFingerTable)
{
//nextHop = new NodeVector();
nextHop=fingerTable->getFinger(i);
EV << "[HChord_U::closestPreceedingNode() @ " << thisNode.ip
<< " (" << thisNode.key.toString(16) << ")]\n"
<< " ClosestPreceedingNode: node " << thisNode
<< " for key " << key << "\n"
<< " finger " << fingerTable->getFinger(i).key
<< " better than \n"
<< " " << tempHandle.key
<< endl;
return nextHop;
}
else
{
//return fingerTable_upper->getFinger(i, key);
}
}
}
EV << "[HChord_U::closestPreceedingNode() @ " << thisNode.ip
<<" (" << thisNode.key.toString(16) << ")]\n"
<< " No finger found"
<< endl;
// if no finger is found lookup the rest of the successor list
for(int i = successorList->getSize() - 1; i >= 0; i--)
{
if(successorList->getSuccessor(i).key.isBetween(thisNode.key, key))
{
nextHop=successorList->getSuccessor(i);
}
}
if(!nextHop.isUnspecified())
return nextHop;
// if this is the first and only node on the ring, it is responsible
if ((predecessorNode.isUnspecified()) &&(successorList->getSuccessor() == thisNode))
{
nextHop=(thisNode);
return nextHop;
}
// if there is still no node found return NodeHandle::UNSPECIFIED_NODE
std::stringstream temp("Error in HChord_U::closestPreceedingNode()!\n");
temp << thisNode.key << " " << key;
error(temp.str().c_str());
nextHop=(NodeHandle::UNSPECIFIED_NODE);
return nextHop;
}
/*void HChord_U::recordOverlaySentStats(BaseOverlayMessage* msg)
{
BaseOverlayMessage* innerMsg;
if (msg->getType() == OVERLAYROUTE)
innerMsg = dynamic_cast<BaseOverlayMessage*>(msg->encapsulatedMsg());
else
innerMsg = msg;
switch (innerMsg->getType()) {
case OVERLAYSIGNALING: {
HChordMessage* chordMsg = dynamic_cast<HChordMessage*>(innerMsg);
switch(chordMsg->getCommand()) {
case NEWSUCCESSORHINT:
RECORD_STATS(newsuccessorhintCount++; newsuccessorhintBytesSent +=
msg->byteLength());
break;
}
break;
}
case RPC: {
if ((dynamic_cast<StabilizeCall*>(innerMsg) != NULL) ||
(dynamic_cast<StabilizeResponse*>(innerMsg) != NULL)) {
RECORD_STATS(stabilizeCount++; stabilizeBytesSent +=
msg->byteLength());
} else if ((dynamic_cast<NotifyCall*>(innerMsg) != NULL) ||
(dynamic_cast<NotifyResponse*>(innerMsg) != NULL)) {
RECORD_STATS(notifyCount++; notifyBytesSent +=
msg->byteLength());
} else if ((dynamic_cast<FixfingersCall*>(innerMsg) != NULL) ||
(dynamic_cast<FixfingersResponse*>(innerMsg) != NULL)) {
RECORD_STATS(fixfingersCount++; fixfingersBytesSent +=
msg->byteLength());
} else if ((dynamic_cast<JoinCall*>(innerMsg) != NULL) ||
(dynamic_cast<JoinResponse*>(innerMsg) != NULL)) {
RECORD_STATS(joinCount++; joinBytesSent += msg->byteLength());
}
break;
}
}
}
*/
void HChord_U::finishOverlay()
{
// remove this node from the bootstrap list
bootstrapList->removeBootstrapNode(thisNode);
}
/* simtime_t time = globalStatistics->calcMeasuredLifetime(creationTime);
if(time == 0) return;
globalStatistics->addStdDev("HChord: Sent JOIN Messages/s",
joinCount / time);
globalStatistics->addStdDev("HChord: Sent NEWSUCCESSORHINT Messages/s",
newsuccessorhintCount / time);
globalStatistics->addStdDev("HChord: Sent STABILIZE Messages/s",
stabilizeCount / time);
globalStatistics->addStdDev("HChord: Sent NOTIFY Messages/s",
notifyCount / time);
globalStatistics->addStdDev("HChord: Sent FIX_FINGERS Messages/s",
fixfingersCount / time);
globalStatistics->addStdDev("HChord: Sent JOIN Bytes/s",
joinBytesSent / time);
globalStatistics->addStdDev("HChord: Sent NEWSUCCESSORHINT Bytes/s",
newsuccessorhintBytesSent / time);
globalStatistics->addStdDev("HChord: Sent STABILIZE Bytes/s",
stabilizeBytesSent / time);
globalStatistics->addStdDev("HChord: Sent NOTIFY Bytes/s",
notifyBytesSent / time);
globalStatistics->addStdDev("HChord: Sent FIX_FINGERS Bytes/s",
fixfingersBytesSent / time);
}
*/
void HChord_U::handleJoinTimerExpired(cMessage* msg)
{
// only process timer, if node is not bootstrapped yet
if (state == READY)
return;
// enter state BOOTSTRAP
if (state != BOOTSTRAP)
changeState(BOOTSTRAP);
// change bootstrap node from time to time
joinRetry--;
if (joinRetry == 0) {
joinRetry = par("joinRetry");
changeState(BOOTSTRAP);
return;
}
// call JOIN
HJoinCall * call = new HJoinCall("HJoinCall");
// call->setOverlay(UPPER_OVERLAY);
call->setLength(HJOINCALL_L(call));
call->setSrc(thisNode);
call->setDest(bootstrapNode);
call->setMesg_type(JOIN_REQUEST);
sendMessageToUDP( bootstrapNode,call);
// schedule next bootstrap process in the case this one fails
cancelEvent(join_timer);
scheduleAt(simulation.simTime() + joinDelay, msg);
}
void HChord_U::handleStabilizeTimerExpired(cMessage* msg)
{
if (state != READY)
return;
if (missingPredecessorStabRequests >= stabilizeRetry) {
// predecessor node seems to be dead
// remove it from the predecessor / successor lists
successorList ->removeSuccessor(predecessorNode);
//predecessorIsDead();
callUpdate(predecessorNode, false);
predecessorNode = NodeHandle::UNSPECIFIED_NODE;
missingPredecessorStabRequests = 0;
updateTooltip();
}
if (missingSuccessorStabResponses >= stabilizeRetry) {
// successor node seems to be dead
// remove it from the predecessor / successor list
//successorIsDead();
NodeHandle successor = successorList->popSuccessor();
// if we had a ring consisting of 2 nodes and our successor seems
// to be dead. Remove also predecessor because the successor
// and predecessor are the same node
if ((!predecessorNode.isUnspecified()) &&
predecessorNode == successor) {
//predecessorIsDead();
callUpdate(predecessorNode, false);
predecessorNode = NodeHandle::UNSPECIFIED_NODE;
}
missingSuccessorStabResponses = 0;
updateTooltip();
if (successorList->isEmpty()) {
changeState(INIT);
changeState(BOOTSTRAP);
return;
}
}
if (!successorList->isEmpty()) {
// call STABILIZE RPC
UHStabilizeCall* call = new UHStabilizeCall("UHStabilizeCall");
call->setLength(HSTABILIZECALL_L(call));
//call->setOverlay(UPPER_OVERLAY);
call->setMesg_type(STABILIZE_CALL);
call->setSrc(thisNode);
call->setLength(HSTABILIZECALL_L(call));
call->setDest(successorList->getSuccessor());
sendMessageToUDP(successorList->getSuccessor(), call);
missingPredecessorStabRequests++;
missingSuccessorStabResponses++;
}
// check if fingers are still alive and remove unreachable finger nodes
// schedule next stabilization process
cancelEvent(stabilize_timer);
scheduleAt(simulation.simTime() + stabilizeDelay, msg);
}
void HChord_U::handleFixFingersTimerExpired(cMessage* msg)
{
EV<<endl<<endl<<thisNode<<":Fix fingers timer reached "<<endl;
if ((state != READY) || successorList->isEmpty())
return;
OverlayKey offset, lookupKey;
for (uint nextFinger = 0; nextFinger < thisNode.key.getLength()-1;nextFinger++)
{
// calculate "n + 2^(i - 1)"
OverlayKey k=thisNode.key;
offset = OverlayKey::pow2(nextFinger);
lookupKey = k + offset;
// send message only for non-trivial fingers
if (offset > successorList->getSuccessor().key - k)
{
EV<<thisNode<<": lookup key "<<lookupKey<<" sending fix fingers call fingerno "<<nextFinger<<endl;
// call FIXFINGER RPC
UHFixfingersCall* call = new UHFixfingersCall("UHFixfingersCall");
call->setFinger(nextFinger);
call->setSrc(thisNode);
call->setLookupKey(lookupKey);
//call->setOverlay(UPPER_OVERLAY);
call->setMesg_type(FIXFINGERS_CALL);
call->setLength(HFIXFINGERSCALL_L(call));
NodeHandle nextHop = findNode_h(lookupKey,1,1,call);
call->setDest(nextHop);
sendMessageToUDP(nextHop,call);
}
else
{
// let trivial fingers point to the successor node
EV<<thisNode<<" lookup key "<<lookupKey<<" setting to successor fingr no "<<nextFinger<<endl;
fingerTable->setFinger(nextFinger, successorList->getSuccessor());
}
}
// schedule next finger repair process
cancelEvent(fixfingers_timer);
scheduleAt(simulation.simTime() + fixfingersDelay, msg);
}
void HChord_U::handleMigrateTimerExpired(cMessage *msg)
{
if(super_peer)
{
//delete msg;
return ;
}
//send migrate req to super peer
HMigrateRequest *migr = new HMigrateRequest("Migrate Request");
migr->setSrc(thisNode);
migr->setDest(lower->SuperPeerNode);
migr->setMesg_type(MIGRATE_REQUEST);
sendMessageToUDP(migr->getDest(),migr);
cancelEvent(migrate_timer);
scheduleAt(simulation.simTime() + migrateDelay, msg);
}
void HChord_U::handleStabilizeSuperPeerTimerExpired(cMessage *msg)
{
if(lower->SuperPeerNode == thisNode)
return;
//send stabilize super peer req to super peer
HStabilizeSuperPeerCall *spstab = new HStabilizeSuperPeerCall("StabilizeSuperPeerCall");
spstab->setSrc(thisNode);
spstab->setDest(lower->SuperPeerNode);
spstab->setMesg_type(STABILIZE_SUPER_PEER_CALL);
sendMessageToUDP(lower->SuperPeerNode,spstab);
cancelEvent(stabilize_super_peer_timer);
scheduleAt(simulation.simTime() + stabilizeSuperPeerDelay, stabilize_super_peer_timer);
}
void HChord_U::handleUpperJoinTimerExpired(cMessage *msg)
{
if(in_upper)
return;
UHJoinCall *ujoincall = new UHJoinCall("UHJoinCall");
ujoincall->setSrc(thisNode);
//send new successor hint should have sent succ list in case of churn
ujoincall->setLowerNewSuc(successorList_lower->getSuccessor());
ujoincall->setDest(lower->SuperPeerNode);
ujoincall->setMesg_type(JOIN_REQUEST);
sendMessageToUDP(lower->SuperPeerNode,ujoincall);
/*HLargestNodeRequest *req = new HLargestNodeRequest("LargestNodeReq");
req->setSrc(thisNode);
req->setDest(lower->SuperPeerNode);
req->setMesg_type(LARGEST_NODE_REQUEST);
sendMessageToUDP(lower->SuperPeerNode,req);*/
cancelEvent(join_timer);
scheduleAt(simulation.simTime() + joinDelay, msg);
//delete msg;
}
void HChord_U::handleNewSuccessorHint(HNewSuccessorHintMsg* hchordMsg)
{
UHNewSuccessorHintMsg* newSuccessorHintMsg =
check_and_cast<UHNewSuccessorHintMsg*>(hchordMsg);
// fetch the successor's predecessor