INET Framework for OMNeT++/OMNEST
BasicMobility Class Reference

Abstract base class for all mobility modules. More...

#include <BasicMobility.h>

Inheritance diagram for BasicMobility:
BasicModule INotifiable CircleMobility ConstSpeedMobility LinearMobility LineSegmentsMobilityBase MassMobility NullMobility RectangleMobility ANSimMobility BonnMotionMobility RandomWPMobility TurtleMobility

List of all members.

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.

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

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


Member Function Documentation

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

Delete dynamically allocated objects.

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

Returns the width of the playground.

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

{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().

{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().

{
    Coord p;
    p.x = uniform(0, cc->getPgs()->x);
    p.y = uniform(0, cc->getPgs()->y);
    return p;
}
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().

{
    switch (policy)
    {
        case REFLECT:       reflectIfOutside(targetPos, step, angle); break;
        case WRAP:          wrapIfOutside(targetPos); break;
        case PLACERANDOMLY: placeRandomlyIfOutside(targetPos); break;
        case RAISEERROR:    raiseErrorIfOutside(); break;
    }
}
void BasicMobility::handleMessage ( cMessage *  msg) [protected, virtual]

This modules should only receive self-messages.

{
    if (!msg->isSelfMessage())
        error("mobility modules can only receive self messages");

    handleSelfMsg(msg);
}
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::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.

{
    BasicModule::initialize(stage);

    EV << "initializing BasicMobility stage " << stage << endl;

    if (stage == 0)
    {
        cc = ChannelControl::get();

        // get a pointer to the host
        hostPtr = findHost();
        myHostRef = cc->registerHost(hostPtr, Coord());
    }
    else if (stage == 1)
    {
        // playground size gets set up by ChannelControl in stage==0 (Andras)
        // read the playgroundsize from ChannelControl
        Coord pgs = cc->getPgs();

        // reading the coordinates from omnetpp.ini makes predefined scenarios a lot easier
        // -1 indicates start at display string position, or random position if it's not present
        pos.x = pos.y = -1;
        if (hasPar("x"))   // not all mobility models have an "x" parameter
            pos.x = par("x");
        if (pos.x == -1)
            pos.x = parseInt(hostPtr->getDisplayString().getTagArg("p",0), -1);
        if (pos.x == -1)
            pos.x = uniform(0, pgs.x);

        if (hasPar("y")) // not all mobility models have an "y" parameter
            pos.y = par("y");
        if (pos.y == -1)
            pos.y = parseInt(hostPtr->getDisplayString().getTagArg("p",1), -1);
        if (pos.y == -1)
            pos.y = uniform(0, pgs.y);

        // check validity of position
        if (pos.x < 0 || pos.y < 0 || pos.x >= pgs.x || pos.y >= pgs.y)
            error("node position (%d,%d) is outside the playground", pos.x, pos.y);

        // adjusting the display string is no longer needed (Andras)

        // print new host position on the screen and update bb info
        updatePosition();
    }
}
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().

{
    if (pos.x<0 || pos.x>=getPlaygroundSizeX() || pos.y<0 || pos.y>=getPlaygroundSizeY())
    {
        Coord newPos = getRandomPosition();
        targetPos += newPos - pos;
        pos = newPos;
    }
}
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().

{
    if (pos.x<0 || pos.x>=getPlaygroundSizeX() || pos.y<0 || pos.y>=getPlaygroundSizeY())
    {
        error("node moved outside the playground of size %gx%g (x=%g,y=%g)",
              getPlaygroundSizeX(), getPlaygroundSizeY(), pos.x, pos.y);
    }
}
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().

{
    if (pos.x < 0)
    {
        pos.x = -pos.x;
        targetPos.x = -targetPos.x;
        step.x = -step.x;
        angle = 180 - angle;
    }
    else if (pos.x >= getPlaygroundSizeX())
    {
        pos.x = 2*getPlaygroundSizeX() - pos.x;
        targetPos.x = 2*getPlaygroundSizeX() - targetPos.x;
        step.x = -step.x;
        angle = 180 - angle;
    }
    if (pos.y < 0)
    {
        pos.y = -pos.y;
        targetPos.y = -targetPos.y;
        step.y = -step.y;
        angle = -angle;
    }
    else if (pos.y >= getPlaygroundSizeY())
    {
        pos.y = 2*getPlaygroundSizeY() - pos.y;
        targetPos.y = 2*getPlaygroundSizeY() - targetPos.y;
        step.y = -step.y;
        angle = -angle;
    }
}
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().

{
    cc->updateHostPosition(myHostRef, pos);

    if (ev.isGUI())
    {
        double r = cc->getCommunicationRange(myHostRef);
        hostPtr->getDisplayString().setTagArg("p", 0, (long) pos.x);
        hostPtr->getDisplayString().setTagArg("p", 1, (long) pos.y);
        hostPtr->getDisplayString().setTagArg("r", 0, (long) r);
    }
    nb->fireChangeNotification(NF_HOSTPOSITION_UPDATED, &pos);
}
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().

{
    if (pos.x < 0)
    {
        pos.x += getPlaygroundSizeX();
        targetPos.x += getPlaygroundSizeX();
    }
    else if (pos.x >= getPlaygroundSizeX())
    {
        pos.x -= getPlaygroundSizeX();
        targetPos.x -= getPlaygroundSizeX();
    }
    if (pos.y < 0)
    {
        pos.y += getPlaygroundSizeY();
        targetPos.y += getPlaygroundSizeY();
    }
    else if (pos.y >= getPlaygroundSizeY())
    {
        pos.y -= getPlaygroundSizeY();
        targetPos.y -= getPlaygroundSizeY();
    }
}

Member Data Documentation

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

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

cModule* BasicMobility::hostPtr [protected]

Pointer to host module, to speed up repeated access.

Referenced by initialize(), and updatePosition().

Identifies this host in the ChannelControl module.

Referenced by initialize(), and updatePosition().


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