INET Framework for OMNeT++/OMNEST
ANSimMobility.cc
Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2005 Andras Varga
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, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 #include "ANSimMobility.h"
00019 #include "FWMath.h"
00020 
00021 
00022 Define_Module(ANSimMobility);
00023 
00024 
00025 static cXMLElement *firstChildWithTag(cXMLElement *node, const char *tagname)
00026 {
00027     cXMLElement *child = node->getFirstChild();
00028     while (child && strcmp(child->getTagName(), tagname)!=0)
00029         child = child->getNextSibling();
00030     if (!child)
00031         opp_error("element <%s> has no <%s> child at %s", node->getTagName(), tagname, node->getSourceLocation());
00032     return child;
00033 }
00034 
00035 
00036 void ANSimMobility::initialize(int stage)
00037 {
00038     LineSegmentsMobilityBase::initialize(stage);
00039 
00040     EV << "initializing ANSimMobility stage " << stage << endl;
00041 
00042     if (stage == 1)
00043     {
00044         nodeId = par("nodeId");
00045         if (nodeId == -1)
00046             nodeId = getParentModule()->getIndex();
00047 
00048         // get script: param should point to <simulation> element
00049         cXMLElement *rootElem = par("ansimTrace");
00050         if (strcmp(rootElem->getTagName(),"simulation")!=0)
00051             error("ansimTrace: <simulation> is expected as root element not <%s> at %s",
00052                   rootElem->getTagName(), rootElem->getSourceLocation());
00053         nextPosChange = rootElem->getElementByPath("mobility/position_change");
00054         if (!nextPosChange)
00055             error("element doesn't have <mobility> child or <position_change> grandchild at %s",
00056                   rootElem->getSourceLocation());
00057 
00058         // set initial position;
00059         setTargetPosition();
00060         pos = targetPos;
00061         updatePosition();
00062     }
00063 }
00064 
00065 
00066 void ANSimMobility::setTargetPosition()
00067 {
00068     // find next <position_update> element with matching <node_id> tag (current one also OK)
00069     while (nextPosChange)
00070     {
00071         const char *nodeIdStr = firstChildWithTag(nextPosChange, "node_id")->getNodeValue();
00072         if (nodeIdStr && atoi(nodeIdStr)==nodeId)
00073             break;
00074         nextPosChange = nextPosChange->getNextSibling();
00075     }
00076 
00077     if (!nextPosChange)
00078     {
00079         stationary = true;
00080         return;
00081     }
00082 
00083     // extract data from it
00084     extractDataFrom(nextPosChange);
00085 
00086     // skip this one
00087     nextPosChange = nextPosChange->getNextSibling();
00088 }
00089 
00090 void ANSimMobility::extractDataFrom(cXMLElement *node)
00091 {
00092     // extract data from <position_update> element
00093     // FIXME start_time has to be taken into account too! as pause from prev element's end_time
00094     const char *startTimeStr = firstChildWithTag(node, "start_time")->getNodeValue();
00095     const char *endTimeStr = firstChildWithTag(node, "end_time")->getNodeValue();
00096     cXMLElement *destElem = firstChildWithTag(node, "destination");
00097     const char *xStr = firstChildWithTag(destElem, "xpos")->getNodeValue();
00098     const char *yStr = firstChildWithTag(destElem, "ypos")->getNodeValue();
00099 
00100     if (!endTimeStr || !xStr || !yStr)
00101         error("no content in <end_time>, <destination>/<xpos> or <ypos> element at %s", node->getSourceLocation());
00102 
00103     targetTime = atof(endTimeStr);
00104     targetPos.x = atof(xStr);
00105     targetPos.y = atof(yStr);
00106 }
00107 
00108 void ANSimMobility::fixIfHostGetsOutside()
00109 {
00110     raiseErrorIfOutside();
00111 }
00112