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