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_"
109 static ParseResult
ParseNs2Line (
const std::string& str);
116 static std::string
TrimNs2Line (
const std::string& str);
123 static bool IsNumber (
const std::string& s);
132 static bool IsVal (
const std::string& str, T& ret);
202 static DestinationPoint
SetMovement (Ptr<ConstantVelocityMobilityModel> model, Vector lastPos,
double at,
203 double xFinalPosition,
double yFinalPosition,
double speed);
212 static Vector
SetInitialPosition (Ptr<ConstantVelocityMobilityModel> model, std::string coord,
double coordVal);
222 static Vector
SetSchedPosition (Ptr<ConstantVelocityMobilityModel> model,
double at, std::string coord,
double coordVal);
226 : m_filename (filename)
235 std::istringstream iss;
247 model = CreateObject<ConstantVelocityMobilityModel> ();
248 object->AggregateObject (model);
257 std::map<int, DestinationPoint> last_pos;
269 while (!
file.eof () )
275 getline (
file, line);
287 if (pr.
tokens.size () != 4)
297 NS_LOG_ERROR (
"Node number couldn't be obtained (corrupted file?): " << line <<
"\n");
307 NS_LOG_ERROR (
"Unknown node ID (corrupted file?): " << nodeId <<
"\n");
321 last_pos[iNodeId] = point;
324 NS_LOG_DEBUG (
"Positions after parse for node " << iNodeId <<
" " << nodeId <<
325 " position = " << last_pos[iNodeId].m_finalPosition);
340 while (!
file.eof () )
346 getline (
file, line);
359 NS_LOG_ERROR (
"Line has not correct number of parameters (corrupted file?): " << line <<
"\n");
368 NS_LOG_ERROR (
"Node number couldn't be obtained (corrupted file?): " << line <<
"\n");
378 NS_LOG_ERROR (
"Unknown node ID (corrupted file?): " << nodeId <<
"\n");
423 if (last_pos[iNodeId].m_targetArrivalTime > at)
425 NS_LOG_LOGIC (
"Did not reach a destination! stoptime = " << last_pos[iNodeId].m_targetArrivalTime <<
", at = "<< at);
426 double actuallytraveled = at - last_pos[iNodeId].m_travelStartTime;
427 Vector reached = Vector (
428 last_pos[iNodeId].m_startPosition.x + last_pos[iNodeId].m_speed.x * actuallytraveled,
429 last_pos[iNodeId].m_startPosition.y + last_pos[iNodeId].m_speed.y * actuallytraveled,
432 NS_LOG_LOGIC (
"Final point = " << last_pos[iNodeId].m_finalPosition <<
", actually reached = " << reached);
433 last_pos[iNodeId].m_stopEvent.Cancel ();
434 last_pos[iNodeId].m_finalPosition = reached;
440 NS_LOG_DEBUG (
"Positions after parse for node " << iNodeId <<
" " << nodeId <<
" position =" << last_pos[iNodeId].m_finalPosition);
452 if (last_pos[iNodeId].m_targetArrivalTime > at)
454 last_pos[iNodeId].m_stopEvent.Cancel ();
456 last_pos[iNodeId].m_targetArrivalTime = at;
457 last_pos[iNodeId].m_travelStartTime = at;
459 NS_LOG_DEBUG (
"Positions after parse for node " << iNodeId <<
" " << nodeId <<
460 " position =" << last_pos[iNodeId].m_finalPosition);
464 NS_LOG_WARN (
"Format Line is not correct: " << line <<
"\n");
477 std::istringstream s;
481 size_t pos_sharp = str.find_first_of (
'#');
482 if (pos_sharp != std::string::npos)
484 line = str.substr (0, pos_sharp);
506 if (
x.length () == 0)
517 ret.
has_ival.push_back (IsVal<int> (
x, ii));
518 ret.
ivals.push_back (ii);
519 ret.
has_dval.push_back (IsVal<double> (
x, d));
520 ret.
dvals.push_back (d);
524 size_t tokensLength = ret.
tokens.size ();
525 size_t lasTokenLength = ret.
tokens[tokensLength - 1].size ();
529 if ( (tokensLength == 7 || tokensLength == 8)
530 && (ret.
tokens[tokensLength - 1][lasTokenLength - 1] ==
'"') )
534 ret.
tokens[tokensLength - 1] = ret.
tokens[tokensLength - 1].substr (0,lasTokenLength - 1);
537 x = ret.
tokens[tokensLength - 1];
547 ret.
has_ival[tokensLength - 1] = IsVal<int> (
x, ii);
548 ret.
ivals[tokensLength - 1] = ii;
549 ret.
has_dval[tokensLength - 1] = IsVal<double> (
x, d);
550 ret.
dvals[tokensLength - 1] = d;
551 ret.
svals[tokensLength - 1] =
x;
554 else if ( (tokensLength == 9 && ret.
tokens[tokensLength - 1] ==
"\"")
555 || (tokensLength == 8 && ret.
tokens[tokensLength - 1] ==
"\""))
560 ret.
tokens.erase (ret.
tokens.begin () + tokensLength - 1);
562 ret.
ivals.erase (ret.
ivals.begin () + tokensLength - 1);
564 ret.
dvals.erase (ret.
dvals.begin () + tokensLength - 1);
565 ret.
svals.erase (ret.
svals.begin () + tokensLength - 1);
580 while (ret.size () > 0 && isblank (ret[0]))
585 while (ret.size () > 0 && (isblank (ret[ret.size () - 1]) || (ret[ret.size () - 1] ==
';')))
587 ret.erase (ret.size () - 1, 1);
598 double v = strtod (s.c_str (), &endp);
600 return endp == s.c_str () + s.size ();
605 bool IsVal (
const std::string& str, T& ret)
607 if (str.size () == 0)
613 std::string s2 = str;
614 std::istringstream s (s2);
630 std::string::size_type startNodeId = str.find_first_of (
"(");
631 std::string::size_type endNodeId = str.find_first_of (
")");
637 if (startNodeId == std::string::npos || endNodeId == std::string::npos)
642 nodeId = str.substr (startNodeId + 1, endNodeId - (startNodeId + 1));
645 if (
IsNumber (nodeId) && (nodeId.find_first_of (
".") == std::string::npos) && (nodeId[0] !=
'-'))
662 std::string::size_type startNodeId = str.find_first_of (
"(");
663 std::string::size_type endNodeId = str.find_first_of (
")");
665 return str.substr (startNodeId + 1, endNodeId - (startNodeId + 1));
678 switch (pr.
tokens.size ())
681 result = pr.
ivals[0];
684 result = pr.
ivals[3];
687 result = pr.
ivals[3];
699 switch (pr.
tokens.size ())
774 double xFinalPosition,
double yFinalPosition,
double speed)
792 double time = std::sqrt (std::pow (xFinalPosition - retval.
m_finalPosition.x, 2) + std::pow (yFinalPosition - retval.
m_finalPosition.y, 2)) / speed;
801 retval.
m_speed = Vector (xSpeed, ySpeed, 0);
806 NS_LOG_DEBUG (
"Calculated Speed: X=" << xSpeed <<
" Y=" << ySpeed <<
" Z=" << zSpeed);