P2pnsCache Class Reference

#include <P2pnsCache.h>

List of all members.

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 BinaryValuegetData (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 BinaryValuegetDataAtPos (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


Member Function Documentation

virtual int P2pnsCache::numInitStages ( void   )  const [inline, virtual]

00059     {
00060         return MAX_STAGE_APP;
00061     }

void P2pnsCache::initialize ( int  stage  )  [virtual]

00045 {
00046     if (stage != MIN_STAGE_APP)
00047         return;
00048 
00049     WATCH_MAP(cache);
00050 }

void P2pnsCache::handleMessage ( cMessage *  msg  )  [virtual]

00053 {
00054     error("This module doesn't handle messages!");
00055 }

uint P2pnsCache::getSize (  )  [virtual]

Returns number of stored data items in the map.

Returns:
number of stored data items
00068 {
00069     return cache.size();
00070 }

void P2pnsCache::clear (  )  [virtual]

Clears all stored data items.

00058 {
00059   map<BinaryValue, P2pnsCacheEntry>::iterator iter;
00060   for( iter = cache.begin(); iter != cache.end(); iter++ ) {
00061     cancelAndDelete(iter->second.ttlMessage);
00062   }
00063   cache.clear();
00064 }

bool P2pnsCache::isEmpty (  )  [virtual]

Checks if the data storage map is empty.

Returns:
returns false if there are stored data items, true otherwise.
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.

Parameters:
name The name of the data item
Returns:
The value of the data item with the given name
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.

Parameters:
name The name of the data item
Returns:
The ttlMessage of the data item with the given name
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.

Parameters:
pos position in data storage map
Returns:
The value of the data item at position pos

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.

Parameters:
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.

Parameters:
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 }


Member Data Documentation


The documentation for this class was generated from the following files:

Generated on Fri Sep 19 13:05:07 2008 for ITM OverSim by  doxygen 1.5.5