Loading...
Searching...
No Matches
satellite-group-helper.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013 Magister Solutions Ltd
4 * Copyright (c) 2018 CNES
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: Bastien Tauran <bastien.tauran@viveris.fr>
20 */
21
23
24#include "ns3/log.h"
25#include "ns3/satellite-id-mapper.h"
26#include "ns3/satellite-topology.h"
27#include "ns3/singleton.h"
28
29#include <algorithm>
30#include <list>
31#include <map>
32#include <set>
33#include <utility>
34#include <vector>
35
36NS_LOG_COMPONENT_DEFINE("SatGroupHelper");
37
38namespace ns3
39{
40
41NS_OBJECT_ENSURE_REGISTERED(SatGroupHelper);
42
43TypeId
45{
46 static TypeId tid =
47 TypeId("ns3::SatGroupHelper").SetParent<Object>().AddConstructor<SatGroupHelper>();
48 return tid;
49}
50
52 : m_scenarioCreated(false),
54{
55 NS_LOG_FUNCTION(this);
56}
57
58void
60{
61 NS_LOG_FUNCTION(this);
62
63 m_groupsMap.clear();
64}
65
66void
68{
69 NS_LOG_FUNCTION(this);
70
71 m_scenarioCreated = true;
72
73 for (std::map<Ptr<Node>, uint32_t>::iterator it = m_nodesToAdd.begin();
74 it != m_nodesToAdd.end();
75 it++)
76 {
77 AddUtNodeToGroup(it->second, it->first);
78 }
79}
80
81void
82SatGroupHelper::AddUtNodeToGroup(uint32_t groupId, Ptr<Node> node)
83{
84 NS_LOG_FUNCTION(this << groupId << node);
85
86 if (m_scenarioCreated == false)
87 {
88 NS_FATAL_ERROR("Method SatGroupHelper::AddUtNodeToGroup has to be called after "
89 "SimulationHelper::CreateSatScenario");
90 }
91
92 if (groupId == 0 && !m_satConstellationEnabled)
93 {
94 NS_FATAL_ERROR("Group ID 0 is reserved for UTs not manually assigned to a group");
95 }
96
97 if (GetGroupId(node) != 0)
98 {
99 NS_FATAL_ERROR("Node " << node << " is already in group " << GetGroupId(node)
100 << ". It cannot be added to group " << groupId);
101 }
102
103 if (IsGroupExisting(groupId) == false)
104 {
105 m_groupsList.push_back(groupId);
106 m_groupsMap[groupId] = std::set<Ptr<Node>>();
107 }
108 m_groupsMap[groupId].insert(node);
109 Singleton<SatIdMapper>::Get()->AttachMacToGroupId(
110 Singleton<SatIdMapper>::Get()->GetUtMacWithNode(node),
111 groupId);
112
113 Singleton<SatTopology>::Get()->UpdateUtGroup(node, groupId);
114}
115
116void
117SatGroupHelper::AddUtNodesToGroup(uint32_t groupId, NodeContainer nodes)
118{
119 NS_LOG_FUNCTION(this << groupId);
120
121 if (m_scenarioCreated == false)
122 {
123 NS_FATAL_ERROR("Method SatGroupHelper::AddUtNodesToGroup has to be called after "
124 "SimulationHelper::CreateSatScenario");
125 }
126
127 for (NodeContainer::Iterator it = nodes.Begin(); it != nodes.End(); it++)
128 {
129 AddUtNodeToGroup(groupId, *it);
130 }
131}
132
133void
135 NodeContainer nodes,
136 GeoCoordinate center,
137 uint32_t radius)
138{
139 NS_LOG_FUNCTION(this << groupId << center << radius);
140
141 if (m_scenarioCreated == false)
142 {
143 NS_FATAL_ERROR("Method SatGroupHelper::CreateGroupFromPosition has to be called after "
144 "SimulationHelper::CreateSatScenario");
145 }
146
147 if (groupId == 0)
148 {
149 NS_FATAL_ERROR("Cannot create new geographical group with a group ID of zero.");
150 }
151 if (GetUtNodes(groupId).GetN() != 0)
152 {
153 NS_FATAL_ERROR("Cannot create new geographical group with a group ID already used.");
154 }
155
156 Vector centerPosition = center.ToVector();
157 GeoCoordinate nodePosition;
158 double distance;
159 NodeContainer nodesNotAlreadyAdded = GetNodesNotAddedFromPosition(nodes);
160 for (NodeContainer::Iterator it = nodesNotAlreadyAdded.Begin();
161 it != nodesNotAlreadyAdded.End();
162 it++)
163 {
164 nodePosition = (*it)->GetObject<SatMobilityModel>()->GetGeoPosition();
165 distance = CalculateDistance(centerPosition, nodePosition.ToVector());
166 if (distance <= radius)
167 {
168 AddUtNodeToGroup(groupId, *it);
169 }
170 }
171}
172
173void
174SatGroupHelper::CreateGroupsUniformly(std::vector<uint32_t> groupIds, NodeContainer nodes)
175{
176 NS_LOG_FUNCTION(this << groupIds);
177
178 if (m_scenarioCreated == false)
179 {
180 NS_FATAL_ERROR("Method SatGroupHelper::CreateGroupsUniformly has to be called after "
181 "SimulationHelper::CreateSatScenario");
182 }
183
184 for (uint32_t groupId : groupIds)
185 {
186 if (GetUtNodes(groupId).GetN() != 0)
187 {
188 NS_FATAL_ERROR("Cannot create new group with a group ID already used: " << groupId);
189 }
190 }
191
192 NodeContainer nodesNotAlreadyAdded = GetNodesNotAddedFromPosition(nodes);
193
194 uint32_t nbNodes = nodesNotAlreadyAdded.GetN();
195 uint32_t counter = 0;
196
197 for (uint32_t i = 0; i < nbNodes; i++)
198 {
199 AddUtNodeToGroup(groupIds[counter], nodesNotAlreadyAdded.Get(i));
200 counter++;
201 counter %= groupIds.size();
202 }
203}
204
205void
207 uint32_t nb,
208 GeoCoordinate center,
209 uint32_t radius)
210{
211 NS_LOG_FUNCTION(this << groupId << nb << center << radius);
212
213 if (m_scenarioCreated == true)
214 {
215 NS_FATAL_ERROR("Method SatGroupHelper::CreateUtNodesFromPosition has to be called before "
216 "SimulationHelper::CreateSatScenario");
217 }
218
219 if (groupId == 0)
220 {
221 NS_FATAL_ERROR("Cannot call CreateUtNodesFromPosition with a group ID of zero.");
222 }
223 if (std::find(m_groupsList.begin(), m_groupsList.end(), groupId) != m_groupsList.end())
224 {
225 NS_FATAL_ERROR(
226 "Cannot call CreateUtNodesFromPosition with a group ID already used: " << groupId);
227 }
228
229 Ptr<SatRandomCirclePositionAllocator> circleAllocator =
230 CreateObject<SatRandomCirclePositionAllocator>(center, radius);
231
232 for (uint32_t i = 0; i < nb; i++)
233 {
234 GeoCoordinate position = circleAllocator->GetNextGeoPosition();
235 m_additionalNodesPerBeam.push_back(std::make_pair(position, groupId));
236 }
237
238 m_groupsList.push_back(groupId);
239}
240
241void
243{
244 NS_LOG_FUNCTION(this << groupId << node);
245
246 m_nodesToAdd[node] = groupId;
247}
248
249std::vector<std::pair<GeoCoordinate, uint32_t>>
251{
252 NS_LOG_FUNCTION(this);
253
255}
256
257NodeContainer
258SatGroupHelper::GetUtNodes(uint32_t groupId) const
259{
260 NS_LOG_FUNCTION(this << groupId);
261
262 if (groupId == 0)
263 {
264 bool found;
265 NodeContainer groupIdZeroUts;
266 NodeContainer uts = Singleton<SatTopology>::Get()->GetUtNodes();
267 for (NodeContainer::Iterator it = uts.Begin(); it != uts.End(); it++)
268 {
269 found = false;
270 for (std::map<uint32_t, std::set<Ptr<Node>>>::const_iterator it2 = m_groupsMap.begin();
271 it2 != m_groupsMap.end();
272 it2++)
273 {
274 if (it2->second.find(*it) != it2->second.end())
275 {
276 found = true;
277 break;
278 }
279 }
280 if (found == false)
281 {
282 groupIdZeroUts.Add(*it);
283 }
284 }
285 return groupIdZeroUts;
286 }
287
288 if (IsGroupExisting(groupId) == false)
289 {
290 return NodeContainer();
291 }
292
293 NodeContainer utNodes;
294 std::set<Ptr<Node>> nodes = m_groupsMap.at(groupId);
295
296 for (std::set<Ptr<Node>>::const_iterator i = nodes.begin(); i != nodes.end(); i++)
297 {
298 utNodes.Add(*i);
299 }
300
301 return utNodes;
302}
303
304uint32_t
306{
307 NS_LOG_FUNCTION(this);
308
309 return m_groupsMap.size();
310}
311
312std::list<uint32_t>
317
318void
323
324bool
325SatGroupHelper::IsGroupExisting(uint32_t groupId) const
326{
327 return m_groupsMap.find(groupId) != m_groupsMap.end();
328}
329
330uint32_t
331SatGroupHelper::GetGroupId(Ptr<Node> node) const
332{
333 for (std::map<uint32_t, std::set<Ptr<Node>>>::const_iterator it = m_groupsMap.begin();
334 it != m_groupsMap.end();
335 it++)
336 {
337 if (it->second.find(node) != it->second.end())
338 {
339 return it->first;
340 }
341 }
342 return 0;
343}
344
345NodeContainer
347{
348 NS_LOG_FUNCTION(this);
349
350 NodeContainer nodesFiltered;
351 Ptr<Node> node;
352 for (uint32_t i = 0; i < nodes.GetN(); i++)
353 {
354 node = nodes.Get(i);
355 if (m_nodesToAdd.count(node) == 0)
356 {
357 nodesFiltered.Add(node);
358 }
359 }
360 return nodesFiltered;
361}
362
363} // namespace ns3
GeoCoordinate class is used to store and operate with geodetic coordinates.
Vector ToVector() const
Converts Geodetic coordinates to Cartesian coordinates.
This helper is used to create groups of UTs.
std::list< uint32_t > GetGroups()
Get the list of groups created.
virtual void DoDispose()
Dispose of this class instance.
NodeContainer GetUtNodes(uint32_t groupId) const
uint32_t GetN()
Count the number of groups created.
void AddUtNodesToGroup(uint32_t groupId, NodeContainer nodes)
Add several nodes to a group.
void Init()
Initialize the helper.
void CreateUtNodesFromPosition(uint32_t groupId, uint32_t nb, GeoCoordinate center, uint32_t radius)
Create a new group using a central position and a radius, and create UT nodes inside this area.
static TypeId GetTypeId(void)
Get the type ID.
std::map< Ptr< Node >, uint32_t > m_nodesToAdd
std::map< uint32_t, std::set< Ptr< Node > > > m_groupsMap
Container to associate nodes to the groups.
void AddNodeToGroupAfterScenarioCreation(uint32_t groupId, Ptr< Node > node)
Schedule a node to be added to a group when scenario creation is finished.
std::list< uint32_t > m_groupsList
List of group ID created.
bool m_satConstellationEnabled
Use a constellation of satellites.
void CreateGroupsUniformly(std::vector< uint32_t > groupIds, NodeContainer nodes)
Create several groups and distribute nodes among them.
NodeContainer GetNodesNotAddedFromPosition(NodeContainer nodes)
Get list of nodes not created from position by group helper.
bool IsGroupExisting(uint32_t groupId) const
Tells if the groupId is already existing in the database.
std::vector< std::pair< GeoCoordinate, uint32_t > > GetAdditionalNodesPerBeam()
Get the position of nodes to add to the scenario.
SatGroupHelper()
Default constructor for SatGroupHelper.
uint32_t GetGroupId(Ptr< Node > node) const
Get the group to which a node belongs.
void CreateGroupFromPosition(uint32_t groupId, NodeContainer nodes, GeoCoordinate center, uint32_t radius)
Create a new group using a central position and a radius.
std::vector< std::pair< GeoCoordinate, uint32_t > > m_additionalNodesPerBeam
Nodes created by position to add to scenario.
void AddUtNodeToGroup(uint32_t groupId, Ptr< Node > node)
Add a node to a group.
Keep track of the current position and velocity of an object in satellite network.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.