A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ipv4-packet-probe.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Bucknell University
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  * Authors: L. Felipe Perrone (perrone@bucknell.edu)
18  * Tiago G. Rodrigues (tgr002@bucknell.edu)
19  *
20  * Modified by: Mitch Watrous (watrous@u.washington.edu)
21  */
22 
23 #include "ipv4-packet-probe.h"
24 
25 #include "ns3/config.h"
26 #include "ns3/log.h"
27 #include "ns3/names.h"
28 #include "ns3/object.h"
29 #include "ns3/trace-source-accessor.h"
30 
31 namespace ns3
32 {
33 
34 NS_LOG_COMPONENT_DEFINE("Ipv4PacketProbe");
35 
36 NS_OBJECT_ENSURE_REGISTERED(Ipv4PacketProbe);
37 
38 TypeId
40 {
41  static TypeId tid =
42  TypeId("ns3::Ipv4PacketProbe")
43  .SetParent<Probe>()
44  .SetGroupName("Internet")
45  .AddConstructor<Ipv4PacketProbe>()
46  .AddTraceSource("Output",
47  "The packet plus its IPv4 object and interface "
48  "that serve as the output for this probe",
50  "ns3::Ipv4L3Protocol::TxRxTracedCallback")
51  .AddTraceSource("OutputBytes",
52  "The number of bytes in the packet",
54  "ns3::Packet::SizeTracedCallback");
55  return tid;
56 }
57 
59 {
60  NS_LOG_FUNCTION(this);
61  m_packet = nullptr;
62  m_packetSizeOld = 0;
63  m_ipv4 = nullptr;
64  m_interface = 0;
65 }
66 
68 {
69  NS_LOG_FUNCTION(this);
70 }
71 
72 void
74 {
75  NS_LOG_FUNCTION(this << packet << ipv4 << interface);
76  m_packet = packet;
77  m_ipv4 = ipv4;
78  m_interface = interface;
79  m_output(packet, ipv4, interface);
80 
81  uint32_t packetSizeNew = packet->GetSize();
82  m_outputBytes(m_packetSizeOld, packetSizeNew);
83  m_packetSizeOld = packetSizeNew;
84 }
85 
86 void
88  Ptr<const Packet> packet,
90  uint32_t interface)
91 {
92  NS_LOG_FUNCTION(path << packet << ipv4 << interface);
93  Ptr<Ipv4PacketProbe> probe = Names::Find<Ipv4PacketProbe>(path);
94  NS_ASSERT_MSG(probe, "Error: Can't find probe for path " << path);
95  probe->SetValue(packet, ipv4, interface);
96 }
97 
98 bool
99 Ipv4PacketProbe::ConnectByObject(std::string traceSource, Ptr<Object> obj)
100 {
101  NS_LOG_FUNCTION(this << traceSource << obj);
102  NS_LOG_DEBUG("Name of probe (if any) in names database: " << Names::FindPath(obj));
103  bool connected =
104  obj->TraceConnectWithoutContext(traceSource,
106  return connected;
107 }
108 
109 void
111 {
112  NS_LOG_FUNCTION(this << path);
113  NS_LOG_DEBUG("Name of probe to search for in config database: " << path);
115 }
116 
117 void
119 {
120  NS_LOG_FUNCTION(this << packet << ipv4 << interface);
121  if (IsEnabled())
122  {
123  m_packet = packet;
124  m_ipv4 = ipv4;
125  m_interface = interface;
126  m_output(packet, ipv4, interface);
127 
128  uint32_t packetSizeNew = packet->GetSize();
129  m_outputBytes(m_packetSizeOld, packetSizeNew);
130  m_packetSizeOld = packetSizeNew;
131  }
132 }
133 
134 } // namespace ns3
This class is designed to probe an underlying ns3 TraceSource exporting a packet, an IPv4 object,...
void ConnectByPath(std::string path) override
connect to a trace source provided by a config path
static void SetValueByPath(std::string path, Ptr< const Packet > packet, Ptr< Ipv4 > ipv4, uint32_t interface)
Set a probe value by its name in the Config system.
uint32_t m_packetSizeOld
The size of the traced packet.
ns3::TracedCallback< uint32_t, uint32_t > m_outputBytes
Traced Callback: the previous packet's size and the actual packet's size.
bool ConnectByObject(std::string traceSource, Ptr< Object > obj) override
connect to a trace source attribute provided by a given object
ns3::TracedCallback< Ptr< const Packet >, Ptr< Ipv4 >, uint32_t > m_output
Traced Callback: the packet, the Ipv4 object and the interface.
void TraceSink(Ptr< const Packet > packet, Ptr< Ipv4 > ipv4, uint32_t interface)
Method to connect to an underlying ns3::TraceSource with arguments of type Ptr<const Packet>,...
uint32_t m_interface
The IPv4 interface for the traced packet.
static TypeId GetTypeId()
Get the type ID.
void SetValue(Ptr< const Packet > packet, Ptr< Ipv4 > ipv4, uint32_t interface)
Set a probe value.
Ptr< Ipv4 > m_ipv4
The IPv4 object for the traced packet.
Ptr< const Packet > m_packet
The traced packet.
static std::string FindPath(Ptr< Object > object)
Given a pointer to an object, look to see if that object has a name associated with it and return the...
Definition: names.cc:836
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:315
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
Base class for probes.
Definition: probe.h:41
bool IsEnabled() const override
Definition: probe.cc:65
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#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:86
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:950
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704