CommunityMov Class Reference

#include <CommunityMov.h>

List of all members.

Protected Member Functions

virtual void initialize (int)
 Initializes mobility model parameters.
virtual void normalize_coordinate (Coord *position)
 translating position to an only positive system
virtual simtime_t extract_stoptime ()
 extracts the stop time of the simulation from the tracefile
virtual void setTargetPosition ()
 sets the next target of the node
virtual void extractDataFrom (cXMLElement *node)
 extract data from given <position_update> element
virtual void fixIfHostGetsOutside ()
 overridden from LineSegmentsMobilityBase
virtual void setInitialPosition (cXMLElement *elem)
 set the initial position of the node

Protected Attributes

int nodeId
 we'll have to compare this to the <node_id> elements
simtime_t sim_stop
 The mobility simulation duration.
cXMLElementnextPosChange
 points to the next <position_change> element
cXMLElementrootElem
 point to the root element of the file

Member Function Documentation

simtime_t CommunityMov::extract_stoptime (  )  [protected, virtual]

extracts the stop time of the simulation from the tracefile

00245 {
00246         // get script: param should point to <simulation> element
00247         cXMLElement *xmlElem = par("MobilityTrace");
00248 
00249         if (strcmp(xmlElem->getTagName(),"simulation")!=0)
00250                 error("MobilityTrace: <simulation> is expected as root element not <%s> at %s", xmlElem->getTagName(), xmlElem->getSourceLocation());
00251 
00252         usleep(10000);
00253 
00254         //Looking for stoptime tag
00255         xmlElem = xmlElem->getElementByPath("statistics");
00256         if (!xmlElem)
00257                 error("element doesn't have <statistics> child at %s", xmlElem->getSourceLocation());
00258 
00259         //Read stoptime value
00260         const char *stoptime = firstChildWithTag(xmlElem, "stoptime")->getNodeValue();
00261 
00262         //Return stoptime as double
00263         return(atof(stoptime));
00264 }

void CommunityMov::extractDataFrom ( cXMLElement node  )  [protected, virtual]

extract data from given <position_update> element

00122 {
00123         double end_time;
00124         double duration;
00125         double imag_duration;
00126         double distance;
00127 
00128     // extract data from <position_update> element
00129     const char *startTimeStr = firstChildWithTag(node, "start_time")->getNodeValue();
00130     cXMLElement *destElem = firstChildWithTag(node, "destination");
00131     const char *xStr = firstChildWithTag(destElem, "xpos")->getNodeValue();
00132     const char *yStr = firstChildWithTag(destElem, "ypos")->getNodeValue();
00133     const char *velocity = firstChildWithTag(node, "velocity")->getNodeValue();
00134 
00135     //If XML file not in correct format give error
00136     if (!velocity || !xStr || !yStr)
00137         error("no content in <velocity>, <destination>/<xpos> or <ypos> element at %s", node->getSourceLocation());
00138 
00139     //Normalize and set goal coordinates and
00140     targetPos.x=atof(xStr);
00141     targetPos.y=atof(yStr);
00142     normalize_coordinate(&targetPos);
00143 
00144     //Calculate the ideal movement distance and duration
00145     distance = sqrt(pow(targetPos.x - pos.x,2)+pow(targetPos.y - pos.y,2));
00146 
00147     //Check if node is paused
00148     if(atof(velocity)!=0)
00149         imag_duration = distance/atof(velocity);
00150     else
00151         imag_duration = sim_stop.dbl();
00152 
00153     //Look for next <position_update> to extract targetTime
00154     cXMLElement *nextElem = node->getNextSibling();
00155     while (nextElem)
00156     {
00157         const char *nodeIdStr = firstChildWithTag(nextElem, "node_id")->getNodeValue();
00158         if (nodeIdStr && atoi(nodeIdStr)==nodeId)
00159             break;
00160         nextElem = nextElem->getNextSibling();
00161     }
00162 
00163     //If this is the last move calculate targetTime only according to given velocity
00164     if (!nextElem)
00165     {
00166         targetTime = imag_duration + atof(startTimeStr);
00167         //Set maximum target at targetTime
00168         if(targetTime>sim_stop) {
00169                 targetTime=sim_stop;
00170         }
00171     }
00172     //else set targetTime the start time of the next movement
00173     else
00174     {
00175         end_time = atof(firstChildWithTag(nextElem, "start_time")->getNodeValue());
00176         targetTime = end_time;
00177         //Calculate the real duration of this movement
00178         duration = end_time - atof(startTimeStr);
00179 
00180         //In case next element wasn't written properly to have start_time
00181         if (!firstChildWithTag(nextElem, "start_time")->getNodeValue())
00182                error("no content in <start_time> at %s", nextElem->getSourceLocation());
00183     }
00184 }

