#include <CoordBasedRouting.h>
Public Member Functions | |
OverlayKey | getNodeId (const std::vector< double > &coords, uint8_t bpd, uint8_t length) const |
returns a NodeID with given length and prefix according to coords' area. | |
uint8_t | getXmlDimensions () const |
returns the number of dimensions set in the XML file. | |
double | getEuclidianDistanceByKeyAndCoords (const OverlayKey &destKey, const std::vector< double > &nodeCoords, uint8_t bpd) const |
Protected Member Functions | |
virtual void | initialize () |
CBR is a global module, stuff in initialize() is run once Parsing the area source XML is done here. | |
void | finish () |
Private Member Functions | |
void | parseSource (const char *areaCoordinateSource) |
parses the area source XML and puts the resulting areas into CBRAreaPool | |
std::string | getPrefix (const std::vector< double > &coords) const |
auxiliary protected function which returns the NodeID prefix of the given coords' area | |
bool | checkDimensions (uint8_t dims) const |
returns if given coords' dimensions value (from underlay or overlay calculations) is matching the dimensions value in the area source XML. | |
Private Attributes | |
const char * | areaCoordinateSource |
uint8_t | cbrStartAtDigit |
uint8_t | cbrStopAtDigit |
uint8_t | xmlDimensions |
std::vector< CBRArea * > | CBRAreaPool |
GlobalNodeList * | globalNodeList |
Static Private Attributes | |
static const std::string | NOPREFIX = "NOPREFIX" |
Definition at line 50 of file CoordBasedRouting.h.
bool CoordBasedRouting::checkDimensions | ( | uint8_t | dims | ) | const [private] |
returns if given coords' dimensions value (from underlay or overlay calculations) is matching the dimensions value in the area source XML.
This is mandatory for correct mapping results!
Definition at line 248 of file CoordBasedRouting.cc.
Referenced by getPrefix().
00249 { 00250 if (dims == xmlDimensions) { 00251 return true; 00252 } else { 00253 EV << "[CoordBasedRouting::checkDimensions()]" << endl; 00254 EV << " ERROR: Given coordinate dimensions do not match dimensions " 00255 "in the used area source file. Mapping results will be wrong." 00256 << endl; 00257 return false; 00258 } 00259 }
void CoordBasedRouting::finish | ( | ) | [protected] |
Definition at line 65 of file CoordBasedRouting.cc.
00066 { 00067 for (uint32_t i = 0; i < CBRAreaPool.size(); i++) { 00068 delete CBRAreaPool[i]; 00069 } 00070 CBRAreaPool.clear(); 00071 }
double CoordBasedRouting::getEuclidianDistanceByKeyAndCoords | ( | const OverlayKey & | destKey, | |
const std::vector< double > & | nodeCoords, | |||
uint8_t | bpd | |||
) | const |
Definition at line 209 of file CoordBasedRouting.cc.
00212 { 00213 assert(!destKey.isUnspecified()); 00214 uint32_t iter = 0; 00215 while (iter < CBRAreaPool.size()) { 00216 CBRArea* thisArea = CBRAreaPool[iter]; 00217 00218 // Take CBR Start/Stop Digit into account 00219 uint8_t startbit = bpd * cbrStartAtDigit; 00220 uint8_t length = (bpd * cbrStopAtDigit - bpd * cbrStartAtDigit < 00221 (uint8_t)thisArea->prefix.length() - bpd * cbrStartAtDigit) 00222 ? (bpd * cbrStopAtDigit - bpd * cbrStartAtDigit) 00223 : (thisArea->prefix.length() - bpd * cbrStartAtDigit); 00224 if (destKey.toString(2).substr(startbit, length) == 00225 thisArea->prefix.substr(startbit, length)) { 00226 // Get euclidian distance of area center to given coords 00227 std::vector<double> areaCenterCoords; 00228 areaCenterCoords.resize(getXmlDimensions()); 00229 double sumofsquares = 0; 00230 for (uint8_t dim = 0; dim < getXmlDimensions(); dim++) { 00231 areaCenterCoords[dim] = 00232 (thisArea->min[dim] + thisArea->max[dim]) / 2; 00233 sumofsquares += pow((coords[dim] - areaCenterCoords[dim]), 2); 00234 } 00235 return sqrt(sumofsquares); 00236 } 00237 iter++; 00238 } 00239 // while loop finished -> no area with needed prefix found 00240 // (this shouldn't happen!) 00241 throw cRuntimeError("[CoordBasedRouting::" 00242 "getEuclidianDistanceByKeyAndCoords]: " 00243 "No prefix for search key found!"); 00244 return -1; 00245 }
OverlayKey CoordBasedRouting::getNodeId | ( | const std::vector< double > & | coords, | |
uint8_t | bpd, | |||
uint8_t | length | |||
) | const |
returns a NodeID with given length and prefix according to coords' area.
Takes the parameters CBRstartAtDigit and CBRstopAtDigit into account. Non-prefix bits are currently randomized.
Definition at line 109 of file CoordBasedRouting.cc.
Referenced by Nps::coordsReqRpcResponse().
00111 { 00112 std::string prefix = getPrefix(coords); 00113 00114 // if no prefix is returned, something is seriously wrong with the Area Source XML 00115 if (prefix == NOPREFIX) { 00116 opp_error("[CoordBasedRouting::getNodeId()]: " 00117 "No prefix for given coords found. " 00118 "Check your area source file!"); 00119 } 00120 std::string idString; 00121 00122 // ID string: 00123 // |- endPos 00124 // 00000000000000011010101010000000000000 00125 // |_startLength_||_prefix_||_endLength_| 00126 // |__ .. beforeEnd .. __| 00127 // |___ .... length .... ___| 00128 // 00129 // startLength and endLength bits are set to 0 at first, then 00130 // randomized 00131 // Prefix will be cut off if stop digit is exceeded 00132 00133 uint8_t startLength = (bpd * cbrStartAtDigit < length) ? 00134 (bpd * cbrStartAtDigit) : length; 00135 uint8_t beforeEnd = (startLength + prefix.length() < length) ? 00136 (startLength + prefix.length()) : length; 00137 uint8_t endPos = (bpd * cbrStopAtDigit < beforeEnd) ? 00138 (bpd * cbrStopAtDigit) : beforeEnd; 00139 uint8_t endLength = length - endPos; 00140 00141 // Fill startLength bits with zeros 00142 for (uint8_t i = 0; i < startLength; i++) 00143 idString += "0"; 00144 00145 // Now add prefix and cut it off if stop digit and/or key length is exceeded 00146 idString += prefix; 00147 if (endPos < idString.length()) 00148 idString.erase(endPos); 00149 if (length < idString.length()) 00150 idString.erase(length); 00151 00152 // fill endLength bits with zeros, thus key length is reached 00153 for (uint8_t i = 0; i < endLength; i++) 00154 idString += "0"; 00155 00156 OverlayKey nodeId(idString, 2); 00157 00158 // randomize non-prefix (zero filled) parts 00159 if (startLength > 0) 00160 nodeId = nodeId.randomPrefix(length - startLength); 00161 if (endLength > 0) 00162 nodeId = nodeId.randomSuffix(endLength); 00163 00164 EV << "[CoordBasedRouting::getNodeId()]\n" 00165 <<" calculated id: " << nodeId << endl; 00166 return nodeId; 00167 }
std::string CoordBasedRouting::getPrefix | ( | const std::vector< double > & | coords | ) | const [private] |
auxiliary protected function which returns the NodeID prefix of the given coords' area
Definition at line 169 of file CoordBasedRouting.cc.
Referenced by getNodeId().
00170 { 00171 bool areaFound = false; 00172 uint32_t iter = 0; 00173 00174 // Return no prefix if coords dimensions don't match area file dimensions 00175 if (!checkDimensions(coords.size())) 00176 return NOPREFIX; 00177 00178 while (!areaFound && iter < CBRAreaPool.size()) { 00179 CBRArea* thisArea = CBRAreaPool[iter]; 00180 00181 // assume we're in the correct area unless any dimension tells us otherwise 00182 areaFound = true; 00183 for (uint8_t thisdim = 0; thisdim < coords.size(); thisdim++) { 00184 if (coords[thisdim] < thisArea->min[thisdim] || 00185 coords[thisdim] > thisArea->max[thisdim]) { 00186 areaFound = false; 00187 break; 00188 } 00189 } 00190 00191 // no borders are broken in any dimension -> we're in the correct area, 00192 // return corresponding prefix 00193 if (areaFound) { 00194 EV << "[CoordBasedRouting::getPrefix()]\n" 00195 <<" calculated prefix: " << thisArea->prefix << endl; 00196 return thisArea->prefix; 00197 } 00198 iter++; 00199 } 00200 00201 // no corresponding prefix found, XML file broken? 00202 EV << "[CoordBasedRouting::getPrefix()]\n" 00203 << " No corresponding prefix found, check your area source file!" 00204 << endl; 00205 00206 return NOPREFIX; 00207 }
uint8_t CoordBasedRouting::getXmlDimensions | ( | ) | const [inline] |
returns the number of dimensions set in the XML file.
Can be strictly regarded as reference whenever it comes to dimensionality
Definition at line 94 of file CoordBasedRouting.h.
Referenced by getEuclidianDistanceByKeyAndCoords().
00094 { return xmlDimensions; }
void CoordBasedRouting::initialize | ( | ) | [protected, virtual] |
CBR is a global module, stuff in initialize() is run once Parsing the area source XML is done here.
Definition at line 41 of file CoordBasedRouting.cc.
00042 { 00043 areaCoordinateSource = par("areaCoordinateSource"); 00044 cbrStartAtDigit = par("CBRstartAtDigit"); 00045 cbrStopAtDigit = par("CBRstopAtDigit"); 00046 globalNodeList = GlobalNodeListAccess().get(); 00047 00048 // XML file found? 00049 std::ifstream check_for_xml_file(areaCoordinateSource); 00050 if (!check_for_xml_file) { 00051 check_for_xml_file.close(); 00052 throw cRuntimeError("CBR area file not found!"); 00053 return; 00054 } 00055 else { 00056 EV << "[CoordBasedRouting::initialize()]\n CBR area file '" 00057 << areaCoordinateSource << "' loaded." << endl; 00058 check_for_xml_file.close(); 00059 } 00060 00061 // XML file found, let's parse it 00062 parseSource(areaCoordinateSource); 00063 }
void CoordBasedRouting::parseSource | ( | const char * | areaCoordinateSource | ) | [private] |
parses the area source XML and puts the resulting areas into CBRAreaPool
Definition at line 74 of file CoordBasedRouting.cc.
Referenced by initialize().
00075 { 00076 cXMLElement* rootElement = ev.getXMLDocument(areaCoordinateSource); 00077 00078 xmlDimensions = atoi(rootElement->getAttribute("dimensions")); 00079 00080 for (cXMLElement *area = rootElement->getFirstChildWithTag("area"); area; 00081 area = area->getNextSiblingWithTag("area") ) { 00082 CBRArea* tmpArea = new CBRArea(xmlDimensions); 00083 for (cXMLElement *areavals = area->getFirstChild(); areavals; 00084 areavals = areavals->getNextSibling() ) { 00085 std::string tagname = std::string(areavals->getTagName()); 00086 if (tagname == "min") { 00087 uint8_t currentdim = atoi(areavals->getAttribute("dimension")); 00088 double value = atof(areavals->getNodeValue()); 00089 tmpArea->min[currentdim] = value; 00090 } 00091 00092 else if (tagname == "max") { 00093 uint8_t currentdim = atoi(areavals->getAttribute("dimension")); 00094 double value = atof(areavals->getNodeValue()); 00095 tmpArea->max[currentdim] = value; 00096 } 00097 00098 else if (tagname == "prefix") { 00099 tmpArea->prefix = areavals->getNodeValue(); 00100 } 00101 } 00102 CBRAreaPool.push_back(tmpArea); 00103 } 00104 00105 EV << "[CoordBasedRouting::parseSource()]" << endl; 00106 EV << " " << CBRAreaPool.size() << " prefix areas detected." << endl; 00107 }
const char* CoordBasedRouting::areaCoordinateSource [private] |
Definition at line 103 of file CoordBasedRouting.h.
Referenced by initialize().
std::vector<CBRArea*> CoordBasedRouting::CBRAreaPool [private] |
Definition at line 108 of file CoordBasedRouting.h.
Referenced by finish(), getEuclidianDistanceByKeyAndCoords(), getPrefix(), and parseSource().
uint8_t CoordBasedRouting::cbrStartAtDigit [private] |
Definition at line 104 of file CoordBasedRouting.h.
Referenced by getEuclidianDistanceByKeyAndCoords(), getNodeId(), and initialize().
uint8_t CoordBasedRouting::cbrStopAtDigit [private] |
Definition at line 105 of file CoordBasedRouting.h.
Referenced by getEuclidianDistanceByKeyAndCoords(), getNodeId(), and initialize().
GlobalNodeList* CoordBasedRouting::globalNodeList [private] |
Definition at line 109 of file CoordBasedRouting.h.
Referenced by initialize().
const std::string CoordBasedRouting::NOPREFIX = "NOPREFIX" [static, private] |
Definition at line 101 of file CoordBasedRouting.h.
Referenced by getNodeId(), and getPrefix().
uint8_t CoordBasedRouting::xmlDimensions [private] |
Definition at line 106 of file CoordBasedRouting.h.
Referenced by checkDimensions(), getXmlDimensions(), and parseSource().