SimpleNcs.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2010 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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) /* + Rx */);
00098     }
00099     return ownCoords->getDistance(abstractInfo) /* + Rx */;
00100 }
00101 
00102 simtime_t SimpleNcs::falsifyDelay(simtime_t oldDelay) const {
00103 
00104     // hash over string of oldDelay
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     // get the hash's first 4 bytes == 32 bits as one unsigned integer
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     // normalize decimal hash value onto 0..1 (decimal number / 2^32-1)
00123     double fraction = (double) decimalhash / (uint32_t) ((2 << 31) - 1);
00124 
00125     // flip a coin if faulty rtt is larger or smaller
00126     char sign = (decimalhash % 2 == 0) ? 1 : -1;
00127 
00128     // get the error ratio according to the distributions in
00129     // "Network Coordinates in the Wild", Figure 7
00130     double errorRatio = 0;
00131 
00132     switch (delayFaultTypeMap[delayFaultTypeString]) {
00133         case delayFaultLiveAll:
00134             // Kumaraswamy, a=2.03, b=14, moved by 0.04 to the right
00135             errorRatio = pow((1.0 - pow(fraction, 1.0 / 14.0)), 1.0 / 2.03) + 0.04;
00136             break;
00137 
00138         case delayFaultLivePlanetlab:
00139             // Kumaraswamy, a=1.95, b=50, moved by 0.105 to the right
00140             errorRatio = pow((1.0 - pow(fraction, 1.0 / 50.0)), 1.0 / 1.95) + 0.105;
00141             break;
00142 
00143         case delayFaultSimulation:
00144             // Kumaraswamy, a=1.96, b=23, moved by 0.02 to the right
00145             errorRatio = pow((1.0 - pow(fraction, 1.0 / 23.0)), 1.0 / 1.96) + 0.02;
00146             //std::cout << "ErrorRatio: " << errorRatio << std::endl;
00147             break;
00148 
00149         default:
00150             break;
00151     }
00152 
00153     // If faulty rtt is smaller, set errorRatio to max 0.6
00154     errorRatio = (sign == -1 && errorRatio > 0.6) ? 0.6 : errorRatio;
00155 
00156     return oldDelay + sign * errorRatio * oldDelay;
00157 }
00158 
00159