GlobalDhtTestMap.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 <GlobalStatisticsAccess.h>
00027
00028 #include <DHTTestAppMessages_m.h>
00029
00030 #include "GlobalDhtTestMap.h"
00031
00032 using namespace std;
00033
00034 Define_Module(GlobalDhtTestMap);
00035
00036 std::ostream& operator<<(std::ostream& stream, const DHTEntry entry)
00037 {
00038 return stream << "Value: " << entry.value
00039 << " Endtime: " << entry.endtime;
00040 }
00041
00042 GlobalDhtTestMap::GlobalDhtTestMap()
00043 {
00044 periodicTimer = NULL;
00045 }
00046
00047 GlobalDhtTestMap::~GlobalDhtTestMap()
00048 {
00049 cancelAndDelete(periodicTimer);
00050 dataMap.clear();
00051 }
00052
00053 void GlobalDhtTestMap::initialize()
00054 {
00055 p2pnsNameCount = 0;
00056 globalStatistics = GlobalStatisticsAccess().get();
00057 WATCH_MAP(dataMap);
00058
00059 periodicTimer = new cMessage("dhtTestMapTimer");
00060
00061 scheduleAt(simTime(), periodicTimer);
00062 }
00063
00064 void GlobalDhtTestMap::finish()
00065 {
00066 }
00067
00068 void GlobalDhtTestMap::handleMessage(cMessage* msg)
00069 {
00070
00071 DhtTestEntryTimer *entryTimer = NULL;
00072
00073 if (msg == periodicTimer) {
00074 RECORD_STATS(globalStatistics->recordOutVector(
00075 "GlobalDhtTestMap: Number of stored DHT entries", dataMap.size()));
00076 scheduleAt(simTime() + TEST_MAP_INTERVAL, msg);
00077 } else if ((entryTimer = dynamic_cast<DhtTestEntryTimer*>(msg)) != NULL) {
00078 dataMap.erase(entryTimer->getKey());
00079 delete msg;
00080 } else {
00081 throw cRuntimeError("GlobalDhtTestMap::handleMessage(): "
00082 "Unknown message type!");
00083 }
00084 }
00085
00086 void GlobalDhtTestMap::insertEntry(const OverlayKey& key, const DHTEntry& entry)
00087 {
00088 Enter_Method_Silent();
00089
00090 dataMap.erase(key);
00091 dataMap.insert(make_pair(key, entry));
00092
00093 DhtTestEntryTimer* msg = new DhtTestEntryTimer("dhtEntryTimer");
00094 msg->setKey(key);
00095
00096 scheduleAt(entry.endtime, msg);
00097 }
00098
00099 void GlobalDhtTestMap::eraseEntry(const OverlayKey& key)
00100 {
00101 dataMap.erase(key);
00102 }
00103
00104 const DHTEntry* GlobalDhtTestMap::findEntry(const OverlayKey& key)
00105 {
00106 std::map<OverlayKey, DHTEntry>::iterator it = dataMap.find(key);
00107
00108 if (it == dataMap.end()) {
00109 return NULL;
00110 } else {
00111 return &(it->second);
00112 }
00113 }
00114
00115 const OverlayKey& GlobalDhtTestMap::getRandomKey()
00116 {
00117 if (dataMap.size() == 0) {
00118 return OverlayKey::UNSPECIFIED_KEY;
00119 }
00120
00121
00122 std::map<OverlayKey, DHTEntry>::iterator it = dataMap.end();
00123 DHTEntry tempEntry = {BinaryValue::UNSPECIFIED_VALUE, 0};
00124
00125 OverlayKey randomKey = OverlayKey::random();
00126 it = dataMap.find(randomKey);
00127
00128 if (it == dataMap.end()) {
00129 it = dataMap.insert(make_pair(randomKey, tempEntry)).first;
00130 dataMap.erase(it++);
00131 }
00132
00133 if (it == dataMap.end()) {
00134 it = dataMap.begin();
00135 }
00136
00137 return it->first;
00138 }
00139
00140