OverSim
P2pnsCache.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 "P2pnsCache.h"
27 
29 
30 using namespace std;
31 
32 std::ostream& operator<<(std::ostream& os, const P2pnsIdCacheEntry entry)
33 {
34  os << "key: " << entry.key << " addr: " << entry.addr
35  << " state: " << entry.state << " lastUsage: " << entry.lastUsage
36  << " queueSize: " << entry.payloadQueue.size();
37 
38  return os;
39 }
40 
41 std::ostream& operator<<(std::ostream& Stream, const P2pnsCacheEntry entry)
42 {
43  Stream << "Value: " << entry.value;
44 
45  if (entry.ttlMessage != NULL) {
46  Stream << "Endtime: " << entry.ttlMessage->getArrivalTime();
47  }
48 
49  return Stream;
50 }
51 
52 
53 void P2pnsCache::initialize(int stage)
54 {
55  if (stage != MIN_STAGE_APP)
56  return;
57 
58  WATCH_MAP(cache);
59  WATCH_MAP(idCache);
60 }
61 
62 void P2pnsCache::handleMessage(cMessage* msg)
63 {
64  error("This module doesn't handle messages!");
65 }
66 
68 {
69  map<BinaryValue, P2pnsCacheEntry>::iterator iter;
70  for( iter = cache.begin(); iter != cache.end(); iter++ ) {
71  cancelAndDelete(iter->second.ttlMessage);
72  }
73  cache.clear();
74 }
75 
76 
78 {
79  return cache.size();
80 }
81 
83 {
84  if (cache.size() == 0)
85  return true;
86  else
87  return false;
88 }
89 
91 {
92  P2pnsIdCache::iterator it = idCache.find(key);
93 
94  if (it != idCache.end()) {
95  return &it->second;
96  } else {
97  return NULL;
98  }
99 }
100 
102  const BinaryValue* payload)
103 {
104  P2pnsIdCache::iterator it = idCache.find(key);
105 
106  if (it == idCache.end()) {
107  it = idCache.insert(make_pair(key, P2pnsIdCacheEntry(key))).first;
108  }
109 
110  if (payload != NULL) {
111  it->second.payloadQueue.push_back(*payload);
112  }
113 
114  it->second.lastUsage = simTime();
115 
116  return &it->second;
117 }
118 
120 {
121  idCache.erase(key);
122 }
123 
125 
126  std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.find(name);
127 
128  if (it == cache.end())
130  else
131  return it->second.value;
132 }
133 
134 
135 
136 cMessage* P2pnsCache::getTtlMessage(const BinaryValue& name){
137  std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.find(name);
138 
139  if (it == cache.end())
140  return NULL;
141  else
142  return it->second.ttlMessage;
143 }
144 
145 
147 {
148  if (pos >= cache.size()) {
149  error("Index out of bound (P2pnsCache, getDataAtPos())");
150  }
151 
152  std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.begin();
153  for (uint32_t i= 0; i < pos; i++) {
154  it++;
155  if (i == (pos-1))
156  return it->second.value;
157  }
158  return it->second.value;
159 }
160 
161 
162 void P2pnsCache::addData(BinaryValue name, BinaryValue value, cMessage* ttlMessage)
163 {
164  P2pnsCacheEntry entry;
165  entry.value = value;
166  entry.ttlMessage = ttlMessage;
167  // replace with new value
168  cache.erase(name);
169  cache.insert(make_pair(name, entry));
170 }
171 
173 {
174  cache.erase(name);
175 }
176 
178 {
179 // FIXME: doesn't work without tcl/tk
180  //if (ev.isGUI()) {
181  if (1) {
182  char buf[80];
183 
184  if (cache.size() == 1) {
185  sprintf(buf, "1 data item");
186  } else {
187  sprintf(buf, "%zi data items", cache.size());
188  }
189 
190  getDisplayString().setTagArg("t", 0, buf);
191  getDisplayString().setTagArg("t", 2, "blue");
192  }
193 
194 }
195 
197 {
198  if (ev.isGUI()) {
199  std::stringstream str;
200  for (uint32_t i = 0; i < cache.size(); i++) {
201  str << getDataAtPos(i);
202 
203  if ( i != cache.size() - 1 )
204  str << endl;
205  }
206 
207 
208  char buf[1024];
209  sprintf(buf, "%s", str.str().c_str());
210  getDisplayString().setTagArg("tt", 0, buf);
211  }
212 }
213 
215 {
216  cout << "Content of P2pnsCache:" << endl;
217  for (std::map<BinaryValue, P2pnsCacheEntry>::iterator it = cache.begin();
218  it != cache.end(); it++) {
219  cout << "name: " << it->first << " Value: " << it->second.value << "End-time: " << it->second.ttlMessage->getArrivalTime() << endl;
220  }
221 }