A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
point-to-point-star.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 
17 #include <iostream>
18 #include <sstream>
19 
20 // ns3 includes
21 #include "ns3/log.h"
22 #include "ns3/point-to-point-star.h"
23 #include "ns3/constant-position-mobility-model.h"
24 
25 #include "ns3/node-list.h"
26 #include "ns3/point-to-point-net-device.h"
27 #include "ns3/vector.h"
28 #include "ns3/ipv6-address-generator.h"
29 
30 NS_LOG_COMPONENT_DEFINE ("PointToPointStarHelper");
31 
32 namespace ns3 {
33 
35  PointToPointHelper p2pHelper)
36 {
37  m_hub.Create (1);
38  m_spokes.Create (numSpokes);
39 
40  for (uint32_t i = 0; i < m_spokes.GetN (); ++i)
41  {
42  NetDeviceContainer nd = p2pHelper.Install (m_hub.Get (0), m_spokes.Get (i));
43  m_hubDevices.Add (nd.Get (0));
44  m_spokeDevices.Add (nd.Get (1));
45  }
46 }
47 
49 {
50 }
51 
54 {
55  return m_hub.Get (0);
56 }
57 
60 {
61  return m_spokes.Get (i);
62 }
63 
66 {
67  return m_hubInterfaces.GetAddress (i);
68 }
69 
72 {
73  return m_spokeInterfaces.GetAddress (i);
74 }
75 
78 {
79  return m_hubInterfaces6.GetAddress (i, 1);
80 }
81 
84 {
85  return m_spokeInterfaces6.GetAddress (i, 1);
86 }
87 
88 uint32_t
90 {
91  return m_spokes.GetN ();
92 }
93 
94 void
96 {
97  stack.Install (m_hub);
98  stack.Install (m_spokes);
99 }
100 
101 void
103 {
104  for (uint32_t i = 0; i < m_spokes.GetN (); ++i)
105  {
106  m_hubInterfaces.Add (address.Assign (m_hubDevices.Get (i)));
108  address.NewNetwork ();
109  }
110 }
111 
112 void
114 {
115  Ipv6AddressGenerator::Init (addrBase, prefix);
116  Ipv6Address v6network;
117  Ipv6AddressHelper addressHelper;
118 
119  for (uint32_t i = 0; i < m_spokes.GetN (); ++i)
120  {
121  v6network = Ipv6AddressGenerator::GetNetwork (prefix);
122  addressHelper.SetBase (v6network, prefix);
123 
124  Ipv6InterfaceContainer ic = addressHelper.Assign (m_hubDevices.Get (i));
125  m_hubInterfaces6.Add (ic);
126  ic = addressHelper.Assign (m_spokeDevices.Get (i));
127  m_spokeInterfaces6.Add (ic);
128 
130  }
131 }
132 
133 void
134 PointToPointStarHelper::BoundingBox (double ulx, double uly,
135  double lrx, double lry)
136 {
137  double xDist;
138  double yDist;
139  if (lrx > ulx)
140  {
141  xDist = lrx - ulx;
142  }
143  else
144  {
145  xDist = ulx - lrx;
146  }
147  if (lry > uly)
148  {
149  yDist = lry - uly;
150  }
151  else
152  {
153  yDist = uly - lry;
154  }
155 
156  // Place the hub
157  Ptr<Node> hub = m_hub.Get (0);
159  if (hubLoc == 0)
160  {
161  hubLoc = CreateObject<ConstantPositionMobilityModel> ();
162  hub->AggregateObject (hubLoc);
163  }
164  Vector hubVec (ulx + xDist/2.0, uly + yDist/2.0, 0);
165  hubLoc->SetPosition (hubVec);
166 
167  double spokeDist;
168  if (xDist > yDist)
169  {
170  spokeDist = yDist/4.0;
171  }
172  else
173  {
174  spokeDist = xDist/4.0;
175  }
176 
177  double theta = 2*M_PI/m_spokes.GetN ();
178  for (uint32_t i = 0; i < m_spokes.GetN (); ++i)
179  {
180  Ptr<Node> spokeNode = m_spokes.Get (i);
182  if (spokeLoc == 0)
183  {
184  spokeLoc = CreateObject<ConstantPositionMobilityModel> ();
185  spokeNode->AggregateObject (spokeLoc);
186  }
187  Vector spokeVec (hubVec.x + cos (theta*i) * spokeDist,
188  hubVec.y + sin (theta*i) * spokeDist,
189  0);
190  spokeLoc->SetPosition (spokeVec);
191  }
192 }
193 
194 } // namespace ns3