BasicMobility Class Reference

#include <BasicMobility.h>

Inheritance diagram for BasicMobility:

BasicModule INotifiable CircleMobility ConstSpeedMobility LinearMobility LineSegmentsMobilityBase MassMobility NullMobility RectangleMobility ANSimMobility BonnMotionMobility RandomWPMobility TurtleMobility

List of all members.


Detailed Description

Abstract base class for all mobility modules.

Subclasses are expected to redefine handleSelfMsg() to update the position and schedule the time of the next position update, and initialize() to read parameters and schedule the first position update.

BasicMobility provides random placement of hosts and display updates as well as registering with the ChannelControl module. Change notifications about position changes are also posted to NotificationBoard.

Author:
Daniel Willkomm, Andras Varga

Public Types

enum  BorderPolicy { REFLECT, WRAP, PLACERANDOMLY, RAISEERROR }

Protected Member Functions

virtual void handleMessage (cMessage *msg)
 This modules should only receive self-messages.
virtual void initialize (int)
 Initializes mobility model parameters.
virtual void finish ()
 Delete dynamically allocated objects.
virtual void handleSelfMsg (cMessage *msg)=0
 Called upon arrival of a self messages.
virtual void updatePosition ()
 Update the position information for this node.
virtual double getPlaygroundSizeX () const
 Returns the width of the playground.
virtual double getPlaygroundSizeY () const
 Returns the height of the playground.
virtual Coord getRandomPosition ()
 Get a new random position for the host.
virtual void reflectIfOutside (Coord &targetPos, Coord &step, double &angle)
 Utility function to reflect the node if it goes outside the playground.
virtual void wrapIfOutside (Coord &targetPos)
 Utility function to wrap the node to the opposite edge (torus) if it goes outside the playground.
virtual void placeRandomlyIfOutside (Coord &targetPos)
 Utility function to place the node randomly if it goes outside the playground.
virtual void raiseErrorIfOutside ()
 Utility function to raise an error if the node gets outside the playground.
virtual void handleIfOutside (BorderPolicy policy, Coord &targetPos, Coord &step, double &angle)
 Invokes one of reflectIfOutside(), wrapIfOutside() and placeRandomlyIfOutside(), depending on the given border policy.

Protected Attributes

ChannelControlcc
 Pointer to ChannelControl -- these two must know each other.
ChannelControl::HostRef myHostRef
 Identifies this host in the ChannelControl module.
cModule * hostPtr
 Pointer to host module, to speed up repeated access.
Coord pos
 Stores the actual position of the host.

Member Enumeration Documentation

Selects how a node should behave if it reaches the edge of the playground.

See also:
handleIfOutside()
Enumerator:
REFLECT  reflect off the wall
WRAP  reappear at the opposite edge (torus)
PLACERANDOMLY  placed at a randomly chosen position on the blackboard
RAISEERROR  stop the simulation with error
00056                       {
00057         REFLECT,       
00058         WRAP,          
00059         PLACERANDOMLY, 
00060         RAISEERROR     
00061     };


Member Function Documentation

void BasicMobility::handleMessage ( cMessage *  msg  )  [protected, virtual]

This modules should only receive self-messages.

00088 {
00089     if (!msg->isSelfMessage())
00090         error("mobility modules can only receive self messages");
00091 
00092     handleSelfMsg(msg);
00093 }

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

Initializes mobility model parameters.

Reimplemented from BasicModule.

Reimplemented in ANSimMobility, BonnMotionMobility, CircleMobility, ConstSpeedMobility, LinearMobility, LineSegmentsMobilityBase, MassMobility, RandomWPMobility, RectangleMobility, and TurtleMobility.

Referenced by RectangleMobility::initialize(), MassMobility::initialize(), LineSegmentsMobilityBase::initialize(), LinearMobility::initialize(), ConstSpeedMobility::initialize(), and CircleMobility::initialize().

