BonnMotionMobility.cc

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, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00019 #include "BonnMotionMobility.h"
00020 #include "BonnMotionFileCache.h"
00021 #include "FWMath.h"
00022 
00023 
00024 Define_Module(BonnMotionMobility);
00025 
00026 
00027 void BonnMotionMobility::initialize(int stage)
00028 {
00029     LineSegmentsMobilityBase::initialize(stage);
00030 
00031     debugEV << "initializing BonnMotionMobility stage " << stage << endl;
00032 
00033     if(stage == 0)
00034     {
00035         int nodeId = par("nodeId");
00036         if (nodeId == -1)
00037             nodeId = getParentModule()->getIndex();
00038 
00039         const char *fname = par("traceFile");
00040         const BonnMotionFile *bmFile = BonnMotionFileCache::getInstance()->getFile(fname);
00041 
00042         vecp = bmFile->getLine(nodeId);
00043         if (!vecp)
00044             error("invalid nodeId %d -- no such line in file '%s'", nodeId, fname);
00045         vecpos = 0;
00046 
00047         // obtain initial position
00048         const BonnMotionFile::Line& vec = *vecp;
00049         if (vec.size()>=3){
00050 
00051           move.setStart(Coord(vec[1], vec[2]), vec[0]);
00052 
00053       //vecpos += 3;
00054       targetPos = move.getStartPos();
00055       targetTime = simTime();
00056       //stepTarget = move.startPos;
00057 
00058       //dummy value; speed not used in BonnMotion
00059       move.setSpeed(1);
00060       debugEV << "start pos: t=" << move.getStartTime() << move.getStartPos().info() << endl;
00061         }
00062     }
00063     else
00064   {
00065     if(!world->use2D()) {
00066       opp_warning("This mobility module does not yet support 3 dimensional movement."\
00067             "Movements will probably be incorrect.");
00068     }
00069   }
00070 }
00071 
00072 BonnMotionMobility::~BonnMotionMobility()
00073 {
00074     BonnMotionFileCache::deleteInstance();
00075 }
00076 
00077 void BonnMotionMobility::setTargetPosition()
00078 {
00079     const BonnMotionFile::Line& vec = *vecp;
00080 
00081     if (vecpos+2 >= vec.size())
00082     {
00083   move.setSpeed(0);
00084   debugEV << "host is stationary now!!!\n";
00085         return;
00086     }
00087 
00088     targetTime = vec[vecpos];
00089     targetPos.setX(vec[vecpos+1]);
00090     targetPos.setY(vec[vecpos+2]);
00091     vecpos += 3;
00092 
00093     debugEV << "TARGET: t=" << targetTime << targetPos.info() << "\n";
00094 }
00095 
00096 void BonnMotionMobility::fixIfHostGetsOutside()
00097 {
00098     Coord dummy(world->use2D());
00099     double dum;
00100 
00101     handleIfOutside( RAISEERROR, stepTarget, dummy, dummy, dum );
00102 }
00103