A Discrete-Event Network Simulator
API
point-to-point-grid.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * Author: Josh Pelkey <jpelkey@gatech.edu>
16  */
17 
18 // Implement an object to create a grid topology.
19 
20 #include "point-to-point-grid.h"
21 
22 #include "ns3/constant-position-mobility-model.h"
23 #include "ns3/internet-stack-helper.h"
24 #include "ns3/ipv6-address-generator.h"
25 #include "ns3/log.h"
26 #include "ns3/point-to-point-helper.h"
27 #include "ns3/string.h"
28 #include "ns3/vector.h"
29 
30 namespace ns3
31 {
32 
33 NS_LOG_COMPONENT_DEFINE("PointToPointGridHelper");
34 
36  uint32_t nCols,
38  : m_xSize(nCols),
39  m_ySize(nRows)
40 {
41  // Bounds check
42  if (m_xSize < 1 || m_ySize < 1 || (m_xSize < 2 && m_ySize < 2))
43  {
44  NS_FATAL_ERROR("Need more nodes for grid.");
45  }
46 
48 
49  for (uint32_t y = 0; y < nRows; ++y)
50  {
51  NodeContainer rowNodes;
52  NetDeviceContainer rowDevices;
53  NetDeviceContainer colDevices;
54 
55  for (uint32_t x = 0; x < nCols; ++x)
56  {
57  rowNodes.Create(1);
58 
59  // install p2p links across the row
60  if (x > 0)
61  {
62  rowDevices.Add(pointToPoint.Install(rowNodes.Get(x - 1), rowNodes.Get(x)));
63  }
64 
65  // install vertical p2p links
66  if (y > 0)
67  {
68  colDevices.Add(pointToPoint.Install((m_nodes.at(y - 1)).Get(x), rowNodes.Get(x)));
69  }
70  }
71 
72  m_nodes.push_back(rowNodes);
73  m_rowDevices.push_back(rowDevices);
74 
75  if (y > 0)
76  {
77  m_colDevices.push_back(colDevices);
78  }
79  }
80 }
81 
83 {
84 }
85 
86 void
88 {
89  for (uint32_t i = 0; i < m_nodes.size(); ++i)
90  {
91  NodeContainer rowNodes = m_nodes[i];
92  for (uint32_t j = 0; j < rowNodes.GetN(); ++j)
93  {
94  stack.Install(rowNodes.Get(j));
95  }
96  }
97 }
98 
99 void
101 {
102  // Assign addresses to all row devices in the grid.
103  // These devices are stored in a vector. Each row
104  // of the grid has all the row devices in one entry
105  // of the vector. These entries come in pairs.
106  for (uint32_t i = 0; i < m_rowDevices.size(); ++i)
107  {
108  Ipv4InterfaceContainer rowInterfaces;
109  NetDeviceContainer rowContainer = m_rowDevices[i];
110  for (uint32_t j = 0; j < rowContainer.GetN(); j += 2)
111  {
112  rowInterfaces.Add(rowIp.Assign(rowContainer.Get(j)));
113  rowInterfaces.Add(rowIp.Assign(rowContainer.Get(j + 1)));
114  rowIp.NewNetwork();
115  }
116  m_rowInterfaces.push_back(rowInterfaces);
117  }
118 
119  // Assign addresses to all col devices in the grid.
120  // These devices are stored in a vector. Each col
121  // of the grid has all the col devices in one entry
122  // of the vector. These entries come in pairs.
123  for (uint32_t i = 0; i < m_colDevices.size(); ++i)
124  {
125  Ipv4InterfaceContainer colInterfaces;
126  NetDeviceContainer colContainer = m_colDevices[i];
127  for (uint32_t j = 0; j < colContainer.GetN(); j += 2)
128  {
129  colInterfaces.Add(colIp.Assign(colContainer.Get(j)));
130  colInterfaces.Add(colIp.Assign(colContainer.Get(j + 1)));
131  colIp.NewNetwork();
132  }
133  m_colInterfaces.push_back(colInterfaces);
134  }
135 }
136 
137 void
139 {
140  Ipv6AddressGenerator::Init(addrBase, prefix);
141  Ipv6Address v6network;
142  Ipv6AddressHelper addrHelper;
143 
144  // Assign addresses to all row devices in the grid.
145  // These devices are stored in a vector. Each row
146  // of the grid has all the row devices in one entry
147  // of the vector. These entries come in pairs.
148  for (uint32_t i = 0; i < m_rowDevices.size(); ++i)
149  {
150  Ipv6InterfaceContainer rowInterfaces;
151  NetDeviceContainer rowContainer = m_rowDevices[i];
152  for (uint32_t j = 0; j < rowContainer.GetN(); j += 2)
153  {
154  v6network = Ipv6AddressGenerator::GetNetwork(prefix);
155  addrHelper.SetBase(v6network, prefix);
156  Ipv6InterfaceContainer ic = addrHelper.Assign(rowContainer.Get(j));
157  rowInterfaces.Add(ic);
158  ic = addrHelper.Assign(rowContainer.Get(j + 1));
159  rowInterfaces.Add(ic);
161  }
162  m_rowInterfaces6.push_back(rowInterfaces);
163  }
164 
165  // Assign addresses to all col devices in the grid.
166  // These devices are stored in a vector. Each col
167  // of the grid has all the col devices in one entry
168  // of the vector. These entries come in pairs.
169  for (uint32_t i = 0; i < m_colDevices.size(); ++i)
170  {
171  Ipv6InterfaceContainer colInterfaces;
172  NetDeviceContainer colContainer = m_colDevices[i];
173  for (uint32_t j = 0; j < colContainer.GetN(); j += 2)
174  {
175  v6network = Ipv6AddressGenerator::GetNetwork(prefix);
176  addrHelper.SetBase(v6network, prefix);
177  Ipv6InterfaceContainer ic = addrHelper.Assign(colContainer.Get(j));
178  colInterfaces.Add(ic);
179  ic = addrHelper.Assign(colContainer.Get(j + 1));
180  colInterfaces.Add(ic);
182  }
183  m_colInterfaces6.push_back(colInterfaces);
184  }
185 }
186 
187 void
188 PointToPointGridHelper::BoundingBox(double ulx, double uly, double lrx, double lry)
189 {
190  double xDist;
191  double yDist;
192  if (lrx > ulx)
193  {
194  xDist = lrx - ulx;
195  }
196  else
197  {
198  xDist = ulx - lrx;
199  }
200  if (lry > uly)
201  {
202  yDist = lry - uly;
203  }
204  else
205  {
206  yDist = uly - lry;
207  }
208  double xAdder = xDist / m_xSize;
209  double yAdder = yDist / m_ySize;
210  double yLoc = yDist / 2;
211  for (uint32_t i = 0; i < m_ySize; ++i)
212  {
213  double xLoc = xDist / 2;
214  for (uint32_t j = 0; j < m_xSize; ++j)
215  {
216  Ptr<Node> node = GetNode(i, j);
219  if (!loc)
220  {
221  loc = CreateObject<ConstantPositionMobilityModel>();
222  node->AggregateObject(loc);
223  }
224  Vector locVec(xLoc, yLoc, 0);
225  loc->SetPosition(locVec);
226 
227  xLoc += xAdder;
228  }
229  yLoc += yAdder;
230  }
231 }
232 
233 Ptr<Node>
234 PointToPointGridHelper::GetNode(uint32_t row, uint32_t col)
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::GetNode.");
239  }
240 
241  return (m_nodes.at(row)).Get(col);
242 }
243 
245 PointToPointGridHelper::GetIpv4Address(uint32_t row, uint32_t col)
246 {
247  if (row > m_nodes.size() - 1 || col > m_nodes.at(row).GetN() - 1)
248  {
249  NS_FATAL_ERROR("Index out of bounds in PointToPointGridHelper::GetIpv4Address.");
250  }
251 
252  // Right now this just gets one of the addresses of the
253  // specified node. The exact device can't be specified.
254  // If you picture the grid, the address returned is the
255  // address of the left (row) device of all nodes, with
256  // the exception of the left-most nodes in the grid;
257  // in which case the right (row) device address is
258  // returned
259  if (col == 0)
260  {
261  return (m_rowInterfaces.at(row)).GetAddress(0);
262  }
263  else
264  {
265  return (m_rowInterfaces.at(row)).GetAddress((2 * col) - 1);
266  }
267 }
268 
270 PointToPointGridHelper::GetIpv6Address(uint32_t row, uint32_t col)
271 {
272  if (row > m_nodes.size() - 1 || col > m_nodes.at(row).GetN() - 1)
273  {
274  NS_FATAL_ERROR("Index out of bounds in PointToPointGridHelper::GetIpv6Address.");
275  }
276 
277  // Right now this just gets one of the addresses of the
278  // specified node. The exact device can't be specified.
279  // If you picture the grid, the address returned is the
280  // address of the left (row) device of all nodes, with
281  // the exception of the left-most nodes in the grid;
282  // in which case the right (row) device address is
283  // returned
284  if (col == 0)
285  {
286  return (m_rowInterfaces6.at(row)).GetAddress(0, 1);
287  }
288  else
289  {
290  return (m_rowInterfaces6.at(row)).GetAddress((2 * col) - 1, 1);
291  }
292 }
293 
294 } // 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.
Definition: ipv4-address.h:42
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.
Definition: ipv6-address.h:49
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.
Definition: ipv6-address.h:455
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.
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
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.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
pointToPoint
Definition: first.py:38
stack
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.