GlobalDhtTestMap.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2008 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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     //cleanupDataMap();
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     // return random OverlayKey in O(log n)
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 
Generated on Wed May 26 16:21:14 2010 for OverSim by  doxygen 1.6.3