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
46 FdNetDeviceHelper::SetTypeId (std::string type)
47 {
49 }
50 
51 void
53 {
54  NS_LOG_FUNCTION (this);
55  m_deviceFactory.Set (n1, v1);
56 }
57 
58 void
59 FdNetDeviceHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
60 {
61  //
62  // All of the Pcap enable functions vector through here including the ones
63  // that are wandering through all of devices on perhaps all of the nodes in
64  // the system. We can only deal with devices of type FdNetDevice.
65  //
66  Ptr<FdNetDevice> device = nd->GetObject<FdNetDevice> ();
67  if (device == 0)
68  {
69  NS_LOG_INFO ("FdNetDeviceHelper::EnablePcapInternal(): Device " << device << " not of type ns3::FdNetDevice");
70  return;
71  }
72 
73  PcapHelper pcapHelper;
74 
75  std::string filename;
76  if (explicitFilename)
77  {
78  filename = prefix;
79  }
80  else
81  {
82  filename = pcapHelper.GetFilenameFromDevice (prefix, device);
83  }
84 
85  Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, PcapHelper::DLT_EN10MB);
86  if (promiscuous)
87  {
88  pcapHelper.HookDefaultSink<FdNetDevice> (device, "PromiscSniffer", file);
89  }
90  else
91  {
92  pcapHelper.HookDefaultSink<FdNetDevice> (device, "Sniffer", file);
93  }
94 }
95 
96 void
99  std::string prefix,
100  Ptr<NetDevice> nd,
101  bool explicitFilename)
102 {
103  //
104  // All of the ascii enable functions vector through here including the ones
105  // that are wandering through all of devices on perhaps all of the nodes in
106  // the system. We can only deal with devices of type FdNetDevice.
107  //
108  Ptr<FdNetDevice> device = nd->GetObject<FdNetDevice> ();
109  if (device == 0)
110  {
111  NS_LOG_INFO ("FdNetDeviceHelper::EnableAsciiInternal(): Device " << device << " not of type ns3::FdNetDevice");
112  return;
113  }
114 
115  //
116  // Our default trace sinks are going to use packet printing, so we have to
117  // make sure that is turned on.
118  //
120 
121  //
122  // If we are not provided an OutputStreamWrapper, we are expected to create
123  // one using the usual trace filename conventions and do a Hook*WithoutContext
124  // since there will be one file per context and therefore the context would
125  // be redundant.
126  //
127  if (stream == 0)
128  {
129  //
130  // Set up an output stream object to deal with private ofstream copy
131  // constructor and lifetime issues. Let the helper decide the actual
132  // name of the file given the prefix.
133  //
134  AsciiTraceHelper asciiTraceHelper;
135 
136  std::string filename;
137  if (explicitFilename)
138  {
139  filename = prefix;
140  }
141  else
142  {
143  filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
144  }
145 
146  Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
147 
148  //
149  // The MacRx trace source provides our "r" event.
150  //
151  asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<FdNetDevice> (device, "MacRx", theStream);
152 
153  return;
154  }
155 
156  //
157  // If we are provided an OutputStreamWrapper, we are expected to use it, and
158  // to providd a context. We are free to come up with our own context if we
159  // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
160  // compatibility and simplicity, we just use Config::Connect and let it deal
161  // with the context.
162  //
163  // Note that we are going to use the default trace sinks provided by the
164  // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
165  // but the default trace sinks are actually publicly available static
166  // functions that are always there waiting for just such a case.
167  //
168  uint32_t deviceid = nd->GetIfIndex ();
169  std::ostringstream oss;
170 
171  oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::FdNetDevice/MacRx";
173 }
174 
177 {
178  return NetDeviceContainer (InstallPriv (node));
179 }
180 
182 FdNetDeviceHelper::Install (std::string nodeName) const
183 {
184  Ptr<Node> node = Names::Find<Node> (nodeName);
185  return NetDeviceContainer (InstallPriv (node));
186 }
187 
190 {
191  NetDeviceContainer devs;
192 
193  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++)
194  {
195  devs.Add (InstallPriv (*i));
196  }
197 
198  return devs;
199 }
200 
203 {
205  device->SetAddress (Mac48Address::Allocate ());
206  node->AddDevice (device);
207  return device;
208 }
209 
210 } // namespace ns3
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
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...
static void DefaultReceiveSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Receive default trace sink.
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
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:528
Hold a value for an Attribute.
Definition: attribute.h:69
void SetAttribute(std::string n1, const AttributeValue &v1)
virtual NetDeviceContainer Install(Ptr< Node > node) const
This method creates a FdNetDevice and associates it to a node.
ObjectFactory m_deviceFactory
factory for the NetDevices
virtual Ptr< NetDevice > InstallPriv(Ptr< Node > node) const
This method creates an ns3::FdNetDevice and associates it to a node.
FdNetDeviceHelper()
Construct a FdNetDeviceHelper.
virtual void EnablePcapInternal(std::string prefix, Ptr< NetDevice > nd, bool promiscuous, bool explicitFilename)
Enable pcap output on the indicated net device.
void SetTypeId(std::string type)
Set the TypeId of the Objects to be created by this helper.
virtual void EnableAsciiInternal(Ptr< OutputStreamWrapper > stream, std::string prefix, Ptr< NetDevice > nd, bool explicitFilename)
Enable ascii trace output on the indicated net device.
a NetDevice to read/write network traffic from/into a file descriptor.
Definition: fd-net-device.h:85
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
virtual Ptr< Node > GetNode(void) const =0
virtual uint32_t GetIfIndex(void) const =0
keep track of a set of node pointers.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
uint32_t GetId(void) const
Definition: node.cc:109
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
Manage pcap files for device models.
Definition: trace-helper.h:39
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
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 HookDefaultSink(Ptr< T > object, std::string traceName, Ptr< PcapFileWrapper > file)
Hook a trace source to the default trace sink.
Definition: trace-helper.h:148
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1709
Every class exported by the ns3 library is enclosed in the ns3 namespace.