A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
mobility-helper.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
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 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19
20#ifndef MOBILITY_HELPER_H
21#define MOBILITY_HELPER_H
22
23#include "ns3/attribute.h"
24#include "ns3/node-container.h"
25#include "ns3/object-factory.h"
26#include "ns3/output-stream-wrapper.h"
27#include "ns3/position-allocator.h"
28
29#include <vector>
30
31namespace ns3
32{
33
34class PositionAllocator;
35class MobilityModel;
36
37/**
38 * \ingroup mobility
39 * \brief Helper class used to assign positions and mobility models to nodes.
40 *
41 * MobilityHelper::Install is the most important method here.
42 */
44{
45 public:
46 /**
47 * Construct a Mobility Helper which is used to make life easier when working
48 * with mobility models.
49 */
51
52 /**
53 * Destroy a Mobility Helper
54 */
56
57 /**
58 * Set the position allocator which will be used to allocate the initial
59 * position of every node initialized during MobilityModel::Install.
60 *
61 * \param allocator allocate initial node positions
62 */
64
65 /**
66 * \tparam Ts \deduced Argument types
67 * \param type the type of mobility model to use.
68 * \param [in] args Name and AttributeValue pairs to set.
69 */
70 template <typename... Ts>
71 void SetPositionAllocator(std::string type, Ts&&... args);
72
73 /**
74 * \tparam Ts \deduced Argument types
75 * \param type the type of mobility model to use.
76 * \param [in] args Name and AttributeValue pairs to set.
77 *
78 * Calls to MobilityHelper::Install will create an instance of a matching
79 * mobility model for each node.
80 */
81 template <typename... Ts>
82 void SetMobilityModel(std::string type, Ts&&... args);
83
84 /**
85 * \param reference item to push.
86 *
87 * Push an item on the top of the stack of "reference mobility models".
88 * The input item should be a node instance to which a mobility model
89 * has already been aggregated (usually by a call to Install).
90 *
91 * If this stack is not empty when MobilityHelper::Install
92 * is called, the model from the top of the stack is used
93 * to create a ns3::HierarchicalMobilityModel to make the
94 * newly-created models define their positions relative to that
95 * of the parent mobility model.
96 *
97 * This method is typically used to create hierarchical mobility
98 * patterns and positions by starting with the large-scale mobility
99 * features, and, then, defining the smaller-scale movements relative
100 * to a few reference points in the large-scale model.
101 */
103 /**
104 * \param referenceName named item to push.
105 *
106 * Push an item on the top of the stack of "reference mobility models".
107 * The input item should be a node instance to which a mobility model
108 * has already been aggregated (usually by a call to Install).
109 *
110 * If this stack is not empty when MobilityHelper::Install
111 * is called, the model from the top of the stack is used
112 * to create a ns3::HierarchicalMobilityModel to make the
113 * newly-created models define their positions relative to that
114 * of the parent mobility model.
115 *
116 * This method is typically used to create hierarchical mobility
117 * patterns and positions by starting with the large-scale mobility
118 * features, and, then, defining the smaller-scale movements relative
119 * to a few reference points in the large-scale model.
120 */
121 void PushReferenceMobilityModel(std::string referenceName);
122 /**
123 * Remove the top item from the top of the stack of
124 * "reference mobility models".
125 */
127
128 /**
129 * \return a string which contains the TypeId of the currently-selected
130 * mobility model.
131 */
132 std::string GetMobilityModelType() const;
133
134 /**
135 * \brief "Layout" a single node according to the current position allocator type.
136 *
137 * This method creates an instance of a ns3::MobilityModel subclass (the
138 * type of which was set with MobilityHelper::SetMobilityModel), aggregates
139 * it to the provided node, and sets an initial position based on the current
140 * position allocator (set through MobilityHelper::SetPositionAllocator).
141 *
142 * \param node The node to "layout."
143 */
144 void Install(Ptr<Node> node) const;
145 /**
146 * \brief "Layout" a single node according to the current position allocator type.
147 *
148 * This method creates an instance of a ns3::MobilityModel subclass (the
149 * type of which was set with MobilityHelper::SetMobilityModel), aggregates
150 * it to the provided node, and sets an initial position based on the current
151 * position allocator (set through MobilityHelper::SetPositionAllocator).
152 *
153 * \param nodeName The name of the node to "layout."
154 */
155 void Install(std::string nodeName) const;
156
157 /**
158 * \brief Layout a collection of nodes according to the current position allocator type.
159 *
160 * For each node in the provided NodeContainer, this method creates an instance
161 * of a ns3::MobilityModel subclass (the type of which was set with
162 * MobilityHelper::SetMobilityModel), aggregates it to the node, and sets an
163 * initial position based on the current position allocator (set through
164 * MobilityHelper::SetPositionAllocator).
165 *
166 * \param container The set of nodes to layout.
167 */
168 void Install(NodeContainer container) const;
169
170 /**
171 * Perform the work of MobilityHelper::Install on _all_ nodes which
172 * exist in the simulation.
173 */
174 void InstallAll() const;
175
176 /**
177 * \param stream an output stream wrapper
178 * \param nodeid the id of the node to generate ascii output for.
179 *
180 * Enable ascii output to record course changes from the mobility model
181 * associated with the specified nodeid and dump that to the specified output
182 * stream. If the Node does not have a MobilityModel aggregated,
183 * this method will not produce any output.
184 */
185 static void EnableAscii(Ptr<OutputStreamWrapper> stream, uint32_t nodeid);
186 /**
187 * \param stream an output stream wrapper
188 * \param n node container
189 *
190 * Enable ascii output to record course changes from the mobility models
191 * associated to the the nodes in the input container and dump that to the
192 * specified output stream. Nodes that do not have a MobilityModel
193 * aggregated will not result in any output.
194 */
196 /**
197 * \param stream an output stream wrapper
198 *
199 * Enable ascii output to record course changes from the mobility models
200 * associated to every node in the system and dump that to the specified
201 * output stream. Nodes that do not have a MobilityModel aggregated
202 * will not result in any output.
203 */
204 static void EnableAsciiAll(Ptr<OutputStreamWrapper> stream);
205 /**
206 * Assign a fixed random variable stream number to the random variables
207 * used by the mobility models on these nodes. Return the number of
208 * streams (possibly zero) that have been assigned. The Install()
209 * method should have previously been called by the user.
210 *
211 * \note If the PositionAllocator used contains random variables, they
212 * will not be affected by this call to AssignStreams because they are
213 * used earlier during Install() time. If the user needs to assign a fixed
214 * stream number to a PositionAllocator used with this helper, the user
215 * should instantiate it outside of the helper, call AssignStreams() on
216 * it, and then pass the pointer of it to this helper.
217 *
218 * \param c NodeContainer of the set of nodes for which the MobilityModels
219 * should be modified to use a fixed stream
220 * \param stream first stream index to use
221 * \return the number of stream indices assigned by this helper
222 */
223 int64_t AssignStreams(NodeContainer c, int64_t stream);
224
225 /**
226 * \param n1 node 1
227 * \param n2 node 2
228 * \return the distance (squared), in meters, between two nodes
229 */
230 static double GetDistanceSquaredBetween(Ptr<Node> n1, Ptr<Node> n2);
231
232 private:
233 /**
234 * Output course change events from mobility model to output stream
235 * \param stream output stream
236 * \param mobility mobility model
237 */
239 std::vector<Ptr<MobilityModel>> m_mobilityStack; //!< Internal stack of mobility models
240 ObjectFactory m_mobility; //!< Object factory to create mobility objects
242 m_position; //!< Position allocator for use in hierarchical mobility model
243};
244
245/***************************************************************
246 * Implementation of the templates declared above.
247 ***************************************************************/
248
249template <typename... Ts>
250void
251MobilityHelper::SetPositionAllocator(std::string type, Ts&&... args)
252{
253 ObjectFactory pos(type, std::forward<Ts>(args)...);
255}
256
257template <typename... Ts>
258void
259MobilityHelper::SetMobilityModel(std::string type, Ts&&... args)
260{
261 m_mobility.SetTypeId(type);
262 m_mobility.Set(std::forward<Ts>(args)...);
263}
264
265} // namespace ns3
266
267#endif /* MOBILITY_HELPER_H */
Helper class used to assign positions and mobility models to nodes.
void PopReferenceMobilityModel()
Remove the top item from the top of the stack of "reference mobility models".
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...
static void EnableAsciiAll(Ptr< OutputStreamWrapper > stream)
static double GetDistanceSquaredBetween(Ptr< Node > n1, Ptr< Node > n2)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
ObjectFactory m_mobility
Object factory to create mobility objects.
static void CourseChanged(Ptr< OutputStreamWrapper > stream, Ptr< const MobilityModel > mobility)
Output course change events from mobility model to output stream.
std::string GetMobilityModelType() const
void SetMobilityModel(std::string type, Ts &&... args)
~MobilityHelper()
Destroy a Mobility Helper.
Ptr< PositionAllocator > m_position
Position allocator for use in hierarchical mobility model.
void PushReferenceMobilityModel(Ptr< Object > reference)
void InstallAll() const
Perform the work of MobilityHelper::Install on all nodes which exist in the simulation.
MobilityHelper()
Construct a Mobility Helper which is used to make life easier when working with mobility models.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
static void EnableAscii(Ptr< OutputStreamWrapper > stream, uint32_t nodeid)
std::vector< Ptr< MobilityModel > > m_mobilityStack
Internal stack of mobility models.
keep track of a set of node pointers.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:522
Allocate a set of positions.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Every class exported by the ns3 library is enclosed in the ns3 namespace.