Make the ns2 mobility helper class more robust

This commit is contained in:
Mitch Watrous
2012-05-10 12:30:23 -07:00
parent a479c64051
commit de6a588486
3 changed files with 118 additions and 14 deletions

View File

@@ -183,6 +183,13 @@ Ns2MobilityHelper::ConfigNodesMovements (const ObjectStore &store) const
{
map<int, DestinationPoint> last_pos; // Stores previous movement scheduled for each node
//*****************************************************************
// Parse the file the first time to get the initial node positions.
//*****************************************************************
// Look through the whole the file for the the initial node
// positions to make this helper robust to handle trace files with
// the initial node positions at the end.
std::ifstream file (m_filename.c_str (), std::ios::in);
if (file.is_open ())
{
@@ -202,6 +209,77 @@ Ns2MobilityHelper::ConfigNodesMovements (const ObjectStore &store) const
ParseResult pr = ParseNs2Line (line); // Parse line and obtain tokens
// Check if the line corresponds with setting the initial
// node positions
if (pr.tokens.size () != 4)
{
continue;
}
// Get the node Id
nodeId = GetNodeIdString (pr);
iNodeId = GetNodeIdInt (pr);
if (iNodeId == -1)
{
NS_LOG_ERROR ("Node number couldn't be obtained (corrupted file?): " << line << "\n");
continue;
}
// get mobility model of node
Ptr<ConstantVelocityMobilityModel> model = GetMobilityModel (nodeId,store);
// if model not exists, continue
if (model == 0)
{
NS_LOG_ERROR ("Unknown node ID (corrupted file?): " << nodeId << "\n");
continue;
}
/*
* In this case a initial position is being seted
* line like $node_(0) set X_ 151.05190721688197
*/
if (IsSetInitialPos (pr))
{
DestinationPoint point;
// coord coord value
point.m_finalPosition = SetInitialPosition (model, pr.tokens[2], pr.dvals[3]);
last_pos[iNodeId] = point;
// Log new position
NS_LOG_DEBUG ("Positions after parse for node " << iNodeId << " " << nodeId <<
" position = " << last_pos[iNodeId].m_finalPosition);
}
}
file.close ();
}
//*****************************************************************
// Parse the file a second time to get the rest of its values
//*****************************************************************
// The reason the file is parsed again is to make this helper robust
// to handle trace files with the initial node positions at the end.
file.open (m_filename.c_str (), std::ios::in);
if (file.is_open ())
{
while (!file.eof () )
{
int iNodeId = 0;
std::string nodeId;
std::string line;
getline (file, line);
// ignore empty lines
if (line.empty ())
{
continue;
}
ParseResult pr = ParseNs2Line (line); // Parse line and obtain tokens
// Check if the line corresponds with one of the three types of line
if (pr.tokens.size () != 4 && pr.tokens.size () != 7 && pr.tokens.size () != 8)
{
@@ -235,14 +313,10 @@ Ns2MobilityHelper::ConfigNodesMovements (const ObjectStore &store) const
*/
if (IsSetInitialPos (pr))
{
DestinationPoint point;
// coord coord value
point.m_finalPosition = SetInitialPosition (model, pr.tokens[2], pr.dvals[3]);
last_pos[iNodeId] = point;
// Log new position
NS_LOG_DEBUG ("Positions after parse for node " << iNodeId << " " << nodeId <<
" position = " << last_pos[iNodeId].m_finalPosition);
// This is the second time this file has been parsed,
// and the initial node positions were already set the
// first time. So, do nothing this time with this line.
continue;
}
else // NOW EVENTS TO BE SCHEDULED

View File

@@ -50,6 +50,18 @@ class ConstantVelocityMobilityModel;
$ns at $time $node set Z_ Z1
\endverbatim
*
* Note that initial position statements may also appear at the end of
* the mobility file like this:
\verbatim
$ns at $time $node setdest x2 y2 speed
$ns at $time $node set X_ x1
$ns at $time $node set Y_ Y1
$ns at $time $node set Z_ Z1
$node set X_ x1
$node set Y_ y1
$node set Z_ z1
\endverbatim
*
* The following tools are known to support this format:
* - BonnMotion http://net.cs.uni-bonn.de/wg/cs/applications/bonnmotion/
* - SUMO http://sourceforge.net/apps/mediawiki/sumo/index.php?title=Main_Page
@@ -57,12 +69,6 @@ class ConstantVelocityMobilityModel;
*
* See usage example in examples/mobility/ns2-mobility-trace.cc
*
* \bug Traces that generate initial position statements at the end of the
* mobility file (e.g. SUMO TraceExporter) will not be read correctly. The
* workaround is to relocate these three initial position statements to the
* beginning of the trace.
* See https://www.nsnam.org/bugzilla/show_bug.cgi?id=1316
*
* \bug Rounding errors may cause movement to diverge from the mobility
* pattern in ns-2 (using the same trace).
* See https://www.nsnam.org/bugzilla/show_bug.cgi?id=1316

View File

@@ -300,6 +300,30 @@ public:
t->AddReferencePoint ("0", 5, Vector (0, 0, 0), Vector (0, 0, 0));
AddTestCase (t);
// Copy of previous test case but with the initial positions at
// the end of the trace rather than at the beginning.
//
// Several set and setdest. Arguments are interpreted as x, y, speed by default
t = new Ns2MobilityHelperTest ("square setdest (initial positions at end)", Seconds (6));
t->SetTrace ("$ns_ at 1.0 \"$node_(0) setdest 15 10 5\"\n"
"$ns_ at 2.0 \"$node_(0) setdest 15 15 5\"\n"
"$ns_ at 3.0 \"$node_(0) setdest 10 15 5\"\n"
"$ns_ at 4.0 \"$node_(0) setdest 10 10 5\"\n"
"$node_(0) set X_ 10.0\n"
"$node_(0) set Y_ 10.0\n"
);
// id t position velocity
t->AddReferencePoint ("0", 0, Vector (10, 10, 0), Vector (0, 0, 0));
t->AddReferencePoint ("0", 1, Vector (10, 10, 0), Vector (5, 0, 0));
t->AddReferencePoint ("0", 2, Vector (15, 10, 0), Vector (0, 0, 0));
t->AddReferencePoint ("0", 2, Vector (15, 10, 0), Vector (0, 5, 0));
t->AddReferencePoint ("0", 3, Vector (15, 15, 0), Vector (0, 0, 0));
t->AddReferencePoint ("0", 3, Vector (15, 15, 0), Vector (-5, 0, 0));
t->AddReferencePoint ("0", 4, Vector (10, 15, 0), Vector (0, 0, 0));
t->AddReferencePoint ("0", 4, Vector (10, 15, 0), Vector (0, -5, 0));
t->AddReferencePoint ("0", 5, Vector (10, 10, 0), Vector (0, 0, 0));
AddTestCase (t);
// Scheduled set position
t = new Ns2MobilityHelperTest ("scheduled set position", Seconds (2));
t->SetTrace ("$ns_ at 1.0 \"$node_(0) set X_ 10\"\n"