#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().
{ if (dims == xmlDimensions) { return true; } else { EV << "[CoordBasedRouting::checkDimensions()]" << endl; EV << " ERROR: Given coordinate dimensions do not match dimensions " "in the used area source file. Mapping results will be wrong." << endl; return false; } }
void CoordBasedRouting::finish | ( | ) | [protected] |
Definition at line 65 of file CoordBasedRouting.cc.
{ for (uint32_t i = 0; i < CBRAreaPool.size(); i++) { delete CBRAreaPool[i]; } CBRAreaPool.clear(); }
double CoordBasedRouting::getEuclidianDistanceByKeyAndCoords | ( | const OverlayKey & | destKey, | |
const std::vector< double > & | nodeCoords, | |||
uint8_t | bpd | |||
) | const |
Definition at line 209 of file CoordBasedRouting.cc.
{ assert(!destKey.isUnspecified()); uint32_t iter = 0; while (iter < CBRAreaPool.size()) { CBRArea* thisArea = CBRAreaPool[iter]; // Take CBR Start/Stop Digit into account uint8_t startbit = bpd * cbrStartAtDigit; uint8_t length = (bpd * cbrStopAtDigit - bpd * cbrStartAtDigit < (uint8_t)thisArea->prefix.length() - bpd * cbrStartAtDigit) ? (bpd * cbrStopAtDigit - bpd * cbrStartAtDigit) : (thisArea->prefix.length() - bpd * cbrStartAtDigit); if (destKey.toString(2).substr(startbit, length) == thisArea->prefix.substr(startbit, length)) { // Get euclidian distance of area center to given coords std::vector<double> areaCenterCoords; areaCenterCoords.resize(getXmlDimensions()); double sumofsquares = 0; for (uint8_t dim = 0; dim < getXmlDimensions(); dim++) { areaCenterCoords[dim] = (thisArea->min[dim] + thisArea->max[dim]) / 2; sumofsquares += pow((coords[dim] - areaCenterCoords[dim]), 2); } return sqrt(sumofsquares); } iter++; } // while loop finished -> no area with needed prefix found // (this shouldn't happen!) throw cRuntimeError("[CoordBasedRouting::" "getEuclidianDistanceByKeyAndCoords]: " "No prefix for search key found!"); return -1; }
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().
{ std::string prefix = getPrefix(coords); // if no prefix is returned, something is seriously wrong with the Area Source XML if (prefix == NOPREFIX) { opp_error("[CoordBasedRouting::getNodeId()]: " "No prefix for given coords found. " "Check your area source file!"); } std::string idString; // ID string: // |- endPos // 00000000000000011010101010000000000000 // |_startLength_||_prefix_||_endLength_| // |__ .. beforeEnd .. __| // |___ .... length .... ___| // // startLength and endLength bits are set to 0 at first, then // randomized // Prefix will be cut off if stop digit is exceeded uint8_t startLength = (bpd * cbrStartAtDigit < length) ? (bpd * cbrStartAtDigit) : length; uint8_t beforeEnd = (startLength + prefix.length() < length) ? (startLength + prefix.length()) : length; uint8_t endPos = (bpd * cbrStopAtDigit < beforeEnd) ? (bpd * cbrStopAtDigit) : beforeEnd; uint8_t endLength = length - endPos; // Fill startLength bits with zeros for (uint8_t i = 0; i < startLength; i++) idString += "0"; // Now add prefix and cut it off if stop digit and/or key length is exceeded idString += prefix; if (endPos < idString.length()) idString.erase(endPos); if (length < idString.length()) idString.erase(length); // fill endLength bits with zeros, thus key length is reached for (uint8_t i = 0; i < endLength; i++) idString += "0"; OverlayKey nodeId(idString, 2); // randomize non-prefix (zero filled) parts if (startLength > 0) nodeId = nodeId.randomPrefix(length - startLength); if (endLength > 0) nodeId = nodeId.randomSuffix(endLength); EV << "[CoordBasedRouting::getNodeId()]\n" <<" calculated id: " << nodeId << endl; return nodeId; }
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().
{ bool areaFound = false; uint32_t iter = 0; // Return no prefix if coords dimensions don't match area file dimensions if (!checkDimensions(coords.size())) return NOPREFIX; while (!areaFound && iter < CBRAreaPool.size()) { CBRArea* thisArea = CBRAreaPool[iter]; // assume we're in the correct area unless any dimension tells us otherwise areaFound = true; for (uint8_t thisdim = 0; thisdim < coords.size(); thisdim++) { if (coords[thisdim] < thisArea->min[thisdim] || coords[thisdim] > thisArea->max[thisdim]) { areaFound = false; break; } } // no borders are broken in any dimension -> we're in the correct area, // return corresponding prefix if (areaFound) { EV << "[CoordBasedRouting::getPrefix()]\n" <<" calculated prefix: " << thisArea->prefix << endl; return thisArea->prefix; } iter++; } // no corresponding prefix found, XML file broken? EV << "[CoordBasedRouting::getPrefix()]\n" << " No corresponding prefix found, check your area source file!" << endl; return NOPREFIX; }
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().
{ 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.
{ areaCoordinateSource = par("areaCoordinateSource"); cbrStartAtDigit = par("CBRstartAtDigit"); cbrStopAtDigit = par("CBRstopAtDigit"); globalNodeList = GlobalNodeListAccess().get(); // XML file found? std::ifstream check_for_xml_file(areaCoordinateSource); if (!check_for_xml_file) { check_for_xml_file.close(); throw cRuntimeError("CBR area file not found!"); return; } else { EV << "[CoordBasedRouting::initialize()]\n CBR area file '" << areaCoordinateSource << "' loaded." << endl; check_for_xml_file.close(); } // XML file found, let's parse it parseSource(areaCoordinateSource); }
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().
{ cXMLElement* rootElement = ev.getXMLDocument(areaCoordinateSource); xmlDimensions = atoi(rootElement->getAttribute("dimensions")); for (cXMLElement *area = rootElement->getFirstChildWithTag("area"); area; area = area->getNextSiblingWithTag("area") ) { CBRArea* tmpArea = new CBRArea(xmlDimensions); for (cXMLElement *areavals = area->getFirstChild(); areavals; areavals = areavals->getNextSibling() ) { std::string tagname = std::string(areavals->getTagName()); if (tagname == "min") { uint8_t currentdim = atoi(areavals->getAttribute("dimension")); double value = atof(areavals->getNodeValue()); tmpArea->min[currentdim] = value; } else if (tagname == "max") { uint8_t currentdim = atoi(areavals->getAttribute("dimension")); double value = atof(areavals->getNodeValue()); tmpArea->max[currentdim] = value; } else if (tagname == "prefix") { tmpArea->prefix = areavals->getNodeValue(); } } CBRAreaPool.push_back(tmpArea); } EV << "[CoordBasedRouting::parseSource()]" << endl; EV << " " << CBRAreaPool.size() << " prefix areas detected." << endl; }
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().