A Discrete-Event Network Simulator
API
fd-net-device-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19  *
20  */
21 
22 #include "fd-net-device-helper.h"
23 
24 #include "ns3/abort.h"
25 #include "ns3/config.h"
26 #include "ns3/fd-net-device.h"
27 #include "ns3/log.h"
28 #include "ns3/names.h"
29 #include "ns3/object-factory.h"
30 #include "ns3/packet.h"
31 #include "ns3/simulator.h"
32 #include "ns3/trace-helper.h"
33 
34 #include <string>
35 
36 namespace ns3 {
37 
38 NS_LOG_COMPONENT_DEFINE ("FdNetDeviceHelper");
39 
41 {
42  m_deviceFactory.SetTypeId ("ns3::FdNetDevice");
43 }
44 
45 void
47 {
48  NS_LOG_FUNCTION (this);
49  m_deviceFactory.Set (n1, v1);
50 }
51 
52 void
53 FdNetDeviceHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
54 {
55  //
56  // All of the Pcap enable functions vector through here including the ones
57  // that are wandering through all of devices on perhaps all of the nodes in
58  // the system. We can only deal with devices of type FdNetDevice.
59  //
60  Ptr<FdNetDevice> device = nd->GetObject<FdNetDevice> ();
61  if (device == 0)
62  {
63  NS_LOG_INFO ("FdNetDeviceHelper::EnablePcapInternal(): Device " << device << " not of type ns3::FdNetDevice");
64  return;
65  }
66 
67  PcapHelper pcapHelper;
68 
69  std::string filename;
70  if (explicitFilename)
71  {
72  filename = prefix;
73  }
74  else
75  {
76  filename = pcapHelper.GetFilenameFromDevice (prefix, device);
77  }
78 
79  Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, PcapHelper::DLT_EN10MB);
80  if (promiscuous)
81  {
82  pcapHelper.HookDefaultSink<FdNetDevice> (device, "PromiscSniffer", file);
83  }
84  else
85  {
86  pcapHelper.HookDefaultSink<FdNetDevice> (device, "Sniffer", file);
87  }
88 }
89 
90 void
93  std::string prefix,
94  Ptr<NetDevice> nd,
95  bool explicitFilename)
96 {
97  //
98  // All of the ascii enable functions vector through here including the ones
99  // that are wandering through all of devices on perhaps all of the nodes in
100  // the system. We can only deal with devices of type FdNetDevice.
101  //
102  Ptr<FdNetDevice> device = nd->GetObject<FdNetDevice> ();
103  if (device == 0)
104  {
105  NS_LOG_INFO ("FdNetDeviceHelper::EnableAsciiInternal(): Device " << device << " not of type ns3::FdNetDevice");
106  return;
107  }
108 
109  //
110  // Our default trace sinks are going to use packet printing, so we have to
111  // make sure that is turned on.
112  //
114 
115  //
116  // If we are not provided an OutputStreamWrapper, we are expected to create
117  // one using the usual trace filename conventions and do a Hook*WithoutContext
118  // since there will be one file per context and therefore the context would
119  // be redundant.
120  //
121  if (stream == 0)
122  {
123  //
124  // Set up an output stream object to deal with private ofstream copy
125  // constructor and lifetime issues. Let the helper decide the actual
126  // name of the file given the prefix.
127  //
128  AsciiTraceHelper asciiTraceHelper;
129 
130  std::string filename;
131  if (explicitFilename)
132  {
133  filename = prefix;
134  }
135  else
136  {
137  filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
138  }
139 
140  Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
141 
142  //
143  // The MacRx trace source provides our "r" event.
144  //
145  asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<FdNetDevice> (device, "MacRx", theStream);
146 
147  return;
148  }
149 
150  //
151  // If we are provided an OutputStreamWrapper, we are expected to use it, and
152  // to providd a context. We are free to come up with our own context if we
153  // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
154  // compatibility and simplicity, we just use Config::Connect and let it deal
155  // with the context.
156  //
157  // Note that we are going to use the default trace sinks provided by the
158  // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
159  // but the default trace sinks are actually publicly available static
160  // functions that are always there waiting for just such a case.
161  //
162  uint32_t deviceid = nd->GetIfIndex ();
163  std::ostringstream oss;
164 
165  oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::FdNetDevice/MacRx";
167 }
168 
171 {
172  return NetDeviceContainer (InstallPriv (node));
173 }
174 
176 FdNetDeviceHelper::Install (std::string nodeName) const
177 {
178  Ptr<Node> node = Names::Find<Node> (nodeName);
179  return NetDeviceContainer (InstallPriv (node));
180 }
181 
184 {
185  NetDeviceContainer devs;
186 
187  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++)
188  {
189  devs.Add (InstallPriv (*i));
190  }
191 
192  return devs;
193 }
194 
197 {
199  device->SetAddress (Mac48Address::Allocate ());
200  node->AddDevice (device);
201  return device;
202 }
203 
204 } // namespace ns3
virtual Ptr< NetDevice > InstallPriv(Ptr< Node > node) const
This method creates an ns3::FdNetDevice and associates it to a node.
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Hold a value for an Attribute.
Definition: attribute.h:68
Manage pcap files for device models.
Definition: trace-helper.h:38
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1686
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
virtual void EnableAsciiInternal(Ptr< OutputStreamWrapper > stream, std::string prefix, Ptr< NetDevice > nd, bool explicitFilename)
Enable ascii trace output on the indicated net device.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
std::string GetFilenameFromDevice(std::string prefix, Ptr< NetDevice > device, bool useObjectNames=true)
Let the ascii trace helper figure out a reasonable filename to use for an ascii trace file associated...
Ptr< PcapFileWrapper > CreateFile(std::string filename, std::ios::openmode filemode, DataLinkType dataLinkType, uint32_t snapLen=std::numeric_limits< uint32_t >::max(), int32_t tzCorrection=0)
Create and initialize a pcap file.
Definition: trace-helper.cc:49
void SetAttribute(std::string n1, const AttributeValue &v1)
virtual void EnablePcapInternal(std::string prefix, Ptr< NetDevice > nd, bool promiscuous, bool explicitFilename)
Enable pcap output on the indicated net device.
std::string GetFilenameFromDevice(std::string prefix, Ptr< NetDevice > device, bool useObjectNames=true)
Let the pcap helper figure out a reasonable filename to use for a pcap file associated with a device...
Definition: trace-helper.cc:80
virtual NetDeviceContainer Install(Ptr< Node > node) const
This method creates a FdNetDevice and associates it to a node.
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
holds a vector of ns3::NetDevice pointers
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:843
static void DefaultReceiveSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Receive default trace sink.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void Set(std::string name, const AttributeValue &value)
Set an attribute to be set during construction.
void HookDefaultReceiveSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default receive operation trace sink that does not accept nor log a trace ...
Definition: trace-helper.h:527
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
a NetDevice to read/write network traffic from/into a file descriptor.
Definition: fd-net-device.h:84
void HookDefaultSink(Ptr< T > object, std::string traceName, Ptr< PcapFileWrapper > file)
Hook a trace source to the default trace sink.
Definition: trace-helper.h:147
FdNetDeviceHelper()
Construct a FdNetDeviceHelper.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
ObjectFactory m_deviceFactory
factory for the NetDevices