P2pnsCache.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 "P2pnsCache.h"
00027 
00028 Define_Module(P2pnsCache);
00029 
00030 using namespace std;
00031 
00032 std::ostream& operator<<(std::ostream& Stream, const P2pnsCacheEntry entry)
00033 {
00034     Stream << "Value: " << entry.value;
00035 
00036     if (entry.ttlMessage != NULL) {
00037         Stream << "Endtime: " << entry.ttlMessage->getArrivalTime();
00038     }
00039 
00040     return Stream;
00041 }
00042 
00043 
00044 void P2pnsCache::initialize(int stage)
00045 {
00046     if (stage != MIN_STAGE_APP)
00047         return;
00048 
00049     WATCH_MAP(cache);
00050 }
00051 
00052 void P2pnsCache::handleMessage(cMessage* msg)
00053 {
00054     error("This module doesn't handle messages!");
00055 }
00056 
00057 void P2pnsCache::clear()
00058 {
00059   map<BinaryValue, P2pnsCacheEntry>::iterator iter;
00060   for( iter = cache.begin(); iter != cache.end(); iter++ ) {
00061     cancelAndDelete(iter->second.ttlMessage);
00062   }
00063   cache.clear();
00064 }
00065 
00066 
00067 uint32_t P2pnsCache::getSize()
00068 {
00069     return cache.size();
00070 }
00071 
00072 bool P2pnsCache::isEmpty()
00073 {
00074     if (cache.size() == 0)
00075         return true;
00076     else
00077         return false;
00078 }
00079 
00080 const BinaryValue& P2pnsCache::getData(const BinaryValue& name) {
00081 
00082     std::map<BinaryValue, P2pnsCacheEntry>::iterator it =  cache.find(name);
00083 
00084     if (it == cache.end())
00085         return BinaryValue::UNSPECIFIED_VALUE;
00086     else
00087         return it->second.value;
00088 }
00089 
00090 
00091 
00092 cMessage* P2pnsCache::getTtlMessage(const BinaryValue& name){
00093     std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.find(name);
00094 
00095     if (it == cache.end())
00096         return NULL;
00097     else
00098         return it->second.ttlMessage;
00099 }
00100 
00101 
00102 const BinaryValue& P2pnsCache::getDataAtPos(uint32_t pos)
00103 {
00104     if (pos >= cache.size()) {
00105         error("Index out of bound (P2pnsCache, getDataAtPos())");
00106     }
00107 
00108     std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.begin();
00109     for (uint32_t i= 0; i < pos; i++) {
00110         it++;
00111         if (i == (pos-1))
00112             return it->second.value;
00113     }
00114     return it->second.value;
00115 }
00116 
00117 
00118 void P2pnsCache::addData(BinaryValue name, BinaryValue value, cMessage* ttlMessage)
00119 {
00120     P2pnsCacheEntry entry;
00121     entry.value = value;
00122     entry.ttlMessage = ttlMessage;
00123     // replace with new value
00124     cache.erase(name);
00125     cache.insert(make_pair(name, entry));
00126 }
00127 
00128 void P2pnsCache::removeData(const BinaryValue& name)
00129 {
00130     cache.erase(name);
00131 }
00132 
00133 void P2pnsCache::updateDisplayString()
00134 {
00135 // FIXME: doesn't work without tcl/tk
00136     //if (ev.isGUI()) {
00137     if (1) {
00138         char buf[80];
00139 
00140         if (cache.size() == 1) {
00141             sprintf(buf, "1 data item");
00142         } else {
00143             sprintf(buf, "%zi data items", cache.size());
00144         }
00145 
00146         getDisplayString().setTagArg("t", 0, buf);
00147         getDisplayString().setTagArg("t", 2, "blue");
00148     }
00149 
00150 }
00151 
00152 void P2pnsCache::updateTooltip()
00153 {
00154     if (ev.isGUI()) {
00155         std::stringstream str;
00156         for (uint32_t i = 0; i < cache.size(); i++)     {
00157             str << getDataAtPos(i);
00158 
00159             if ( i != cache.size() - 1 )
00160                 str << endl;
00161         }
00162 
00163 
00164         char buf[1024];
00165         sprintf(buf, "%s", str.str().c_str());
00166         getDisplayString().setTagArg("tt", 0, buf);
00167     }
00168 }
00169 
00170 void P2pnsCache::display()
00171 {
00172     cout << "Content of P2pnsCache:" << endl;
00173     for (std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.begin();
00174          it != cache.end(); it++) {
00175         cout << "name: " << it->first << " Value: " << it->second.value << "End-time: " << it->second.ttlMessage->getArrivalTime() << endl;
00176     }
00177 }

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