void CommunityMov::fixIfHostGetsOutside (  )  [protected, virtual]

overridden from LineSegmentsMobilityBase

00191 {
00192     raiseErrorIfOutside();
00193 }

void CommunityMov::initialize ( int  stage  )  [protected, virtual]

Initializes mobility model parameters.

00046 {
00047     BasicMobility::initialize(stage);
00048 
00049     EV << "initializing CommunityMov stage " << stage << endl;
00050 
00051     if (stage == 1)
00052     {
00053         //Read ned parameters
00054         updateInterval = par("updateInterval");
00055         stationary = par("stationary");
00056 
00057         //Find simulation duration
00058         sim_stop=extract_stoptime();
00059 
00060         nodeId = par("nodeId");
00061         if (nodeId == -1)
00062             nodeId = getParentModule()->getIndex();
00063 
00064         // get script: param should point to <simulation> element
00065         rootElem = par("MobilityTrace");
00066         //std::cout << rootElem->getTagName() << endl;
00067         if (strcmp(rootElem->getTagName(),"simulation")!=0)
00068             error("MobilityTrace: <simulation> is expected as root element not <%s> at %s", rootElem->getTagName(), rootElem->getSourceLocation());
00069 
00070         //Set initial position
00071         setInitialPosition(rootElem);
00072 
00073         //Looking for first position_change tag
00074         nextPosChange = rootElem->getElementByPath("mobility/position_change");
00075         if (!nextPosChange)
00076             error("element doesn't have <mobility> child or <position_change> grandchild at %s", rootElem->getSourceLocation());
00077 
00078         // host moves for the first time now
00079         beginNextMove(new cMessage("move"));
00080 
00081         WATCH(pos);
00082         WATCH(targetPos);
00083         WATCH(step);
00084     }
00085 }

void CommunityMov::normalize_coordinate ( Coord *  position  )  [protected, virtual]

translating position to an only positive system

00232 {
00233         int playgroundlength = getParentModule()->getParentModule()->par("playgroundSizeX");
00234         int playgroundwidth = getParentModule()->getParentModule()->par("playgroundSizeY");
00235 
00236         position->x+=(playgroundlength/2);
00237         position->y+=(playgroundwidth/2);
00238 }

void CommunityMov::setInitialPosition ( cXMLElement elem  )  [protected, virtual]

set the initial position of the node

00200 {
00201         //Go to <node_settings>/<node> in xml file
00202         nextPosChange = elem->getElementByPath("node_settings/node");
00203         if (!nextPosChange)
00204             error("element doesn't have <node_settings> child or <node> grandchild at %s", elem->getSourceLocation());
00205 
00206         //Find the first child with the specific node_id
00207         while (nextPosChange)
00208         {
00209                 const char *nodeIdStr = firstChildWithTag(nextPosChange, "node_id")->getNodeValue();
00210                 if (nodeIdStr && atoi(nodeIdStr)==nodeId)
00211                         break;
00212                 nextPosChange = nextPosChange->getNextSibling();
00213         }
00214         cXMLElement *posElem = firstChildWithTag(nextPosChange, "position");
00215         pos.x =atof( firstChildWithTag(posElem, "xpos")->getNodeValue() );
00216         pos.y =atof( firstChildWithTag(posElem, "ypos")->getNodeValue() );
00217 
00218         //Translating coordinates to only positive system and saving them to pos variable
00219         normalize_coordinate(&pos);
00220         updatePosition();
00221 
00222         //There is no target - initial position
00223         targetPos.x=pos.x;
00224         targetPos.y=pos.y;
00225 }

void CommunityMov::setTargetPosition (  )  [protected, virtual]

sets the next target of the node

00092 {
00093     // find next <position_update> element with matching <node_id> tag (current one also OK)
00094 
00095     while (nextPosChange)
00096     {
00097         const char *nodeIdStr = firstChildWithTag(nextPosChange, "node_id")->getNodeValue();
00098         if (nodeIdStr && atoi(nodeIdStr)==nodeId)
00099             break;
00100         nextPosChange = nextPosChange->getNextSibling();
00101     }
00102 
00103     if (!nextPosChange)
00104     {
00105         stationary = true;
00106         //par("stationary")=true;
00107         return;
00108     }
00109 
00110     // extract data from it
00111     extractDataFrom(nextPosChange);
00112 
00113     // skip this one
00114     nextPosChange = nextPosChange->getNextSibling();
00115 }


Member Data Documentation

points to the next <position_change> element

int CommunityMov::nodeId [protected]

we'll have to compare this to the <node_id> elements

point to the root element of the file

simtime_t CommunityMov::sim_stop [protected]

The mobility simulation duration.


The documentation for this class was generated from the following files:
Generated on Mon Aug 13 18:29:03 2012 for ECMM by  doxygen 1.6.3