OverSim
P2pnsCache Class Reference

P2PNS name cache module. More...

#include <P2pnsCache.h>

Public Member Functions

virtual int numInitStages () const
virtual void initialize (int stage)
virtual void handleMessage (cMessage *msg)
virtual uint32_t 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 P2pnsIdCacheEntrygetIdCacheEntry (const OverlayKey &key)
virtual P2pnsIdCacheEntryaddIdCacheEntry (const OverlayKey &key, const BinaryValue *payload=NULL)
virtual void removeIdCacheEntry (const OverlayKey &key)
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 (uint32_t 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
P2pnsIdCache idCache
 internal representation of the KBR nodeId cache

Detailed Description

P2PNS name cache module.

This modul contains the name cache of the P2PNS implementation.

Author
Ingmar Baumgart
See Also
P2pns.cc

Definition at line 75 of file P2pnsCache.h.

Member Function Documentation

void P2pnsCache::addData ( BinaryValue  name,
BinaryValue  value,
cMessage *  ttlMessage = NULL 
)
virtual

Store a new data item in the map.

Parameters
nameThe name of the data item to be stored
valueThe value of the data item to be stored
ttlMessageThe self-message sent for the ttl expiration

Definition at line 162 of file P2pnsCache.cc.

{
entry.value = value;
entry.ttlMessage = ttlMessage;
// replace with new value
cache.erase(name);
cache.insert(make_pair(name, entry));
}
P2pnsIdCacheEntry * P2pnsCache::addIdCacheEntry ( const OverlayKey key,
const BinaryValue payload = NULL 
)
virtual

Definition at line 101 of file P2pnsCache.cc.

{
P2pnsIdCache::iterator it = idCache.find(key);
if (it == idCache.end()) {
it = idCache.insert(make_pair(key, P2pnsIdCacheEntry(key))).first;
}
if (payload != NULL) {
it->second.payloadQueue.push_back(*payload);
}
it->second.lastUsage = simTime();
return &it->second;
}
void P2pnsCache::clear ( )
virtual

Clears all stored data items.

Definition at line 67 of file P2pnsCache.cc.

{
map<BinaryValue, P2pnsCacheEntry>::iterator iter;
for( iter = cache.begin(); iter != cache.end(); iter++ ) {
cancelAndDelete(iter->second.ttlMessage);
}
cache.clear();
}
void P2pnsCache::display ( )

Definition at line 214 of file P2pnsCache.cc.

{
cout << "Content of P2pnsCache:" << endl;
for (std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.begin();
it != cache.end(); it++) {
cout << "name: " << it->first << " Value: " << it->second.value << "End-time: " << it->second.ttlMessage->getArrivalTime() << endl;
}
}
const BinaryValue & P2pnsCache::getData ( const BinaryValue name)
virtual

Returns the value of a stored data item with a given name.

Parameters
nameThe name of the data item
Returns
The value of the data item with the given name

Definition at line 124 of file P2pnsCache.cc.

{
std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.find(name);
if (it == cache.end())
else
return it->second.value;
}
const BinaryValue & P2pnsCache::getDataAtPos ( uint32_t  pos = 0)
virtual

Returns the value of the data item stored at position pos.

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

Definition at line 146 of file P2pnsCache.cc.

{
if (pos >= cache.size()) {
error("Index out of bound (P2pnsCache, getDataAtPos())");
}
std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.begin();
for (uint32_t i= 0; i < pos; i++) {
it++;
if (i == (pos-1))
return it->second.value;
}
return it->second.value;
}
P2pnsIdCacheEntry * P2pnsCache::getIdCacheEntry ( const OverlayKey key)
virtual

Definition at line 90 of file P2pnsCache.cc.

{
P2pnsIdCache::iterator it = idCache.find(key);
if (it != idCache.end()) {
return &it->second;
} else {
return NULL;
}
}
uint32_t P2pnsCache::getSize ( )
virtual

Returns number of stored data items in the map.

Returns
number of stored data items

Definition at line 77 of file P2pnsCache.cc.

{
return cache.size();
}
cMessage * P2pnsCache::getTtlMessage ( const BinaryValue name)
virtual

Returns the ttlMessage of a stored data item with a given name.

Parameters
nameThe name of the data item
Returns
The ttlMessage of the data item with the given name

Definition at line 136 of file P2pnsCache.cc.

{
std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.find(name);
if (it == cache.end())
return NULL;
else
return it->second.ttlMessage;
}
void P2pnsCache::handleMessage ( cMessage *  msg)
virtual

Definition at line 62 of file P2pnsCache.cc.

{
error("This module doesn't handle messages!");
}
void P2pnsCache::initialize ( int  stage)
virtual

Definition at line 53 of file P2pnsCache.cc.

{
if (stage != MIN_STAGE_APP)
return;
WATCH_MAP(cache);
WATCH_MAP(idCache);
}
bool P2pnsCache::isEmpty ( )
virtual

Checks if the data storage map is empty.

Returns
returns false if there are stored data items, true otherwise.

Definition at line 82 of file P2pnsCache.cc.

{
if (cache.size() == 0)
return true;
else
return false;
}
virtual int P2pnsCache::numInitStages ( void  ) const
inlinevirtual

Definition at line 78 of file P2pnsCache.h.

{
return MAX_STAGE_APP;
}
void P2pnsCache::removeData ( const BinaryValue name)
virtual

Removes a certain data item from the map.

Parameters
nameThe name of the data item to be removed

Definition at line 172 of file P2pnsCache.cc.

{
cache.erase(name);
}
void P2pnsCache::removeIdCacheEntry ( const OverlayKey key)
virtual

Definition at line 119 of file P2pnsCache.cc.

{
idCache.erase(key);
}
void P2pnsCache::updateDisplayString ( )
protected

Updates the display string.

Definition at line 177 of file P2pnsCache.cc.

{
// FIXME: doesn't work without tcl/tk
//if (ev.isGUI()) {
if (1) {
char buf[80];
if (cache.size() == 1) {
sprintf(buf, "1 data item");
} else {
sprintf(buf, "%zi data items", cache.size());
}
getDisplayString().setTagArg("t", 0, buf);
getDisplayString().setTagArg("t", 2, "blue");
}
}
void P2pnsCache::updateTooltip ( )
protected

Updates the tooltip.

Definition at line 196 of file P2pnsCache.cc.

{
if (ev.isGUI()) {
std::stringstream str;
for (uint32_t i = 0; i < cache.size(); i++) {
str << getDataAtPos(i);
if ( i != cache.size() - 1 )
str << endl;
}
char buf[1024];
sprintf(buf, "%s", str.str().c_str());
getDisplayString().setTagArg("tt", 0, buf);
}
}

Member Data Documentation

std::map<BinaryValue, P2pnsCacheEntry> P2pnsCache::cache
protected

internal representation of the cache

Definition at line 155 of file P2pnsCache.h.

P2pnsIdCache P2pnsCache::idCache
protected

internal representation of the KBR nodeId cache

Definition at line 156 of file P2pnsCache.h.


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