A Discrete-Event Network Simulator
API
ns2-mobility-trace.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INRIA
4  * 2009,2010 Contributors
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Martín Giachino <martin.giachino@gmail.com>
20  *
21  *
22  * This example demonstrates the use of Ns2MobilityHelper class to work with mobility.
23  *
24  * Detailed example description.
25  *
26  * - intended usage: this should be used in order to load ns2 movement trace files into ns3.
27  * - behavior:
28  * - Ns2MobilityHelper object is created, associated to the specified trace file.
29  * - A log file is created, using the log file name argument.
30  * - A node container is created with the number of nodes specified in the command line. For the default ns-2 trace, specify the value 2 for this argument.
31  * - the program calls the Install() method of Ns2MobilityHelper to set mobility to nodes. At this moment, the file is read line by line, and the movement is scheduled in the simulator.
32  * - A callback is configured, so each time a node changes its course a log message is printed.
33  * - expected output: example prints out messages generated by each read line from the ns2 movement trace file.
34  * For each line, it shows if the line is correct, or of it has errors and in this case it will
35  * be ignored.
36  *
37  * Usage of ns2-mobility-trace:
38  *
39  * ./ns3 run "ns2-mobility-trace \
40  * --traceFile=src/mobility/examples/default.ns_movements
41  * --nodeNum=2 --duration=100.0 --logFile=ns2-mobility-trace.log"
42  *
43  * NOTE: ns2-traces-file could be an absolute or relative path. You could use the file default.ns_movements
44  * included in the same directory as the example file.
45  * NOTE 2: Number of nodes present in the trace file must match with the command line argument.
46  * Note that you must know it before to be able to load it.
47  * NOTE 3: Duration must be a positive number and should match the trace file. Note that you must know it before to be able to load it.
48  */
49 
50 
51 #include <iostream>
52 #include <fstream>
53 #include <sstream>
54 #include "ns3/core-module.h"
55 #include "ns3/mobility-module.h"
56 #include "ns3/ns2-mobility-helper.h"
57 
58 using namespace ns3;
59 
60 // Prints actual position and velocity when a course change event occurs
61 static void
62 CourseChange (std::ostream *os, std::string foo, Ptr<const MobilityModel> mobility)
63 {
64  Vector pos = mobility->GetPosition (); // Get position
65  Vector vel = mobility->GetVelocity (); // Get velocity
66 
67  // Prints position and velocities
68  *os << Simulator::Now () << " POS: x=" << pos.x << ", y=" << pos.y
69  << ", z=" << pos.z << "; VEL:" << vel.x << ", y=" << vel.y
70  << ", z=" << vel.z << std::endl;
71 }
72 
73 // Example to use ns2 traces file in ns3
74 int main (int argc, char *argv[])
75 {
76  std::string traceFile;
77  std::string logFile;
78 
79  int nodeNum;
80  double duration;
81 
82  // Enable logging from the ns2 helper
83  LogComponentEnable ("Ns2MobilityHelper",LOG_LEVEL_DEBUG);
84 
85  // Parse command line attribute
86  CommandLine cmd (__FILE__);
87  cmd.AddValue ("traceFile", "Ns2 movement trace file", traceFile);
88  cmd.AddValue ("nodeNum", "Number of nodes", nodeNum);
89  cmd.AddValue ("duration", "Duration of Simulation", duration);
90  cmd.AddValue ("logFile", "Log file", logFile);
91  cmd.Parse (argc,argv);
92 
93  // Check command line arguments
94  if (traceFile.empty () || nodeNum <= 0 || duration <= 0 || logFile.empty ())
95  {
96  std::cout << "Usage of " << argv[0] << " :\n\n"
97  "./ns3 run \"ns2-mobility-trace"
98  " --traceFile=src/mobility/examples/default.ns_movements"
99  " --nodeNum=2 --duration=100.0 --logFile=ns2-mob.log\" \n\n"
100  "NOTE: ns2-traces-file could be an absolute or relative path. You could use the file default.ns_movements\n"
101  " included in the same directory of this example file.\n\n"
102  "NOTE 2: Number of nodes present in the trace file must match with the command line argument and must\n"
103  " be a positive number. Note that you must know it before to be able to load it.\n\n"
104  "NOTE 3: Duration must be a positive number. Note that you must know it before to be able to load it.\n\n";
105 
106  return 0;
107  }
108 
109  // Create Ns2MobilityHelper with the specified trace log file as parameter
110  Ns2MobilityHelper ns2 = Ns2MobilityHelper (traceFile);
111 
112  // open log file for output
113  std::ofstream os;
114  os.open (logFile.c_str ());
115 
116  // Create all nodes.
117  NodeContainer stas;
118  stas.Create (nodeNum);
119 
120  ns2.Install (); // configure movements for each node, while reading trace file
121 
122  // Configure callback for logging
123  Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange",
125 
126  Simulator::Stop (Seconds (duration));
127  Simulator::Run ();
129 
130  os.close (); // close log file
131  return 0;
132 }
Parse command-line arguments.
Definition: command-line.h:229
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Helper class which can read ns-2 movement files and configure nodes mobility.
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.
Definition: ptr.h:74
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1709
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ LOG_LEVEL_DEBUG
LOG_DEBUG and above.
Definition: log.h:104
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
cmd
Definition: second.py:35
mobility
Definition: third.py:108
static void CourseChange(std::ostream *os, std::string foo, Ptr< const MobilityModel > mobility)