OverSim
GiaMessageBookkeeping.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 <omnetpp.h>
27 #include "GiaMessageBookkeeping.h"
28 
29 
31 {
32  this->neighbors = neighbors;
33  this->timeout = timeout;
34 }
35 
37 {}
38 
40 {
41  return messages.size();
42 }
43 
45 {
46  assert(!(msg->getID().isUnspecified()));
47 
48  std::vector<GiaNode> remainNodes;
49  // push all neighbors except the node where message was comming from
50  // to remainNodes
51  for ( uint32_t i=0; i<neighbors->getSize(); i++ ) {
52  if ( neighbors->get(i).getKey() != msg->getSrcNode().getKey())
53  remainNodes.push_back(neighbors->get(i));
54  }
55  MessageItem messageItem;
56  messageItem.remainNodes = remainNodes;
57  messageItem.timestamp = simTime();
58  messages[msg->getID()] = messageItem;
59 }
60 
62 {
63  std::map<OverlayKey, MessageItem>::iterator it = messages.find(msg->getID());
64  // delete message if key is equal
65  if ( it->first == msg->getID() )
66  messages.erase(messages.find(msg->getID()));
67 }
68 
70 {
71  std::map<OverlayKey, MessageItem>::iterator it = messages.find(msg->getID());
72 
73  if(it != messages.end())
74  return true;
75  return false;
76 }
77 
79 {
80  if ( neighbors->getSize() > 0 ) {
81  std::map<OverlayKey, MessageItem>::iterator it = messages.find(msg->getID());
82  std::priority_queue<FullGiaNodeInfo, std::vector<FullGiaNodeInfo>, GiaNodeQueueCompare> nodeQueue;
83 
84  if ( it != messages.end() && it->first == msg->getID() ) {
85  MessageItem messageItem = it->second;
86  std::vector<GiaNode> remNodes = messageItem.remainNodes;
87  if ( remNodes.size() == 0) {
88  for ( uint32_t i=0; i<neighbors->getSize(); i++ ) {
89  remNodes.push_back(neighbors->get(i));
90  }
91  }
92 
93  for ( uint32_t i=0; i<remNodes.size(); i++ ) {
94  if(!(remNodes[i].isUnspecified())) {
95  FullGiaNodeInfo temp;
96  temp.node = remNodes[i];
97  temp.info = neighbors->get(temp.node);
98  if (temp.info) nodeQueue.push(temp);
99  }
100  }
101 
102  if (!nodeQueue.empty()) {
103  NodeHandle nextHop = nodeQueue.top().node;
104  GiaNeighborInfo* nextHopInfo = neighbors->get(nextHop);
105  nodeQueue.pop();
106 
107  if (nextHopInfo != NULL && nextHopInfo->receivedTokens > 0 ) {
108  remNodes.clear();
109  while ( !nodeQueue.empty() ) {
110  remNodes.push_back(nodeQueue.top().node);
111  nodeQueue.pop();
112  }
113  messageItem.remainNodes = remNodes;
114  messageItem.timestamp = simTime();
115  messages[msg->getID()] = messageItem;
116  return nextHop;
117  }
118  }
119  }
120  }
122 }
123 
125 {
126  std::map<OverlayKey, MessageItem>::iterator it = messages.begin();
127  std::map<OverlayKey, MessageItem>::iterator it2 = messages.begin();
128  for ( uint32_t i=0; i<messages.size(); i++) {
129  OverlayKey key = it->first;
130  MessageItem messageItem = it->second;
131  it2 = it++;
132  if (simTime() > (messageItem.timestamp + timeout))
133  messages.erase(it2);
134  }
135 }
136 
138  const FullGiaNodeInfo& y)
139 {
140  if (x.info->receivedTokens > y.info->receivedTokens) {
141  if (y.info->receivedTokens == 0)
142  return false;
143  else {
144  if (x.node.getCapacity() >= y.node.getCapacity())
145  return false;
146  else
147  return true;
148  }
149  }
150  else if (x.info->receivedTokens < y.info->receivedTokens) {
151  if (x.info->receivedTokens == 0)
152  return true;
153  else {
154  if (x.node.getCapacity() > y.node.getCapacity())
155  return false;
156  else
157  return true;
158  }
159  }
160  else {
161  if (x.info->receivedTokens == 0)
162  return true;
163  else {
164  if (x.node.getCapacity() > y.node.getCapacity())
165  return false;
166  else
167  return true;
168  }
169  }
170 }