00040 {
00041     BasicModule::initialize(stage);
00042 
00043     EV << "initializing BasicMobility stage " << stage << endl;
00044 
00045     if (stage == 0)
00046     {
00047         cc = ChannelControl::get();
00048 
00049         // get a pointer to the host
00050         hostPtr = findHost();
00051         myHostRef = cc->registerHost(hostPtr, Coord());
00052     }
00053     else if (stage == 1)
00054     {
00055         // playground size gets set up by ChannelControl in stage==0 (Andras)
00056         // read the playgroundsize from ChannelControl
00057         Coord pgs = cc->getPgs();
00058 
00059         // reading the coordinates from omnetpp.ini makes predefined scenarios a lot easier
00060         // -1 indicates start at display string position, or random position if it's not present
00061         pos.x = pos.y = -1;
00062         if (hasPar("x"))   // not all mobility models have an "x" parameter
00063             pos.x = par("x");
00064         if (pos.x == -1)
00065             pos.x = parseInt(hostPtr->getDisplayString().getTagArg("p",0), -1);
00066         if (pos.x == -1)
00067             pos.x = uniform(0, pgs.x);
00068 
00069         if (hasPar("y")) // not all mobility models have an "y" parameter
00070             pos.y = par("y");
00071         if (pos.y == -1)
00072             pos.y = parseInt(hostPtr->getDisplayString().getTagArg("p",1), -1);
00073         if (pos.y == -1)
00074             pos.y = uniform(0, pgs.y);
00075 
00076         // check validity of position
00077         if (pos.x < 0 || pos.y < 0 || pos.x >= pgs.x || pos.y >= pgs.y)
00078             error("node position (%d,%d) is outside the playground", pos.x, pos.y);
00079 
00080         // adjusting the display string is no longer needed (Andras)
00081 
00082         // print new host position on the screen and update bb info
00083         updatePosition();
00084     }
00085 }

virtual void BasicMobility::finish (  )  [inline, protected, virtual]

Delete dynamically allocated objects.

00084 {}

virtual void BasicMobility::handleSelfMsg ( cMessage *  msg  )  [protected, pure virtual]

Called upon arrival of a self messages.

Implemented in CircleMobility, ConstSpeedMobility, LinearMobility, LineSegmentsMobilityBase, MassMobility, NullMobility, and RectangleMobility.

Referenced by handleMessage().

void BasicMobility::updatePosition (  )  [protected, virtual]

Update the position information for this node.

This function tells NotificationBoard that the position has changed, and it also moves the host's icon to the new position on the screen.

This function has to be called every time the position of the host changes!

Referenced by RectangleMobility::handleSelfMsg(), MassMobility::handleSelfMsg(), LineSegmentsMobilityBase::handleSelfMsg(), LinearMobility::handleSelfMsg(), ConstSpeedMobility::handleSelfMsg(), CircleMobility::handleSelfMsg(), TurtleMobility::initialize(), RectangleMobility::initialize(), CircleMobility::initialize(), BonnMotionMobility::initialize(), initialize(), and ANSimMobility::initialize().

00097 {
00098     cc->updateHostPosition(myHostRef, pos);
00099 
00100     if (ev.isGUI())
00101     { 
00102         double r = cc->getCommunicationRange(myHostRef);
00103         hostPtr->getDisplayString().setTagArg("p", 0, (long) pos.x);
00104         hostPtr->getDisplayString().setTagArg("p", 1, (long) pos.y);
00105         hostPtr->getDisplayString().setTagArg("r", 0, (long) r);
00106     }
00107     nb->fireChangeNotification(NF_HOSTPOSITION_UPDATED, &pos);
00108 }

virtual double BasicMobility::getPlaygroundSizeX (  )  const [inline, protected, virtual]

Returns the width of the playground.

Referenced by TurtleMobility::getValue(), placeRandomlyIfOutside(), raiseErrorIfOutside(), reflectIfOutside(), and wrapIfOutside().

00101 {return cc->getPgs()->x;}

virtual double BasicMobility::getPlaygroundSizeY (  )  const [inline, protected, virtual]

Returns the height of the playground.

Referenced by TurtleMobility::getValue(), placeRandomlyIfOutside(), raiseErrorIfOutside(), reflectIfOutside(), and wrapIfOutside().

00104 {return cc->getPgs()->y;}

Coord BasicMobility::getRandomPosition (  )  [protected, virtual]

Get a new random position for the host.

You can redefine this function if you want to use another calculation

Referenced by placeRandomlyIfOutside(), RandomWPMobility::setTargetPosition(), and ConstSpeedMobility::setTargetPosition().

00116 {
00117     Coord p;
00118     p.x = uniform(0, cc->getPgs()->x);
00119     p.y = uniform(0, cc->getPgs()->y);
00120     return p;
00121 }

void BasicMobility::reflectIfOutside ( Coord targetPos,
Coord step,
double &  angle 
) [protected, virtual]

Utility function to reflect the node if it goes outside the playground.

