45 #include "ns3/unused.h"
46 #include "ns3/simulator.h"
47 #include "ns3/node-list.h"
49 #include "ns3/constant-velocity-mobility-model.h"
58 #define NS2_X_COORD "X_"
59 #define NS2_Y_COORD "Y_"
60 #define NS2_Z_COORD "Z_"
61 #define NS2_SETDEST "setdest"
63 #define NS2_NODEID "$node_("
64 #define NS2_NS_SCH "$ns_"
103 static ParseResult
ParseNs2Line (
const std::string& str);
106 static std::string
TrimNs2Line (
const std::string& str);
109 static bool IsNumber (
const std::string&
s);
113 static bool IsVal (
const std::string& str, T& ret);
140 static DestinationPoint
SetMovement (Ptr<ConstantVelocityMobilityModel> model,
Vector lastPos,
double at,
141 double xFinalPosition,
double yFinalPosition,
double speed);
147 static Vector SetSchedPosition (Ptr<ConstantVelocityMobilityModel> model,
double at, std::string coord,
double coordVal);
151 : m_filename (filename)
153 std::ifstream file (
m_filename.c_str (), std::ios::in);
154 if (!(file.is_open ()))
NS_FATAL_ERROR(
"Could not open trace file " <<
m_filename.c_str() <<
" for reading, aborting here \n");
160 std::istringstream iss;
172 model = CreateObject<ConstantVelocityMobilityModel> ();
173 object->AggregateObject (model);
182 std::map<int, DestinationPoint> last_pos;
191 std::ifstream file (
m_filename.c_str (), std::ios::in);
194 while (!file.eof () )
200 getline (file, line);
212 if (pr.
tokens.size () != 4)
222 NS_LOG_ERROR (
"Node number couldn't be obtained (corrupted file?): " << line <<
"\n");
232 NS_LOG_ERROR (
"Unknown node ID (corrupted file?): " << nodeId <<
"\n");
246 last_pos[iNodeId] = point;
249 NS_LOG_DEBUG (
"Positions after parse for node " << iNodeId <<
" " << nodeId <<
250 " position = " << last_pos[iNodeId].m_finalPosition);
262 file.open (
m_filename.c_str (), std::ios::in);
265 while (!file.eof () )
271 getline (file, line);
284 NS_LOG_ERROR (
"Line has not correct number of parameters (corrupted file?): " << line <<
"\n");
293 NS_LOG_ERROR (
"Node number couldn't be obtained (corrupted file?): " << line <<
"\n");
303 NS_LOG_ERROR (
"Unknown node ID (corrupted file?): " << nodeId <<
"\n");
348 if (last_pos[iNodeId].m_targetArrivalTime > at)
350 NS_LOG_LOGIC (
"Did not reach a destination! stoptime = " << last_pos[iNodeId].m_targetArrivalTime <<
", at = "<< at);
351 double actuallytraveled = at - last_pos[iNodeId].m_travelStartTime;
353 last_pos[iNodeId].m_startPosition.x + last_pos[iNodeId].m_speed.x * actuallytraveled,
354 last_pos[iNodeId].m_startPosition.y + last_pos[iNodeId].m_speed.y * actuallytraveled,
357 NS_LOG_LOGIC (
"Final point = " << last_pos[iNodeId].m_finalPosition <<
", actually reached = " << reached);
358 last_pos[iNodeId].m_stopEvent.Cancel ();
359 last_pos[iNodeId].m_finalPosition = reached;
365 NS_LOG_DEBUG (
"Positions after parse for node " << iNodeId <<
" " << nodeId <<
" position =" << last_pos[iNodeId].m_finalPosition);
377 if (last_pos[iNodeId].m_targetArrivalTime > at)
379 last_pos[iNodeId].m_stopEvent.Cancel ();
381 last_pos[iNodeId].m_targetArrivalTime = at;
382 last_pos[iNodeId].m_travelStartTime = at;
384 NS_LOG_DEBUG (
"Positions after parse for node " << iNodeId <<
" " << nodeId <<
385 " position =" << last_pos[iNodeId].m_finalPosition);
389 NS_LOG_WARN (
"Format Line is not correct: " << line <<
"\n");
402 std::istringstream
s;
406 size_t pos_sharp = str.find_first_of (
'#');
407 if (pos_sharp != std::string::npos)
409 line = str.substr (0, pos_sharp);
431 if (x.length () == 0)
442 ret.
has_ival.push_back (IsVal<int> (x, ii));
443 ret.
ivals.push_back (ii);
444 ret.
has_dval.push_back (IsVal<double> (x, d));
445 ret.
dvals.push_back (d);
446 ret.
svals.push_back (x);
449 size_t tokensLength = ret.
tokens.size ();
450 size_t lasTokenLength = ret.
tokens[tokensLength - 1].size ();
454 if ( (tokensLength == 7 || tokensLength == 8)
455 && (ret.
tokens[tokensLength - 1][lasTokenLength - 1] ==
'"') )
459 ret.
tokens[tokensLength - 1] = ret.
tokens[tokensLength - 1].substr (0,lasTokenLength - 1);
462 x = ret.
tokens[tokensLength - 1];
472 ret.
has_ival[tokensLength - 1] = IsVal<int> (
x, ii);
473 ret.
ivals[tokensLength - 1] = ii;
474 ret.
has_dval[tokensLength - 1] = IsVal<double> (
x, d);
475 ret.
dvals[tokensLength - 1] = d;
476 ret.
svals[tokensLength - 1] =
x;
479 else if ( (tokensLength == 9 && ret.
tokens[tokensLength - 1] ==
"\"")
480 || (tokensLength == 8 && ret.
tokens[tokensLength - 1] ==
"\""))
485 ret.
tokens.erase (ret.
tokens.begin () + tokensLength - 1);
487 ret.
ivals.erase (ret.
ivals.begin () + tokensLength - 1);
489 ret.
dvals.erase (ret.
dvals.begin () + tokensLength - 1);
490 ret.
svals.erase (ret.
svals.begin () + tokensLength - 1);
505 while (ret.size () > 0 && isblank (ret[0]))
510 while (ret.size () > 0 && isblank (ret[ret.size () - 1]))
512 ret.erase (ret.size () - 1, 1);
523 double v = strtod (s.c_str (), &endp);
525 return endp == s.c_str () + s.size ();
530 bool IsVal (
const std::string& str, T& ret)
532 if (str.size () == 0)
538 std::string s2 = str;
539 std::istringstream
s (s2);
555 std::string::size_type startNodeId = str.find_first_of (
"(");
556 std::string::size_type endNodeId = str.find_first_of (
")");
562 if (startNodeId == std::string::npos || endNodeId == std::string::npos)
567 nodeId = str.substr (startNodeId + 1, endNodeId - (startNodeId + 1));
570 if (
IsNumber (nodeId) && (nodeId.find_first_of (
".") == std::string::npos) && (nodeId[0] !=
'-'))
587 std::string::size_type startNodeId = str.find_first_of (
"(");
588 std::string::size_type endNodeId = str.find_first_of (
")");
590 return str.substr (startNodeId + 1, endNodeId - (startNodeId + 1));
603 switch (pr.
tokens.size ())
606 result = pr.
ivals[0];
609 result = pr.
ivals[3];
612 result = pr.
ivals[3];
624 switch (pr.
tokens.size ())
699 double xFinalPosition,
double yFinalPosition,
double speed)
731 NS_LOG_DEBUG (
"Calculated Speed: X=" << xSpeed <<
" Y=" << ySpeed <<
" Z=" << zSpeed);
void ConfigNodesMovements(const ObjectStore &store) const
std::vector< bool > has_dval
double x
x coordinate of vector
smart pointer class similar to boost::intrusive_ptr
std::vector< bool > has_ival
static Vector SetOneInitialCoord(Vector actPos, std::string &coord, double value)
static Vector SetInitialPosition(Ptr< ConstantVelocityMobilityModel > model, std::string coord, double coordVal)
static bool IsSchedSetPos(ParseResult pr)
static int GetNodeIdInt(ParseResult pr)
static bool HasNodeIdNumber(std::string str)
Vector GetPosition(void) const
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Ptr< ConstantVelocityMobilityModel > GetMobilityModel(std::string idString, const ObjectStore &store) const
std::vector< std::string > tokens
#define NS_FATAL_ERROR(msg)
fatal error handling
NS_LOG_COMPONENT_DEFINE("Ns2MobilityHelper")
void Install(void) const
Read the ns2 trace file and configure the movement patterns of all nodes contained in the global ns3:...
static bool IsSetInitialPos(ParseResult pr)
static Iterator End(void)
static std::string GetNodeIdFromToken(std::string str)
static DestinationPoint SetMovement(Ptr< ConstantVelocityMobilityModel > model, Vector lastPos, double at, double xFinalPosition, double yFinalPosition, double speed)
std::vector< std::string > svals
virtual Ptr< Object > Get(uint32_t i) const =0
static ParseResult ParseNs2Line(const std::string &str)
#define NS_LOG_LOGIC(msg)
static std::string GetNodeIdString(ParseResult pr)
std::vector< double > dvals
double y
y coordinate of vector
Keeps last movement schedule.
Ns2MobilityHelper(std::string filename)
static Vector SetSchedPosition(Ptr< ConstantVelocityMobilityModel > model, double at, std::string coord, double coordVal)
void SetPosition(const Vector &position)
an identifier for simulation events.
static Iterator Begin(void)
#define NS_LOG_DEBUG(msg)
Mobility model for which the current speed does not change once it has been set and until it is set a...
static std::string TrimNs2Line(const std::string &str)
#define NS_LOG_ERROR(msg)
static bool IsSchedMobilityPos(ParseResult pr)
void SetVelocity(const Vector &speed)
static bool IsVal(const std::string &str, T &ret)
double m_targetArrivalTime
static bool IsNumber(const std::string &s)
double z
z coordinate of vector