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 globalStatistics = GlobalStatisticsAccess().get();
00056 WATCH_MAP(dataMap);
00057
00058 periodicTimer = new cMessage("dhtTestMapTimer");
00059
00060 scheduleAt(simTime(), periodicTimer);
00061 }
00062
00063 void GlobalDhtTestMap::finish()
00064 {
00065 }
00066
00067 void GlobalDhtTestMap::handleMessage(cMessage* msg)
00068 {
00069
00070 DhtTestEntryTimer *entryTimer = NULL;
00071
00072 if (msg == periodicTimer) {
00073 RECORD_STATS(globalStatistics->recordOutVector(
00074 "GlobalDhtTestMap: Number of stored DHT entries", dataMap.size()));
00075 scheduleAt(simTime() + TEST_MAP_INTERVAL, msg);
00076 } else if ((entryTimer = dynamic_cast<DhtTestEntryTimer*>(msg)) != NULL) {
00077 dataMap.erase(entryTimer->getKey());
00078 delete msg;
00079 } else {
00080 throw cRuntimeError("GlobalDhtTestMap::handleMessage(): "
00081 "Unknown message type!");
00082 }
00083 }
00084
00085 void GlobalDhtTestMap::insertEntry(const OverlayKey& key, const DHTEntry& entry)
00086 {
00087 Enter_Method_Silent();
00088
00089 dataMap.erase(key);
00090 dataMap.insert(make_pair(key, entry));
00091
00092 DhtTestEntryTimer* msg = new DhtTestEntryTimer("dhtEntryTimer");
00093 msg->setKey(key);
00094
00095 scheduleAt(entry.endtime, msg);
00096 }
00097
00098 void GlobalDhtTestMap::eraseEntry(const OverlayKey& key)
00099 {
00100 dataMap.erase(key);
00101 }
00102
00103 const DHTEntry* GlobalDhtTestMap::findEntry(const OverlayKey& key)
00104 {
00105 std::map<OverlayKey, DHTEntry>::iterator it = dataMap.find(key);
00106
00107 if (it == dataMap.end()) {
00108 return NULL;
00109 } else {
00110 return &(it->second);
00111 }
00112 }
00113
00114 const OverlayKey& GlobalDhtTestMap::getRandomKey()
00115 {
00116 if (dataMap.size() == 0) {
00117 return OverlayKey::UNSPECIFIED_KEY;
00118 }
00119
00120
00121 std::map<OverlayKey, DHTEntry>::iterator it = dataMap.end();
00122 DHTEntry tempEntry = {BinaryValue::UNSPECIFIED_VALUE, 0};
00123
00124 OverlayKey randomKey = OverlayKey::random();
00125 it = dataMap.find(randomKey);
00126
00127 if (it == dataMap.end()) {
00128 it = dataMap.insert(make_pair(randomKey, tempEntry)).first;
00129 dataMap.erase(it++);
00130 }
00131
00132 if (it == dataMap.end()) {
00133 it = dataMap.begin();
00134 }
00135
00136 return it->first;
00137 }
00138
00139