#include <ChordSuccessorList.h>
This modul contains the successor list of the Chord implementation.
Public Member Functions | |
virtual int | numInitStages () const |
virtual void | initialize (int stage) |
virtual void | handleMessage (cMessage *msg) |
virtual void | initializeList (uint size, NodeHandle owner) |
Initializes the successor list. | |
virtual uint | getSize () |
Returns number of neighbors in the successor list. | |
virtual void | clear () |
Clears the successor list. | |
virtual bool | isEmpty () |
Checks if the successor list is empty. | |
virtual const NodeHandle & | getSuccessor (uint pos=0) |
Returns a particular successor. | |
virtual void | addSuccessor (NodeHandle successor) |
Adds new successor nodes to the successor list. | |
virtual NodeHandle | popSuccessor () |
Removes all occurrences of the first successor node from the list. | |
virtual void | removeSuccessor (NodeHandle successor) |
Removes a certain successor from the successor list. | |
void | display () |
Protected Member Functions | |
void | buildSuccessorList () |
Derives a new successor list from the internal successor set. | |
void | updateDisplayString () |
Displays the current number of successors in the list. | |
void | updateTooltip () |
Displays the first 4 successor nodes as tooltip. | |
Protected Attributes | |
NodeHandle | thisNode |
own node handle | |
std::map< OverlayKey, NodeHandle > | successorMap |
internal representation of the successor list | |
uint | successorListSize |
maximum size of the successor list |
void ChordSuccessorList::addSuccessor | ( | NodeHandle | successor | ) | [virtual] |
Adds new successor nodes to the successor list.
Adds new successor nodes to the successor list and sorts the list using the corresponding chord keys. If the list size exceeds the maximum size nodes at the end of the list will be removed.
successor | the node handle of the successor to be added |
00096 { 00097 OverlayKey sum = successor.key - (thisNode.key + OverlayKey::ONE); 00098 // perhaps this step can be optimized 00099 successorMap.insert(make_pair(sum, successor)); 00100 if (successorMap.size() > successorListSize) { 00101 std::map<OverlayKey, NodeHandle>::iterator it = successorMap.end(); 00102 it--; 00103 successorMap.erase(it); 00104 } 00105 }
void ChordSuccessorList::buildSuccessorList | ( | ) | [protected] |
Derives a new successor list from the internal successor set.
Derives a new successor list from the internal successor set. This method is called whenever nodes are added or removed from the set.
void ChordSuccessorList::clear | ( | ) | [virtual] |
void ChordSuccessorList::display | ( | ) |
00174 { 00175 cout << "Content of ChordSuccessorList:" << endl; 00176 for (std::map<OverlayKey,NodeHandle>::iterator it = successorMap.begin(); it != successorMap.end(); it++) 00177 cout << it->first << " with Node: " << it->second << endl; 00178 }
uint ChordSuccessorList::getSize | ( | ) | [virtual] |
Returns number of neighbors in the successor list.
00064 { 00065 return successorMap.size(); 00066 }
const NodeHandle & ChordSuccessorList::getSuccessor | ( | uint | pos = 0 |
) | [virtual] |
Returns a particular successor.
pos | position in the successor list |
00077 { 00078 // check boundaries 00079 if (pos == 0 && successorMap.size() == 0) 00080 return NodeHandle::UNSPECIFIED_NODE; 00081 00082 if (pos >= successorMap.size()) { 00083 error("Index out of bound (ChordSuccessorList, getSuccessor())"); 00084 } 00085 00086 std::map<OverlayKey, NodeHandle>::iterator it = successorMap.begin(); 00087 for (uint i= 0; i < pos; i++) { 00088 it++; 00089 if (i == (pos-1)) 00090 return it->second; 00091 } 00092 return it->second; 00093 }
void ChordSuccessorList::handleMessage | ( | cMessage * | msg | ) | [virtual] |
void ChordSuccessorList::initialize | ( | int | stage | ) | [virtual] |
00035 { 00036 // because of IPAddressResolver, we need to wait until interfaces 00037 // are registered, address auto-assignment takes place etc. 00038 if (stage != MIN_STAGE_OVERLAY) 00039 return; 00040 00041 WATCH_MAP(successorMap); 00042 }
void ChordSuccessorList::initializeList | ( | uint | size, | |
NodeHandle | owner | |||
) | [virtual] |
Initializes the successor list.
This should be called on startup
size | maximum number of neighbors in the successor list | |
owner | the node owner is added to the successor list |
00050 { 00051 successorMap.clear(); 00052 successorListSize = size; 00053 thisNode = owner; 00054 addSuccessor(thisNode); 00055 }
bool ChordSuccessorList::isEmpty | ( | ) | [virtual] |
Checks if the successor list is empty.
00069 { 00070 if (successorMap.size() == 1 && getSuccessor() == thisNode) 00071 return true; 00072 else 00073 return false; 00074 }
virtual int ChordSuccessorList::numInitStages | ( | ) | const [inline, virtual] |
NodeHandle ChordSuccessorList::popSuccessor | ( | ) | [virtual] |
Removes all occurrences of the first successor node from the list.
00109 { 00110 OverlayKey succKey = successorMap.begin()->first; 00111 NodeHandle succNode = successorMap.begin()->second; 00112 successorMap.erase(successorMap.find(succKey)); 00113 00114 // ensure that thisNode is always in the successor list 00115 if (getSize() == 0) { 00116 addSuccessor(thisNode); 00117 succNode = thisNode; 00118 } 00119 00120 return succNode; 00121 }
void ChordSuccessorList::removeSuccessor | ( | NodeHandle | successor | ) | [virtual] |
Removes a certain successor from the successor list.
successor | the successor to be removed |
00124 { 00125 OverlayKey tmp = successor.key - (thisNode.key + OverlayKey::ONE); 00126 std::map<OverlayKey, NodeHandle>::iterator iter = successorMap.find(tmp); 00127 if (iter != successorMap.end()) { 00128 if (iter->second != successor) 00129 cout << "wrong mapping" << endl; 00130 successorMap.erase(iter); 00131 } 00132 00133 // ensure that thisNode is always in the successor list 00134 if (getSize() == 0) 00135 addSuccessor(thisNode); 00136 }
void ChordSuccessorList::updateDisplayString | ( | ) | [protected] |
Displays the current number of successors in the list.
00139 { 00140 // FIXME: doesn't work without tcl/tk 00141 if (ev.isGUI()) { 00142 char buf[80]; 00143 00144 if (successorMap.size() == 1) { 00145 sprintf(buf, "1 successor"); 00146 } else { 00147 sprintf(buf, "%zi successors", successorMap.size()); 00148 } 00149 00150 displayString().setTagArg("t", 0, buf); 00151 displayString().setTagArg("t", 2, "blue"); 00152 } 00153 00154 }
void ChordSuccessorList::updateTooltip | ( | ) | [protected] |
Displays the first 4 successor nodes as tooltip.
00157 { 00158 if (ev.isGUI()) { 00159 std::stringstream str; 00160 for (uint i = 0; i < successorMap.size(); i++) { 00161 str << getSuccessor(i); 00162 if ( i != successorMap.size() - 1 ) 00163 str << endl; 00164 } 00165 00166 00167 char buf[1024]; 00168 sprintf(buf, "%s", str.str().c_str()); 00169 displayString().setTagArg("tt", 0, buf); 00170 } 00171 }
uint ChordSuccessorList::successorListSize [protected] |
maximum size of the successor list
std::map<OverlayKey, NodeHandle> ChordSuccessorList::successorMap [protected] |
internal representation of the successor list
NodeHandle ChordSuccessorList::thisNode [protected] |
own node handle