45 #include "ns3/simulator.h"
46 #include "ns3/node-list.h"
48 #include "ns3/constant-velocity-mobility-model.h"
57 #define NS2_X_COORD "X_"
58 #define NS2_Y_COORD "Y_"
59 #define NS2_Z_COORD "Z_"
60 #define NS2_SETDEST "setdest"
62 #define NS2_NODEID "$node_("
63 #define NS2_NS_SCH "$ns_"
108 static ParseResult
ParseNs2Line (
const std::string& str);
115 static std::string
TrimNs2Line (
const std::string& str);
122 static bool IsNumber (
const std::string& s);
131 static bool IsVal (
const std::string& str, T& ret);
201 static DestinationPoint
SetMovement (Ptr<ConstantVelocityMobilityModel> model, Vector lastPos,
double at,
202 double xFinalPosition,
double yFinalPosition,
double speed);
211 static Vector
SetInitialPosition (Ptr<ConstantVelocityMobilityModel> model, std::string coord,
double coordVal);
221 static Vector
SetSchedPosition (Ptr<ConstantVelocityMobilityModel> model,
double at, std::string coord,
double coordVal);
225 : m_filename (filename)
234 std::istringstream iss;
246 model = CreateObject<ConstantVelocityMobilityModel> ();
247 object->AggregateObject (model);
256 std::map<int, DestinationPoint> last_pos;
268 while (!
file.eof () )
274 getline (
file, line);
286 if (pr.
tokens.size () != 4)
296 NS_LOG_ERROR (
"Node number couldn't be obtained (corrupted file?): " << line <<
"\n");
306 NS_LOG_ERROR (
"Unknown node ID (corrupted file?): " << nodeId <<
"\n");
320 last_pos[iNodeId] = point;
323 NS_LOG_DEBUG (
"Positions after parse for node " << iNodeId <<
" " << nodeId <<
324 " position = " << last_pos[iNodeId].m_finalPosition);
339 while (!
file.eof () )
345 getline (
file, line);
358 NS_LOG_ERROR (
"Line has not correct number of parameters (corrupted file?): " << line <<
"\n");
367 NS_LOG_ERROR (
"Node number couldn't be obtained (corrupted file?): " << line <<
"\n");
377 NS_LOG_ERROR (
"Unknown node ID (corrupted file?): " << nodeId <<
"\n");
422 if (last_pos[iNodeId].m_targetArrivalTime > at)
424 NS_LOG_LOGIC (
"Did not reach a destination! stoptime = " << last_pos[iNodeId].m_targetArrivalTime <<
", at = "<< at);
425 double actuallytraveled = at - last_pos[iNodeId].m_travelStartTime;
426 Vector reached = Vector (
427 last_pos[iNodeId].m_startPosition.x + last_pos[iNodeId].m_speed.x * actuallytraveled,
428 last_pos[iNodeId].m_startPosition.y + last_pos[iNodeId].m_speed.y * actuallytraveled,
431 NS_LOG_LOGIC (
"Final point = " << last_pos[iNodeId].m_finalPosition <<
", actually reached = " << reached);
432 last_pos[iNodeId].m_stopEvent.Cancel ();
433 last_pos[iNodeId].m_finalPosition = reached;
439 NS_LOG_DEBUG (
"Positions after parse for node " << iNodeId <<
" " << nodeId <<
" position =" << last_pos[iNodeId].m_finalPosition);
451 if (last_pos[iNodeId].m_targetArrivalTime > at)
453 last_pos[iNodeId].m_stopEvent.Cancel ();
455 last_pos[iNodeId].m_targetArrivalTime = at;
456 last_pos[iNodeId].m_travelStartTime = at;
458 NS_LOG_DEBUG (
"Positions after parse for node " << iNodeId <<
" " << nodeId <<
459 " position =" << last_pos[iNodeId].m_finalPosition);
463 NS_LOG_WARN (
"Format Line is not correct: " << line <<
"\n");
476 std::istringstream s;
480 size_t pos_sharp = str.find_first_of (
'#');
481 if (pos_sharp != std::string::npos)
483 line = str.substr (0, pos_sharp);
505 if (
x.length () == 0)
516 ret.
has_ival.push_back (IsVal<int> (
x, ii));
517 ret.
ivals.push_back (ii);
518 ret.
has_dval.push_back (IsVal<double> (
x, d));
519 ret.
dvals.push_back (d);
523 size_t tokensLength = ret.
tokens.size ();
524 size_t lasTokenLength = ret.
tokens[tokensLength - 1].size ();
528 if ( (tokensLength == 7 || tokensLength == 8)
529 && (ret.
tokens[tokensLength - 1][lasTokenLength - 1] ==
'"') )
533 ret.
tokens[tokensLength - 1] = ret.
tokens[tokensLength - 1].substr (0,lasTokenLength - 1);
536 x = ret.
tokens[tokensLength - 1];
546 ret.
has_ival[tokensLength - 1] = IsVal<int> (
x, ii);
547 ret.
ivals[tokensLength - 1] = ii;
548 ret.
has_dval[tokensLength - 1] = IsVal<double> (
x, d);
549 ret.
dvals[tokensLength - 1] = d;
550 ret.
svals[tokensLength - 1] =
x;
553 else if ( (tokensLength == 9 && ret.
tokens[tokensLength - 1] ==
"\"")
554 || (tokensLength == 8 && ret.
tokens[tokensLength - 1] ==
"\""))
559 ret.
tokens.erase (ret.
tokens.begin () + tokensLength - 1);
561 ret.
ivals.erase (ret.
ivals.begin () + tokensLength - 1);
563 ret.
dvals.erase (ret.
dvals.begin () + tokensLength - 1);
564 ret.
svals.erase (ret.
svals.begin () + tokensLength - 1);
579 while (ret.size () > 0 && isblank (ret[0]))
584 while (ret.size () > 0 && (isblank (ret[ret.size () - 1]) || (ret[ret.size () - 1] ==
';')))
586 ret.erase (ret.size () - 1, 1);
597 [[maybe_unused]]
double v = strtod (s.c_str (), &endp);
598 return endp == s.c_str () + s.size ();
603 bool IsVal (
const std::string& str, T& ret)
605 if (str.size () == 0)
611 std::string s2 = str;
612 std::istringstream s (s2);
628 std::string::size_type startNodeId = str.find_first_of (
"(");
629 std::string::size_type endNodeId = str.find_first_of (
")");
635 if (startNodeId == std::string::npos || endNodeId == std::string::npos)
640 nodeId = str.substr (startNodeId + 1, endNodeId - (startNodeId + 1));
643 if (
IsNumber (nodeId) && (nodeId.find_first_of (
".") == std::string::npos) && (nodeId[0] !=
'-'))
660 std::string::size_type startNodeId = str.find_first_of (
"(");
661 std::string::size_type endNodeId = str.find_first_of (
")");
663 return str.substr (startNodeId + 1, endNodeId - (startNodeId + 1));
676 switch (pr.
tokens.size ())
697 switch (pr.
tokens.size ())
772 double xFinalPosition,
double yFinalPosition,
double speed)
790 double time = std::sqrt (std::pow (xFinalPosition - retval.
m_finalPosition.x, 2) + std::pow (yFinalPosition - retval.
m_finalPosition.y, 2)) / speed;
799 retval.
m_speed = Vector (xSpeed, ySpeed, 0);
804 NS_LOG_DEBUG (
"Calculated Speed: X=" << xSpeed <<
" Y=" << ySpeed <<
" Z=" << zSpeed);
823 position.x = model->GetPosition ().x;
824 position.y = model->GetPosition ().y;
825 position.z = model->GetPosition ().z;
838 position.x = model->GetPosition ().x;
839 position.y = model->GetPosition ().y;
840 position.z = model->GetPosition ().z;
Mobility model for which the current speed does not change once it has been set and until it is set a...
void SetVelocity(const Vector &speed)
An identifier for simulation events.
void SetPosition(const Vector &position)
static Iterator End(void)
static Iterator Begin(void)
a class to hold input objects internally
virtual Ptr< Object > Get(uint32_t i) const =0
Return ith object in store.
void ConfigNodesMovements(const ObjectStore &store) const
Parses ns-2 mobility file to create ns-3 mobility events.
std::string m_filename
filename of file containing ns-2 mobility trace
Ptr< ConstantVelocityMobilityModel > GetMobilityModel(std::string idString, const ObjectStore &store) const
Get or create a ConstantVelocityMobilityModel corresponding to idString.
Ns2MobilityHelper(std::string filename)
void Install(void) const
Read the ns2 trace file and configure the movement patterns of all nodes contained in the global ns3:...
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Time Seconds(double value)
Construct a Time in the indicated unit.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static bool IsSchedMobilityPos(ParseResult pr)
Check if this corresponds to a line like this: $ns_ at 1 "$node_(0) set X_ 2".
static bool IsNumber(const std::string &s)
Checks if a string represents a number or it has others characters than digits and point.
static std::string TrimNs2Line(const std::string &str)
Put out blank spaces at the start and end of a line.
static ParseResult ParseNs2Line(const std::string &str)
Parses a line of ns2 mobility.
static Vector SetOneInitialCoord(Vector actPos, std::string &coord, double value)
Add one coord to a vector position.
static bool IsSetInitialPos(ParseResult pr)
Check if this corresponds to a line like this: $node_(0) set X_ 123.
static std::string GetNodeIdFromToken(std::string str)
Gets nodeId number in string format from the string like $node_(4)
static bool HasNodeIdNumber(std::string str)
Checks if the value between brackets is a correct nodeId number.
static Vector SetInitialPosition(Ptr< ConstantVelocityMobilityModel > model, std::string coord, double coordVal)
Set initial position for a node.
static DestinationPoint SetMovement(Ptr< ConstantVelocityMobilityModel > model, Vector lastPos, double at, double xFinalPosition, double yFinalPosition, double speed)
Set waypoints and speed for movement.
static Vector SetSchedPosition(Ptr< ConstantVelocityMobilityModel > model, double at, std::string coord, double coordVal)
Schedule a set of position for a node.
static bool IsVal(const std::string &str, T &ret)
Check if s string represents a numeric value.
static bool IsSchedSetPos(ParseResult pr)
Check if this corresponds to a line like this: $ns_ at 1 "$node_(0) setdest 2 3 4".
static int GetNodeIdInt(ParseResult pr)
Get node id number in int format.
static std::string GetNodeIdString(ParseResult pr)
Get node id number in string format.
list x
Random number samples.
Keeps last movement schedule.
double m_travelStartTime
Travel start time is needed to calculate actually traveled time.
EventId m_stopEvent
Event scheduling node's stop.
Vector m_finalPosition
Final destination to be reached before next schedule.
double m_targetArrivalTime
When a station arrives to a destination.
Vector m_speed
Speed of the last movement (needed to derive reached destination at next schedule = start + velocity ...
Vector m_startPosition
Start position of last movement.
Type to maintain line parsed and its values.
std::vector< double > dvals
double values for each tokens
std::vector< std::string > svals
string value for each token
std::vector< std::string > tokens
tokens from a line
std::vector< int > ivals
int values for each tokens
std::vector< bool > has_ival
points if a tokens has an int value
std::vector< bool > has_dval
points if a tokens has a double value