DHTDataStorage.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2007 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 "DHTDataStorage.h"
00027 
00028 Define_Module(DHTDataStorage);
00029 
00030 using namespace std;
00031 
00032 std::ostream& operator<<(std::ostream& Stream, const DHTData entry)
00033 {
00034     return Stream << "Value: " << entry.value
00035                   << " Endtime: " << entry.ttlMessage->getArrivalTime()
00036                   << " Modifiable: " << entry.is_modifiable
00037                   << " SourceNode: " << entry.sourceNode;
00038 }
00039 
00040 
00041 void DHTDataStorage::initialize(int stage)
00042 {
00043     if (stage != MIN_STAGE_APP)
00044         return;
00045 
00046     WATCH_MAP(dataMap);
00047 }
00048 
00049 void DHTDataStorage::handleMessage(cMessage* msg)
00050 {
00051     error("This module doesn't handle messages!");
00052 }
00053 
00054 void DHTDataStorage::clear()
00055 {
00056     map<OverlayKey, DHTData>::iterator iter;
00057 
00058     for( iter = dataMap.begin(); iter != dataMap.end(); iter++ ) {
00059         cancelAndDelete(iter->second.ttlMessage);
00060     }
00061 
00062     dataMap.clear();
00063 }
00064 
00065 
00066 uint32_t DHTDataStorage::getSize()
00067 {
00068     return dataMap.size();
00069 }
00070 
00071 bool DHTDataStorage::isEmpty()
00072 {
00073     if (dataMap.size() == 0)
00074         return true;
00075     else
00076         return false;
00077 }
00078 
00079 const BinaryValue& DHTDataStorage::getData(const OverlayKey& key) {
00080 
00081     std::map<OverlayKey, DHTData>::iterator it =  dataMap.find(key);
00082 
00083     if (it == dataMap.end())
00084         return BinaryValue::UNSPECIFIED_VALUE;
00085     else
00086         return it->second.value;
00087 }
00088 
00089 const NodeHandle& DHTDataStorage::getSourceNode(const OverlayKey& key) {
00090 
00091     std::map<OverlayKey, DHTData>::iterator it =  dataMap.find(key);
00092 
00093     if (it == dataMap.end())
00094         return NodeHandle::UNSPECIFIED_NODE;
00095     else
00096         return it->second.sourceNode;
00097 }
00098 
00099 cMessage* DHTDataStorage::getTtlMessage(const OverlayKey& key){
00100     std::map<OverlayKey, DHTData>::iterator it =  dataMap.find(key);
00101 
00102     if (it == dataMap.end())
00103         return NULL;
00104     else
00105         return it->second.ttlMessage;
00106 }
00107 
00108 const bool DHTDataStorage::isModifiable(const OverlayKey& key) {
00109 
00110     std::map<OverlayKey, DHTData>::iterator it =  dataMap.find(key);
00111 
00112     if (it == dataMap.end())
00113         return true;
00114     else
00115         return it->second.is_modifiable;
00116 }
00117 
00118 
00119 const BinaryValue& DHTDataStorage::getDataAtPos(uint32_t pos)
00120 {
00121     if (pos >= dataMap.size()) {
00122         error("Index out of bound (DHTDataStorage, getDataAtPos())");
00123     }
00124 
00125     std::map<OverlayKey, DHTData>::iterator it = dataMap.begin();
00126 
00127     for (uint32_t i= 0; i < pos; i++) {
00128         it++;
00129         if (i == (pos-1))
00130             return it->second.value;
00131     }
00132 
00133     return it->second.value;
00134 }
00135 
00136 const std::map<OverlayKey, DHTData>::iterator DHTDataStorage::begin()
00137 {
00138     return dataMap.begin();
00139 }
00140 
00141 void DHTDataStorage::addData(OverlayKey key, BinaryValue value, cMessage* ttlMessage, bool is_modifiable, NodeHandle sourceNode, bool responsible)
00142 {
00143     DHTData entry;
00144     entry.value = value;
00145     entry.ttlMessage = ttlMessage;
00146     entry.sourceNode = sourceNode;
00147     entry.is_modifiable = is_modifiable;
00148     entry.responsible = responsible;
00149     // replace with new value
00150     dataMap.erase(key);
00151     dataMap.insert(make_pair(key, entry));
00152 }
00153 
00154 void DHTDataStorage::removeData(OverlayKey key)
00155 {
00156     dataMap.erase(key);
00157 }
00158 
00159 DhtDataVector* DHTDataStorage::get_range(const OverlayKey lkey, const OverlayKey rkey)
00160 {
00161     DhtDataVector* vect = new DhtDataVector();
00162     map<OverlayKey, DHTData>::iterator iter;
00163     if (lkey > rkey) { //the zero key is between this to keys
00164         //we fetch everything after lkey
00165         for( iter = dataMap.lower_bound(lkey); iter != dataMap.end(); iter++ ) {
00166             vect->insert(vect->end(), std::make_pair(iter->first, iter->second));
00167         }
00168         //and everything before rkey
00169         for( iter = dataMap.upper_bound(rkey); iter != (dataMap.begin()--); iter-- ) {
00170             vect->insert(vect->end(), std::make_pair(iter->first, iter->second));
00171         }
00172     }
00173     else {
00174         for (iter = dataMap.lower_bound(lkey); iter != (dataMap.upper_bound(rkey) ++); iter++) {
00175             vect->insert(vect->end(), std::make_pair(iter->first, iter->second));
00176         }
00177     }
00178 
00179     return vect;
00180 }
00181 
00182 DhtDumpVector* DHTDataStorage::dumpDht()
00183 {
00184     DhtDumpVector* vect = new DhtDumpVector();
00185     DhtDumpEntry entry;
00186 
00187     map<OverlayKey, DHTData>::iterator iter;
00188 
00189     for (iter = dataMap.begin(); iter != dataMap.end(); iter++) {
00190         entry.key = iter->first;
00191         entry.value = iter->second.value;
00192         entry.ttl = (int)SIMTIME_DBL(iter->second.ttlMessage->getArrivalTime() - simTime());
00193         entry.owner = iter->second.sourceNode;
00194         entry.is_modifiable = iter->second.is_modifiable;
00195         entry.responsible = iter->second.responsible;
00196         vect->push_back(entry);
00197     }
00198 
00199     return vect;
00200 }
00201 
00202 
00203 void DHTDataStorage::updateDisplayString()
00204 {
00205     // FIXME: doesn't work without tcl/tk
00206     //          if (ev.isGUI()) {
00207     if (ev.isGUI()) {
00208         char buf[80];
00209 
00210         if (dataMap.size() == 1) {
00211             sprintf(buf, "1 data item");
00212         } else {
00213             sprintf(buf, "%zi data items", dataMap.size());
00214         }
00215 
00216         getDisplayString().setTagArg("t", 0, buf);
00217         getDisplayString().setTagArg("t", 2, "blue");
00218     }
00219 
00220 }
00221 
00222 void DHTDataStorage::updateTooltip()
00223 {
00224     if (ev.isGUI()) {
00225         std::stringstream str;
00226         for (uint32_t i = 0; i < dataMap.size(); i++)   {
00227             str << getDataAtPos(i);
00228 
00229             if ( i != dataMap.size() - 1 )
00230                 str << endl;
00231         }
00232 
00233 
00234         char buf[1024];
00235         sprintf(buf, "%s", str.str().c_str());
00236         getDisplayString().setTagArg("tt", 0, buf);
00237     }
00238 }
00239 
00240 void DHTDataStorage::display()
00241 {
00242     cout << "Content of DHTDataStorage:" << endl;
00243     for (std::map<OverlayKey, DHTData>::iterator it = dataMap.begin();
00244     it != dataMap.end(); it++) {
00245         cout << "Key: " << it->first << " Value: " << it->second.value << "End-time: " << it->second.ttlMessage->getArrivalTime() << endl;
00246     }
00247 }

Generated on Wed Mar 25 16:32:48 2009 for OverSim by  doxygen 1.5.5