A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
bonnmotion-ns2-example.cc
Go to the documentation of this file.
1/*
2 * Copyright 2012 Eric Gamess
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8/*
9 * Example program using a ns-2-formatted mobility trace generated
10 * by the BonnMotion mobility framework.
11 *
12 * With the provided tracefile (bonnmotion.ns_movements), the movements of
13 * one node are simulated for 1000 seconds. There are a few other files
14 * that provide metadata about the mobility:
15 * - src/mobility/examples/bonnmotion.ns_params
16 * - src/mobility/examples/bonnmotion.params
17 *
18 * These files are documented in the BonnMotion documentation.
19 *
20 * It is important to remember that the trace file dictates how many nodes
21 * (in this case, one) and how long (in this case, 1000 seconds) that the
22 * ns-3 program should use. If you want to change the mobility pattern,
23 * number of nodes, or duration, you need to use BonnMotion or another
24 * tool to generate a new trace.
25 *
26 * Finally, note that you can visualize this program using the pyviz
27 * visualizer:
28 * ./ns3 run bonnmotion-ns2-example --vis
29 */
30
31#include "ns3/core-module.h"
32#include "ns3/mobility-module.h"
33
34using namespace ns3;
35
36void
37showPosition(Ptr<Node> node, double deltaTime)
38{
39 uint32_t nodeId = node->GetId();
40 Ptr<MobilityModel> mobModel = node->GetObject<MobilityModel>();
41 Vector3D pos = mobModel->GetPosition();
42 Vector3D speed = mobModel->GetVelocity();
43 std::cout << "At " << Simulator::Now().GetSeconds() << " node " << nodeId << ": Position("
44 << pos.x << ", " << pos.y << ", " << pos.z << "); Speed(" << speed.x << ", "
45 << speed.y << ", " << speed.z << ")" << std::endl;
46
47 Simulator::Schedule(Seconds(deltaTime), &showPosition, node, deltaTime);
48}
49
50int
51main(int argc, char* argv[])
52{
53 std::cout.precision(2);
54 std::cout.setf(std::ios::fixed);
55
56 double deltaTime = 100;
57 std::string traceFile = "src/mobility/examples/bonnmotion.ns_movements";
58
59 CommandLine cmd(__FILE__);
60 cmd.AddValue("traceFile", "Ns2 movement trace file", traceFile);
61 cmd.AddValue("deltaTime", "time interval (s) between updates (default 100)", deltaTime);
62 cmd.Parse(argc, argv);
63
65
66 Ns2MobilityHelper ns2 = Ns2MobilityHelper(traceFile);
67 ns2.Install();
68
69 Simulator::Schedule(Seconds(0.0), &showPosition, n0, deltaTime);
70
71 Simulator::Stop(Seconds(1000.0));
74 return 0;
75}
void showPosition(Ptr< Node > node, double deltaTime)
Parse command-line arguments.
Keep track of the current position and velocity of an object.
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 EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
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
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
a 3d vector
Definition vector.h:35
double x
x coordinate of vector
Definition vector.h:48
double z
z coordinate of vector
Definition vector.h:50
double y
y coordinate of vector
Definition vector.h:49
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
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.