#include <BrooseBucket.h>
This modul contains the bucket of the Broose implementation.
Public Member Functions | |
virtual int | numInitStages () const |
virtual void | initialize (int stage) |
virtual void | handleMessage (cMessage *msg) |
virtual void | add (BrooseHandle node) |
adds a broose node handle to the bucket | |
virtual void | remove (BrooseHandle node) |
removes a broose node handle from the bucket | |
virtual const BrooseHandle & | get (uint pos=0) |
returns a specific broose handle | |
virtual const OverlayKey & | getDist (uint pos=0) |
returns distance of a specific broose handle | |
virtual void | initializeBucket (int shiftingBits, uint prefix, BrooseHandle node, int size, Broose *overlay, bool isBBucket=false) |
initializes a bucket | |
virtual uint | getSize () |
returns number of current entries | |
virtual uint | getMaxSize () |
returns number of maximal entries | |
virtual bool | isEmpty () |
checks if the bucket is empty | |
virtual void | clear () |
removes all entries from the bucket | |
virtual BrooseHandle | getClosestNode (const OverlayKey &destKey) |
returns the closest node to a specific key | |
virtual int | longestPrefix (void) |
return the longest prefix of all entries | |
virtual bool | keyInRange (const OverlayKey &key) |
checks if the key close to the owner's id | |
virtual bool | nodeInBucket (const BrooseHandle &node) |
checks if a node is already in the bucket | |
virtual int | getFailedResponses (const BrooseHandle &node) |
returns the number of failed responses to a specific broose handle | |
virtual void | increaseFailedResponses (const BrooseHandle &node) |
increase the number of failed responses to a specific broose handle | |
virtual void | resetFailedResponses (const BrooseHandle &node) |
resets the counter of failed responses to a specific broose handle | |
virtual void | setRTT (BrooseHandle node, simtime_t rpcRTT) |
sets the round trip time to a specific broose handle | |
virtual simtime_t | getRTT (BrooseHandle node) |
returns the round trip time to a specific broose handle | |
virtual void | setLastSeen (BrooseHandle node, simtime_t lastSeen) |
updates the timestamp of a specific node | |
virtual simtime_t | getLastSeen (BrooseHandle node) |
returns the timestamp of a specific node | |
void | output (int maxEntries=0) |
displays the content of the bucket on standard out | |
void | binaryOutput (const OverlayKey &key) |
displays a key in binary form | |
Protected Member Functions | |
void | test () |
Protected Attributes | |
std::map< OverlayKey, BrooseHandle > | bucket |
data structure representing the bucket | |
std::map< OverlayKey, BrooseHandle >::iterator | bucketIter |
iterator to navigate through the bucket | |
unsigned int | maxSize |
maximal size of the bucket | |
BrooseHandle | thisNode |
node handle to the node managing this bucket | |
OverlayKey | key |
the node's key shifted to fit the bucket and used to measure distance to other keys | |
int | bits |
number of bits the node's key has to be shifted to fit the apropriate bucket | |
Broose * | overlay |
pointer to the main Broose module | |
bool | isBBucket |
true, if this bucket is the node's BBucket |
void BrooseBucket::initialize | ( | int | stage | ) | [virtual] |
00034 { 00035 // because of IPAddressResolver, we need to wait until interfaces 00036 // are registered, address auto-assignment takes place etc. 00037 if (stage != 5) 00038 return; 00039 00040 WATCH_MAP(bucket); 00041 00042 }
void BrooseBucket::handleMessage | ( | cMessage * | msg | ) | [virtual] |
void BrooseBucket::add | ( | BrooseHandle | node | ) | [virtual] |
adds a broose node handle to the bucket
node | broose handle which will be added |
00074 { 00075 OverlayKey tmp = key ^ node.key; 00076 if (bucket.size() < maxSize) { 00077 if (bucket.insert(make_pair(tmp,node)).second) { 00078 if (isBBucket) { 00079 overlay->callUpdate(node, true); 00080 } 00081 } 00082 } else { 00083 if (bucket.find(tmp) == bucket.end()) { 00084 bucketIter = bucket.end(); 00085 bucketIter--; 00086 00087 // is the new node closer than the one farthest away, 00088 // remove the one and add the other 00089 00090 if (bucketIter->first > tmp) { 00091 if (isBBucket) { 00092 overlay->callUpdate(bucketIter->second, false); 00093 } 00094 bucket.erase(bucketIter); 00095 if (bucket.insert(make_pair(tmp,node)).second) { 00096 if (isBBucket) { 00097 overlay->callUpdate(node, true); 00098 } 00099 } 00100 } 00101 } 00102 } 00103 00104 }
void BrooseBucket::remove | ( | BrooseHandle | node | ) | [virtual] |
removes a broose node handle from the bucket
node | broose handle which will be removed |
00107 { 00108 for (bucketIter = bucket.begin(); bucketIter != bucket.end();) { 00109 if (bucketIter->second.ip == node.ip) { 00110 if (isBBucket) { 00111 overlay->callUpdate(node, false); 00112 } 00113 bucket.erase(bucketIter++); 00114 } else { 00115 ++bucketIter; 00116 } 00117 } 00118 }
const BrooseHandle & BrooseBucket::get | ( | uint | pos = 0 |
) | [virtual] |
returns a specific broose handle
pos | position of the broose handle in the bucket |
00123 { 00124 if (pos > bucket.size()) { 00125 error("Index out of bounds(BrooseBucket)."); 00126 } 00127 00128 uint i = 0; 00129 for (bucketIter = bucket.begin(); bucketIter != bucket.end(); bucketIter++){ 00130 if (pos == i) { 00131 return bucketIter->second; 00132 } 00133 i++; 00134 } 00135 return BrooseHandle::UNSPECIFIED_NODE; 00136 }
const OverlayKey & BrooseBucket::getDist | ( | uint | pos = 0 |
) | [virtual] |
returns distance of a specific broose handle
pos | position of the broose handle in the bucket |
00139 { 00140 if(pos > bucket.size()) { 00141 error("Index out of bounds(BrooseBucket)."); 00142 } 00143 00144 uint i = 0; 00145 for (bucketIter = bucket.begin(); bucketIter != bucket.end(); bucketIter++){ 00146 if (pos == i) { 00147 return bucketIter->first; 00148 } 00149 i++; 00150 } 00151 return OverlayKey::UNSPECIFIED_KEY; 00152 }
void BrooseBucket::initializeBucket | ( | int | shiftingBits, | |
uint | prefix, | |||
BrooseHandle | node, | |||
int | size, | |||
Broose * | overlay, | |||
bool | isBBucket = false | |||
) | [virtual] |
initializes a bucket
shiftingBits | specifies the kind of the bucket | |
prefix | in case of a R bucket specifies the prefix | |
node | broose handle to the node which owns this bucket | |
size | maximal size of the bucket | |
overlay | pointer to the main Broose module | |
isBBucket | true, is this bucket is the node's BBucket |
00052 { 00053 maxSize = size; 00054 thisNode = node; 00055 bits = shiftingBits; 00056 this->overlay = overlay; 00057 this->isBBucket = isBBucket; 00058 00059 if (shiftingBits < 0) { 00060 key = thisNode.key << -shiftingBits; 00061 } else { 00062 key = thisNode.key >> shiftingBits; 00063 } 00064 00065 if (prefix != 0) { 00066 OverlayKey tmp(prefix); // constraint 00067 tmp = tmp << (thisNode.key.getLength() - shiftingBits); 00068 key = key + tmp; 00069 } 00070 bucket.clear(); 00071 }
uint BrooseBucket::getSize | ( | ) | [virtual] |
returns number of current entries
00156 { 00157 return bucket.size(); 00158 }
uint BrooseBucket::getMaxSize | ( | ) | [virtual] |
returns number of maximal entries
00161 { 00162 return maxSize; 00163 }
bool BrooseBucket::isEmpty | ( | ) | [virtual] |
checks if the bucket is empty
00166 { 00167 return bucket.empty(); 00168 }
void BrooseBucket::clear | ( | ) | [virtual] |
BrooseHandle BrooseBucket::getClosestNode | ( | const OverlayKey & | destKey | ) | [virtual] |
returns the closest node to a specific key
destKey | key to compare |
00176 { 00177 BrooseHandle closestNode; 00178 OverlayKey lastDist = OverlayKey::max(); 00179 OverlayKey dist; 00180 00181 if (bucket.empty()) { 00182 EV << "--- BUCKET EMPTY ---" << endl; 00183 } 00184 00185 for (bucketIter = bucket.begin(); bucketIter!=bucket.end(); bucketIter++) { 00186 dist = bucketIter->second.key ^ destKey; 00187 if (dist < lastDist) { 00188 closestNode = bucketIter->second; 00189 lastDist = dist; 00190 } 00191 } 00192 return closestNode; 00193 }
int BrooseBucket::longestPrefix | ( | void | ) | [virtual] |
return the longest prefix of all entries
00196 { 00197 int bits = 0; 00198 int maxBits = MAXBITS; 00199 int tmp = 0; 00200 BrooseHandle node1, node2; 00201 00202 if (bucket.size() <= 1) 00203 return 0; 00204 00205 for (bucketIter = bucket.begin(); bucketIter != bucket.end(); ++bucketIter){ 00206 tmp = 0; 00207 node1 = bucketIter->second; 00208 ++bucketIter; 00209 00210 if (bucketIter != bucket.end()) { 00211 node2 = bucketIter->second; 00212 --bucketIter; 00213 00214 for (uint i = 1; i <= node1.key.getLength(); i++) { 00215 if (node1.key.bitAtPlace(i) == node2.key.bitAtPlace(i)) 00216 tmp++; 00217 else 00218 break; 00219 } 00220 00221 if (tmp < maxBits) { 00222 bits = tmp; 00223 maxBits = tmp; 00224 } 00225 } else { 00226 --bucketIter; 00227 } 00228 } 00229 return bits; 00230 }
bool BrooseBucket::keyInRange | ( | const OverlayKey & | key | ) | [virtual] |
checks if the key close to the owner's id
key | key to check |
00254 { 00255 OverlayKey dist; 00256 00257 if (bucket.size() == 0) 00258 return false; 00259 00260 // check if the function was called to perform on a B bucket 00261 if (bits == 0) { 00262 if (bucket.size() <= (maxSize / 7)) 00263 dist = OverlayKey::max(); 00264 else 00265 dist = getDist((maxSize / 7) - 1); 00266 } else 00267 dist = getDist(bucket.size()-1); 00268 00269 if ((key ^ thisNode.key) <= dist) 00270 return true; 00271 else 00272 return false; 00273 00274 }
bool BrooseBucket::nodeInBucket | ( | const BrooseHandle & | node | ) | [virtual] |
checks if a node is already in the bucket
node | broose handle to check |
00277 { 00278 for (bucketIter = bucket.begin(); bucketIter!=bucket.end(); bucketIter++) { 00279 if (node.ip == bucketIter->second.ip) 00280 return true; 00281 } 00282 return false; 00283 }
int BrooseBucket::getFailedResponses | ( | const BrooseHandle & | node | ) | [virtual] |
returns the number of failed responses to a specific broose handle
node | broose handle to check |
00286 { 00287 for (bucketIter = bucket.begin(); bucketIter!=bucket.end(); bucketIter++) { 00288 if (node.ip == bucketIter->second.ip) 00289 return bucketIter->second.failedResponses; 00290 } 00291 return -1; 00292 }
void BrooseBucket::increaseFailedResponses | ( | const BrooseHandle & | node | ) | [virtual] |
increase the number of failed responses to a specific broose handle
node | broose handle which counter will be increased |
00295 { 00296 for (bucketIter = bucket.begin(); bucketIter!=bucket.end(); bucketIter++) { 00297 if (node.ip == bucketIter->second.ip) 00298 bucketIter->second.failedResponses++; 00299 } 00300 }
void BrooseBucket::resetFailedResponses | ( | const BrooseHandle & | node | ) | [virtual] |
resets the counter of failed responses to a specific broose handle
node | broose handle which counter will be reset |
00303 { 00304 for (bucketIter = bucket.begin(); bucketIter!=bucket.end(); bucketIter++) { 00305 if (node.ip == bucketIter->second.ip) 00306 bucketIter->second.failedResponses = 0; 00307 } 00308 }
void BrooseBucket::setRTT | ( | BrooseHandle | node, | |
simtime_t | rpcRTT | |||
) | [virtual] |
sets the round trip time to a specific broose handle
node | broose handle to which the rtt will be stored | |
rpcRTT | rtt to the specific node |
00312 { 00313 for (bucketIter = bucket.begin(); bucketIter!=bucket.end(); bucketIter++) { 00314 if (node.ip == bucketIter->second.ip) { 00315 bucketIter->second.rtt = rpcRTT; 00316 } 00317 } 00318 }
simtime_t BrooseBucket::getRTT | ( | BrooseHandle | node | ) | [virtual] |
returns the round trip time to a specific broose handle
node | broose handle to which the rtt will be retrieved |
00321 { 00322 for (bucketIter = bucket.begin(); bucketIter!=bucket.end(); bucketIter++) { 00323 if (node.ip == bucketIter->second.ip) 00324 return bucketIter->second.rtt; 00325 } 00326 return -2; 00327 }
void BrooseBucket::setLastSeen | ( | BrooseHandle | node, | |
simtime_t | lastSeen | |||
) | [virtual] |
updates the timestamp of a specific node
node | broose handle to which the timestamp will be updated | |
lastSeen | timestamp of the last message |
00330 { 00331 for (bucketIter = bucket.begin(); bucketIter!=bucket.end(); bucketIter++) { 00332 if (node.ip == bucketIter->second.ip) 00333 bucketIter->second.lastSeen = time; 00334 } 00335 }
simtime_t BrooseBucket::getLastSeen | ( | BrooseHandle | node | ) | [virtual] |
returns the timestamp of a specific node
node | broose handle to which the timestamp will be retrieved |
00338 { 00339 for (bucketIter = bucket.begin(); bucketIter!=bucket.end(); bucketIter++) { 00340 if (node.ip == bucketIter->second.ip) 00341 return bucketIter->second.lastSeen; 00342 } 00343 return -2; 00344 }
void BrooseBucket::output | ( | int | maxEntries = 0 |
) |
displays the content of the bucket on standard out
maxEntries | the maximal number of entries which will be printed |
00233 { 00234 EV << "(" << bucket.size() << "/" << maxSize <<")" << endl; 00235 BrooseHandle node; 00236 OverlayKey dist; 00237 00238 int max; 00239 max = bucket.size(); 00240 if (maxEntries != 0 && maxEntries < max) { 00241 max = maxEntries; 00242 } 00243 int i; 00244 00245 for (bucketIter = bucket.begin(), i = 0; i < max; bucketIter++, i++) { 00246 dist = bucketIter->first; 00247 node = bucketIter->second; 00248 EV << dist << " " << node.key << " " << node.ip << " RTT: " 00249 << node.rtt << " LS: " << node.lastSeen << endl; 00250 } 00251 }
void BrooseBucket::binaryOutput | ( | const OverlayKey & | key | ) |
displays a key in binary form
key | key which is displayed |
00347 { 00348 if(key.isUnspecified()) 00349 cout << "<unspec>"; 00350 else { 00351 for (unsigned int i = 1; i <= key.getLength(); i++) 00352 cout << key.bitAtPlace(i); 00353 } 00354 }
void BrooseBucket::test | ( | ) | [protected] |
std::map<OverlayKey, BrooseHandle> BrooseBucket::bucket [protected] |
data structure representing the bucket
std::map<OverlayKey, BrooseHandle>::iterator BrooseBucket::bucketIter [protected] |
iterator to navigate through the bucket
unsigned int BrooseBucket::maxSize [protected] |
maximal size of the bucket
BrooseHandle BrooseBucket::thisNode [protected] |
node handle to the node managing this bucket
OverlayKey BrooseBucket::key [protected] |
the node's key shifted to fit the bucket and used to measure distance to other keys
int BrooseBucket::bits [protected] |
number of bits the node's key has to be shifted to fit the apropriate bucket
Broose* BrooseBucket::overlay [protected] |
pointer to the main Broose module
bool BrooseBucket::isBBucket [protected] |
true, if this bucket is the node's BBucket