GiaNeighbors.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #include <assert.h>
00025
00026 #include <InitStages.h>
00027
00028 #include "GiaNeighbors.h"
00029
00030
00031 Define_Module(GiaNeighbors);
00032
00033 void GiaNeighbors::initialize(int stage)
00034 {
00035
00036 if(stage != MIN_STAGE_OVERLAY)
00037 return;
00038
00039 WATCH_MAP(neighbors);
00040 timeout = getParentModule()->getSubmodule("gia")->par("neighborTimeout");
00041
00042 }
00043
00044
00045 void GiaNeighbors::handleMessages( cMessage* msg )
00046 {
00047 error("this module doesn't handle messages, it runs only in initialize()");
00048 }
00049
00050 uint32_t GiaNeighbors::getSize() const
00051 {
00052 return neighbors.size();
00053 }
00054
00055 bool GiaNeighbors::contains(const OverlayKey& key) const
00056 {
00057 NeighborsConstIterator it = neighbors.begin();
00058
00059 for(it = neighbors.begin(); it != neighbors.end(); it++)
00060 if(it->first.getKey() == key)
00061 break;
00062
00063 if (it != neighbors.end())
00064 return true;
00065 return false;
00066 }
00067
00068 bool GiaNeighbors::contains(const GiaNode& node) const
00069 {
00070 NeighborsConstIterator it = neighbors.find(node);
00071
00072 if(it != neighbors.end())
00073 return true;
00074 return false;
00075 }
00076
00077 void GiaNeighbors::add(const GiaNode& node, unsigned int degree)
00078 {
00079 GiaNeighborInfo info = {degree,
00080 5,
00081 5,
00082 simTime(),
00083 GiaKeyList()};
00084
00085 neighbors.insert(std::make_pair(node, info));
00086
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 void GiaNeighbors::remove(const GiaNode& node)
00099 {
00100 neighbors.erase(node);
00101 }
00102
00103 const GiaNode& GiaNeighbors::get(unsigned int i)
00104 {
00105 assert( getSize() && i <= getSize() );
00106 NeighborsIterator it = neighbors.begin();
00107
00108 for(unsigned int j = 0; j < i; j++)
00109 it++;
00110
00111 if (it != neighbors.end()) return it->first;
00112 return GiaNode::UNSPECIFIED_NODE;
00113 }
00114
00115 GiaNeighborInfo* GiaNeighbors::get(const GiaNode& node)
00116 {
00117 if (node.isUnspecified()) return NULL;
00118
00119 NeighborsIterator it = neighbors.find(node);
00120
00121 if(it != neighbors.end())
00122 return &(it->second);
00123 return NULL;
00124 }
00125
00126 const GiaNode& GiaNeighbors::get(const OverlayKey& key)
00127 {
00128 NeighborsIterator it;
00129
00130 for(it = neighbors.begin(); it != neighbors.end(); it++)
00131 if(it->first.getKey() == key)
00132 break;
00133
00134 if(it != neighbors.end())
00135 return it->first;
00136 return GiaNode::UNSPECIFIED_NODE;
00137 }
00138
00139 void GiaNeighbors::updateTimestamp(const GiaNode& node)
00140 {
00141 NeighborsIterator it = neighbors.find(node);
00142
00143 if(it != neighbors.end())
00144 it->second.timestamp = simTime();
00145 }
00146
00147 void GiaNeighbors::removeTimedoutNodes()
00148 {
00149 NeighborsIterator it = neighbors.begin();
00150
00151 while(it != neighbors.end()) {
00152 if(simTime() > (it->second.timestamp + timeout)) {
00153 neighbors.erase(it);
00154 it = neighbors.begin();
00155 }
00156 else
00157 it++;
00158 }
00159
00160 }
00161
00162
00163 void GiaNeighbors::setNeighborKeyList(const GiaNode& node,
00164 const GiaKeyList& keyList)
00165 {
00166 NeighborsIterator it = neighbors.find(node);
00167
00168 if(it != neighbors.end())
00169 it->second.keyList = keyList;
00170 }
00171
00172 GiaKeyList* GiaNeighbors::getNeighborKeyList(const GiaNode& node)
00173 {
00174 NeighborsIterator it = neighbors.find(node);
00175
00176 if(it != neighbors.end())
00177 return &(it->second.keyList);
00178 return NULL;
00179 }
00180
00181 double GiaNeighbors::getCapacity(const GiaNode& node) const
00182 {
00183 NeighborsConstIterator it = neighbors.find(node);
00184
00185 if(it != neighbors.end())
00186 return it->first.getCapacity();
00187 return 0;
00188 }
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 unsigned int GiaNeighbors::getConnectionDegree(const GiaNode& node) const
00199 {
00200 NeighborsConstIterator it = neighbors.find(node);
00201
00202 if(it != neighbors.end())
00203 return it->second.connectionDegree;
00204 return 0;
00205 }
00206
00207 void GiaNeighbors::setConnectionDegree(const GiaNode& node,
00208 unsigned int degree)
00209 {
00210 NeighborsIterator it = neighbors.find(node);
00211
00212 if(it != neighbors.end())
00213 it->second.connectionDegree = degree;
00214 }
00215
00216 void GiaNeighbors::setReceivedTokens(const GiaNode& node,
00217 unsigned int tokens)
00218 {
00219 NeighborsIterator it = neighbors.find(node);
00220
00221 if(it != neighbors.end()) {
00222 std::cout << "recieved: " << it->second.receivedTokens << " -> " << tokens << std::endl;
00223 it->second.receivedTokens = tokens;
00224 }
00225 }
00226
00227 void GiaNeighbors::increaseReceivedTokens(const GiaNode& node)
00228 {
00229 NeighborsIterator it = neighbors.find(node);
00230
00231 if(it != neighbors.end())
00232 it->second.receivedTokens++;
00233 }
00234
00235 void GiaNeighbors::decreaseReceivedTokens(const GiaNode& node)
00236 {
00237 NeighborsIterator it = neighbors.find(node);
00238
00239 if(it != neighbors.end())
00240 it->second.receivedTokens--;
00241 }
00242
00243 unsigned int GiaNeighbors::getReceivedTokens(const GiaNode& node) const
00244 {
00245 NeighborsConstIterator it = neighbors.find(node);
00246
00247 if(it != neighbors.end())
00248 return it->second.receivedTokens;
00249 return 0;
00250 }
00251
00252
00253 void GiaNeighbors::setSentTokens(const GiaNode& node, unsigned int tokens)
00254 {
00255 NeighborsIterator it = neighbors.find(node);
00256
00257 if(it != neighbors.end()) {
00258 std::cout << "sent: " << it->second.sentTokens << " -> " << tokens << std::endl;
00259 it->second.sentTokens = tokens;
00260 }
00261 }
00262
00263 void GiaNeighbors::increaseSentTokens(const GiaNode& node)
00264 {
00265 NeighborsIterator it = neighbors.find(node);
00266
00267 if(it != neighbors.end() && it->second.sentTokens >= 0)
00268 it->second.sentTokens++;
00269 }
00270
00271 unsigned int GiaNeighbors::getSentTokens(const GiaNode& node) const
00272 {
00273 NeighborsConstIterator it = neighbors.find(node);
00274
00275 if(it != neighbors.end())
00276 return it->second.sentTokens;
00277 return 0;
00278 }
00279
00280 const GiaNode& GiaNeighbors::getDropCandidate(double capacity,
00281 unsigned int degree) const
00282 {
00283
00284 unsigned int subset = 0;
00285 double maxCapacity = 0;
00286 unsigned int dropDegree = 0;
00287 GiaNode dropCandidate;
00288
00289 NeighborsConstIterator it, candIt;
00290 for(it = neighbors.begin(); it != neighbors.end(); it++) {
00291 if(it->first.getCapacity() <= capacity) {
00292 subset++;
00293 if(it->first.getCapacity() > maxCapacity) {
00294 candIt = it;
00295 dropDegree = it->second.connectionDegree;
00296 maxCapacity = it->first.getCapacity();
00297 }
00298 }
00299 }
00300
00301 if(subset > 0 &&
00302 (dropDegree > degree) &&
00303 dropDegree > 1) {
00304 return candIt->first;
00305 }
00306
00307 return GiaNode::UNSPECIFIED_NODE;
00308 }
00309
00310 std::ostream& operator<<(std::ostream& os, const GiaNeighborInfo& info)
00311 {
00312 os
00313 << ", degree: " << info.connectionDegree
00314 << ", rTokens " << info.receivedTokens
00315 << ", sTokens " << info.sentTokens
00316 << ", tStamp: " << info.timestamp;
00317 return os;
00318 }