A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
fd-net-device-helper.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18 *
19 */
20
22
23#include "ns3/abort.h"
24#include "ns3/config.h"
25#include "ns3/fd-net-device.h"
26#include "ns3/log.h"
27#include "ns3/names.h"
28#include "ns3/object-factory.h"
29#include "ns3/packet.h"
30#include "ns3/simulator.h"
31#include "ns3/trace-helper.h"
32
33#include <string>
34
35namespace ns3
36{
37
38NS_LOG_COMPONENT_DEFINE("FdNetDeviceHelper");
39
41{
42 m_deviceFactory.SetTypeId("ns3::FdNetDevice");
43}
44
45void
47{
49}
50
51void
53{
54 NS_LOG_FUNCTION(this);
55 m_deviceFactory.Set(n1, v1);
56}
57
58void
61 bool promiscuous,
62 bool explicitFilename)
63{
64 //
65 // All of the Pcap enable functions vector through here including the ones
66 // that are wandering through all of devices on perhaps all of the nodes in
67 // the system. We can only deal with devices of type FdNetDevice.
68 //
69 Ptr<FdNetDevice> device = nd->GetObject<FdNetDevice>();
70 if (!device)
71 {
72 NS_LOG_INFO("FdNetDeviceHelper::EnablePcapInternal(): Device "
73 << device << " not of type ns3::FdNetDevice");
74 return;
75 }
76
77 PcapHelper pcapHelper;
78
79 std::string filename;
80 if (explicitFilename)
81 {
82 filename = prefix;
83 }
84 else
85 {
86 filename = pcapHelper.GetFilenameFromDevice(prefix, device);
87 }
88
90 pcapHelper.CreateFile(filename, std::ios::out, PcapHelper::DLT_EN10MB);
91 if (promiscuous)
92 {
93 pcapHelper.HookDefaultSink<FdNetDevice>(device, "PromiscSniffer", file);
94 }
95 else
96 {
97 pcapHelper.HookDefaultSink<FdNetDevice>(device, "Sniffer", file);
98 }
99}
100
101void
103 std::string prefix,
105 bool explicitFilename)
106{
107 //
108 // All of the ascii enable functions vector through here including the ones
109 // that are wandering through all of devices on perhaps all of the nodes in
110 // the system. We can only deal with devices of type FdNetDevice.
111 //
112 Ptr<FdNetDevice> device = nd->GetObject<FdNetDevice>();
113 if (!device)
114 {
115 NS_LOG_INFO("FdNetDeviceHelper::EnableAsciiInternal(): Device "
116 << device << " not of type ns3::FdNetDevice");
117 return;
118 }
119
120 //
121 // Our default trace sinks are going to use packet printing, so we have to
122 // make sure that is turned on.
123 //
125
126 //
127 // If we are not provided an OutputStreamWrapper, we are expected to create
128 // one using the usual trace filename conventions and do a Hook*WithoutContext
129 // since there will be one file per context and therefore the context would
130 // be redundant.
131 //
132 if (!stream)
133 {
134 //
135 // Set up an output stream object to deal with private ofstream copy
136 // constructor and lifetime issues. Let the helper decide the actual
137 // name of the file given the prefix.
138 //
139 AsciiTraceHelper asciiTraceHelper;
140
141 std::string filename;
142 if (explicitFilename)
143 {
144 filename = prefix;
145 }
146 else
147 {
148 filename = asciiTraceHelper.GetFilenameFromDevice(prefix, device);
149 }
150
151 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream(filename);
152
153 //
154 // The MacRx trace source provides our "r" event.
155 //
156 asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<FdNetDevice>(device,
157 "MacRx",
158 theStream);
159
160 return;
161 }
162
163 //
164 // If we are provided an OutputStreamWrapper, we are expected to use it, and
165 // to providd a context. We are free to come up with our own context if we
166 // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
167 // compatibility and simplicity, we just use Config::Connect and let it deal
168 // with the context.
169 //
170 // Note that we are going to use the default trace sinks provided by the
171 // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
172 // but the default trace sinks are actually publicly available static
173 // functions that are always there waiting for just such a case.
174 //
175 uint32_t deviceid = nd->GetIfIndex();
176 std::ostringstream oss;
177
178 oss << "/NodeList/" << nd->GetNode()->GetId() << "/DeviceList/" << deviceid
179 << "/$ns3::FdNetDevice/MacRx";
180 Config::Connect(oss.str(),
182}
183
186{
187 return NetDeviceContainer(InstallPriv(node));
188}
189
191FdNetDeviceHelper::Install(std::string nodeName) const
192{
193 Ptr<Node> node = Names::Find<Node>(nodeName);
194 return NetDeviceContainer(InstallPriv(node));
195}
196
199{
201
202 for (auto i = c.Begin(); i != c.End(); i++)
203 {
204 devs.Add(InstallPriv(*i));
205 }
206
207 return devs;
208}
209
212{
214 device->SetAddress(Mac48Address::Allocate());
215 node->AddDevice(device);
216 return device;
217}
218
219} // namespace ns3
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
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:592
Hold a value for an Attribute.
Definition: attribute.h:70
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.
void EnablePcapInternal(std::string prefix, Ptr< NetDevice > nd, bool promiscuous, bool explicitFilename) override
Enable pcap output on the indicated net device.
FdNetDeviceHelper()
Construct a FdNetDeviceHelper.
void SetTypeId(std::string type)
Set the TypeId of the Objects to be created by this helper.
void EnableAsciiInternal(Ptr< OutputStreamWrapper > stream, std::string prefix, Ptr< NetDevice > nd, bool explicitFilename) override
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:84
static Mac48Address Allocate()
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.
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
static void EnablePrinting()
Enable printing packets metadata.
Definition: packet.cc:596
Manage pcap files for device models.
Definition: trace-helper.h:40
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:79
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:158
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:978
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:275
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:765
Every class exported by the ns3 library is enclosed in the ns3 namespace.