#include <P2pnsCache.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 BinaryValue &name) |
Returns the value of a stored data item with a given name. | |
virtual cMessage * | getTtlMessage (const BinaryValue &name) |
Returns the ttlMessage of a stored data item with a given name. | |
virtual const BinaryValue & | getDataAtPos (uint pos=0) |
Returns the value of the data item stored at position pos. | |
virtual void | addData (BinaryValue name, BinaryValue value, cMessage *ttlMessage=NULL) |
Store a new data item in the map. | |
virtual void | removeData (const BinaryValue &name) |
Removes a certain data item from the map. | |
void | display () |
Protected Member Functions | |
void | updateDisplayString () |
Updates the display string. | |
void | updateTooltip () |
Updates the tooltip. | |
Protected Attributes | |
std::map< BinaryValue, P2pnsCacheEntry > | cache |
internal representation of the cache |
virtual int P2pnsCache::numInitStages | ( | void | ) | const [inline, virtual] |
void P2pnsCache::initialize | ( | int | stage | ) | [virtual] |
void P2pnsCache::handleMessage | ( | cMessage * | msg | ) | [virtual] |
uint P2pnsCache::getSize | ( | ) | [virtual] |
Returns number of stored data items in the map.
00068 { 00069 return cache.size(); 00070 }
void P2pnsCache::clear | ( | ) | [virtual] |
bool P2pnsCache::isEmpty | ( | ) | [virtual] |
Checks if the data storage map is empty.
00073 { 00074 if (cache.size() == 0) 00075 return true; 00076 else 00077 return false; 00078 }
const BinaryValue & P2pnsCache::getData | ( | const BinaryValue & | name | ) | [virtual] |
Returns the value of a stored data item with a given name.
name | The name of the data item |
00080 { 00081 00082 std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.find(name); 00083 00084 if (it == cache.end()) 00085 return BinaryValue::UNSPECIFIED_VALUE; 00086 else 00087 return it->second.value; 00088 }
cMessage * P2pnsCache::getTtlMessage | ( | const BinaryValue & | name | ) | [virtual] |
Returns the ttlMessage of a stored data item with a given name.
name | The name of the data item |
00092 { 00093 std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.find(name); 00094 00095 if (it == cache.end()) 00096 return NULL; 00097 else 00098 return it->second.ttlMessage; 00099 }
const BinaryValue & P2pnsCache::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().
00103 { 00104 if (pos >= cache.size()) { 00105 error("Index out of bound (P2pnsCache, getDataAtPos())"); 00106 } 00107 00108 std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.begin(); 00109 for (uint i= 0; i < pos; i++) { 00110 it++; 00111 if (i == (pos-1)) 00112 return it->second.value; 00113 } 00114 return it->second.value; 00115 }
void P2pnsCache::addData | ( | BinaryValue | name, | |
BinaryValue | value, | |||
cMessage * | ttlMessage = NULL | |||
) | [virtual] |
Store a new data item in the map.
name | The name 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 |
Referenced by P2pns::handleDHTgetCAPIResponse(), and P2pns::p2pnsRegisterRpc().
00119 { 00120 P2pnsCacheEntry entry; 00121 entry.value = value; 00122 entry.ttlMessage = ttlMessage; 00123 // replace with new value 00124 cache.erase(name); 00125 cache.insert(make_pair(name, entry)); 00126 }
void P2pnsCache::removeData | ( | const BinaryValue & | name | ) | [virtual] |
Removes a certain data item from the map.
name | The name of the data item to be removed |
00129 { 00130 cache.erase(name); 00131 }
void P2pnsCache::display | ( | ) |
00171 { 00172 cout << "Content of P2pnsCache:" << endl; 00173 for (std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.begin(); 00174 it != cache.end(); it++) { 00175 cout << "name: " << it->first << " Value: " << it->second.value << "End-time: " << it->second.ttlMessage->arrivalTime() << endl; 00176 } 00177 }
void P2pnsCache::updateDisplayString | ( | ) | [protected] |
Updates the display string.
00134 { 00135 // FIXME: doesn't work without tcl/tk 00136 //if (ev.isGUI()) { 00137 if (1) { 00138 char buf[80]; 00139 00140 if (cache.size() == 1) { 00141 sprintf(buf, "1 data item"); 00142 } else { 00143 sprintf(buf, "%zi data items", cache.size()); 00144 } 00145 00146 displayString().setTagArg("t", 0, buf); 00147 displayString().setTagArg("t", 2, "blue"); 00148 } 00149 00150 }
void P2pnsCache::updateTooltip | ( | ) | [protected] |
Updates the tooltip.
00153 { 00154 if (ev.isGUI()) { 00155 std::stringstream str; 00156 for (uint i = 0; i < cache.size(); i++) { 00157 str << getDataAtPos(i); 00158 00159 if ( i != cache.size() - 1 ) 00160 str << endl; 00161 } 00162 00163 00164 char buf[1024]; 00165 sprintf(buf, "%s", str.str().c_str()); 00166 displayString().setTagArg("tt", 0, buf); 00167 } 00168 }
std::map<BinaryValue, P2pnsCacheEntry> P2pnsCache::cache [protected] |
internal representation of the cache
Referenced by addData(), clear(), display(), getData(), getDataAtPos(), getSize(), getTtlMessage(), initialize(), isEmpty(), removeData(), updateDisplayString(), and updateTooltip().