A Discrete-Event Network Simulator
API
trace-helper.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 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 #ifndef TRACE_HELPER_H
20 #define TRACE_HELPER_H
21 
22 #include "ns3/assert.h"
23 #include "ns3/net-device-container.h"
24 #include "ns3/node-container.h"
25 #include "ns3/simulator.h"
26 #include "ns3/pcap-file-wrapper.h"
27 #include "ns3/output-stream-wrapper.h"
28 
29 namespace ns3 {
30 
39 {
40 public:
50  enum DataLinkType {
51  DLT_NULL = 0,
53  DLT_PPP = 9,
54  DLT_RAW = 101,
61  };
62 
66  PcapHelper ();
67 
71  ~PcapHelper ();
72 
82  std::string GetFilenameFromDevice (std::string prefix, Ptr<NetDevice> device, bool useObjectNames = true);
83 
94  std::string GetFilenameFromInterfacePair (std::string prefix, Ptr<Object> object,
95  uint32_t interface, bool useObjectNames = true);
96 
107  Ptr<PcapFileWrapper> CreateFile (std::string filename,
108  std::ios::openmode filemode,
109  DataLinkType dataLinkType,
110  uint32_t snapLen = std::numeric_limits<uint32_t>::max (),
111  int32_t tzCorrection = 0);
119  template <typename T> void HookDefaultSink (Ptr<T> object, std::string traceName, Ptr<PcapFileWrapper> file);
120 
121 private:
131  static void DefaultSink (Ptr<PcapFileWrapper> file, Ptr<const Packet> p);
132 
143  static void SinkWithHeader (Ptr<PcapFileWrapper> file, const Header& header, Ptr<const Packet> p);
144 };
145 
146 template <typename T> void
147 PcapHelper::HookDefaultSink (Ptr<T> object, std::string tracename, Ptr<PcapFileWrapper> file)
148 {
149  bool result =
150  object->TraceConnectWithoutContext (tracename.c_str (), MakeBoundCallback (&DefaultSink, file));
151  NS_ASSERT_MSG (result == true, "PcapHelper::HookDefaultSink(): Unable to hook \"" << tracename << "\"");
152 }
153 
162 {
163 public:
167  AsciiTraceHelper ();
168 
173 
183  std::string GetFilenameFromDevice (std::string prefix, Ptr<NetDevice> device, bool useObjectNames = true);
184 
195  std::string GetFilenameFromInterfacePair (std::string prefix, Ptr<Object> object,
196  uint32_t interface, bool useObjectNames = true);
197 
222  Ptr<OutputStreamWrapper> CreateFileStream (std::string filename,
223  std::ios::openmode filemode = std::ios::out);
224 
233  template <typename T>
234  void HookDefaultEnqueueSinkWithoutContext (Ptr<T> object, std::string traceName, Ptr<OutputStreamWrapper> stream);
235 
245  template <typename T>
247  std::string context, std::string traceName, Ptr<OutputStreamWrapper> stream);
248 
257  template <typename T>
258  void HookDefaultDropSinkWithoutContext (Ptr<T> object, std::string traceName, Ptr<OutputStreamWrapper> stream);
259 
269  template <typename T>
271  std::string context, std::string traceName, Ptr<OutputStreamWrapper> stream);
272 
281  template <typename T>
282  void HookDefaultDequeueSinkWithoutContext (Ptr<T> object, std::string traceName, Ptr<OutputStreamWrapper> stream);
283 
293  template <typename T>
295  std::string context, std::string traceName, Ptr<OutputStreamWrapper> stream);
296 
305  template <typename T>
306  void HookDefaultReceiveSinkWithoutContext (Ptr<T> object, std::string traceName, Ptr<OutputStreamWrapper> stream);
307 
317  template <typename T>
319  std::string context, std::string traceName, Ptr<OutputStreamWrapper> stream);
320 
336 
352  static void DefaultEnqueueSinkWithContext (Ptr<OutputStreamWrapper> file, std::string context, Ptr<const Packet> p);
353 
370 
387  static void DefaultDropSinkWithContext (Ptr<OutputStreamWrapper> file, std::string context, Ptr<const Packet> p);
388 
406 
424  static void DefaultDequeueSinkWithContext (Ptr<OutputStreamWrapper> file, std::string context, Ptr<const Packet> p);
425 
441 
457  static void DefaultReceiveSinkWithContext (Ptr<OutputStreamWrapper> file, std::string context, Ptr<const Packet> p);
458 };
459 
460 template <typename T> void
462 {
463  bool result =
464  object->TraceConnectWithoutContext (tracename, MakeBoundCallback (&DefaultEnqueueSinkWithoutContext, file));
465  NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultEnqueueSinkWithoutContext(): Unable to hook \""
466  << tracename << "\"");
467 }
468 
469 template <typename T> void
471  Ptr<T> object,
472  std::string context,
473  std::string tracename,
475 {
476  bool result =
477  object->TraceConnect (tracename, context, MakeBoundCallback (&DefaultEnqueueSinkWithContext, stream));
478  NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultEnqueueSinkWithContext(): Unable to hook \""
479  << tracename << "\"");
480 }
481 
482 template <typename T> void
484 {
485  bool result =
486  object->TraceConnectWithoutContext (tracename, MakeBoundCallback (&DefaultDropSinkWithoutContext, file));
487  NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultDropSinkWithoutContext(): Unable to hook \""
488  << tracename << "\"");
489 }
490 
491 template <typename T> void
493  Ptr<T> object,
494  std::string context,
495  std::string tracename,
497 {
498  bool result =
499  object->TraceConnect (tracename, context, MakeBoundCallback (&DefaultDropSinkWithContext, stream));
500  NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultDropSinkWithContext(): Unable to hook \""
501  << tracename << "\"");
502 }
503 
504 template <typename T> void
506 {
507  bool result =
508  object->TraceConnectWithoutContext (tracename, MakeBoundCallback (&DefaultDequeueSinkWithoutContext, file));
509  NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultDequeueSinkWithoutContext(): Unable to hook \""
510  << tracename << "\"");
511 }
512 
513 template <typename T> void
515  Ptr<T> object,
516  std::string context,
517  std::string tracename,
519 {
520  bool result =
521  object->TraceConnect (tracename, context, MakeBoundCallback (&DefaultDequeueSinkWithContext, stream));
522  NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultDequeueSinkWithContext(): Unable to hook \""
523  << tracename << "\"");
524 }
525 
526 template <typename T> void
528 {
529  bool result =
530  object->TraceConnectWithoutContext (tracename, MakeBoundCallback (&DefaultReceiveSinkWithoutContext, file));
531  NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultReceiveSinkWithoutContext(): Unable to hook \""
532  << tracename << "\"");
533 }
534 
535 template <typename T> void
537  Ptr<T> object,
538  std::string context,
539  std::string tracename,
541 {
542  bool result =
543  object->TraceConnect (tracename, context, MakeBoundCallback (&DefaultReceiveSinkWithContext, stream));
544  NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultReceiveSinkWithContext(): Unable to hook \""
545  << tracename << "\"");
546 }
547 
553 {
554 public:
559 
563  virtual ~PcapHelperForDevice () {}
564 
573  virtual void EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename) = 0;
574 
583  void EnablePcap (std::string prefix, Ptr<NetDevice> nd, bool promiscuous = false, bool explicitFilename = false);
584 
594  void EnablePcap (std::string prefix, std::string ndName, bool promiscuous = false, bool explicitFilename = false);
595 
604  void EnablePcap (std::string prefix, NetDeviceContainer d, bool promiscuous = false);
605 
614  void EnablePcap (std::string prefix, NodeContainer n, bool promiscuous = false);
615 
625  void EnablePcap (std::string prefix, uint32_t nodeid, uint32_t deviceid, bool promiscuous = false);
626 
634  void EnablePcapAll (std::string prefix, bool promiscuous = false);
635 };
636 
642 {
643 public:
648 
653 
677  virtual void EnableAsciiInternal (Ptr<OutputStreamWrapper> stream,
678  std::string prefix,
679  Ptr<NetDevice> nd,
680  bool explicitFilename) = 0;
681 
689  void EnableAscii (std::string prefix, Ptr<NetDevice> nd, bool explicitFilename = false);
690 
699 
708  void EnableAscii (std::string prefix, std::string ndName, bool explicitFilename = false);
709 
718  void EnableAscii (Ptr<OutputStreamWrapper> stream, std::string ndName);
719 
727  void EnableAscii (std::string prefix, NetDeviceContainer d);
728 
738 
746  void EnableAscii (std::string prefix, NodeContainer n);
747 
757 
764  void EnableAsciiAll (std::string prefix);
765 
774 
786  void EnableAscii (std::string prefix, uint32_t nodeid, uint32_t deviceid, bool explicitFilename);
787 
799  void EnableAscii (Ptr<OutputStreamWrapper> stream, uint32_t nodeid, uint32_t deviceid);
800 
801 private:
817  std::string prefix,
818  uint32_t nodeid,
819  uint32_t deviceid,
820  bool explicitFilename);
821 
831  void EnableAsciiImpl (Ptr<OutputStreamWrapper> stream, std::string prefix, NodeContainer n);
832 
842  void EnableAsciiImpl (Ptr<OutputStreamWrapper> stream, std::string prefix, NetDeviceContainer d);
843 
854  void EnableAsciiImpl (Ptr<OutputStreamWrapper> stream, std::string prefix, std::string ndName, bool explicitFilename);
855 
865  void EnableAsciiImpl (Ptr<OutputStreamWrapper> stream, std::string prefix, Ptr<NetDevice> nd, bool explicitFilename);
866 };
867 
868 } // namespace ns3
869 
870 #endif /* TRACE_HELPER_H */
Protocol header serialization and deserialization.
Definition: header.h:42
Base class providing common user-level ascii trace operations for helpers representing net devices...
Definition: trace-helper.h:641
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
static void DefaultEnqueueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Enqueue default trace sink.
void EnableAsciiImpl(Ptr< OutputStreamWrapper > stream, std::string prefix, uint32_t nodeid, uint32_t deviceid, bool explicitFilename)
Enable ascii trace output on the device specified by a global node-id (of a previously created node) ...
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:483
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
Base class providing common user-level pcap operations for helpers representing net devices...
Definition: trace-helper.h:552
static void DefaultDropSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Drop default trace sink.
void HookDefaultDequeueSinkWithContext(Ptr< T > object, std::string context, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default dequeue operation trace sink that does accept and log a trace cont...
Definition: trace-helper.h:514
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 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:461
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
static void DefaultReceiveSinkWithoutContext(Ptr< OutputStreamWrapper > file, Ptr< const Packet > p)
Basic Receive default trace sink.
static void SinkWithHeader(Ptr< PcapFileWrapper > file, const Header &header, Ptr< const Packet > p)
This trace sink passes a header separately from the packet to prevent creating a new packet (for perf...
DataLinkType
This enumeration holds the data link types that will be written to the pcap file. ...
Definition: trace-helper.h:50
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
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
#define max(a, b)
Definition: 80211b.c:45
holds a vector of ns3::NetDevice pointers
static void DefaultEnqueueSinkWithoutContext(Ptr< OutputStreamWrapper > file, Ptr< const Packet > p)
Basic Enqueue default trace sink.
std::string GetFilenameFromInterfacePair(std::string prefix, Ptr< Object > object, uint32_t interface, bool useObjectNames=true)
Let the pcap helper figure out a reasonable filename to use for the pcap file associated with a node...
static void DefaultDequeueSinkWithoutContext(Ptr< OutputStreamWrapper > file, Ptr< const Packet > p)
Basic Dequeue default trace sink.
AsciiTraceHelperForDevice()
Construct an AsciiTraceHelperForDevice.
Definition: trace-helper.h:647
static void DefaultReceiveSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Receive default trace sink.
static void DefaultSink(Ptr< PcapFileWrapper > file, Ptr< const Packet > p)
The basic default trace sink.
static void DefaultDequeueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Dequeue default trace sink.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
virtual void EnablePcapInternal(std::string prefix, Ptr< NetDevice > nd, bool promiscuous, bool explicitFilename)=0
Enable pcap output the indicated net device.
void HookDefaultReceiveSinkWithContext(Ptr< T > object, std::string context, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default receive operation trace sink that does accept and log a trace cont...
Definition: trace-helper.h:536
AsciiTraceHelper()
Create an ascii trace helper.
static void DefaultDropSinkWithoutContext(Ptr< OutputStreamWrapper > file, Ptr< const Packet > p)
Basic Drop default trace sink.
PcapHelper()
Create a pcap helper.
Definition: trace-helper.cc:38
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
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
void EnableAscii(std::string prefix, Ptr< NetDevice > nd, bool explicitFilename=false)
Enable ascii trace output on the indicated net device.
std::string GetFilenameFromInterfacePair(std::string prefix, Ptr< Object > object, uint32_t interface, bool useObjectNames=true)
Let the ascii trace helper figure out a reasonable filename to use for an ascii trace file associated...
virtual void EnableAsciiInternal(Ptr< OutputStreamWrapper > stream, std::string prefix, Ptr< NetDevice > nd, bool explicitFilename)=0
Enable ascii trace output on the indicated net device.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
PcapHelperForDevice()
Construct a PcapHelperForDevice.
Definition: trace-helper.h:558
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
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
void HookDefaultEnqueueSinkWithContext(Ptr< T > object, std::string context, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default enqueue operation trace sink that does accept and log a trace cont...
Definition: trace-helper.h:470
~AsciiTraceHelper()
Destroy an ascii trace helper.
virtual ~PcapHelperForDevice()
Destroy a PcapHelperForDevice.
Definition: trace-helper.h:563
~PcapHelper()
Destroy a pcap helper.
Definition: trace-helper.cc:43
virtual ~AsciiTraceHelperForDevice()
Destroy an AsciiTraceHelperForDevice.
Definition: trace-helper.h:652
void HookDefaultDropSinkWithContext(Ptr< T > object, std::string context, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default drop operation trace sink that does accept and log a trace context...
Definition: trace-helper.h:492
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:505