From 3262a4e103fed6bf4fa5bc7320d28a1304b241e6 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 11 Dec 2007 13:51:03 +0100 Subject: [PATCH] add application tracing support --- src/applications/onoff/onoff-application.cc | 15 +++++++++++ src/applications/onoff/onoff-application.h | 3 +++ src/applications/packet-sink/packet-sink.cc | 18 ++++++++++++- src/applications/packet-sink/packet-sink.h | 5 ++++ src/node/node.cc | 29 +++++++++++++++++++++ src/node/node.h | 19 ++++++++++++++ 6 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/applications/onoff/onoff-application.cc b/src/applications/onoff/onoff-application.cc index e17be3182..eb49558d0 100644 --- a/src/applications/onoff/onoff-application.cc +++ b/src/applications/onoff/onoff-application.cc @@ -33,6 +33,7 @@ #include "ns3/socket-factory.h" #include "ns3/default-value.h" #include "ns3/packet.h" +#include "ns3/composite-trace-resolver.h" #include "onoff-application.h" NS_LOG_COMPONENT_DEFINE ("OnOffApplication"); @@ -241,6 +242,7 @@ void OnOffApplication::SendPacket() NS_ASSERT (m_sendEvent.IsExpired ()); Ptr packet = Create (m_pktSize); + m_txTrace (packet); m_socket->Send (packet); m_totBytes += m_pktSize; m_lastStartTime = Simulator::Now(); @@ -262,4 +264,17 @@ void OnOffApplication::ConnectionFailed(Ptr) cout << "OnOffApplication, Connection Failed" << endl; } +Ptr +OnOffApplication::GetTraceResolver (void) const +{ + Ptr resolver = Create (); + resolver->AddSource ("tx", + TraceDoc ("A new packet is created and is sent", + "Ptr", + "The newly-created packet."), + m_txTrace); + resolver->SetParentResolver (Application::GetTraceResolver ()); + return resolver; +} + } // Namespace ns3 diff --git a/src/applications/onoff/onoff-application.h b/src/applications/onoff/onoff-application.h index 92dceb7f7..c16730cad 100644 --- a/src/applications/onoff/onoff-application.h +++ b/src/applications/onoff/onoff-application.h @@ -29,6 +29,7 @@ #include "ns3/event-id.h" #include "ns3/ptr.h" #include "ns3/data-rate.h" +#include "ns3/callback-trace-source.h" namespace ns3 { @@ -135,8 +136,10 @@ private: EventId m_sendEvent; // Eventid of pending "send packet" event bool m_sending; // True if currently in sending state std::string m_iid; + CallbackTraceSource > m_txTrace; private: + virtual Ptr GetTraceResolver (void) const; void ScheduleNextTx(); void ScheduleStartEvent(); void ScheduleStopEvent(); diff --git a/src/applications/packet-sink/packet-sink.cc b/src/applications/packet-sink/packet-sink.cc index e6bc0d024..9fe6ab925 100644 --- a/src/applications/packet-sink/packet-sink.cc +++ b/src/applications/packet-sink/packet-sink.cc @@ -25,6 +25,7 @@ #include "ns3/simulator.h" #include "ns3/socket-factory.h" #include "ns3/packet.h" +#include "ns3/composite-trace-resolver.h" #include "packet-sink.h" using namespace std; @@ -101,8 +102,23 @@ void PacketSink::Receive(Ptr socket, Ptr packet, NS_LOG_INFO ("Received " << packet->GetSize() << " bytes from " << address.GetIpv4() << " [" << address << "]---'" << packet->PeekData() << "'"); - // TODO: Add a tracing source here } + m_rxTrace (packet, from); +} + +Ptr +PacketSink::GetTraceResolver (void) const +{ + Ptr resolver = Create (); + resolver->AddSource ("rx", + TraceDoc ("A new packet has been received", + "Ptr", + "The newly-received packet.", + "const Address &", + "The source address of the received packet."), + m_rxTrace); + resolver->SetParentResolver (Application::GetTraceResolver ()); + return resolver; } } // Namespace ns3 diff --git a/src/applications/packet-sink/packet-sink.h b/src/applications/packet-sink/packet-sink.h index f13a4bca4..96fc7f98f 100644 --- a/src/applications/packet-sink/packet-sink.h +++ b/src/applications/packet-sink/packet-sink.h @@ -24,6 +24,8 @@ #include "ns3/application.h" #include "ns3/event-id.h" #include "ns3/ptr.h" +#include "ns3/callback-trace-source.h" +#include "ns3/address.h" namespace ns3 { @@ -68,6 +70,8 @@ private: // inherited from Application base class. virtual void StartApplication (void); // Called at time specified by Start virtual void StopApplication (void); // Called at time specified by Stop + // inherited from Object base class. + virtual Ptr GetTraceResolver (void) const; void Construct (Ptr n, const Address &local, @@ -78,6 +82,7 @@ private: Ptr m_socket; // Associated socket Address m_local; // Local address to bind to std::string m_iid; // Protocol name (e.g., "Udp") + CallbackTraceSource, const Address &> m_rxTrace; }; diff --git a/src/node/node.cc b/src/node/node.cc index 8aaa56f16..1ac13b522 100644 --- a/src/node/node.cc +++ b/src/node/node.cc @@ -60,6 +60,34 @@ NodeNetDeviceIndex::GetTypeName (void) const } +NodeApplicationIndex::NodeApplicationIndex () + : m_index (0) +{} +NodeApplicationIndex::NodeApplicationIndex (uint32_t index) + : m_index (index) +{} +uint32_t +NodeApplicationIndex::Get (void) const +{ + return m_index; +} +void +NodeApplicationIndex::Print (std::ostream &os) const +{ + os << "device=" << m_index; +} +uint16_t +NodeApplicationIndex::GetUid (void) +{ + static uint16_t uid = AllocateUid ("NodeApplicationIndex"); + return uid; +} +std::string +NodeApplicationIndex::GetTypeName (void) const +{ + return "ns3::NodeApplicationIndex"; +} + Node::Node() : m_id(0), @@ -92,6 +120,7 @@ Node::GetTraceResolver (void) const { Ptr resolver = Create (); resolver->AddArray ("devices", m_devices.begin (), m_devices.end (), NodeNetDeviceIndex ()); + resolver->AddArray ("applications", m_applications.begin (), m_applications.end (), NodeApplicationIndex ()); resolver->SetParentResolver (Object::GetTraceResolver ()); return resolver; } diff --git a/src/node/node.h b/src/node/node.h index bc4639b48..24caa9317 100644 --- a/src/node/node.h +++ b/src/node/node.h @@ -57,6 +57,25 @@ private: uint32_t m_index; }; +/** + * \brief hold in a TraceContext the index of an Application within a Node + */ +class NodeApplicationIndex : public TraceContextElement +{ +public: + NodeApplicationIndex (); + NodeApplicationIndex (uint32_t index); + /** + * \returns the index of the Application within its container Node. + */ + uint32_t Get (void) const; + void Print (std::ostream &os) const; + std::string GetTypeName (void) const; + static uint16_t GetUid (void); +private: + uint32_t m_index; +}; + /** * \brief A network Node. *