#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.
00066 { 00067 return cache.size(); 00068 }
void P2pnsCache::clear | ( | ) | [virtual] |
bool P2pnsCache::isEmpty | ( | ) | [virtual] |
Checks if the data storage map is empty.
00071 { 00072 if (cache.size() == 0) 00073 return true; 00074 else 00075 return false; 00076 }
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 |
00078 { 00079 00080 std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.find(name); 00081 00082 if (it == cache.end()) 00083 return BinaryValue::UNSPECIFIED_VALUE; 00084 else 00085 return it->second.value; 00086 }
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 |
00090 { 00091 std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.find(name); 00092 00093 if (it == cache.end()) 00094 return NULL; 00095 else 00096 return it->second.ttlMessage; 00097 }
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 |
00101 { 00102 if (pos >= cache.size()) { 00103 error("Index out of bound (P2pnsCache, getDataAtPos())"); 00104 } 00105 00106 std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.begin(); 00107 for (uint i= 0; i < pos; i++) { 00108 it++; 00109 if (i == (pos-1)) 00110 return it->second.value; 00111 } 00112 return it->second.value; 00113 }
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 |
00117 { 00118 P2pnsCacheEntry entry; 00119 entry.value = value; 00120 entry.ttlMessage = ttlMessage; 00121 // replace with new value 00122 cache.erase(name); 00123 cache.insert(make_pair(name, entry)); 00124 }
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 |
00127 { 00128 cache.erase(name); 00129 }
void P2pnsCache::display | ( | ) |
00169 { 00170 cout << "Content of P2pnsCache:" << endl; 00171 for (std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.begin(); 00172 it != cache.end(); it++) { 00173 cout << "name: " << it->first << " Value: " << it->second.value << "End-time: " << it->second.ttlMessage->arrivalTime() << endl; 00174 } 00175 }
void P2pnsCache::updateDisplayString | ( | ) | [protected] |
Updates the display string.
00132 { 00133 // FIXME: doesn't work without tcl/tk 00134 //if (ev.isGUI()) { 00135 if (1) { 00136 char buf[80]; 00137 00138 if (cache.size() == 1) { 00139 sprintf(buf, "1 data item"); 00140 } else { 00141 sprintf(buf, "%zi data items", cache.size()); 00142 } 00143 00144 displayString().setTagArg("t", 0, buf); 00145 displayString().setTagArg("t", 2, "blue"); 00146 } 00147 00148 }
void P2pnsCache::updateTooltip | ( | ) | [protected] |
Updates the tooltip.
00151 { 00152 if (ev.isGUI()) { 00153 std::stringstream str; 00154 for (uint i = 0; i < cache.size(); i++) { 00155 str << getDataAtPos(i); 00156 00157 if ( i != cache.size() - 1 ) 00158 str << endl; 00159 } 00160 00161 00162 char buf[1024]; 00163 sprintf(buf, "%s", str.str().c_str()); 00164 displayString().setTagArg("tt", 0, buf); 00165 } 00166 }
std::map<BinaryValue, P2pnsCacheEntry> P2pnsCache::cache [protected] |
internal representation of the cache