P2pnsCache.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #include <omnetpp.h>
00025
00026 #include "P2pnsCache.h"
00027
00028 Define_Module(P2pnsCache);
00029
00030 using namespace std;
00031
00032 std::ostream& operator<<(std::ostream& os, const P2pnsIdCacheEntry entry)
00033 {
00034 os << "key: " << entry.key << " addr: " << entry.addr
00035 << " state: " << entry.state << " lastUsage: " << entry.lastUsage
00036 << " queueSize: " << entry.payloadQueue.size();
00037
00038 return os;
00039 }
00040
00041 std::ostream& operator<<(std::ostream& Stream, const P2pnsCacheEntry entry)
00042 {
00043 Stream << "Value: " << entry.value;
00044
00045 if (entry.ttlMessage != NULL) {
00046 Stream << "Endtime: " << entry.ttlMessage->getArrivalTime();
00047 }
00048
00049 return Stream;
00050 }
00051
00052
00053 void P2pnsCache::initialize(int stage)
00054 {
00055 if (stage != MIN_STAGE_APP)
00056 return;
00057
00058 WATCH_MAP(cache);
00059 WATCH_MAP(idCache);
00060 }
00061
00062 void P2pnsCache::handleMessage(cMessage* msg)
00063 {
00064 error("This module doesn't handle messages!");
00065 }
00066
00067 void P2pnsCache::clear()
00068 {
00069 map<BinaryValue, P2pnsCacheEntry>::iterator iter;
00070 for( iter = cache.begin(); iter != cache.end(); iter++ ) {
00071 cancelAndDelete(iter->second.ttlMessage);
00072 }
00073 cache.clear();
00074 }
00075
00076
00077 uint32_t P2pnsCache::getSize()
00078 {
00079 return cache.size();
00080 }
00081
00082 bool P2pnsCache::isEmpty()
00083 {
00084 if (cache.size() == 0)
00085 return true;
00086 else
00087 return false;
00088 }
00089
00090 P2pnsIdCacheEntry* P2pnsCache::getIdCacheEntry(const OverlayKey& key)
00091 {
00092 P2pnsIdCache::iterator it = idCache.find(key);
00093
00094 if (it != idCache.end()) {
00095 return &it->second;
00096 } else {
00097 return NULL;
00098 }
00099 }
00100
00101 P2pnsIdCacheEntry* P2pnsCache::addIdCacheEntry(const OverlayKey& key,
00102 const BinaryValue* payload)
00103 {
00104 P2pnsIdCache::iterator it = idCache.find(key);
00105
00106 if (it == idCache.end()) {
00107 it = idCache.insert(make_pair(key, P2pnsIdCacheEntry(key))).first;
00108 }
00109
00110 if (payload != NULL) {
00111 it->second.payloadQueue.push_back(*payload);
00112 }
00113
00114 it->second.lastUsage = simTime();
00115
00116 return &it->second;
00117 }
00118
00119 void P2pnsCache::removeIdCacheEntry(const OverlayKey& key)
00120 {
00121 idCache.erase(key);
00122 }
00123
00124 const BinaryValue& P2pnsCache::getData(const BinaryValue& name) {
00125
00126 std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.find(name);
00127
00128 if (it == cache.end())
00129 return BinaryValue::UNSPECIFIED_VALUE;
00130 else
00131 return it->second.value;
00132 }
00133
00134
00135
00136 cMessage* P2pnsCache::getTtlMessage(const BinaryValue& name){
00137 std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.find(name);
00138
00139 if (it == cache.end())
00140 return NULL;
00141 else
00142 return it->second.ttlMessage;
00143 }
00144
00145
00146 const BinaryValue& P2pnsCache::getDataAtPos(uint32_t pos)
00147 {
00148 if (pos >= cache.size()) {
00149 error("Index out of bound (P2pnsCache, getDataAtPos())");
00150 }
00151
00152 std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.begin();
00153 for (uint32_t i= 0; i < pos; i++) {
00154 it++;
00155 if (i == (pos-1))
00156 return it->second.value;
00157 }
00158 return it->second.value;
00159 }
00160
00161
00162 void P2pnsCache::addData(BinaryValue name, BinaryValue value, cMessage* ttlMessage)
00163 {
00164 P2pnsCacheEntry entry;
00165 entry.value = value;
00166 entry.ttlMessage = ttlMessage;
00167
00168 cache.erase(name);
00169 cache.insert(make_pair(name, entry));
00170 }
00171
00172 void P2pnsCache::removeData(const BinaryValue& name)
00173 {
00174 cache.erase(name);
00175 }
00176
00177 void P2pnsCache::updateDisplayString()
00178 {
00179
00180
00181 if (1) {
00182 char buf[80];
00183
00184 if (cache.size() == 1) {
00185 sprintf(buf, "1 data item");
00186 } else {
00187 sprintf(buf, "%zi data items", cache.size());
00188 }
00189
00190 getDisplayString().setTagArg("t", 0, buf);
00191 getDisplayString().setTagArg("t", 2, "blue");
00192 }
00193
00194 }
00195
00196 void P2pnsCache::updateTooltip()
00197 {
00198 if (ev.isGUI()) {
00199 std::stringstream str;
00200 for (uint32_t i = 0; i < cache.size(); i++) {
00201 str << getDataAtPos(i);
00202
00203 if ( i != cache.size() - 1 )
00204 str << endl;
00205 }
00206
00207
00208 char buf[1024];
00209 sprintf(buf, "%s", str.str().c_str());
00210 getDisplayString().setTagArg("tt", 0, buf);
00211 }
00212 }
00213
00214 void P2pnsCache::display()
00215 {
00216 cout << "Content of P2pnsCache:" << endl;
00217 for (std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.begin();
00218 it != cache.end(); it++) {
00219 cout << "name: " << it->first << " Value: " << it->second.value << "End-time: " << it->second.ttlMessage->getArrivalTime() << endl;
00220 }
00221 }