|
INET Framework for OMNeT++/OMNEST
|
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 <algorithm> // min,max 00019 #include "LineSegmentsMobilityBase.h" 00020 #include "FWMath.h" 00021 00022 00023 void LineSegmentsMobilityBase::initialize(int stage) 00024 { 00025 BasicMobility::initialize(stage); 00026 00027 if (stage == 1) 00028 { 00029 updateInterval = par("updateInterval"); 00030 stationary = false; 00031 targetPos = pos; 00032 targetTime = simTime(); 00033 00034 // host moves the first time after some random delay to avoid synchronized movements 00035 scheduleAt(simTime() + uniform(0, updateInterval), new cMessage("move")); 00036 } 00037 } 00038 00039 void LineSegmentsMobilityBase::beginNextMove(cMessage *msg) 00040 { 00041 // go to exact position where previous statement was supposed to finish 00042 pos = targetPos; 00043 simtime_t now = targetTime; 00044 00045 // choose new targetTime and targetPos 00046 setTargetPosition(); 00047 00048 if (targetTime<now) 00049 error("LineSegmentsMobilityBase: targetTime<now was set in %s's beginNextMove()", getClassName()); 00050 00051 if (stationary) 00052 { 00053 // end of movement 00054 step.x = step.y = 0; 00055 delete msg; 00056 } 00057 else if (targetPos==pos) 00058 { 00059 // no movement, just wait 00060 step.x = step.y = 0; 00061 scheduleAt(std::max(targetTime,simTime()), msg); 00062 } 00063 else 00064 { 00065 // keep moving 00066 double numIntervals = SIMTIME_DBL(targetTime-now) / updateInterval; 00067 // int numSteps = floor(numIntervals); -- currently unused, 00068 // although we could use step counting instead of comparing 00069 // simTime() to targetTime each step. 00070 00071 // Note: step = speed*updateInterval = distance/time*updateInterval = 00072 // = (targetPos-pos) / (targetTime-now) * updateInterval = 00073 // = (targetPos-pos) / numIntervals 00074 step = (targetPos - pos) / numIntervals; 00075 scheduleAt(simTime() + updateInterval, msg); 00076 } 00077 } 00078 00079 void LineSegmentsMobilityBase::handleSelfMsg(cMessage *msg) 00080 { 00081 if (stationary) 00082 { 00083 delete msg; 00084 return; 00085 } 00086 else if (simTime()+updateInterval >= targetTime) 00087 { 00088 beginNextMove(msg); 00089 } 00090 else 00091 { 00092 scheduleAt(simTime() + updateInterval, msg); 00093 } 00094 00095 // update position 00096 pos += step; 00097 00098 // do something if we reach the wall 00099 fixIfHostGetsOutside(); 00100 00101 //EV << " xpos=" << pos.x << " ypos=" << pos.y << endl; 00102 00103 updatePosition(); 00104 } 00105 00106