OverSim
GiaNeighbors.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 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 <assert.h>
25 
26 #include <InitStages.h>
27 
28 #include "GiaNeighbors.h"
29 
30 
32 
33 void GiaNeighbors::initialize(int stage)
34 {
35  // wait until IPAddressResolver finished his initialization
36  if(stage != MIN_STAGE_OVERLAY)
37  return;
38 
39  WATCH_MAP(neighbors);
40  timeout = getParentModule()->getSubmodule("gia")->par("neighborTimeout");
41  //unspecNode = GiaNode::UNSPECIFIED_NODE;
42 }
43 
44 
45 void GiaNeighbors::handleMessages( cMessage* msg )
46 {
47  error("this module doesn't handle messages, it runs only in initialize()");
48 }
49 
50 uint32_t GiaNeighbors::getSize() const
51 {
52  return neighbors.size();
53 }
54 
55 bool GiaNeighbors::contains(const OverlayKey& key) const
56 {
57  NeighborsConstIterator it = neighbors.begin();
58 
59  for(it = neighbors.begin(); it != neighbors.end(); it++)
60  if(it->first.getKey() == key)
61  break;
62 
63  if (it != neighbors.end())
64  return true;
65  return false;
66 }
67 
68 bool GiaNeighbors::contains(const GiaNode& node) const
69 {
70  NeighborsConstIterator it = neighbors.find(node);
71 
72  if(it != neighbors.end())
73  return true;
74  return false;
75 }
76 
77 void GiaNeighbors::add(const GiaNode& node, unsigned int degree)
78 {
79  GiaNeighborInfo info = {degree,
80  5,
81  5,
82  simTime(),
83  GiaKeyList()};
84 
85  neighbors.insert(std::make_pair(node, info));
86  //neighbors.insert(node);
87 }
88 
89 // void GiaNeighbors::add(const NodeHandle& node)
90 // {
91 // GiaNeighborInfo info = {node.getCapacity(),
92 // node.getConnectionDegree(),
93 // NULL}
94 // neighbors.insert(std::make_pair(node, info));
95 // }
96 
97 
98 void GiaNeighbors::remove(const GiaNode& node)
99 {
100  neighbors.erase(node);
101 }
102 
103 const GiaNode& GiaNeighbors::get(unsigned int i)
104 {
105  assert( getSize() && i <= getSize() );
106  NeighborsIterator it = neighbors.begin();
107 
108  for(unsigned int j = 0; j < i; j++)
109  it++;
110 
111  if (it != neighbors.end()) return it->first;
113 }
114 
116 {
117  if (node.isUnspecified()) return NULL;
118 
119  NeighborsIterator it = neighbors.find(node);
120 
121  if(it != neighbors.end())
122  return &(it->second);
123  return NULL;
124 }
125 
127 {
129 
130  for(it = neighbors.begin(); it != neighbors.end(); it++)
131  if(it->first.getKey() == key)
132  break;
133 
134  if(it != neighbors.end())
135  return it->first;
137 }
138 
140 {
141  NeighborsIterator it = neighbors.find(node);
142 
143  if(it != neighbors.end())
144  it->second.timestamp = simTime();
145 }
146 
148 {
149  NeighborsIterator it = neighbors.begin();
150 
151  while(it != neighbors.end()) {
152  if(simTime() > (it->second.timestamp + timeout)) {
153  neighbors.erase(it);
154  it = neighbors.begin();//not efficient
155  }
156  else
157  it++;
158  }
159 
160 }
161 
162 //TODO keyList pointer
164  const GiaKeyList& keyList)
165 {
166  NeighborsIterator it = neighbors.find(node);
167 
168  if(it != neighbors.end())
169  it->second.keyList = keyList;
170 }
171 
173 {
174  NeighborsIterator it = neighbors.find(node);
175 
176  if(it != neighbors.end())
177  return &(it->second.keyList);
178  return NULL;
179 }
180 
181 double GiaNeighbors::getCapacity(const GiaNode& node) const
182 {
183  NeighborsConstIterator it = neighbors.find(node);
184 
185  if(it != neighbors.end())
186  return it->first.getCapacity();
187  return 0;
188 }
189 
190 // void GiaNeighbors::setCapacity(const GiaNode& node, double capacity)
191 // {
192 // NeighborsIterator it = neighbors.find(node);
193 
194 // if(it != neighbors.end())
195 // it->first.setCapacity(capacity);
196 // }
197 
198 unsigned int GiaNeighbors::getConnectionDegree(const GiaNode& node) const
199 {
200  NeighborsConstIterator it = neighbors.find(node);
201 
202  if(it != neighbors.end())
203  return it->second.connectionDegree;
204  return 0;
205 }
206 
208  unsigned int degree)
209 {
210  NeighborsIterator it = neighbors.find(node);
211 
212  if(it != neighbors.end())
213  it->second.connectionDegree = degree;
214 }
215 
217  unsigned int tokens)
218 {
219  NeighborsIterator it = neighbors.find(node);
220 
221  if(it != neighbors.end()) {
222  std::cout << "recieved: " << it->second.receivedTokens << " -> " << tokens << std::endl;
223  it->second.receivedTokens = tokens;
224  }
225 }
226 
228 {
229  NeighborsIterator it = neighbors.find(node);
230 
231  if(it != neighbors.end())
232  it->second.receivedTokens++;
233 }
234 
236 {
237  NeighborsIterator it = neighbors.find(node);
238 
239  if(it != neighbors.end())
240  it->second.receivedTokens--;
241 }
242 
243 unsigned int GiaNeighbors::getReceivedTokens(const GiaNode& node) const
244 {
245  NeighborsConstIterator it = neighbors.find(node);
246 
247  if(it != neighbors.end())
248  return it->second.receivedTokens;
249  return 0;
250 }
251 
252 
253 void GiaNeighbors::setSentTokens(const GiaNode& node, unsigned int tokens)
254 {
255  NeighborsIterator it = neighbors.find(node);
256 
257  if(it != neighbors.end()) {
258  std::cout << "sent: " << it->second.sentTokens << " -> " << tokens << std::endl;
259  it->second.sentTokens = tokens;
260  }
261 }
262 
264 {
265  NeighborsIterator it = neighbors.find(node);
266 
267  if(it != neighbors.end() && it->second.sentTokens >= 0)
268  it->second.sentTokens++;
269 }
270 
271 unsigned int GiaNeighbors::getSentTokens(const GiaNode& node) const
272 {
273  NeighborsConstIterator it = neighbors.find(node);
274 
275  if(it != neighbors.end())
276  return it->second.sentTokens;
277  return 0;
278 }
279 
281  unsigned int degree) const
282 {
283  // determine node with highest capacity
284  unsigned int subset = 0;
285  double maxCapacity = 0;
286  unsigned int dropDegree = 0;
287  GiaNode dropCandidate;
288 
289  NeighborsConstIterator it, candIt;
290  for(it = neighbors.begin(); it != neighbors.end(); it++) {
291  if(it->first.getCapacity() <= capacity) {
292  subset++;
293  if(it->first.getCapacity() > maxCapacity) {
294  candIt = it;
295  dropDegree = it->second.connectionDegree;
296  maxCapacity = it->first.getCapacity();
297  }
298  }
299  }
300 
301  if(subset > 0 &&
302  (/*subset == neighbors->getSize() || */dropDegree > degree) &&
303  dropDegree > 1) {
304  return candIt->first;
305  }
306 
308  }
309 
310 std::ostream& operator<<(std::ostream& os, const GiaNeighborInfo& info)
311 {
312  os //<< "C: " << info.capacity
313  << ", degree: " << info.connectionDegree
314  << ", rTokens " << info.receivedTokens
315  << ", sTokens " << info.sentTokens
316  << ", tStamp: " << info.timestamp;
317  return os;
318 }