A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
emu-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 University of Washington
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 
19 #include <string>
20 
21 #include "ns3/log.h"
22 #include "ns3/simulator.h"
23 #include "ns3/object-factory.h"
24 #include "ns3/names.h"
25 #include "ns3/queue.h"
26 #include "ns3/emu-net-device.h"
27 #include "ns3/config.h"
28 #include "ns3/packet.h"
29 
30 #include "ns3/trace-helper.h"
31 #include "emu-helper.h"
32 
33 NS_LOG_COMPONENT_DEFINE ("EmuHelper");
34 
35 namespace ns3 {
36 
38 {
40  m_queueFactory.SetTypeId ("ns3::DropTailQueue");
41  m_deviceFactory.SetTypeId ("ns3::EmuNetDevice");
42 }
43 
44 void
46  std::string type,
47  std::string n1, const AttributeValue &v1,
48  std::string n2, const AttributeValue &v2,
49  std::string n3, const AttributeValue &v3,
50  std::string n4, const AttributeValue &v4)
51 {
54  m_queueFactory.Set (n1, v1);
55  m_queueFactory.Set (n2, v2);
56  m_queueFactory.Set (n3, v3);
57  m_queueFactory.Set (n4, v4);
58 }
59 
60 void
61 EmuHelper::SetAttribute (std::string n1, const AttributeValue &v1)
62 {
64  m_deviceFactory.Set (n1, v1);
65 }
66 
67 void
68 EmuHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
69 {
70  //
71  // All of the Pcap enable functions vector through here including the ones
72  // that are wandering through all of devices on perhaps all of the nodes in
73  // the system. We can only deal with devices of type EmuNetDevice.
74  //
75  Ptr<EmuNetDevice> device = nd->GetObject<EmuNetDevice> ();
76  if (device == 0)
77  {
78  NS_LOG_INFO ("EmuHelper::EnablePcapInternal(): Device " << device << " not of type ns3::EmuNetDevice");
79  return;
80  }
81 
82  PcapHelper pcapHelper;
83 
84  std::string filename;
85  if (explicitFilename)
86  {
87  filename = prefix;
88  }
89  else
90  {
91  filename = pcapHelper.GetFilenameFromDevice (prefix, device);
92  }
93 
94  Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, PcapHelper::DLT_EN10MB);
95  if (promiscuous)
96  {
97  pcapHelper.HookDefaultSink<EmuNetDevice> (device, "PromiscSniffer", file);
98  }
99  else
100  {
101  pcapHelper.HookDefaultSink<EmuNetDevice> (device, "Sniffer", file);
102  }
103 }
104 
105 void
107  Ptr<OutputStreamWrapper> stream,
108  std::string prefix,
109  Ptr<NetDevice> nd,
110  bool explicitFilename)
111 {
112  //
113  // All of the ascii enable functions vector through here including the ones
114  // that are wandering through all of devices on perhaps all of the nodes in
115  // the system. We can only deal with devices of type EmuNetDevice.
116  //
117  Ptr<EmuNetDevice> device = nd->GetObject<EmuNetDevice> ();
118  if (device == 0)
119  {
120  NS_LOG_INFO ("EmuHelper::EnableAsciiInternal(): Device " << device << " not of type ns3::EmuNetDevice");
121  return;
122  }
123 
124  //
125  // Our default trace sinks are going to use packet printing, so we have to
126  // make sure that is turned on.
127  //
129 
130  //
131  // If we are not provided an OutputStreamWrapper, we are expected to create
132  // one using the usual trace filename conventions and do a Hook*WithoutContext
133  // since there will be one file per context and therefore the context would
134  // be redundant.
135  //
136  if (stream == 0)
137  {
138  //
139  // Set up an output stream object to deal with private ofstream copy
140  // constructor and lifetime issues. Let the helper decide the actual
141  // name of the file given the prefix.
142  //
143  AsciiTraceHelper asciiTraceHelper;
144 
145  std::string filename;
146  if (explicitFilename)
147  {
148  filename = prefix;
149  }
150  else
151  {
152  filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
153  }
154 
155  Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
156 
157  //
158  // The MacRx trace source provides our "r" event.
159  //
160  asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<EmuNetDevice> (device, "MacRx", theStream);
161 
162  //
163  // The "+", '-', and 'd' events are driven by trace sources actually in the
164  // transmit queue.
165  //
166  Ptr<Queue> queue = device->GetQueue ();
167  asciiTraceHelper.HookDefaultEnqueueSinkWithoutContext<Queue> (queue, "Enqueue", theStream);
168  asciiTraceHelper.HookDefaultDropSinkWithoutContext<Queue> (queue, "Drop", theStream);
169  asciiTraceHelper.HookDefaultDequeueSinkWithoutContext<Queue> (queue, "Dequeue", theStream);
170 
171  return;
172  }
173 
174  //
175  // If we are provided an OutputStreamWrapper, we are expected to use it, and
176  // to providd a context. We are free to come up with our own context if we
177  // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
178  // compatibility and simplicity, we just use Config::Connect and let it deal
179  // with the context.
180  //
181  // Note that we are going to use the default trace sinks provided by the
182  // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
183  // but the default trace sinks are actually publicly available static
184  // functions that are always there waiting for just such a case.
185  //
186  uint32_t nodeid = nd->GetNode ()->GetId ();
187  uint32_t deviceid = nd->GetIfIndex ();
188  std::ostringstream oss;
189 
190  oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::EmuNetDevice/MacRx";
192 
193  oss.str ("");
194  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::EmuNetDevice/TxQueue/Enqueue";
196 
197  oss.str ("");
198  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::EmuNetDevice/TxQueue/Dequeue";
200 
201  oss.str ("");
202  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::EmuNetDevice/TxQueue/Drop";
204 }
205 
208 {
209  return NetDeviceContainer (InstallPriv (node));
210 }
211 
213 EmuHelper::Install (std::string nodeName) const
214 {
215  Ptr<Node> node = Names::Find<Node> (nodeName);
216  return NetDeviceContainer (InstallPriv (node));
217 }
218 
221 {
222  NetDeviceContainer devs;
223 
224  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++)
225  {
226  devs.Add (InstallPriv (*i));
227  }
228 
229  return devs;
230 }
231 
234 {
236  device->SetAddress (Mac48Address::Allocate ());
237  node->AddDevice (device);
239  device->SetQueue (queue);
240 
241  return device;
242 }
243 
244 } // namespace ns3
virtual void EnableAsciiInternal(Ptr< OutputStreamWrapper > stream, std::string prefix, Ptr< NetDevice > nd, bool explicitFilename)
Enable ascii trace output on the indicated net device.
Definition: emu-helper.cc:106
Manage ASCII trace files for device models.
Definition: trace-helper.h:128
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
static void DefaultEnqueueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Ptr< NetDevice > InstallPriv(Ptr< Node > node) const
Definition: emu-helper.cc:233
Ptr< PcapFileWrapper > CreateFile(std::string filename, std::ios::openmode filemode, uint32_t dataLinkType, uint32_t snapLen=65535, int32_t tzCorrection=0)
Create and initialize a pcap file.
Definition: trace-helper.cc:49
void HookDefaultDropSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default drop operation trace sink that does not accept nor log a trace con...
Definition: trace-helper.h:321
std::vector< Ptr< Node > >::const_iterator Iterator
Hold a value for an Attribute.
Definition: attribute.h:56
Manage pcap files for device models.
Definition: trace-helper.h:38
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Build bound Callbacks which take varying numbers of arguments, and potentially returning a value...
Definition: callback.h:1463
static void DefaultDropSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Ptr< Queue > GetQueue(void) const
Get a copy of the attached Queue.
void SetTypeId(TypeId tid)
void SetAttribute(std::string n1, const AttributeValue &v1)
Definition: emu-helper.cc:61
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
#define NS_LOG_INFO(msg)
Definition: log.h:298
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
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 Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
void HookDefaultEnqueueSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default enqueue operation trace sink that does not accept nor log a trace ...
Definition: trace-helper.h:299
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...
Abstract base class for packet Queues.
Definition: queue.h:45
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
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
static void EnablePrinting(void)
By default, packets do not keep around enough metadata to perform the operations requested by the Pri...
Definition: packet.cc:552
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
static void DefaultReceiveSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
ObjectFactory m_deviceFactory
Definition: emu-helper.h:149
static void DefaultDequeueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
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)
NS_LOG_COMPONENT_DEFINE("EmuHelper")
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:365
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:118
virtual void EnablePcapInternal(std::string prefix, Ptr< NetDevice > nd, bool promiscuous, bool explicitFilename)
Enable pcap output the indicated net device.
Definition: emu-helper.cc:68
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::EmuNetDevice with the attributes configured by EmuHelper::SetDeviceAttrib...
Definition: emu-helper.cc:207
void SetQueue(std::string type, 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())
Definition: emu-helper.cc:45
ObjectFactory m_queueFactory
Definition: emu-helper.h:148
A Device for an Emu Network Link.
void HookDefaultSink(Ptr< T > object, std::string traceName, Ptr< PcapFileWrapper > file)
Hook a trace source to the default trace sink.
Definition: trace-helper.h:114
void HookDefaultDequeueSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default dequeue operation trace sink that does not accept nor log a trace ...
Definition: trace-helper.h:343