A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mobility-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3/mobility-helper.h"
21 #include "ns3/mobility-model.h"
22 #include "ns3/position-allocator.h"
23 #include "ns3/hierarchical-mobility-model.h"
24 #include "ns3/log.h"
25 #include "ns3/pointer.h"
26 #include "ns3/config.h"
27 #include "ns3/simulator.h"
28 #include "ns3/names.h"
29 #include "ns3/string.h"
30 #include <iostream>
31 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("MobilityHelper")
35  ;
36 
38 {
39  m_position = CreateObjectWithAttributes<RandomRectanglePositionAllocator>
40  ("X", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"),
41  "Y", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
42  m_mobility.SetTypeId ("ns3::ConstantPositionMobilityModel");
43 }
45 {
46 }
47 void
49 {
50  m_position = allocator;
51 }
52 
53 void
55  std::string n1, const AttributeValue &v1,
56  std::string n2, const AttributeValue &v2,
57  std::string n3, const AttributeValue &v3,
58  std::string n4, const AttributeValue &v4,
59  std::string n5, const AttributeValue &v5,
60  std::string n6, const AttributeValue &v6,
61  std::string n7, const AttributeValue &v7,
62  std::string n8, const AttributeValue &v8,
63  std::string n9, const AttributeValue &v9)
64 {
65  ObjectFactory pos;
66  pos.SetTypeId (type);
67  pos.Set (n1, v1);
68  pos.Set (n2, v2);
69  pos.Set (n3, v3);
70  pos.Set (n4, v4);
71  pos.Set (n5, v5);
72  pos.Set (n6, v6);
73  pos.Set (n7, v7);
74  pos.Set (n8, v8);
75  pos.Set (n9, v9);
77 }
78 
79 void
81  std::string n1, const AttributeValue &v1,
82  std::string n2, const AttributeValue &v2,
83  std::string n3, const AttributeValue &v3,
84  std::string n4, const AttributeValue &v4,
85  std::string n5, const AttributeValue &v5,
86  std::string n6, const AttributeValue &v6,
87  std::string n7, const AttributeValue &v7,
88  std::string n8, const AttributeValue &v8,
89  std::string n9, const AttributeValue &v9)
90 {
91  m_mobility.SetTypeId (type);
92  m_mobility.Set (n1, v1);
93  m_mobility.Set (n2, v2);
94  m_mobility.Set (n3, v3);
95  m_mobility.Set (n4, v4);
96  m_mobility.Set (n5, v5);
97  m_mobility.Set (n6, v6);
98  m_mobility.Set (n7, v7);
99  m_mobility.Set (n8, v8);
100  m_mobility.Set (n9, v9);
101 }
102 
103 void
105 {
106  Ptr<MobilityModel> mobility = reference->GetObject<MobilityModel> ();
107  m_mobilityStack.push_back (mobility);
108 }
109 
110 void
112 {
113  Ptr<MobilityModel> mobility = Names::Find<MobilityModel> (referenceName);
114  m_mobilityStack.push_back (mobility);
115 }
116 
117 void
119 {
120  m_mobilityStack.pop_back ();
121 }
122 
123 
124 std::string
126 {
127  return m_mobility.GetTypeId ().GetName ();
128 }
129 
130 void
132 {
133  Ptr<Object> object = node;
134  Ptr<MobilityModel> model = object->GetObject<MobilityModel> ();
135  if (model == 0)
136  {
137  model = m_mobility.Create ()->GetObject<MobilityModel> ();
138  if (model == 0)
139  {
140  NS_FATAL_ERROR ("The requested mobility model is not a mobility model: \""<<
141  m_mobility.GetTypeId ().GetName ()<<"\"");
142  }
143  if (m_mobilityStack.empty ())
144  {
145  NS_LOG_DEBUG ("node="<<object<<", mob="<<model);
146  object->AggregateObject (model);
147  }
148  else
149  {
150  // we need to setup a hierarchical mobility model
151  Ptr<MobilityModel> parent = m_mobilityStack.back ();
152  Ptr<MobilityModel> hierarchical =
153  CreateObjectWithAttributes<HierarchicalMobilityModel> ("Child", PointerValue (model),
154  "Parent", PointerValue (parent));
155  object->AggregateObject (hierarchical);
156  NS_LOG_DEBUG ("node="<<object<<", mob="<<hierarchical);
157  }
158  }
159  Vector position = m_position->GetNext ();
160  model->SetPosition (position);
161 }
162 
163 void
164 MobilityHelper::Install (std::string nodeName) const
165 {
166  Ptr<Node> node = Names::Find<Node> (nodeName);
167  Install (node);
168 }
169 void
171 {
172  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
173  {
174  Install (*i);
175  }
176 }
177 
178 void
180 {
182 }
183 static double
184 DoRound (double v)
185 {
186  if (v <= 1e-4 && v >= -1e-4)
187  {
188  return 0.0;
189  }
190  else if (v <= 1e-3 && v >= 0)
191  {
192  return 1e-3;
193  }
194  else if (v >= -1e-3 && v <= 0)
195  {
196  return -1e-3;
197  }
198  else
199  {
200  return v;
201  }
202 }
203 void
205 {
206  std::ostream* os = stream->GetStream ();
207  Ptr<Node> node = mobility->GetObject<Node> ();
208  *os << "now=" << Simulator::Now ()
209  << " node=" << node->GetId ();
210  Vector pos = mobility->GetPosition ();
211  pos.x = DoRound (pos.x);
212  pos.y = DoRound (pos.y);
213  pos.z = DoRound (pos.z);
214  Vector vel = mobility->GetVelocity ();
215  vel.x = DoRound (vel.x);
216  vel.y = DoRound (vel.y);
217  vel.z = DoRound (vel.z);
218  std::streamsize saved_precision = os->precision ();
219  std::ios::fmtflags saved_flags = os->flags ();
220  os->precision (3);
221  os->setf (std::ios::fixed,std::ios::floatfield);
222  *os << " pos=" << pos.x << ":" << pos.y << ":" << pos.z
223  << " vel=" << vel.x << ":" << vel.y << ":" << vel.z
224  << std::endl;
225  os->flags (saved_flags);
226  os->precision (saved_precision);
227 }
228 
229 void
231 {
232  std::ostringstream oss;
233  oss << "/NodeList/" << nodeid << "/$ns3::MobilityModel/CourseChange";
234  Config::ConnectWithoutContext (oss.str (),
236 }
237 void
239 {
240  for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
241  {
242  EnableAscii (stream, (*i)->GetId ());
243  }
244 }
245 void
247 {
249 }
250 int64_t
252 {
253  int64_t currentStream = stream;
254  Ptr<Node> node;
255  Ptr<MobilityModel> mobility;
256  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
257  {
258  node = (*i);
259  mobility = node->GetObject<MobilityModel> ();
260  if (mobility)
261  {
262  currentStream += mobility->AssignStreams (currentStream);
263  }
264  }
265  return (currentStream - stream);
266 }
267 
268 } // namespace ns3
void InstallAll(void)
Perform the work of MobilityHelper::Install on all nodes which exist in the simulation.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
double x
x coordinate of vector
Definition: vector.h:49
TypeId GetTypeId(void) const
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
std::vector< Ptr< Node > >::const_iterator Iterator
hold variables of type string
Definition: string.h:19
Hold a value for an Attribute.
Definition: attribute.h:56
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:1463
void SetTypeId(TypeId tid)
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
int64_t AssignStreams(NodeContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by the mobility models (inc...
void PushReferenceMobilityModel(Ptr< Object > reference)
a 3d vector
Definition: vector.h:31
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
Ptr< PositionAllocator > m_position
Keep track of the current position and velocity of an object.
static double DoRound(double v)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
Ptr< Object > Create(void) const
static void CourseChanged(Ptr< OutputStreamWrapper > stream, Ptr< const MobilityModel > mobility)
void AggregateObject(Ptr< Object > other)
Definition: object.cc:243
static void EnableAscii(Ptr< OutputStreamWrapper > stream, uint32_t nodeid)
ObjectFactory m_mobility
keep track of a set of node pointers.
hold objects of type Ptr
Definition: pointer.h:33
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
std::string GetName(void) const
Definition: type-id.cc:658
void Set(std::string name, const AttributeValue &value)
double y
y coordinate of vector
Definition: vector.h:53
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
void SetPosition(const Vector &position)
static NodeContainer GetGlobal(void)
Create a NodeContainer that contains a list of all nodes created through NodeContainer::Create() and ...
std::vector< Ptr< MobilityModel > > m_mobilityStack
instantiate subclasses of ns3::Object.
MobilityHelper()
Construct a Mobility Helper which is used to make life easier when working with mobility models...
uint32_t GetId(void) const
Definition: node.cc:104
A network Node.
Definition: node.h:55
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
static void EnableAsciiAll(Ptr< OutputStreamWrapper > stream)
std::string GetMobilityModelType(void) const
void PopReferenceMobilityModel(void)
Remove the top item from the top of the stack of "reference mobility models".
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
Ptr< T > GetObject(void) const
Definition: object.h:361
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:717
std::ostream * GetStream(void)
Return a pointer to an ostream previously set in the wrapper.
double z
z coordinate of vector
Definition: vector.h:57
Allocate a set of positions.