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 "SimpleNcs.h"
00025
00026 #include <SHA1.h>
00027 #include <GlobalNodeListAccess.h>
00028 #include <OverlayAccess.h>
00029 #include <SimpleNodeEntry.h>
00030 #include <SimpleInfo.h>
00031
00032 std::string SimpleNcs::delayFaultTypeString;
00033 std::map<std::string, SimpleNcs::delayFaultTypeNum> SimpleNcs::delayFaultTypeMap;
00034 bool SimpleNcs::faultyDelay;
00035
00036 void SimpleNcs::init(NeighborCache* neighborCache)
00037 {
00038 delayFaultTypeMap["live_all"] = delayFaultLiveAll;
00039 delayFaultTypeMap["live_planetlab"] = delayFaultLivePlanetlab;
00040 delayFaultTypeMap["simulation"] = delayFaultSimulation;
00041
00042 this->neighborCache = neighborCache;
00043
00044 delayFaultTypeString =
00045 neighborCache->par("simpleNcsDelayFaultType").stdstringValue();
00046
00047 switch (delayFaultTypeMap[delayFaultTypeString]) {
00048 case SimpleNcs::delayFaultLiveAll:
00049 case SimpleNcs::delayFaultLivePlanetlab:
00050 case SimpleNcs::delayFaultSimulation:
00051 faultyDelay = true;
00052 break;
00053 default:
00054 faultyDelay = false;
00055 }
00056
00057 PeerInfo* peerInfo =
00058 GlobalNodeListAccess().get()
00059 ->getPeerInfo(this->neighborCache->getOverlayThisNode().getIp());
00060
00061 if(peerInfo == NULL) {
00062 throw cRuntimeError("No PeerInfo Found");
00063 }
00064
00065 SimpleNodeEntry* entry = dynamic_cast<SimpleInfo*>(peerInfo)->getEntry();
00066
00067 SimpleCoordsInfo::setDimension(entry->getDim());
00068 ownCoords = new SimpleCoordsInfo();
00069
00070 for (uint8_t i = 0; i < entry->getDim(); i++) {
00071 ownCoords->setCoords(i, entry->getCoords(i) * 0.001);
00072 }
00073 ownCoords->setAccessDelay((entry->getRxAccessDelay() +
00074 800 / entry->getRxBandwidth() +
00075 entry->getTxAccessDelay() +
00076 800 / entry->getTxBandwidth()) / 2.0);
00077 }
00078
00079 AbstractNcsNodeInfo* SimpleNcs::createNcsInfo(const std::vector<double>& coords) const
00080 {
00081 assert(coords.size() > 1);
00082 SimpleCoordsInfo* info = new SimpleCoordsInfo();
00083
00084 uint8_t i;
00085 for (i = 0; i < coords.size() - 1; ++i) {
00086 info->setCoords(i, coords[i]);
00087 }
00088 info->setAccessDelay(coords[i]);
00089
00090 return info;
00091 }
00092
00093
00094 Prox SimpleNcs::getCoordinateBasedProx(const AbstractNcsNodeInfo& abstractInfo) const
00095 {
00096 if (faultyDelay) {
00097 return falsifyDelay(ownCoords->getDistance(abstractInfo) );
00098 }
00099 return ownCoords->getDistance(abstractInfo) ;
00100 }
00101
00102 simtime_t SimpleNcs::falsifyDelay(simtime_t oldDelay) const {
00103
00104
00105 char delaystring[35];
00106 sprintf(delaystring, "%.30f", SIMTIME_DBL(oldDelay));
00107
00108 CSHA1 sha1;
00109 uint8_t hashOverDelays[20];
00110 sha1.Reset();
00111 sha1.Update((uint8_t*)delaystring, 32);
00112 sha1.Final();
00113 sha1.GetHash(hashOverDelays);
00114
00115
00116 uint32_t decimalhash = 0;
00117 for (int i = 0; i < 4; i++) {
00118 decimalhash += (unsigned int) hashOverDelays[i] *
00119 (2 << (8 * (3 - i) - 1));
00120 }
00121
00122
00123 double fraction = (double) decimalhash / (uint32_t) ((2 << 31) - 1);
00124
00125
00126 char sign = (decimalhash % 2 == 0) ? 1 : -1;
00127
00128
00129
00130 double errorRatio = 0;
00131
00132 switch (delayFaultTypeMap[delayFaultTypeString]) {
00133 case delayFaultLiveAll:
00134
00135 errorRatio = pow((1.0 - pow(fraction, 1.0 / 14.0)), 1.0 / 2.03) + 0.04;
00136 break;
00137
00138 case delayFaultLivePlanetlab:
00139
00140 errorRatio = pow((1.0 - pow(fraction, 1.0 / 50.0)), 1.0 / 1.95) + 0.105;
00141 break;
00142
00143 case delayFaultSimulation:
00144
00145 errorRatio = pow((1.0 - pow(fraction, 1.0 / 23.0)), 1.0 / 1.96) + 0.02;
00146
00147 break;
00148
00149 default:
00150 break;
00151 }
00152
00153
00154 errorRatio = (sign == -1 && errorRatio > 0.6) ? 0.6 : errorRatio;
00155
00156 return oldDelay + sign * errorRatio * oldDelay;
00157 }
00158
00159