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