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