A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
bonnmotion-ns2-example.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright 2012 Eric Gamess
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  */
19 
20 /*
21  * Example program using a ns-2-formatted mobility trace generated
22  * by the BonnMotion mobility framework.
23  *
24  * With the provided tracefile (bonnmotion.ns_movements), the movements of
25  * one node are simulated for 1000 seconds. There are a few other files
26  * that provide metadata about the mobility:
27  * - src/mobility/examples/bonnmotion.ns_params
28  * - src/mobility/examples/bonnmotion.params
29  *
30  * These files are documented in the BonnMotion documentation.
31  *
32  * It is important to remember that the trace file dictates how many nodes
33  * (in this case, one) and how long (in this case, 1000 seconds) that the
34  * ns-3 program should use. If you want to change the mobility pattern,
35  * number of nodes, or duration, you need to use BonnMotion or another
36  * tool to generate a new trace.
37  *
38  * Finally, note that you can visualize this program using the pyviz
39  * visualizer:
40  * ./waf --run bonnmotion-ns2-example --vis
41  */
42 
43 #include "ns3/core-module.h"
44 #include "ns3/mobility-module.h"
45 
46 using namespace ns3;
47 
48 void 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
55  << ": Position(" << pos.x << ", " << pos.y << ", " << pos.z
56  << "); Speed(" << speed.x << ", " << speed.y << ", " << speed.z
57  << ")" << std::endl;
58 
59  Simulator::Schedule (Seconds (deltaTime), &showPosition, node, deltaTime);
60 }
61 
62 int 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;
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));
83  Simulator::Run ();
85  return 0;
86 }
87 
88 
double x
x coordinate of vector
Definition: vector.h:49
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
Vector GetPosition(void) const
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
void showPosition(Ptr< Node > node, double deltaTime)
a 3d vector
Definition: vector.h:31
Vector GetVelocity(void) const
Keep track of the current position and velocity of an object.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:322
Helper class which can read ns-2 movement files and configure nodes mobility.
Parse command-line arguments.
Definition: command-line.h:196
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
int main(int argc, char *argv[])
double y
y coordinate of vector
Definition: vector.h:53
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
uint32_t GetId(void) const
Definition: node.cc:106
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:471
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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:845
void Parse(int argc, char *argv[])
Parse the program arguments.
Ptr< T > GetObject(void) const
Definition: object.h:362
double z
z coordinate of vector
Definition: vector.h:57