A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  * ./waf --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 
55 #include "ns3/core-module.h"
56 #include "ns3/mobility-module.h"
57 #include "ns3/mobility-module.h"
58 #include "ns3/ns2-mobility-helper.h"
59 
60 using namespace ns3;
61 
62 // Prints actual position and velocity when a course change event occurs
63 static void
64 CourseChange (std::ostream *os, std::string foo, Ptr<const MobilityModel> mobility)
65 {
66  Vector pos = mobility->GetPosition (); // Get position
67  Vector vel = mobility->GetVelocity (); // Get velocity
68 
69  // Prints position and velocities
70  *os << Simulator::Now () << " POS: x=" << pos.x << ", y=" << pos.y
71  << ", z=" << pos.z << "; VEL:" << vel.x << ", y=" << vel.y
72  << ", z=" << vel.z << std::endl;
73 }
74 
75 // Example to use ns2 traces file in ns3
76 int main (int argc, char *argv[])
77 {
78  std::string traceFile;
79  std::string logFile;
80 
81  int nodeNum;
82  double duration;
83 
84  // Enable logging from the ns2 helper
85  LogComponentEnable ("Ns2MobilityHelper",LOG_LEVEL_DEBUG);
86 
87  // Parse command line attribute
88  CommandLine cmd;
89  cmd.AddValue ("traceFile", "Ns2 movement trace file", traceFile);
90  cmd.AddValue ("nodeNum", "Number of nodes", nodeNum);
91  cmd.AddValue ("duration", "Duration of Simulation", duration);
92  cmd.AddValue ("logFile", "Log file", logFile);
93  cmd.Parse (argc,argv);
94 
95  // Check command line arguments
96  if (traceFile.empty () || nodeNum <= 0 || duration <= 0 || logFile.empty ())
97  {
98  std::cout << "Usage of " << argv[0] << " :\n\n"
99  "./waf --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 the file default.ns_movements\n"
103  " included in the same directory of this example file.\n\n"
104  "NOTE 2: Number of nodes present in the trace file must match with the command line argument and must\n"
105  " be a positive number. Note that you must know it before to be able to load it.\n\n"
106  "NOTE 3: Duration must be a positive number. Note that you must know it before to be able to load it.\n\n";
107 
108  return 0;
109  }
110 
111  // Create Ns2MobilityHelper with the specified trace log file as parameter
112  Ns2MobilityHelper ns2 = Ns2MobilityHelper (traceFile);
113 
114  // open log file for output
115  std::ofstream os;
116  os.open (logFile.c_str ());
117 
118  // Create all nodes.
119  NodeContainer stas;
120  stas.Create (nodeNum);
121 
122  ns2.Install (); // configure movements for each node, while reading trace file
123 
124  // Configure callback for logging
125  Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange",
127 
128  Simulator::Stop (Seconds (duration));
129  Simulator::Run ();
131 
132  os.close (); // close log file
133  return 0;
134 }
double x
x coordinate of vector
Definition: vector.h:49
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Build bound Callbacks which take varying numbers of arguments, and potentially returning a value...
Definition: callback.h:1467
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
Vector GetPosition(void) const
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
a 3d vector
Definition: vector.h:31
Vector GetVelocity(void) const
static void CourseChange(std::ostream *os, std::string foo, Ptr< const MobilityModel > mobility)
void Install(void) const
Read the ns2 trace file and configure the movement patterns of all nodes contained in the global ns3:...
Helper class which can read ns-2 movement files and configure nodes mobility.
Parse command-line arguments.
Definition: command-line.h:177
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
int main(int argc, char *argv[])
keep track of a set of node pointers.
double y
y coordinate of vector
Definition: vector.h:53
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:435
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
void Parse(int argc, char *argv[])
Parse the program arguments.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:318
double z
z coordinate of vector
Definition: vector.h:57