Decision is made on pos, but the variables passed as args will also be updated. (Pass dummies you don't have some of them).

Referenced by handleIfOutside().

00124 {
00125     if (pos.x < 0)
00126     {
00127         pos.x = -pos.x;
00128         targetPos.x = -targetPos.x;
00129         step.x = -step.x;
00130         angle = 180 - angle;
00131     }
00132     else if (pos.x >= getPlaygroundSizeX())
00133     {
00134         pos.x = 2*getPlaygroundSizeX() - pos.x;
00135         targetPos.x = 2*getPlaygroundSizeX() - targetPos.x;
00136         step.x = -step.x;
00137         angle = 180 - angle;
00138     }
00139     if (pos.y < 0)
00140     {
00141         pos.y = -pos.y;
00142         targetPos.y = -targetPos.y;
00143         step.y = -step.y;
00144         angle = -angle;
00145     }
00146     else if (pos.y >= getPlaygroundSizeY())
00147     {
00148         pos.y = 2*getPlaygroundSizeY() - pos.y;
00149         targetPos.y = 2*getPlaygroundSizeY() - targetPos.y;
00150         step.y = -step.y;
00151         angle = -angle;
00152     }
00153 }

void BasicMobility::wrapIfOutside ( Coord targetPos  )  [protected, virtual]

Utility function to wrap the node to the opposite edge (torus) if it goes outside the playground.

Decision is made on pos, but targetPos will also be updated. (Pass a dummy you don't have it).

Referenced by handleIfOutside().

00156 {
00157     if (pos.x < 0)
00158     {
00159         pos.x += getPlaygroundSizeX();
00160         targetPos.x += getPlaygroundSizeX();
00161     }
00162     else if (pos.x >= getPlaygroundSizeX())
00163     {
00164         pos.x -= getPlaygroundSizeX();
00165         targetPos.x -= getPlaygroundSizeX();
00166     }
00167     if (pos.y < 0)
00168     {
00169         pos.y += getPlaygroundSizeY();
00170         targetPos.y += getPlaygroundSizeY();
00171     }
00172     else if (pos.y >= getPlaygroundSizeY())
00173     {
00174         pos.y -= getPlaygroundSizeY();
00175         targetPos.y -= getPlaygroundSizeY();
00176     }
00177 }

void BasicMobility::placeRandomlyIfOutside ( Coord targetPos  )  [protected, virtual]

Utility function to place the node randomly if it goes outside the playground.

Decision is made on pos, but targetPos will also be updated. (Pass a dummy you don't have it).

Referenced by handleIfOutside().

00180 {
00181     if (pos.x<0 || pos.x>=getPlaygroundSizeX() || pos.y<0 || pos.y>=getPlaygroundSizeY())
00182     {
00183         Coord newPos = getRandomPosition();
00184         targetPos += newPos - pos;
00185         pos = newPos;
00186     }
00187 }

void BasicMobility::raiseErrorIfOutside (  )  [protected, virtual]

Utility function to raise an error if the node gets outside the playground.

Referenced by RandomWPMobility::fixIfHostGetsOutside(), BonnMotionMobility::fixIfHostGetsOutside(), ANSimMobility::fixIfHostGetsOutside(), and handleIfOutside().

00190 {
00191     if (pos.x<0 || pos.x>=getPlaygroundSizeX() || pos.y<0 || pos.y>=getPlaygroundSizeY())
00192     {
00193         error("node moved outside the playground of size %gx%g (x=%g,y=%g)",
00194               getPlaygroundSizeX(), getPlaygroundSizeY(), pos.x, pos.y);
00195     }
00196 }

void BasicMobility::handleIfOutside ( BorderPolicy  policy,
Coord targetPos,
Coord step,
double &  angle 
) [protected, virtual]

Invokes one of reflectIfOutside(), wrapIfOutside() and placeRandomlyIfOutside(), depending on the given border policy.

Referenced by TurtleMobility::fixIfHostGetsOutside(), MassMobility::move(), and LinearMobility::move().

00199 {
00200     switch (policy)
00201     {
00202         case REFLECT:       reflectIfOutside(targetPos, step, angle); break;
00203         case WRAP:          wrapIfOutside(targetPos); break;
00204         case PLACERANDOMLY: placeRandomlyIfOutside(targetPos); break;
00205         case RAISEERROR:    raiseErrorIfOutside(); break;
00206     }
00207 }


Member Data Documentation

Pointer to ChannelControl -- these two must know each other.

Referenced by getRandomPosition(), initialize(), and updatePosition().

Identifies this host in the ChannelControl module.

Referenced by initialize(), and updatePosition().

cModule* BasicMobility::hostPtr [protected]

Pointer to host module, to speed up repeated access.

Referenced by initialize(), and updatePosition().


The documentation for this class was generated from the following files:

Generated on Fri Mar 20 18:51:18 2009 for INET Framework for OMNeT++/OMNEST by  doxygen 1.5.5