#include <DHTDataStorage.h>
Public Member Functions | |
virtual int | numInitStages () const |
virtual void | initialize (int stage) |
virtual void | handleMessage (cMessage *msg) |
virtual uint | getSize () |
Returns number of stored data items in the map. | |
virtual void | clear () |
Clears all stored data items. | |
virtual bool | isEmpty () |
Checks if the data storage map is empty. | |
virtual const BinaryValue & | getData (const OverlayKey &key) |
Returns the value of a stored data item with a given key. | |
virtual const NodeHandle & | getSourceNode (const OverlayKey &key) |
Returns the source node of a stored data item with a given key. | |
virtual cMessage * | getTtlMessage (const OverlayKey &key) |
Returns the ttlMessage of a stored data item with a given key. | |
virtual const bool | isModifiable (const OverlayKey &key) |
Returns a boolean telling if this data if modifiable. | |
virtual const BinaryValue & | getDataAtPos (uint pos=0) |
Returns the value of the data item stored at position pos. | |
virtual const std::map < OverlayKey, DHTData > ::iterator | begin () |
Returns an iterator to the beginning of the map. | |
virtual void | addData (OverlayKey key, BinaryValue value, cMessage *ttlMessage, bool is_modifiable=true, NodeHandle sourceNode=NodeHandle::UNSPECIFIED_NODE, bool responsible=true) |
Store a new data item in the map. | |
virtual void | removeData (OverlayKey key) |
Removes a certain data item from the map. | |
void | display () |
DhtDataVector * | get_range (const OverlayKey lkey, const OverlayKey rkey) |
Return a vector containing every keys/values between the two bounds. | |
DhtDumpVector * | dumpDht () |
Dump all local data records into a vector. | |
Protected Member Functions | |
void | updateDisplayString () |
Displays the current number of successors in the list. | |
void | updateTooltip () |
Displays the first 4 successor nodes as tooltip. | |
Protected Attributes | |
std::map< OverlayKey, DHTData > | dataMap |
internal representation of the data storage |
virtual int DHTDataStorage::numInitStages | ( | ) | const [inline, virtual] |
void DHTDataStorage::initialize | ( | int | stage | ) | [virtual] |
void DHTDataStorage::handleMessage | ( | cMessage * | msg | ) | [virtual] |
uint DHTDataStorage::getSize | ( | ) | [virtual] |
Returns number of stored data items in the map.
Referenced by DHT::update().
00067 { 00068 return dataMap.size(); 00069 }
void DHTDataStorage::clear | ( | ) | [virtual] |
Clears all stored data items.
Referenced by DHT::~DHT().
00055 { 00056 map<OverlayKey, DHTData>::iterator iter; 00057 00058 for( iter = dataMap.begin(); iter != dataMap.end(); iter++ ) { 00059 cancelAndDelete(iter->second.ttlMessage); 00060 } 00061 00062 dataMap.clear(); 00063 }
bool DHTDataStorage::isEmpty | ( | ) | [virtual] |
Checks if the data storage map is empty.
00072 { 00073 if (dataMap.size() == 0) 00074 return true; 00075 else 00076 return false; 00077 }
const BinaryValue & DHTDataStorage::getData | ( | const OverlayKey & | key | ) | [virtual] |
Returns the value of a stored data item with a given key.
key | The key of the data item |
Referenced by DHT::handleGetRequest().
00079 { 00080 00081 std::map<OverlayKey, DHTData>::iterator it = dataMap.find(key); 00082 00083 if (it == dataMap.end()) 00084 return BinaryValue::UNSPECIFIED_VALUE; 00085 else 00086 return it->second.value; 00087 }
const NodeHandle & DHTDataStorage::getSourceNode | ( | const OverlayKey & | key | ) | [virtual] |
Returns the source node of a stored data item with a given key.
key | The key of the data item |
Referenced by DHT::handlePutRequest().
00089 { 00090 00091 std::map<OverlayKey, DHTData>::iterator it = dataMap.find(key); 00092 00093 if (it == dataMap.end()) 00094 return NodeHandle::UNSPECIFIED_NODE; 00095 else 00096 return it->second.sourceNode; 00097 }
cMessage * DHTDataStorage::getTtlMessage | ( | const OverlayKey & | key | ) | [virtual] |
Returns the ttlMessage of a stored data item with a given key.
key | The key of the data item |
Referenced by DHT::handlePutRequest().
00099 { 00100 std::map<OverlayKey, DHTData>::iterator it = dataMap.find(key); 00101 00102 if (it == dataMap.end()) 00103 return NULL; 00104 else 00105 return it->second.ttlMessage; 00106 }
const bool DHTDataStorage::isModifiable | ( | const OverlayKey & | key | ) | [virtual] |
Returns a boolean telling if this data if modifiable.
key | The key of the data item |
Referenced by DHT::handlePutRequest().
00108 { 00109 00110 std::map<OverlayKey, DHTData>::iterator it = dataMap.find(key); 00111 00112 if (it == dataMap.end()) 00113 return true; 00114 else 00115 return it->second.is_modifiable; 00116 }
const BinaryValue & DHTDataStorage::getDataAtPos | ( | uint | pos = 0 |
) | [virtual] |
Returns the value of the data item stored at position pos.
pos | position in data storage map |
Referenced by updateTooltip().
00120 { 00121 if (pos >= dataMap.size()) { 00122 error("Index out of bound (DHTDataStorage, getDataAtPos())"); 00123 } 00124 00125 std::map<OverlayKey, DHTData>::iterator it = dataMap.begin(); 00126 00127 for (uint i= 0; i < pos; i++) { 00128 it++; 00129 if (i == (pos-1)) 00130 return it->second.value; 00131 } 00132 00133 return it->second.value; 00134 }
const std::map< OverlayKey, DHTData >::iterator DHTDataStorage::begin | ( | ) | [virtual] |
Returns an iterator to the beginning of the map.
Referenced by DHT::update().
00137 { 00138 return dataMap.begin(); 00139 }
void DHTDataStorage::addData | ( | OverlayKey | key, | |
BinaryValue | value, | |||
cMessage * | ttlMessage, | |||
bool | is_modifiable = true , |
|||
NodeHandle | sourceNode = NodeHandle::UNSPECIFIED_NODE , |
|||
bool | responsible = true | |||
) | [virtual] |
Store a new data item in the map.
key | The key of the data item to be stored | |
value | The value of the data item to be stored | |
ttlMessage | The self-message sent for the ttl expiration | |
is_modifiable | Flag that tell if the data can be change by anyone, or just by the sourceNode | |
sourceNode | Node which asked to store the value | |
responsible |
Referenced by DHT::handlePutRequest().
00142 { 00143 DHTData entry; 00144 entry.value = value; 00145 entry.ttlMessage = ttlMessage; 00146 entry.sourceNode = sourceNode; 00147 entry.is_modifiable = is_modifiable; 00148 entry.responsible = responsible; 00149 // replace with new value 00150 dataMap.erase(key); 00151 dataMap.insert(make_pair(key, entry)); 00152 }
void DHTDataStorage::removeData | ( | OverlayKey | key | ) | [virtual] |
Removes a certain data item from the map.
key | The key of the data item to be removed |
Referenced by DHT::handlePutRequest(), and DHT::handleTimerEvent().
00155 { 00156 dataMap.erase(key); 00157 }
void DHTDataStorage::display | ( | ) |
00241 { 00242 cout << "Content of DHTDataStorage:" << endl; 00243 for (std::map<OverlayKey, DHTData>::iterator it = dataMap.begin(); 00244 it != dataMap.end(); it++) { 00245 cout << "Key: " << it->first << " Value: " << it->second.value << "End-time: " << it->second.ttlMessage->arrivalTime() << endl; 00246 } 00247 }
DhtDataVector * DHTDataStorage::get_range | ( | const OverlayKey | lkey, | |
const OverlayKey | rkey | |||
) |
Return a vector containing every keys/values between the two bounds.
lkey | The beginning of the range | |
rkey | The end of the range |
00160 { 00161 DhtDataVector* vect = new DhtDataVector(); 00162 map<OverlayKey, DHTData>::iterator iter; 00163 if (lkey > rkey) { //the zero key is between this to keys 00164 //we fetch everything after lkey 00165 for( iter = dataMap.lower_bound(lkey); iter != dataMap.end(); iter++ ) { 00166 vect->insert(vect->end(), std::make_pair(iter->first, iter->second)); 00167 } 00168 //and everything before rkey 00169 for( iter = dataMap.upper_bound(rkey); iter != (dataMap.begin()--); iter-- ) { 00170 vect->insert(vect->end(), std::make_pair(iter->first, iter->second)); 00171 } 00172 } 00173 else { 00174 for (iter = dataMap.lower_bound(lkey); iter != (dataMap.upper_bound(rkey) ++); iter++) { 00175 vect->insert(vect->end(), std::make_pair(iter->first, iter->second)); 00176 } 00177 } 00178 00179 return vect; 00180 }
DhtDumpVector * DHTDataStorage::dumpDht | ( | ) |
Dump all local data records into a vector.
Referenced by DHT::handleDumpDhtRequest().
00183 { 00184 DhtDumpVector* vect = new DhtDumpVector(); 00185 DhtDumpEntry entry; 00186 00187 map<OverlayKey, DHTData>::iterator iter; 00188 00189 for (iter = dataMap.begin(); iter != dataMap.end(); iter++) { 00190 entry.key = iter->first; 00191 entry.value = iter->second.value; 00192 entry.ttl = iter->second.ttlMessage->arrivalTime() - simTime(); 00193 entry.owner = iter->second.sourceNode; 00194 entry.is_modifiable = iter->second.is_modifiable; 00195 entry.responsible = iter->second.responsible; 00196 vect->push_back(entry); 00197 } 00198 00199 return vect; 00200 }
void DHTDataStorage::updateDisplayString | ( | ) | [protected] |
Displays the current number of successors in the list.
00204 { 00205 // FIXME: doesn't work without tcl/tk 00206 // if (ev.isGUI()) { 00207 if (1) { 00208 char buf[80]; 00209 00210 if (dataMap.size() == 1) { 00211 sprintf(buf, "1 data item"); 00212 } else { 00213 sprintf(buf, "%zi data items", dataMap.size()); 00214 } 00215 00216 displayString().setTagArg("t", 0, buf); 00217 displayString().setTagArg("t", 2, "blue"); 00218 } 00219 00220 }
void DHTDataStorage::updateTooltip | ( | ) | [protected] |
Displays the first 4 successor nodes as tooltip.
00223 { 00224 if (ev.isGUI()) { 00225 std::stringstream str; 00226 for (uint i = 0; i < dataMap.size(); i++) { 00227 str << getDataAtPos(i); 00228 00229 if ( i != dataMap.size() - 1 ) 00230 str << endl; 00231 } 00232 00233 00234 char buf[1024]; 00235 sprintf(buf, "%s", str.str().c_str()); 00236 displayString().setTagArg("tt", 0, buf); 00237 } 00238 }
std::map<OverlayKey, DHTData> DHTDataStorage::dataMap [protected] |
internal representation of the data storage
Referenced by addData(), begin(), clear(), display(), dumpDht(), get_range(), getData(), getDataAtPos(), getSize(), getSourceNode(), getTtlMessage(), initialize(), isEmpty(), isModifiable(), removeData(), updateDisplayString(), and updateTooltip().