A Discrete-Event Network Simulator
API
group-mobility-helper.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 INRIA
3  * Copyright (c) 2021 University of Washington: Group mobility changes
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  * Adapted from 'mobility-helper.cc' for group mobility by Tom Henderson
20  */
21 
22 #include "group-mobility-helper.h"
23 
24 #include "ns3/config.h"
25 #include "ns3/hierarchical-mobility-model.h"
26 #include "ns3/log.h"
27 #include "ns3/mobility-model.h"
28 #include "ns3/names.h"
29 #include "ns3/pointer.h"
30 #include "ns3/position-allocator.h"
31 #include "ns3/simulator.h"
32 #include "ns3/string.h"
33 
34 #include <iostream>
35 
36 namespace ns3
37 {
38 
39 NS_LOG_COMPONENT_DEFINE("GroupMobilityHelper");
40 
42  : NS_LOG_TEMPLATE_DEFINE("GroupMobilityHelper")
43 {
44 }
45 
47 {
48 }
49 
50 void
52 {
53  m_referencePosition = allocator;
54 }
55 
56 void
58 {
59  m_memberPosition = allocator;
60 }
61 
62 void
64 {
66 }
67 
68 void
70 {
71  NS_ABORT_MSG_IF(node->GetObject<MobilityModel>(), "Mobility model already installed");
72  NS_ABORT_MSG_IF(!m_referenceMobility, "Reference mobility model is empty");
73  NS_ABORT_MSG_UNLESS(m_memberMobilityFactory.IsTypeIdSet(), "Member mobility factory is unset");
75  {
76  Vector referencePosition = m_referencePosition->GetNext();
77  m_referenceMobility->SetPosition(referencePosition);
79  }
80  Ptr<HierarchicalMobilityModel> hierarchical = CreateObject<HierarchicalMobilityModel>();
81  hierarchical->SetParent(m_referenceMobility);
83  NS_ABORT_MSG_IF(!child, "Member mobility factory did not produce a MobilityModel");
84  if (m_memberPosition)
85  {
86  Vector position = m_memberPosition->GetNext();
87  child->SetPosition(position);
88  }
89  hierarchical->SetChild(child);
90  NS_LOG_DEBUG("node=" << node << ", mob=" << hierarchical);
91  node->AggregateObject(hierarchical);
92 }
93 
94 void
95 GroupMobilityHelper::Install(std::string nodeName)
96 {
97  Ptr<Node> node = Names::Find<Node>(nodeName);
98  Install(node);
99 }
100 
101 void
103 {
104  for (auto i = c.Begin(); i != c.End(); ++i)
105  {
106  Install(*i);
107  }
108 }
109 
110 int64_t
112 {
113  int64_t currentStream = stream;
114  Ptr<Node> node;
115  bool firstNode = true;
117  for (auto i = c.Begin(); i != c.End(); ++i)
118  {
119  node = (*i);
121  if (!mobility)
122  {
123  NS_FATAL_ERROR("Did not find a HierarchicalMobilityModel");
124  }
125  if (firstNode)
126  {
127  // Assign streams only once for the reference node
128  currentStream += mobility->GetParent()->AssignStreams(currentStream);
129  firstNode = false;
130  }
131  currentStream += mobility->GetChild()->AssignStreams(currentStream);
132  }
133  return (currentStream - stream);
134 }
135 
136 } // namespace ns3
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.
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 End() const
Get an iterator which indicates past-the-last Node in the container.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
bool IsTypeIdSet() const
Check if the ObjectFactory has been configured with a TypeId.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:259
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
#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:179
#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:202
#define NS_LOG_TEMPLATE_DEFINE(name)
Initialize a reference to a Log component.
Definition: log.h:236
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
Every class exported by the ns3 library is enclosed in the ns3 namespace.
mobility
Definition: third.py:105