A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
point-to-point-grid.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Author: Josh Pelkey <jpelkey@gatech.edu>
5 */
6
7// Implement an object to create a grid topology.
8
10
11#include "ns3/constant-position-mobility-model.h"
12#include "ns3/internet-stack-helper.h"
13#include "ns3/ipv6-address-generator.h"
14#include "ns3/log.h"
15#include "ns3/point-to-point-helper.h"
16#include "ns3/string.h"
17#include "ns3/vector.h"
18
19namespace ns3
20{
21
22NS_LOG_COMPONENT_DEFINE("PointToPointGridHelper");
23
25 uint32_t nCols,
26 PointToPointHelper pointToPoint)
27 : m_xSize(nCols),
28 m_ySize(nRows)
29{
30 // Bounds check
31 if (m_xSize < 1 || m_ySize < 1 || (m_xSize < 2 && m_ySize < 2))
32 {
33 NS_FATAL_ERROR("Need more nodes for grid.");
34 }
35
37
38 for (uint32_t y = 0; y < nRows; ++y)
39 {
40 NodeContainer rowNodes;
41 NetDeviceContainer rowDevices;
42 NetDeviceContainer colDevices;
43
44 for (uint32_t x = 0; x < nCols; ++x)
45 {
46 rowNodes.Create(1);
47
48 // install p2p links across the row
49 if (x > 0)
50 {
51 rowDevices.Add(pointToPoint.Install(rowNodes.Get(x - 1), rowNodes.Get(x)));
52 }
53
54 // install vertical p2p links
55 if (y > 0)
56 {
57 colDevices.Add(pointToPoint.Install((m_nodes.at(y - 1)).Get(x), rowNodes.Get(x)));
58 }
59 }
60
61 m_nodes.push_back(rowNodes);
62 m_rowDevices.push_back(rowDevices);
63
64 if (y > 0)
65 {
66 m_colDevices.push_back(colDevices);
67 }
68 }
69}
70
74
75void
77{
78 for (uint32_t i = 0; i < m_nodes.size(); ++i)
79 {
80 NodeContainer rowNodes = m_nodes[i];
81 for (uint32_t j = 0; j < rowNodes.GetN(); ++j)
82 {
83 stack.Install(rowNodes.Get(j));
84 }
85 }
86}
87
88void
90{
91 // Assign addresses to all row devices in the grid.
92 // These devices are stored in a vector. Each row
93 // of the grid has all the row devices in one entry
94 // of the vector. These entries come in pairs.
95 for (uint32_t i = 0; i < m_rowDevices.size(); ++i)
96 {
97 Ipv4InterfaceContainer rowInterfaces;
98 NetDeviceContainer rowContainer = m_rowDevices[i];
99 for (uint32_t j = 0; j < rowContainer.GetN(); j += 2)
100 {
101 rowInterfaces.Add(rowIp.Assign(rowContainer.Get(j)));
102 rowInterfaces.Add(rowIp.Assign(rowContainer.Get(j + 1)));
103 rowIp.NewNetwork();
104 }
105 m_rowInterfaces.push_back(rowInterfaces);
106 }
107
108 // Assign addresses to all col devices in the grid.
109 // These devices are stored in a vector. Each col
110 // of the grid has all the col devices in one entry
111 // of the vector. These entries come in pairs.
112 for (uint32_t i = 0; i < m_colDevices.size(); ++i)
113 {
114 Ipv4InterfaceContainer colInterfaces;
115 NetDeviceContainer colContainer = m_colDevices[i];
116 for (uint32_t j = 0; j < colContainer.GetN(); j += 2)
117 {
118 colInterfaces.Add(colIp.Assign(colContainer.Get(j)));
119 colInterfaces.Add(colIp.Assign(colContainer.Get(j + 1)));
120 colIp.NewNetwork();
121 }
122 m_colInterfaces.push_back(colInterfaces);
123 }
124}
125
126void
128{
129 Ipv6AddressGenerator::Init(addrBase, prefix);
130 Ipv6Address v6network;
131 Ipv6AddressHelper addrHelper;
132
133 // Assign addresses to all row devices in the grid.
134 // These devices are stored in a vector. Each row
135 // of the grid has all the row devices in one entry
136 // of the vector. These entries come in pairs.
137 for (uint32_t i = 0; i < m_rowDevices.size(); ++i)
138 {
139 Ipv6InterfaceContainer rowInterfaces;
140 NetDeviceContainer rowContainer = m_rowDevices[i];
141 for (uint32_t j = 0; j < rowContainer.GetN(); j += 2)
142 {
143 v6network = Ipv6AddressGenerator::GetNetwork(prefix);
144 addrHelper.SetBase(v6network, prefix);
145 Ipv6InterfaceContainer ic = addrHelper.Assign(rowContainer.Get(j));
146 rowInterfaces.Add(ic);
147 ic = addrHelper.Assign(rowContainer.Get(j + 1));
148 rowInterfaces.Add(ic);
150 }
151 m_rowInterfaces6.push_back(rowInterfaces);
152 }
153
154 // Assign addresses to all col devices in the grid.
155 // These devices are stored in a vector. Each col
156 // of the grid has all the col devices in one entry
157 // of the vector. These entries come in pairs.
158 for (uint32_t i = 0; i < m_colDevices.size(); ++i)
159 {
160 Ipv6InterfaceContainer colInterfaces;
161 NetDeviceContainer colContainer = m_colDevices[i];
162 for (uint32_t j = 0; j < colContainer.GetN(); j += 2)
163 {
164 v6network = Ipv6AddressGenerator::GetNetwork(prefix);
165 addrHelper.SetBase(v6network, prefix);
166 Ipv6InterfaceContainer ic = addrHelper.Assign(colContainer.Get(j));
167 colInterfaces.Add(ic);
168 ic = addrHelper.Assign(colContainer.Get(j + 1));
169 colInterfaces.Add(ic);
171 }
172 m_colInterfaces6.push_back(colInterfaces);
173 }
174}
175
176void
177PointToPointGridHelper::BoundingBox(double ulx, double uly, double lrx, double lry)
178{
179 double xDist;
180 double yDist;
181 if (lrx > ulx)
182 {
183 xDist = lrx - ulx;
184 }
185 else
186 {
187 xDist = ulx - lrx;
188 }
189 if (lry > uly)
190 {
191 yDist = lry - uly;
192 }
193 else
194 {
195 yDist = uly - lry;
196 }
197 double xAdder = xDist / m_xSize;
198 double yAdder = yDist / m_ySize;
199 double yLoc = yDist / 2;
200 for (uint32_t i = 0; i < m_ySize; ++i)
201 {
202 double xLoc = xDist / 2;
203 for (uint32_t j = 0; j < m_xSize; ++j)
204 {
205 Ptr<Node> node = GetNode(i, j);
207 node->GetObject<ConstantPositionMobilityModel>();
208 if (!loc)
209 {
211 node->AggregateObject(loc);
212 }
213 Vector locVec(xLoc, yLoc, 0);
214 loc->SetPosition(locVec);
215
216 xLoc += xAdder;
217 }
218 yLoc += yAdder;
219 }
220}
221
224{
225 if (row > m_nodes.size() - 1 || col > m_nodes.at(row).GetN() - 1)
226 {
227 NS_FATAL_ERROR("Index out of bounds in PointToPointGridHelper::GetNode.");
228 }
229
230 return (m_nodes.at(row)).Get(col);
231}
232
235{
236 if (row > m_nodes.size() - 1 || col > m_nodes.at(row).GetN() - 1)
237 {
238 NS_FATAL_ERROR("Index out of bounds in PointToPointGridHelper::GetIpv4Address.");
239 }
240
241 // Right now this just gets one of the addresses of the
242 // specified node. The exact device can't be specified.
243 // If you picture the grid, the address returned is the
244 // address of the left (row) device of all nodes, with
245 // the exception of the left-most nodes in the grid;
246 // in which case the right (row) device address is
247 // returned
248 if (col == 0)
249 {
250 return (m_rowInterfaces.at(row)).GetAddress(0);
251 }
252 else
253 {
254 return (m_rowInterfaces.at(row)).GetAddress((2 * col) - 1);
255 }
256}
257
260{
261 if (row > m_nodes.size() - 1 || col > m_nodes.at(row).GetN() - 1)
262 {
263 NS_FATAL_ERROR("Index out of bounds in PointToPointGridHelper::GetIpv6Address.");
264 }
265
266 // Right now this just gets one of the addresses of the
267 // specified node. The exact device can't be specified.
268 // If you picture the grid, the address returned is the
269 // address of the left (row) device of all nodes, with
270 // the exception of the left-most nodes in the grid;
271 // in which case the right (row) device address is
272 // returned
273 if (col == 0)
274 {
275 return (m_rowInterfaces6.at(row)).GetAddress(0, 1);
276 }
277 else
278 {
279 return (m_rowInterfaces6.at(row)).GetAddress((2 * col) - 1, 1);
280 }
281}
282
283} // namespace ns3
Mobility model for which the current position does not change once it has been set and until it is se...
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4Address NewNetwork()
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
static void Init(const Ipv6Address net, const Ipv6Prefix prefix, const Ipv6Address interfaceId="::1")
Initialise the base network and interfaceId for the generator.
static Ipv6Address GetNetwork(const Ipv6Prefix prefix)
Get the current network of the given Ipv6Prefix.
static Ipv6Address NextNetwork(const Ipv6Prefix prefix)
Get the next network according to the given Ipv6Prefix.
Helper class to auto-assign global IPv6 unicast addresses.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Describes an IPv6 address.
Keep track of a set of IPv6 interfaces.
void Add(Ptr< Ipv6 > ipv6, uint32_t interface)
Add a couple IPv6/interface.
Describes an IPv6 prefix.
holds a vector of ns3::NetDevice pointers
uint32_t GetN() const
Get the number of Ptr<NetDevice> stored in this container.
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
uint32_t GetN() const
Get the number of Ptr<Node> stored in this container.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
std::vector< Ipv6InterfaceContainer > m_colInterfaces6
IPv6 interfaces in a column.
void InstallStack(InternetStackHelper stack)
void AssignIpv6Addresses(Ipv6Address network, Ipv6Prefix prefix)
Assigns Ipv6 addresses to all the row and column interfaces.
Ipv4Address GetIpv4Address(uint32_t row, uint32_t col)
This returns an Ipv4 address at the node specified by the (row, col) address.
Ptr< Node > GetNode(uint32_t row, uint32_t col)
uint32_t m_ySize
Y size of the grid (number of rows)
Ipv6Address GetIpv6Address(uint32_t row, uint32_t col)
This returns an Ipv6 address at the node specified by the (row, col) address.
std::vector< NodeContainer > m_nodes
all the nodes in the grid
std::vector< NetDeviceContainer > m_colDevices
NetDevices in a column.
std::vector< NetDeviceContainer > m_rowDevices
NetDevices in a row.
std::vector< Ipv6InterfaceContainer > m_rowInterfaces6
IPv6 interfaces in a row.
void BoundingBox(double ulx, double uly, double lrx, double lry)
Sets up the node canvas locations for every node in the grid.
std::vector< Ipv4InterfaceContainer > m_colInterfaces
IPv4 interfaces in a column.
void AssignIpv4Addresses(Ipv4AddressHelper rowIp, Ipv4AddressHelper colIp)
Assigns Ipv4 addresses to all the row and column interfaces.
uint32_t m_xSize
X size of the grid (number of columns)
PointToPointGridHelper(uint32_t nRows, uint32_t nCols, PointToPointHelper pointToPoint)
Create a PointToPointGridHelper in order to easily create grid topologies using p2p links.
std::vector< Ipv4InterfaceContainer > m_rowInterfaces
IPv4 interfaces in a row.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Every class exported by the ns3 library is enclosed in the ns3 namespace.