45 #include "ns3/unused.h"
46 #include "ns3/simulator.h"
47 #include "ns3/node-list.h"
49 #include "ns3/constant-velocity-mobility-model.h"
60 #define NS2_X_COORD "X_"
61 #define NS2_Y_COORD "Y_"
62 #define NS2_Z_COORD "Z_"
63 #define NS2_SETDEST "setdest"
65 #define NS2_NODEID "$node_("
66 #define NS2_NS_SCH "$ns_"
95 m_startPosition (
Vector (0,0,0)),
97 m_finalPosition (
Vector (0,0,0)),
98 m_travelStartTime (0),
99 m_targetArrivalTime (0)
111 static bool IsNumber (
const string& s);
115 static bool IsVal (
const string& str, T& ret);
142 static DestinationPoint
SetMovement (Ptr<ConstantVelocityMobilityModel> model,
Vector lastPos,
double at,
143 double xFinalPosition,
double yFinalPosition,
double speed);
149 static Vector SetSchedPosition (Ptr<ConstantVelocityMobilityModel> model,
double at,
string coord,
double coordVal);
152 Ns2MobilityHelper::Ns2MobilityHelper (std::string filename)
153 : m_filename (filename)
155 std::ifstream file (
m_filename.c_str (), std::ios::in);
156 if (!(file.is_open ()))
NS_FATAL_ERROR(
"Could not open trace file " <<
m_filename.c_str() <<
" for reading, aborting here \n");
162 std::istringstream iss;
174 model = CreateObject<ConstantVelocityMobilityModel> ();
175 object->AggregateObject (model);
184 map<int, DestinationPoint> last_pos;
193 std::ifstream file (
m_filename.c_str (), std::ios::in);
196 while (!file.eof () )
202 getline (file, line);
214 if (pr.
tokens.size () != 4)
224 NS_LOG_ERROR (
"Node number couldn't be obtained (corrupted file?): " << line <<
"\n");
234 NS_LOG_ERROR (
"Unknown node ID (corrupted file?): " << nodeId <<
"\n");
248 last_pos[iNodeId] = point;
251 NS_LOG_DEBUG (
"Positions after parse for node " << iNodeId <<
" " << nodeId <<
252 " position = " << last_pos[iNodeId].m_finalPosition);
264 file.open (
m_filename.c_str (), std::ios::in);
267 while (!file.eof () )
273 getline (file, line);
286 NS_LOG_ERROR (
"Line has not correct number of parameters (corrupted file?): " << line <<
"\n");
295 NS_LOG_ERROR (
"Node number couldn't be obtained (corrupted file?): " << line <<
"\n");
305 NS_LOG_ERROR (
"Unknown node ID (corrupted file?): " << nodeId <<
"\n");
350 if (last_pos[iNodeId].m_targetArrivalTime > at)
352 NS_LOG_LOGIC (
"Did not reach a destination! stoptime = " << last_pos[iNodeId].m_targetArrivalTime <<
", at = "<< at);
353 double actuallytraveled = at - last_pos[iNodeId].m_travelStartTime;
355 last_pos[iNodeId].m_startPosition.x + last_pos[iNodeId].m_speed.x * actuallytraveled,
356 last_pos[iNodeId].m_startPosition.y + last_pos[iNodeId].m_speed.y * actuallytraveled,
359 NS_LOG_LOGIC (
"Final point = " << last_pos[iNodeId].m_finalPosition <<
", actually reached = " << reached);
360 last_pos[iNodeId].m_stopEvent.Cancel ();
361 last_pos[iNodeId].m_finalPosition = reached;
367 NS_LOG_DEBUG (
"Positions after parse for node " << iNodeId <<
" " << nodeId <<
" position =" << last_pos[iNodeId].m_finalPosition);
379 if (last_pos[iNodeId].m_targetArrivalTime > at)
381 last_pos[iNodeId].m_stopEvent.Cancel ();
383 last_pos[iNodeId].m_targetArrivalTime = at;
384 last_pos[iNodeId].m_travelStartTime = at;
386 NS_LOG_DEBUG (
"Positions after parse for node " << iNodeId <<
" " << nodeId <<
387 " position =" << last_pos[iNodeId].m_finalPosition);
391 NS_LOG_WARN (
"Format Line is not correct: " << line <<
"\n");
408 size_t pos_sharp = str.find_first_of (
'#');
409 if (pos_sharp != string::npos)
411 line = str.substr (0, pos_sharp);
433 if (x.length () == 0)
444 ret.
has_ival.push_back (IsVal<int> (x, ii));
445 ret.
ivals.push_back (ii);
446 ret.
has_dval.push_back (IsVal<double> (x, d));
447 ret.
dvals.push_back (d);
448 ret.
svals.push_back (x);
451 size_t tokensLength = ret.
tokens.size ();
452 size_t lasTokenLength = ret.
tokens[tokensLength - 1].size ();
456 if ( (tokensLength == 7 || tokensLength == 8)
457 && (ret.
tokens[tokensLength - 1][lasTokenLength - 1] ==
'"') )
461 ret.
tokens[tokensLength - 1] = ret.
tokens[tokensLength - 1].substr (0,lasTokenLength - 1);
464 x = ret.
tokens[tokensLength - 1];
474 ret.
has_ival[tokensLength - 1] = IsVal<int> (
x, ii);
475 ret.
ivals[tokensLength - 1] = ii;
476 ret.
has_dval[tokensLength - 1] = IsVal<double> (
x, d);
477 ret.
dvals[tokensLength - 1] = d;
478 ret.
svals[tokensLength - 1] =
x;
481 else if ( (tokensLength == 9 && ret.
tokens[tokensLength - 1] ==
"\"")
482 || (tokensLength == 8 && ret.
tokens[tokensLength - 1] ==
"\""))
487 ret.
tokens.erase (ret.
tokens.begin () + tokensLength - 1);
489 ret.
ivals.erase (ret.
ivals.begin () + tokensLength - 1);
491 ret.
dvals.erase (ret.
dvals.begin () + tokensLength - 1);
492 ret.
svals.erase (ret.
svals.begin () + tokensLength - 1);
507 while (ret.size () > 0 && isblank (ret[0]))
512 while (ret.size () > 0 && isblank (ret[ret.size () - 1]))
514 ret.erase (ret.size () - 1, 1);
525 double v = strtod (s.c_str (), &endp);
527 return endp == s.c_str () + s.size ();
532 bool IsVal (
const string& str, T& ret)
534 if (str.size () == 0)
541 istringstream s (s2);
557 std::string::size_type startNodeId = str.find_first_of (
"(");
558 std::string::size_type endNodeId = str.find_first_of (
")");
564 if (startNodeId == std::string::npos || endNodeId == std::string::npos)
569 nodeId = str.substr (startNodeId + 1, endNodeId - (startNodeId + 1));
572 if (
IsNumber (nodeId) && (nodeId.find_first_of (
".") == std::string::npos) && (nodeId[0] !=
'-'))
589 std::string::size_type startNodeId = str.find_first_of (
"(");
590 std::string::size_type endNodeId = str.find_first_of (
")");
592 return str.substr (startNodeId + 1, endNodeId - (startNodeId + 1));
605 switch (pr.
tokens.size ())
608 result = pr.
ivals[0];
611 result = pr.
ivals[3];
614 result = pr.
ivals[3];
626 switch (pr.
tokens.size ())
701 double xFinalPosition,
double yFinalPosition,
double speed)
733 NS_LOG_DEBUG (
"Calculated Speed: X=" << xSpeed <<
" Y=" << ySpeed <<
" Z=" << zSpeed);