A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
uan-helper.cc
1 /*
2  * uan-helper.cc
3  *
4  * Copyright (c) 2008 University of Washington
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  * Created on: 9-Oct-2008
20  * Author: Leonard Tracy <lentracy@u.washington.edu>
21  *
22  *
23  */
24 
25 #include "uan-helper.h"
26 #include "ns3/uan-net-device.h"
27 #include "ns3/uan-mac.h"
28 #include "ns3/uan-phy.h"
29 #include "ns3/uan-channel.h"
30 #include "ns3/uan-prop-model.h"
31 #include "ns3/uan-prop-model-ideal.h"
32 #include "ns3/uan-transducer.h"
33 #include "ns3/mobility-model.h"
34 #include "ns3/log.h"
35 #include "ns3/uan-tx-mode.h"
36 #include "ns3/config.h"
37 #include "ns3/simulator.h"
38 #include "ns3/uan-noise-model-default.h"
39 
40 #include <sstream>
41 #include <string>
42 
43 NS_LOG_COMPONENT_DEFINE ("UanHelper");
44 namespace ns3 {
45 
46 static void AsciiPhyTxEvent (std::ostream *os, std::string context,
47  Ptr<const Packet> packet, double txPowerDb, UanTxMode mode)
48 {
49  *os << "+ " << Simulator::Now ().GetSeconds () << " " << context << " " << *packet << std::endl;
50 }
51 
52 static void AsciiPhyRxOkEvent (std::ostream *os, std::string context,
53  Ptr<const Packet> packet, double snr, UanTxMode mode)
54 {
55  *os << "r " << Simulator::Now ().GetSeconds () << " " << context << " " << *packet << std::endl;
56 }
57 
58 UanHelper::UanHelper ()
59 {
60  m_mac.SetTypeId ("ns3::UanMacAloha");
61  m_phy.SetTypeId ("ns3::UanPhyGen");
62  m_transducer.SetTypeId ("ns3::UanTransducerHd");
63 
64 }
65 
66 UanHelper::~UanHelper ()
67 {
68 
69 }
70 
71 
72 void
73 UanHelper::SetMac (std::string macType,
74  std::string n0, const AttributeValue &v0,
75  std::string n1, const AttributeValue &v1,
76  std::string n2, const AttributeValue &v2,
77  std::string n3, const AttributeValue &v3,
78  std::string n4, const AttributeValue &v4,
79  std::string n5, const AttributeValue &v5,
80  std::string n6, const AttributeValue &v6,
81  std::string n7, const AttributeValue &v7)
82 {
83  m_mac = ObjectFactory ();
84  m_mac.SetTypeId (macType);
85  m_mac.Set (n0, v0);
86  m_mac.Set (n1, v1);
87  m_mac.Set (n2, v2);
88  m_mac.Set (n3, v3);
89  m_mac.Set (n4, v4);
90  m_mac.Set (n5, v5);
91  m_mac.Set (n6, v6);
92  m_mac.Set (n7, v7);
93 }
94 
95 void
96 UanHelper::SetPhy (std::string phyType,
97  std::string n0, const AttributeValue &v0,
98  std::string n1, const AttributeValue &v1,
99  std::string n2, const AttributeValue &v2,
100  std::string n3, const AttributeValue &v3,
101  std::string n4, const AttributeValue &v4,
102  std::string n5, const AttributeValue &v5,
103  std::string n6, const AttributeValue &v6,
104  std::string n7, const AttributeValue &v7)
105 {
106  m_phy = ObjectFactory ();
107  m_phy.SetTypeId (phyType);
108  m_phy.Set (n0, v0);
109  m_phy.Set (n1, v1);
110  m_phy.Set (n2, v2);
111  m_phy.Set (n3, v3);
112  m_phy.Set (n4, v4);
113  m_phy.Set (n5, v5);
114  m_phy.Set (n6, v6);
115  m_phy.Set (n7, v7);
116 
117 }
118 
119 void
120 UanHelper::SetTransducer (std::string type,
121  std::string n0, const AttributeValue &v0,
122  std::string n1, const AttributeValue &v1,
123  std::string n2, const AttributeValue &v2,
124  std::string n3, const AttributeValue &v3,
125  std::string n4, const AttributeValue &v4,
126  std::string n5, const AttributeValue &v5,
127  std::string n6, const AttributeValue &v6,
128  std::string n7, const AttributeValue &v7)
129 {
130  m_transducer = ObjectFactory ();
131  m_transducer.SetTypeId (type);
132  m_transducer.Set (n0, v0);
133  m_transducer.Set (n1, v1);
134  m_transducer.Set (n2, v2);
135  m_transducer.Set (n3, v3);
136  m_transducer.Set (n4, v4);
137  m_transducer.Set (n5, v5);
138  m_transducer.Set (n6, v6);
139  m_transducer.Set (n7, v7);
140 
141 }
142 
143 void
144 UanHelper::EnableAscii (std::ostream &os, uint32_t nodeid, uint32_t deviceid)
145 {
147  std::ostringstream oss;
148 
149  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::UanNetDevice/Phy/RxOk";
150  Config::Connect (oss.str (), MakeBoundCallback (&AsciiPhyRxOkEvent, &os));
151 
152  oss.str ("");
153 
154  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::UanNetDevice/Phy/Tx";
155  Config::Connect (oss.str (), MakeBoundCallback (&AsciiPhyTxEvent, &os));
156 }
157 
158 void
160 {
161  for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
162  {
163  Ptr<NetDevice> dev = *i;
164  EnableAscii (os, dev->GetNode ()->GetId (), dev->GetIfIndex ());
165  }
166 }
167 
168 void
170 {
171  NetDeviceContainer devs;
172  for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
173  {
174  Ptr<Node> node = *i;
175  for (uint32_t j = 0; j < node->GetNDevices (); ++j)
176  {
177  devs.Add (node->GetDevice (j));
178  }
179  }
180  EnableAscii (os, devs);
181 }
182 
183 void
184 UanHelper::EnableAsciiAll (std::ostream &os)
185 {
187 }
188 
191 {
192  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
193  Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> ();
194  channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ());
195  channel->SetNoiseModel (noise);
196 
197  return Install (c, channel);
198 }
199 
202 {
203  NetDeviceContainer devices;
204  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++)
205  {
206  Ptr<Node> node = *i;
207 
208  Ptr<UanNetDevice> device = Install (node, channel);
209 
210  devices.Add (device);
211  NS_LOG_DEBUG ("node=" << node << ", mob=" << node->GetObject<MobilityModel> ());
212  }
213  return devices;
214 }
215 
218 {
219  Ptr<UanNetDevice> device = CreateObject<UanNetDevice> ();
220 
221  Ptr<UanMac> mac = m_mac.Create<UanMac> ();
222  Ptr<UanPhy> phy = m_phy.Create<UanPhy> ();
223  Ptr<UanTransducer> trans = m_transducer.Create<UanTransducer> ();
224 
225  mac->SetAddress (UanAddress::Allocate ());
226  device->SetMac (mac);
227  device->SetPhy (phy);
228  device->SetTransducer (trans);
229  device->SetChannel (channel);
230 
231  node->AddDevice (device);
232 
233  return device;
234 }
235 
236 int64_t
238 {
239  int64_t currentStream = stream;
240  Ptr<NetDevice> netDevice;
241  for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i)
242  {
243  netDevice = (*i);
244  Ptr<UanNetDevice> uan = DynamicCast<UanNetDevice> (netDevice);
245  if (uan)
246  {
247  currentStream += uan->GetPhy ()->AssignStreams (currentStream);
248  currentStream += uan->GetMac ()->AssignStreams (currentStream);
249  }
250  }
251  return (currentStream - stream);
252 }
253 
254 } // end namespace ns3
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
Hold a value for an Attribute.
Definition: attribute.h:51
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
void SetTypeId(TypeId tid)
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
Keep track of the current position and velocity of an object.
double GetSeconds(void) const
Definition: nstime.h:262
Virtual base class for all UAN MAC protocols.
Definition: uan-mac.h:47
static void EnablePrinting(void)
Definition: packet.cc:575
void SetMac(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: uan-helper.cc:73
void SetTransducer(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: uan-helper.cc:120
Ptr< Object > Create(void) const
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
holds a vector of ns3::NetDevice pointers
int64_t AssignStreams(NetDeviceContainer c, int64_t stream)
Definition: uan-helper.cc:237
Ptr< NetDevice > GetDevice(uint32_t index) const
Definition: node.cc:133
Virtual base for Transducer objects.
static void EnableAscii(std::ostream &os, uint32_t nodeid, uint32_t deviceid)
Definition: uan-helper.cc:144
uint32_t GetNDevices(void) const
Definition: node.cc:141
keep track of a set of node pointers.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
void Set(std::string name, const AttributeValue &value)
static Time Now(void)
Definition: simulator.cc:179
static void EnableAsciiAll(std::ostream &os)
Definition: uan-helper.cc:184
Base class for UAN Phy models.
Definition: uan-phy.h:166
static NodeContainer GetGlobal(void)
Create a NodeContainer that contains a list of all nodes created through NodeContainer::Create() and ...
instantiate subclasses of ns3::Object.
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:119
static UanAddress Allocate()
Allocates UanAddress from 0-254 (will wrap back to 0 if more than 255 are allocated) ...
Definition: uan-address.cc:97
#define NS_LOG_DEBUG(msg)
Definition: log.h:255
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
NetDeviceContainer Install(NodeContainer c) const
Definition: uan-helper.cc:190
Ptr< T > GetObject(void) const
Definition: object.h:332
void SetPhy(std::string phyType, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: uan-helper.cc:96