OverSim
PastryStateObject.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 <InitStages.h>
25 
26 #include "PastryStateObject.h"
27 #include "PastryTypes.h"
28 
29 
31 
32 
34 {
35  return MAX_STAGE_OVERLAY;
36 }
37 
38 
40 {
41  if (stage != MIN_STAGE_OVERLAY)
42  return;
43  earlyInit();
44 }
45 
46 
48 {
49  throw cRuntimeError("A PastryStateObject should never receive a message!");
50 }
51 
52 
54  destination)
55 {
57 }
58 
59 
62  prox)
63 {
65 }
66 
67 
69  const PastryStateMsgProximity* prox)
70 {
71  bool ret = false;
72  int lsSize = msg->getLeafSetArraySize();
73  int rtSize = msg->getRoutingTableArraySize();
74  int nsSize = msg->getNeighborhoodSetArraySize();
75  const NodeHandle* node;
76  simtime_t rtt;
77 
78  // walk through msg's LeafSet
79  for (int i = 0; i < lsSize; i++) {
80  node = &(msg->getLeafSet(i));
81  rtt = prox ? (*(prox->pr_ls.begin() + i)) : SimTime::getMaxTime();
82 
83  // unspecified nodes, own node and dead nodes not considered
84  if (!(rtt < 0 || node->isUnspecified() || *node == owner ||
85  static_cast<TransportAddress>(*node) == owner)) {
86  if (mergeNode(*node, rtt)) ret = true;
87  }
88  }
89 
90  // walk through msg's IRoutingTable
91  for (int i = 0; i < rtSize; i++) {
92  node = &(msg->getRoutingTable(i));
93  rtt = prox ? (*(prox->pr_rt.begin() + i)) : SimTime::getMaxTime();
94 
95  // unspecified nodes, own node and dead nodes not considered
96  if (!(rtt < 0 || node->isUnspecified() || *node == owner ||
97  static_cast<TransportAddress>(*node) == owner)) {
98  if (mergeNode(*node, rtt)) ret = true;
99  }
100  }
101 
102  // walk through msg's NeighborhoodSet
103  for (int i = 0; i < nsSize; i++) {
104  node = &(msg->getNeighborhoodSet(i));
105  rtt = prox ? (*(prox->pr_ns.begin() + i)) : SimTime::getMaxTime();
106 
107  // unspecified nodes, own node and dead nodes not considered
108  if (!(rtt < 0 || node->isUnspecified() || *node == owner ||
109  static_cast<TransportAddress>(*node) == owner)) {
110  if (mergeNode(*node, rtt)) ret = true;
111  }
112  }
113 
114  return ret;
115 }
116 
117 
119  const OverlayKey& b) const
120 {
121  const OverlayKey* smaller;
122  const OverlayKey* bigger;
123 
124  if (a > b) {
125  smaller = &b;
126  bigger = &a;
127  } else {
128  smaller = &a;
129  bigger = &b;
130  }
131 
132  OverlayKey diff1(*bigger - *smaller);
133  OverlayKey diff2(*smaller + (OverlayKey::getMax() - *bigger) + 1);
134 
135  const OverlayKey* dist;
136  if (diff1 > diff2) {
137  dist = new OverlayKey(diff2);
138  } else {
139  dist = new OverlayKey(diff1);
140  }
141 
142  return dist;
143 }
144 
145 
147  const OverlayKey& destination,
148  const NodeHandle& reference) const
149 {
150  const NodeHandle* ref = &reference;
151  if (ref->isUnspecified()) ref = &owner;
152 
153  if ((ref->getKey() == destination) || (test == *ref)) {
154  return false;
155  }
156 
157  bool closer = false;
158  const OverlayKey* refDist = keyDist(ref->getKey(), destination);
159  const OverlayKey* testDist = keyDist(test.getKey(), destination);
160  if (*testDist < *refDist)
161  closer = true;
162  delete refDist;
163  delete testDist;
164  return closer;
165 }
166 
167 
169  const OverlayKey& destination,
170  const NodeHandle& reference)
171  const
172 {
173  if (test.getKey().sharedPrefixLength(destination, bitsPerDigit)
174  < owner.getKey().sharedPrefixLength(destination, bitsPerDigit)) {
175  return false;
176  }
177 
178  return isCloser(test, destination, reference);
179 }