OverSim
GlobalDhtTestMap.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2008 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #include <omnetpp.h>
25 
26 #include <GlobalStatisticsAccess.h>
27 
28 #include <DHTTestAppMessages_m.h>
29 
30 #include "GlobalDhtTestMap.h"
31 
32 using namespace std;
33 
35 
36 std::ostream& operator<<(std::ostream& stream, const DHTEntry entry)
37 {
38  return stream << "Value: " << entry.value
39  << " Endtime: " << entry.endtime;
40 }
41 
43 {
44  periodicTimer = NULL;
45 }
46 
48 {
49  cancelAndDelete(periodicTimer);
50  dataMap.clear();
51 }
52 
54 {
55  p2pnsNameCount = 0;
56  globalStatistics = GlobalStatisticsAccess().get();
57  WATCH_MAP(dataMap);
58 
59  periodicTimer = new cMessage("dhtTestMapTimer");
60 
61  scheduleAt(simTime(), periodicTimer);
62 }
63 
65 {
66 }
67 
69 {
70  //cleanupDataMap();
71  DhtTestEntryTimer *entryTimer = NULL;
72 
73  if (msg == periodicTimer) {
74  RECORD_STATS(globalStatistics->recordOutVector(
75  "GlobalDhtTestMap: Number of stored DHT entries", dataMap.size()));
76  scheduleAt(simTime() + TEST_MAP_INTERVAL, msg);
77  } else if ((entryTimer = dynamic_cast<DhtTestEntryTimer*>(msg)) != NULL) {
78  dataMap.erase(entryTimer->getKey());
79  delete msg;
80  } else {
81  throw cRuntimeError("GlobalDhtTestMap::handleMessage(): "
82  "Unknown message type!");
83  }
84 }
85 
86 void GlobalDhtTestMap::insertEntry(const OverlayKey& key, const DHTEntry& entry)
87 {
88  Enter_Method_Silent();
89 
90  dataMap.erase(key);
91  dataMap.insert(make_pair(key, entry));
92 
93  DhtTestEntryTimer* msg = new DhtTestEntryTimer("dhtEntryTimer");
94  msg->setKey(key);
95 
96  scheduleAt(entry.endtime, msg);
97 }
98 
100 {
101  dataMap.erase(key);
102 }
103 
105 {
106  std::map<OverlayKey, DHTEntry>::iterator it = dataMap.find(key);
107 
108  if (it == dataMap.end()) {
109  return NULL;
110  } else {
111  return &(it->second);
112  }
113 }
114 
116 {
117  if (dataMap.size() == 0) {
119  }
120 
121  // return random OverlayKey in O(log n)
122  std::map<OverlayKey, DHTEntry>::iterator it = dataMap.end();
123  DHTEntry tempEntry = {BinaryValue::UNSPECIFIED_VALUE, 0};
124 
125  OverlayKey randomKey = OverlayKey::random();
126  it = dataMap.find(randomKey);
127 
128  if (it == dataMap.end()) {
129  it = dataMap.insert(make_pair(randomKey, tempEntry)).first;
130  dataMap.erase(it++);
131  }
132 
133  if (it == dataMap.end()) {
134  it = dataMap.begin();
135  }
136 
137  return it->first;
138 }
139 
140