23 #include <IPAddressResolver.h>
24 #include <IPvXAddress.h>
25 #include <IInterfaceTable.h>
26 #include <IPv4InterfaceData.h>
37 void Koorde::initializeOverlay(
int stage)
45 deBruijnDelay = par(
"deBruijnDelay");
46 deBruijnListSize = par(
"deBruijnListSize");
47 shiftingBits = par(
"shiftingBits");
48 useOtherLookup = par(
"useOtherLookup");
49 useSucList = par(
"useSucList");
50 setupDeBruijnBeforeJoin = par(
"setupDeBruijnBeforeJoin");
51 setupDeBruijnAtJoin = par(
"setupDeBruijnAtJoin");
58 deBruijnNodes =
new NodeHandle[deBruijnListSize];
62 deBruijnBytesSent = 0;
65 WATCH(deBruijnNumber);
69 deBruijn_timer =
new cMessage(
"deBruijn_timer");
71 Chord::initializeOverlay(stage);
76 cancelAndDelete(deBruijn_timer);
79 void Koorde::changeState(
int toState)
81 Chord::changeState(toState);
88 for (
int i=0; i < deBruijnListSize; i++) {
95 if (setupDeBruijnBeforeJoin) {
97 cancelEvent(join_timer);
98 cancelEvent(deBruijn_timer);
99 scheduleAt(simTime(), deBruijn_timer);
100 }
else if (setupDeBruijnAtJoin) {
101 cancelEvent(deBruijn_timer);
102 scheduleAt(simTime(), deBruijn_timer);
107 cancelEvent(deBruijn_timer);
108 scheduleAt(simTime(), deBruijn_timer);
111 cancelEvent(fixfingers_timer);
119 void Koorde::handleTimerEvent(cMessage* msg)
121 if (msg->isName(
"deBruijn_timer")) {
122 handleDeBruijnTimerExpired();
123 }
else if (msg->isName(
"fixfingers_timer")) {
124 handleFixFingersTimerExpired(msg);
126 Chord::handleTimerEvent(msg);
132 if (!deBruijnNode.isUnspecified()) {
133 if (failed == deBruijnNode) {
134 deBruijnNode = deBruijnNodes[0];
135 for (
int i = 0; i < deBruijnNumber - 1; i++) {
136 deBruijnNodes[i] = deBruijnNodes[i+1];
139 if (deBruijnNumber > 0) {
144 bool removed =
false;
145 for (
int i = 0; i < deBruijnNumber - 1; i++) {
146 if ((!deBruijnNodes[i].isUnspecified()) &&
147 (failed == deBruijnNodes[i])) {
151 ((!deBruijnNodes[deBruijnNumber - 1].isUnspecified())
152 && failed == deBruijnNodes[deBruijnNumber - 1])) {
153 deBruijnNodes[deBruijnNumber - 1] =
161 return Chord::handleFailedNode(failed);
164 void Koorde::handleDeBruijnTimerExpired()
166 OverlayKey lookup = thisNode.getKey() << shiftingBits;
168 if (state == READY) {
169 if (successorList->getSize() > 0) {
172 lookup -= (successorList->getSuccessor(successorList->getSize() /
173 2).getKey() - thisNode.getKey());
177 successorList->getSuccessor().getKey())
178 || successorList->isEmpty()) {
180 int sucNum = successorList->getSize();
181 if (sucNum > deBruijnListSize)
182 sucNum = deBruijnListSize;
184 deBruijnNode = thisNode;
185 for (
int i = 0; i < sucNum; i++) {
186 deBruijnNodes[i] = successorList->getSuccessor(i);
187 deBruijnNumber = i+1;
191 }
else if (lookup.
isBetweenR(predecessorNode.getKey(),
192 thisNode.getKey())) {
193 int sucNum = successorList->getSize();
194 if ((sucNum + 1) > deBruijnListSize)
195 sucNum = deBruijnListSize - 1;
197 deBruijnNode = predecessorNode;
198 deBruijnNodes[0] = thisNode;
199 for (
int i = 0; i < sucNum; i++) {
200 deBruijnNodes[i+1] = successorList->getSuccessor(i);
201 deBruijnNumber = i+2;
215 cancelEvent(deBruijn_timer);
216 scheduleAt(simTime() + deBruijnDelay, deBruijn_timer);
218 if (setupDeBruijnBeforeJoin || setupDeBruijnAtJoin) {
226 scheduleAt(simTime() + deBruijnDelay, deBruijn_timer);
232 void Koorde::handleFixFingersTimerExpired(cMessage* msg)
241 Chord::handleUDPMessage(msg);
247 if (state == READY) {
255 EV <<
"[Koorde::handleRpcCall() @ " << thisNode.getIp()
256 <<
" (" << thisNode.getKey().toString(16) <<
")]\n"
257 <<
" Received RPC call and state != READY!"
261 return Chord::handleRpcCall(msg);
265 cPolymorphic* context,
266 int rpcId, simtime_t rtt)
268 Chord::handleRpcResponse(msg, context, rpcId, rtt);
272 handleRpcDeBruijnResponse(_DeBruijnResponse);
273 EV <<
"[Koorde::handleRpcResponse() @ " << thisNode.getIp()
274 <<
" (" << thisNode.getKey().toString(16) <<
")]\n"
275 <<
" DeBruijn RPC Response received: id=" << rpcId
276 <<
"\n msg=" << *_DeBruijnResponse <<
" rtt=" << rtt
285 cPolymorphic* context,
int rpcId,
288 Chord::handleRpcTimeout(msg, dest, context, rpcId, destKey);
292 handleDeBruijnTimeout(_DeBruijnCall);
293 EV <<
"[Koorde::handleRpcTimeout() @ " << thisNode.getIp()
294 <<
" (" << thisNode.getKey().toString(16) <<
")]\n"
295 <<
" DeBruijn RPC Call timed out: id=" << rpcId
296 <<
"\n msg=" << *_DeBruijnCall
306 Chord::handleRpcJoinResponse(joinResponse);
309 cancelEvent(fixfingers_timer);
312 cancelEvent(deBruijn_timer);
313 scheduleAt(simTime(), deBruijn_timer);
319 Chord::rpcJoin(joinCall);
321 if (predecessorNode == successorList->getSuccessor()) {
323 handleDeBruijnTimerExpired();
337 if ((predecessorNode.isUnspecified() && successorList->isEmpty())
339 thisNode.getKey())) {
343 if (predecessorNode.isUnspecified()) {
346 deBruijnResponse->
setDBNode(predecessorNode);
349 int sucNum = successorList->getSize() + 1;
354 for (
int k = 1; k < sucNum; k++) {
355 deBruijnResponse->
setSucNode(k, successorList->getSuccessor(k-1));
359 sendRpcResponse(deBruijnCall, deBruijnResponse);
361 successorList->getSuccessor().getKey())) {
362 error(
"Koorde::handleRpcDeBruijnRequest() - unknown error.");
364 error(
"Koorde::handleRpcDeBruijnRequest() - "
365 "Request couldn't be delivered!");
371 int sucNum = deBruijnResponse->
getSucNum();
372 if (sucNum > deBruijnListSize)
373 sucNum = deBruijnListSize;
375 for (
int i = 0; i < sucNum; i++) {
376 deBruijnNodes[i] = deBruijnResponse->
getSucNode(i);
377 deBruijnNumber = i+1;
380 deBruijnNode = deBruijnResponse->
getDBNode();
384 if (setupDeBruijnBeforeJoin && (state == JOIN)) {
386 if (!join_timer->isScheduled()) {
387 scheduleAt(simTime(), join_timer);
394 if (setupDeBruijnBeforeJoin && (state == JOIN)) {
401 cancelEvent(deBruijn_timer);
402 scheduleAt(simTime(), deBruijn_timer);
406 int numRedundantNodes,
421 if (!msg->hasObject(
"findNodeExt")) {
426 msg->addObject( findNodeExt );
433 error(
"Koorde::findNode() - direct Messaging is no longer in use.");
434 }
else if (key.
isBetweenR(predecessorNode.getKey(), thisNode.getKey())) {
436 nextHop->push_back(thisNode);
438 successorList->getSuccessor().getKey())){
440 nextHop->push_back(successorList->getSuccessor());
444 if (useOtherLookup) {
447 successorList->getSuccessor(successorList->getSize() - 1)) {
448 nextHop->push_back(tmpNode);
450 NodeHandle tmpHandle = findDeBruijnHop(key, findNodeExt);
451 if (tmpHandle != thisNode || breakLookup) {
452 nextHop->push_back(tmpHandle);
455 return findNode(key, numRedundantNodes, numSiblings, msg);
461 NodeHandle tmpHandle = findDeBruijnHop(key, findNodeExt);
462 if (tmpHandle != thisNode || breakLookup) {
463 nextHop->push_back(tmpHandle);
466 return findNode(key, numRedundantNodes, numSiblings, msg);
477 if (!deBruijnNode.isUnspecified()) {
479 findNodeExt->
setRouteKey(findStartKey(thisNode.getKey(),
480 successorList->getSuccessor().getKey(), destKey,
485 return successorList->getSuccessor();
492 successorList->getSuccessor().getKey())) {
494 error(
"Koorde::findDeBruijnHop - Bounding error: "
495 "trying to get non existing bit out of overlay key!");
500 for (
int i = 1; i < shiftingBits; i++) {
509 if (deBruijnNode.isUnspecified()) {
512 return walkSuccessorList(findNodeExt->
getRouteKey());
514 return successorList->getSuccessor();
519 if (deBruijnNumber > 0) {
521 deBruijnNodes[0].getKey())) {
526 NodeHandle nextHop = walkDeBruijnList(findNodeExt->
538 if (deBruijnNode.isUnspecified()) {
539 return walkSuccessorList(findNodeExt->
getRouteKey());
545 if (deBruijnNode.getKey().isBetween(tmpHandle.
getKey(),
553 return successorList->getSuccessor();
560 if (deBruijnNumber == 0)
563 for (
int i = 0; i < deBruijnNumber-1; i++) {
564 if (key.
isBetweenR(deBruijnNodes[i].getKey(),deBruijnNodes[i+1].getKey())) {
565 return deBruijnNodes[i];
569 return deBruijnNodes[deBruijnNumber-1];
574 for (
unsigned int i = 0; i < successorList->getSize()-1; i++) {
575 if (key.
isBetweenR(successorList->getSuccessor(i).getKey(),
576 successorList->getSuccessor(i+1).getKey())) {
577 return successorList->getSuccessor(i);
581 return successorList->getSuccessor(successorList->getSize()-1);
584 void Koorde::updateTooltip()
591 std::stringstream ttString;
594 ttString <<
"Pred "<< predecessorNode << endl <<
"This "
596 <<
"Suc " << successorList->getSuccessor() << endl
597 <<
"DeBr " << deBruijnNode << endl;
600 for (
unsigned int i = 0; i < successorList->getSize(); i++) {
601 ttString << successorList->getSuccessor(i).
getIp() <<
" ";
605 ttString <<
"DList ";
607 for (
int i = 0; i < deBruijnNumber; i++) {
608 ttString << deBruijnNodes[i].getIp() <<
" ";
613 getParentModule()->getParentModule()->
614 getDisplayString().setTagArg(
"tt", 0, ttString.str().c_str());
615 getParentModule()->getDisplayString().setTagArg(
"tt", 0,
616 ttString.str().c_str());
617 getDisplayString().setTagArg(
"tt", 0, ttString.str().c_str());
620 showOverlayNeighborArrow(successorList->getSuccessor(),
true,
621 "m=m,50,0,50,0;ls=red,1");
626 void Koorde::finishOverlay()
629 simtime_t time = globalStatistics->calcMeasuredLifetime(creationTime);
632 globalStatistics->addStdDev(
"Koorde: Sent DEBRUIJN Messages/s",
633 deBruijnCount / time);
634 globalStatistics->addStdDev(
"Koorde: Sent DEBRUIJN Bytes/s",
635 deBruijnBytesSent / time);
638 Chord::finishOverlay();
643 Chord::recordOverlaySentStats(msg);
647 innerMsg->getEncapsulatedPacket() != NULL) {
654 if ((dynamic_cast<DeBruijnCall*>(innerMsg) != NULL) ||
657 msg->getByteLength());
669 OverlayKey diffKey, newStart, tmpDest, newKey, powKey;
672 if (startKey == endKey)
675 diffKey = endKey - startKey;
676 nBits = diffKey.
log_2();
682 while ((startKey.getLength() - nBits) % shiftingBits != 0) {
691 for (shared = 0; shared < (startKey.getLength() - nBits); shared += shiftingBits) {
692 if (destKey.
sharedPrefixLength(startKey << shared) >= (startKey.getLength() - nBits - shared)) {
697 uint nBits2 = startKey.getLength() - shared;
699 newStart = (startKey >> nBits2) << nBits2;
701 tmpDest = destKey >> (destKey.
getLength() - nBits2);
702 newKey = tmpDest + newStart;
704 std::cout <<
"startKey: " << startKey.
toString(2) << endl
705 <<
"endKey : " << endKey.
toString(2) << endl
706 <<
"diff : " << (endKey-startKey).toString(2) << endl
707 <<
"newKey : " << newKey.toString(2) << endl
708 <<
"destKey : " << destKey.
toString(2) << endl
709 <<
"nbits : " << nBits << endl
710 <<
"nbits2 : " << nBits2 << endl;
713 if (newKey.isBetweenR(startKey, endKey)) {
714 std::cout <<
"HIT" << endl;
717 nBits2 -= shiftingBits;
718 newStart = (startKey >> nBits2) << nBits2;
720 tmpDest = destKey >> (destKey.
getLength() - nBits2);
721 newKey = tmpDest + newStart;
723 if (newKey.isBetweenR(startKey, endKey)) {
724 std::cout <<
"startKey: " << startKey.
toString(2) << endl
725 <<
"endKey : " << endKey.
toString(2) << endl
726 <<
"diff : " << (endKey-startKey).toString(2) << endl
727 <<
"newKey : " << newKey.toString(2) << endl
728 <<
"destKey : " << destKey.
toString(2) << endl
729 <<
"nbits : " << nBits << endl
730 <<
"nbits2 : " << nBits2 << endl;
731 std::cout <<
"HIT2" << endl;
736 std::cout <<
"MISS" << endl;
739 newStart = (startKey >> nBits) << nBits;
741 tmpDest = destKey >> (destKey.
getLength() - nBits);
742 newKey = tmpDest + newStart;
745 if (newKey.isBetweenR(startKey, endKey)) {
753 newKey += powKey.
pow2(nBits);
755 if (newKey.isBetweenR(startKey, endKey)) {
759 throw cRuntimeError(
"Koorde::findStartKey(): Invalid start key");
764 void Koorde::findFriendModules()
767 (getParentModule()->getSubmodule(
"successorList"));
770 void Koorde::initializeFriendModules()
773 successorList->
initializeList(par(
"successorListSize"), thisNode,
this);