OverSim
SimpleNcs.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2010 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 "SimpleNcs.h"
25 
26 #include <SHA1.h>
27 #include <GlobalNodeListAccess.h>
28 #include <OverlayAccess.h>
29 #include <SimpleNodeEntry.h>
30 #include <SimpleInfo.h>
31 
33 std::map<std::string, SimpleNcs::delayFaultTypeNum> SimpleNcs::delayFaultTypeMap;
35 
36 void SimpleNcs::init(NeighborCache* neighborCache)
37 {
39  delayFaultTypeMap["live_planetlab"] = delayFaultLivePlanetlab;
41 
42  this->neighborCache = neighborCache;
43 
45  neighborCache->par("simpleNcsDelayFaultType").stdstringValue();
46 
51  faultyDelay = true;
52  break;
53  default:
54  faultyDelay = false;
55  }
56 
57  PeerInfo* peerInfo =
59  ->getPeerInfo(this->neighborCache->getOverlayThisNode().getIp());
60 
61  if(peerInfo == NULL) {
62  throw cRuntimeError("No PeerInfo Found");
63  }
64 
65  SimpleNodeEntry* entry = dynamic_cast<SimpleInfo*>(peerInfo)->getEntry();
66 
69 
70  for (uint8_t i = 0; i < entry->getDim(); i++) {
71  ownCoords->setCoords(i, entry->getCoords(i));
72  }
73  ownCoords->setAccessDelay((entry->getRxAccessDelay() +
74  800 / entry->getRxBandwidth() +
75  entry->getTxAccessDelay() +
76  800 / entry->getTxBandwidth()) / 2.0);
77 }
78 
79 AbstractNcsNodeInfo* SimpleNcs::createNcsInfo(const std::vector<double>& coords) const
80 {
81  assert(coords.size() > 1);
82  SimpleCoordsInfo* info = new SimpleCoordsInfo();
83 
84  uint8_t i;
85  for (i = 0; i < coords.size() - 1; ++i) {
86  info->setCoords(i, coords[i]);
87  }
88  info->setAccessDelay(coords[i]);
89 
90  return info;
91 }
92 
93 
95 {
96  if (faultyDelay) {
97  return falsifyDelay(ownCoords->getDistance(abstractInfo) /* + Rx */);
98  }
99  return ownCoords->getDistance(abstractInfo) /* + Rx */;
100 }
101 
102 simtime_t SimpleNcs::falsifyDelay(simtime_t oldDelay) const {
103 
104  // hash over string of oldDelay
105  char delaystring[35];
106  sprintf(delaystring, "%.30f", SIMTIME_DBL(oldDelay));
107 
108  CSHA1 sha1;
109  uint8_t hashOverDelays[20];
110  sha1.Reset();
111  sha1.Update((uint8_t*)delaystring, 32);
112  sha1.Final();
113  sha1.GetHash(hashOverDelays);
114 
115  // get the hash's first 4 bytes == 32 bits as one unsigned integer
116  uint32_t decimalhash = 0;
117  for (int i = 0; i < 4; i++) {
118  decimalhash += (unsigned int) hashOverDelays[i] *
119  (2 << (8 * (3 - i) - 1));
120  }
121 
122  // normalize decimal hash value onto 0..1 (decimal number / 2^32-1)
123  double fraction = (double) decimalhash / (uint32_t) ((2 << 31) - 1);
124 
125  // flip a coin if faulty rtt is larger or smaller
126  char sign = (decimalhash % 2 == 0) ? 1 : -1;
127 
128  // get the error ratio according to the distributions in
129  // "Network Coordinates in the Wild", Figure 7
130  double errorRatio = 0;
131 
133  case delayFaultLiveAll:
134  // Kumaraswamy, a=2.03, b=14, moved by 0.04 to the right
135  errorRatio = pow((1.0 - pow(fraction, 1.0 / 14.0)), 1.0 / 2.03) + 0.04;
136  break;
137 
139  // Kumaraswamy, a=1.95, b=50, moved by 0.105 to the right
140  errorRatio = pow((1.0 - pow(fraction, 1.0 / 50.0)), 1.0 / 1.95) + 0.105;
141  break;
142 
144  // Kumaraswamy, a=1.96, b=23, moved by 0.02 to the right
145  errorRatio = pow((1.0 - pow(fraction, 1.0 / 23.0)), 1.0 / 1.96) + 0.02;
146  //std::cout << "ErrorRatio: " << errorRatio << std::endl;
147  break;
148 
149  default:
150  break;
151  }
152 
153  // If faulty rtt is smaller, set errorRatio to max 0.6
154  errorRatio = (sign == -1 && errorRatio > 0.6) ? 0.6 : errorRatio;
155 
156  return oldDelay + sign * errorRatio * oldDelay;
157 }
158 
159