A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
point-to-point-dumbbell.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: George F. Riley<riley@ece.gatech.edu>
17  */
18 
19 // Implement an object to create a dumbbell topology.
20 
21 #include <iostream>
22 #include <sstream>
23 
24 // ns3 includes
25 #include "ns3/log.h"
26 #include "ns3/point-to-point-dumbbell.h"
27 #include "ns3/constant-position-mobility-model.h"
28 
29 #include "ns3/node-list.h"
30 #include "ns3/point-to-point-net-device.h"
31 #include "ns3/vector.h"
32 #include "ns3/ipv6-address-generator.h"
33 
34 NS_LOG_COMPONENT_DEFINE ("PointToPointDumbbellHelper");
35 
36 namespace ns3 {
37 
39  PointToPointHelper leftHelper,
40  uint32_t nRightLeaf,
41  PointToPointHelper rightHelper,
42  PointToPointHelper bottleneckHelper)
43 {
44  // Create the bottleneck routers
45  m_routers.Create (2);
46  // Create the leaf nodes
47  m_leftLeaf.Create (nLeftLeaf);
48  m_rightLeaf.Create (nRightLeaf);
49 
50  // Add the link connecting routers
51  m_routerDevices = bottleneckHelper.Install (m_routers);
52  // Add the left side links
53  for (uint32_t i = 0; i < nLeftLeaf; ++i)
54  {
55  NetDeviceContainer c = leftHelper.Install (m_routers.Get (0),
56  m_leftLeaf.Get (i));
57  m_leftRouterDevices.Add (c.Get (0));
58  m_leftLeafDevices.Add (c.Get (1));
59  }
60  // Add the right side links
61  for (uint32_t i = 0; i < nRightLeaf; ++i)
62  {
63  NetDeviceContainer c = rightHelper.Install (m_routers.Get (1),
64  m_rightLeaf.Get (i));
66  m_rightLeafDevices.Add (c.Get (1));
67  }
68 }
69 
71 {
72 }
73 
75 { // Get the left side bottleneck router
76  return m_routers.Get (0);
77 }
78 
80 { // Get the i'th left side leaf
81  return m_leftLeaf.Get (i);
82 }
83 
85 { // Get the right side bottleneck router
86  return m_routers.Get (1);
87 }
88 
90 { // Get the i'th right side leaf
91  return m_rightLeaf.Get (i);
92 }
93 
95 {
97 }
98 
100 {
102 }
103 
105 {
106  return m_leftLeafInterfaces6.GetAddress (i, 1);
107 }
108 
110 {
111  return m_rightLeafInterfaces6.GetAddress (i, 1);
112 }
113 
115 { // Number of left side nodes
116  return m_leftLeaf.GetN ();
117 }
118 
120 { // Number of right side nodes
121  return m_rightLeaf.GetN ();
122 }
123 
125 {
126  stack.Install (m_routers);
127  stack.Install (m_leftLeaf);
128  stack.Install (m_rightLeaf);
129 }
130 
132  Ipv4AddressHelper rightIp,
133  Ipv4AddressHelper routerIp)
134 {
135  // Assign the router network
137  // Assign to left side
138  for (uint32_t i = 0; i < LeftCount (); ++i)
139  {
140  NetDeviceContainer ndc;
141  ndc.Add (m_leftLeafDevices.Get (i));
142  ndc.Add (m_leftRouterDevices.Get (i));
143  Ipv4InterfaceContainer ifc = leftIp.Assign (ndc);
144  m_leftLeafInterfaces.Add (ifc.Get (0));
145  m_leftRouterInterfaces.Add (ifc.Get (1));
146  leftIp.NewNetwork ();
147  }
148  // Assign to right side
149  for (uint32_t i = 0; i < RightCount (); ++i)
150  {
151  NetDeviceContainer ndc;
152  ndc.Add (m_rightLeafDevices.Get (i));
153  ndc.Add (m_rightRouterDevices.Get (i));
154  Ipv4InterfaceContainer ifc = rightIp.Assign (ndc);
155  m_rightLeafInterfaces.Add (ifc.Get (0));
156  m_rightRouterInterfaces.Add (ifc.Get (1));
157  rightIp.NewNetwork ();
158  }
159 }
160 
162 {
163  // Assign the router network
164  Ipv6AddressGenerator::Init (addrBase, prefix);
165  Ipv6Address v6network;
166  Ipv6AddressHelper addressHelper;
167 
168  v6network = Ipv6AddressGenerator::GetNetwork (prefix);
169  addressHelper.SetBase (v6network, prefix);
170  m_routerInterfaces6 = addressHelper.Assign (m_routerDevices);
172 
173  // Assign to left side
174  for (uint32_t i = 0; i < LeftCount (); ++i)
175  {
176  v6network = Ipv6AddressGenerator::GetNetwork (prefix);
177  addressHelper.SetBase (v6network, prefix);
178 
179  NetDeviceContainer ndc;
180  ndc.Add (m_leftLeafDevices.Get (i));
181  ndc.Add (m_leftRouterDevices.Get (i));
182  Ipv6InterfaceContainer ifc = addressHelper.Assign (ndc);
184  m_leftLeafInterfaces6.Add ((*it).first, (*it).second);
185  it++;
186  m_leftRouterInterfaces6.Add ((*it).first, (*it).second);
188  }
189  // Assign to right side
190  for (uint32_t i = 0; i < RightCount (); ++i)
191  {
192  v6network = Ipv6AddressGenerator::GetNetwork (prefix);
193  addressHelper.SetBase (v6network, prefix);
194 
195  NetDeviceContainer ndc;
196  ndc.Add (m_rightLeafDevices.Get (i));
197  ndc.Add (m_rightRouterDevices.Get (i));
198  Ipv6InterfaceContainer ifc = addressHelper.Assign (ndc);
200  m_rightLeafInterfaces6.Add ((*it).first, (*it).second);
201  it++;
202  m_rightRouterInterfaces6.Add ((*it).first, (*it).second);
204  }
205 }
206 
207 
208 void PointToPointDumbbellHelper::BoundingBox (double ulx, double uly, // Upper left x/y
209  double lrx, double lry) // Lower right y
210 {
211  double xDist;
212  double yDist;
213  if (lrx > ulx)
214  {
215  xDist = lrx - ulx;
216  }
217  else
218  {
219  xDist = ulx - lrx;
220  }
221  if (lry > uly)
222  {
223  yDist = lry - uly;
224  }
225  else
226  {
227  yDist = uly - lry;
228  }
229 
230  double xAdder = xDist / 3.0;
231  double thetaL = M_PI / (LeftCount () + 1.0);
232  double thetaR = M_PI / (RightCount () + 1.0);
233 
234  // Place the left router
235  Ptr<Node> lr = GetLeft ();
237  if (loc == 0)
238  {
239  loc = CreateObject<ConstantPositionMobilityModel> ();
240  lr->AggregateObject (loc);
241  }
242  Vector lrl (ulx + xAdder, uly + yDist/2.0, 0);
243  loc->SetPosition (lrl);
244 
245  // Place the right router
246  Ptr<Node> rr = GetRight ();
248  if (loc == 0)
249  {
250  loc = CreateObject<ConstantPositionMobilityModel> ();
251  rr->AggregateObject (loc);
252  }
253  Vector rrl (ulx + xAdder * 2, uly + yDist/2.0, 0); // Right router location
254  loc->SetPosition (rrl);
255 
256  // Place the left leaf nodes
257  double theta = -M_PI_2 + thetaL;
258  for (uint32_t l = 0; l < LeftCount (); ++l)
259  {
260  // Make them in a circular pattern to make all line lengths the same
261  // Special case when theta = 0, to be sure we get a straight line
262  if ((LeftCount () % 2) == 1)
263  { // Count is odd, see if we are in middle
264  if (l == (LeftCount () / 2))
265  {
266  theta = 0.0;
267  }
268  }
269  Ptr<Node> ln = GetLeft (l);
271  if (loc == 0)
272  {
273  loc = CreateObject<ConstantPositionMobilityModel> ();
274  ln->AggregateObject (loc);
275  }
276  Vector lnl (lrl.x - cos (theta) * xAdder,
277  lrl.y + sin (theta) * xAdder, 0); // Left Node Location
278  // Insure did not exceed bounding box
279  if (lnl.y < uly)
280  {
281  lnl.y = uly; // Set to upper right y
282  }
283  if (lnl.y > lry)
284  {
285  lnl.y = lry; // Set to lower right y
286  }
287  loc->SetPosition (lnl);
288  theta += thetaL;
289  }
290  // Place the right nodes
291  theta = -M_PI_2 + thetaR;
292  for (uint32_t r = 0; r < RightCount (); ++r)
293  {
294  // Special case when theta = 0, to be sure we get a straight line
295  if ((RightCount () % 2) == 1)
296  { // Count is odd, see if we are in middle
297  if (r == (RightCount () / 2))
298  {
299  theta = 0.0;
300  }
301  }
302  Ptr<Node> rn = GetRight (r);
304  if (loc == 0)
305  {
306  loc = CreateObject<ConstantPositionMobilityModel> ();
307  rn->AggregateObject (loc);
308  }
309  Vector rnl (rrl.x + cos (theta) * xAdder, // Right node location
310  rrl.y + sin (theta) * xAdder, 0);
311  // Insure did not exceed bounding box
312  if (rnl.y < uly)
313  {
314  rnl.y = uly; // Set to upper right y
315  }
316  if (rnl.y > lry)
317  {
318  rnl.y = lry; // Set to lower right y
319  }
320  loc->SetPosition (rnl);
321  theta += thetaR;
322  }
323 }
324 
325 } // namespace ns3