A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
bonnmotion-ns2-example.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Eric Gamess
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  */
18 
19 /*
20  * Example program using a ns-2-formatted mobility trace generated
21  * by the BonnMotion mobility framework.
22  *
23  * With the provided tracefile (bonnmotion.ns_movements), the movements of
24  * one node are simulated for 1000 seconds. There are a few other files
25  * that provide metadata about the mobility:
26  * - src/mobility/examples/bonnmotion.ns_params
27  * - src/mobility/examples/bonnmotion.params
28  *
29  * These files are documented in the BonnMotion documentation.
30  *
31  * It is important to remember that the trace file dictates how many nodes
32  * (in this case, one) and how long (in this case, 1000 seconds) that the
33  * ns-3 program should use. If you want to change the mobility pattern,
34  * number of nodes, or duration, you need to use BonnMotion or another
35  * tool to generate a new trace.
36  *
37  * Finally, note that you can visualize this program using the pyviz
38  * visualizer:
39  * ./ns3 run bonnmotion-ns2-example --vis
40  */
41 
42 #include "ns3/core-module.h"
43 #include "ns3/mobility-module.h"
44 
45 using namespace ns3;
46 
47 void
48 showPosition(Ptr<Node> node, double deltaTime)
49 {
50  uint32_t nodeId = node->GetId();
51  Ptr<MobilityModel> mobModel = node->GetObject<MobilityModel>();
52  Vector3D pos = mobModel->GetPosition();
53  Vector3D speed = mobModel->GetVelocity();
54  std::cout << "At " << Simulator::Now().GetSeconds() << " node " << nodeId << ": Position("
55  << pos.x << ", " << pos.y << ", " << pos.z << "); Speed(" << speed.x << ", "
56  << speed.y << ", " << speed.z << ")" << std::endl;
57 
58  Simulator::Schedule(Seconds(deltaTime), &showPosition, node, deltaTime);
59 }
60 
61 int
62 main(int argc, char* argv[])
63 {
64  std::cout.precision(2);
65  std::cout.setf(std::ios::fixed);
66 
67  double deltaTime = 100;
68  std::string traceFile = "src/mobility/examples/bonnmotion.ns_movements";
69 
70  CommandLine cmd(__FILE__);
71  cmd.AddValue("traceFile", "Ns2 movement trace file", traceFile);
72  cmd.AddValue("deltaTime", "time interval (s) between updates (default 100)", deltaTime);
73  cmd.Parse(argc, argv);
74 
75  Ptr<Node> n0 = CreateObject<Node>();
76 
77  Ns2MobilityHelper ns2 = Ns2MobilityHelper(traceFile);
78  ns2.Install();
79 
80  Simulator::Schedule(Seconds(0.0), &showPosition, n0, deltaTime);
81 
82  Simulator::Stop(Seconds(1000.0));
85  return 0;
86 }
void showPosition(Ptr< Node > node, double deltaTime)
Parse command-line arguments.
Definition: command-line.h:232
Keep track of the current position and velocity of an object.
Vector GetVelocity() const
Vector GetPosition() const
uint32_t GetId() const
Definition: node.cc:117
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:...
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
a 3d vector
Definition: vector.h:46
double x
x coordinate of vector
Definition: vector.h:59
double z
z coordinate of vector
Definition: vector.h:61
double y
y coordinate of vector
Definition: vector.h:60
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40