A Discrete-Event Network Simulator
API
group-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  * Copyright (c) 2021 University of Washington: Group mobility changes
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20  * Adapted from 'mobility-helper.cc' for group mobility by Tom Henderson
21  */
22 
23 #include "ns3/group-mobility-helper.h"
24 #include "ns3/mobility-model.h"
25 #include "ns3/position-allocator.h"
26 #include "ns3/hierarchical-mobility-model.h"
27 #include "ns3/log.h"
28 #include "ns3/pointer.h"
29 #include "ns3/config.h"
30 #include "ns3/simulator.h"
31 #include "ns3/names.h"
32 #include "ns3/string.h"
33 #include <iostream>
34 
35 namespace ns3 {
36 
37 NS_LOG_COMPONENT_DEFINE ("GroupMobilityHelper");
38 
40 {
41 }
42 
44 {
45 }
46 
47 void
49 {
50  m_referencePosition = 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  NS_ABORT_MSG_IF (m_referencePosition == nullptr, "Unable to create allocator from TypeId " << type);
78 }
79 
80 void
82 {
83  m_memberPosition = allocator;
84 }
85 
86 void
88  std::string n1, const AttributeValue &v1,
89  std::string n2, const AttributeValue &v2,
90  std::string n3, const AttributeValue &v3,
91  std::string n4, const AttributeValue &v4,
92  std::string n5, const AttributeValue &v5,
93  std::string n6, const AttributeValue &v6,
94  std::string n7, const AttributeValue &v7,
95  std::string n8, const AttributeValue &v8,
96  std::string n9, const AttributeValue &v9)
97 {
98  ObjectFactory pos;
99  pos.SetTypeId (type);
100  pos.Set (n1, v1);
101  pos.Set (n2, v2);
102  pos.Set (n3, v3);
103  pos.Set (n4, v4);
104  pos.Set (n5, v5);
105  pos.Set (n6, v6);
106  pos.Set (n7, v7);
107  pos.Set (n8, v8);
108  pos.Set (n9, v9);
110  NS_ABORT_MSG_IF (m_memberPosition == nullptr, "Unable to create allocator from TypeId " << type);
111 }
112 
113 void
115 {
117 }
118 
119 void
121  std::string n1, const AttributeValue &v1,
122  std::string n2, const AttributeValue &v2,
123  std::string n3, const AttributeValue &v3,
124  std::string n4, const AttributeValue &v4,
125  std::string n5, const AttributeValue &v5,
126  std::string n6, const AttributeValue &v6,
127  std::string n7, const AttributeValue &v7,
128  std::string n8, const AttributeValue &v8,
129  std::string n9, const AttributeValue &v9)
130 {
131  NS_LOG_FUNCTION (this << type);
132  ObjectFactory mob;
133  mob.SetTypeId (type);
134  mob.Set (n1, v1);
135  mob.Set (n2, v2);
136  mob.Set (n3, v3);
137  mob.Set (n4, v4);
138  mob.Set (n5, v5);
139  mob.Set (n6, v6);
140  mob.Set (n7, v7);
141  mob.Set (n8, v8);
142  mob.Set (n9, v9);
144  NS_ABORT_MSG_IF (m_referenceMobility == nullptr, "Unable to create mobility from TypeId " << type);
145 }
146 
147 void
149  std::string n1, const AttributeValue &v1,
150  std::string n2, const AttributeValue &v2,
151  std::string n3, const AttributeValue &v3,
152  std::string n4, const AttributeValue &v4,
153  std::string n5, const AttributeValue &v5,
154  std::string n6, const AttributeValue &v6,
155  std::string n7, const AttributeValue &v7,
156  std::string n8, const AttributeValue &v8,
157  std::string n9, const AttributeValue &v9)
158 {
159  NS_LOG_FUNCTION (this << type);
161  m_memberMobilityFactory.Set (n1, v1);
162  m_memberMobilityFactory.Set (n2, v2);
163  m_memberMobilityFactory.Set (n3, v3);
164  m_memberMobilityFactory.Set (n4, v4);
165  m_memberMobilityFactory.Set (n5, v5);
166  m_memberMobilityFactory.Set (n6, v6);
167  m_memberMobilityFactory.Set (n7, v7);
168  m_memberMobilityFactory.Set (n8, v8);
169  m_memberMobilityFactory.Set (n9, v9);
170 }
171 
172 void
174 {
175  NS_ABORT_MSG_IF (node->GetObject<MobilityModel> () != nullptr, "Mobility model already installed");
176  NS_ABORT_MSG_IF (m_referenceMobility == nullptr, "Reference mobility model is empty");
177  NS_ABORT_MSG_UNLESS (m_memberMobilityFactory.IsTypeIdSet (), "Member mobility factory is unset");
179  {
180  Vector referencePosition = m_referencePosition->GetNext ();
181  m_referenceMobility->SetPosition (referencePosition);
182  m_referencePositionSet = true;
183  }
184  Ptr<HierarchicalMobilityModel> hierarchical = CreateObject<HierarchicalMobilityModel> ();
185  hierarchical->SetParent (m_referenceMobility);
187  NS_ABORT_MSG_IF (child == nullptr, "Member mobility factory did not produce a MobilityModel");
188  if (m_memberPosition)
189  {
190  Vector position = m_memberPosition->GetNext ();
191  child->SetPosition (position);
192  }
193  hierarchical->SetChild (child);
194  NS_LOG_DEBUG ("node="<<node<<", mob="<<hierarchical);
195  node->AggregateObject (hierarchical);
196 }
197 
198 void
199 GroupMobilityHelper::Install (std::string nodeName)
200 {
201  Ptr<Node> node = Names::Find<Node> (nodeName);
202  Install (node);
203 }
204 
205 void
207 {
208  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
209  {
210  Install (*i);
211  }
212 }
213 int64_t
215 {
216  int64_t currentStream = stream;
217  Ptr<Node> node;
218  bool firstNode = true;
220  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
221  {
222  node = (*i);
224  if (!mobility)
225  {
226  NS_FATAL_ERROR ("Did not find a HierarchicalMobilityModel");
227  }
228  if (firstNode)
229  {
230  // Assign streams only once for the reference node
231  currentStream += mobility->GetParent ()->AssignStreams (currentStream);
232  firstNode = false;
233  }
234  currentStream += mobility->GetChild ()->AssignStreams (currentStream);
235  }
236  return (currentStream - stream);
237 }
238 
239 } // namespace ns3
Hold a value for an Attribute.
Definition: attribute.h:69
int64_t AssignStreams(NodeContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by the mobility models on t...
ObjectFactory m_memberMobilityFactory
Object factory to create member mobility models.
void SetMemberPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of the member mobility...
void Install(Ptr< Node > node)
Install and configure a hierarchical mobility model to the given node, based on the configured refere...
void SetReferenceMobilityModel(Ptr< MobilityModel > mobility)
Set the reference mobility model which will be installed as the parent mobility model during GroupMob...
bool m_referencePositionSet
flag for avoiding multiple SetPosition calls on the reference model
~GroupMobilityHelper()
Destroy a group mobility helper.
Ptr< PositionAllocator > m_referencePosition
Position allocator for use as reference position allocator.
Ptr< PositionAllocator > m_memberPosition
Position allocator for use as member position allocator.
void SetReferencePositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of the reference mobil...
Ptr< MobilityModel > m_referenceMobility
Reference mobility model.
GroupMobilityHelper()
Construct a group mobility helper.
void SetMemberMobilityModel(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())
Configure the mobility model which will be installed as the member (child) mobility model during Grou...
Keep track of the current position and velocity of an object.
void SetPosition(const Vector &position)
keep track of a set of node pointers.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Instantiate subclasses of ns3::Object.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
bool IsTypeIdSet(void) const
Check if the ObjectFactory has been configured with a TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
Allocate a set of positions.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
mobility
Definition: third.py:108