# HG changeset patch # User Tom Henderson # Date 1189144043 25200 # Node ID ea8d7a1cab1d9751948354bce5cc7381b200ef78 # Parent 4c8b5cc015474552a9b6233c7771dc89e03d5dfa Application PacketSink to pair up with OnOffApplication diff -r 4c8b5cc01547 -r ea8d7a1cab1d examples/simple-global-routing.cc --- a/examples/simple-global-routing.cc Tue Sep 04 23:59:56 2007 -0700 +++ b/examples/simple-global-routing.cc Thu Sep 06 22:47:23 2007 -0700 @@ -65,6 +65,7 @@ #include "ns3/ipv4-route.h" #include "ns3/point-to-point-topology.h" #include "ns3/onoff-application.h" +#include "ns3/packet-sink.h" #include "ns3/global-route-manager.h" using namespace ns3; @@ -151,6 +152,15 @@ int main (int argc, char *argv[]) ooff->Start (Seconds (1.0)); ooff->Stop (Seconds (10.0)); + // Create a packet sink to receive these packets + Ptr sink = Create ( + n3, + InetSocketAddress (Ipv4Address::GetAny (), 80), + "Udp"); + // Start the sink + sink->Start (Seconds (1.0)); + sink->Stop (Seconds (10.0)); + // Create a similar flow from n3 to n1, starting at time 1.1 seconds ooff = Create ( n3, @@ -162,6 +172,15 @@ int main (int argc, char *argv[]) ooff->Start (Seconds (1.1)); ooff->Stop (Seconds (10.0)); + // Create a packet sink to receive these packets + sink = Create ( + n1, + InetSocketAddress (Ipv4Address::GetAny (), 80), + "Udp"); + // Start the sink + sink->Start (Seconds (1.1)); + sink->Stop (Seconds (10.0)); + // Configure tracing of all enqueue, dequeue, and NetDevice receive events // Trace output will be sent to the simple-global-routing.tr file AsciiTrace asciitrace ("simple-global-routing.tr"); diff -r 4c8b5cc01547 -r ea8d7a1cab1d src/applications/packet-sink.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/applications/packet-sink.cc Thu Sep 06 22:47:23 2007 -0700 @@ -0,0 +1,100 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +// +// Copyright (c) 2006 Georgia Tech Research Corporation +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +#include "ns3/address.h" +#include "ns3/inet-socket-address.h" +#include "ns3/node.h" +#include "ns3/socket.h" +#include "ns3/simulator.h" +#include "ns3/socket-factory.h" +#include "ns3/packet.h" +#include "packet-sink.h" + +using namespace std; + +namespace ns3 { + +// Constructors + +PacketSink::PacketSink (Ptr n, + const Address &local, + std::string iid) + : Application(n) +{ + Construct (n, local, iid); +} + +void +PacketSink::Construct (Ptr n, + const Address &local, + std::string iid) +{ + m_socket = 0; + m_local = local; + m_iid = iid; +} + +PacketSink::~PacketSink() +{} + +void +PacketSink::DoDispose (void) +{ + m_socket = 0; + + // chain up + Application::DoDispose (); +} + + +// Application Methods +void PacketSink::StartApplication() // Called at time specified by Start +{ + // Create the socket if not already + if (!m_socket) + { + InterfaceId iid = InterfaceId::LookupByName (m_iid); + Ptr socketFactory = + GetNode ()->QueryInterface (iid); + m_socket = socketFactory->CreateSocket (); + m_socket->Bind (m_local); + } + m_socket->SetRecvCallback((Callback, const Packet &, + const Address &>) MakeCallback(&PacketSink::Receive, this)); +} + +void PacketSink::StopApplication() // Called at time specified by Stop +{ + if (!m_socket) + { + m_socket->SetRecvCallback((Callback, const Packet &, + const Address &>) NULL); + + } +} + +// This callback body suggested by Joe Kopena's wiki +void PacketSink::Receive(Ptr socket, const Packet &packet, + const Address &from) +{ + InetSocketAddress address = InetSocketAddress::ConvertFrom (from); + cout << __PRETTY_FUNCTION__ << ": Received " << packet.GetSize() << + " bytes from " << address.GetIpv4() << " [" << address << "]---'" << + packet.PeekData() << "'" << endl; +} + +} // Namespace ns3 diff -r 4c8b5cc01547 -r ea8d7a1cab1d src/applications/packet-sink.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/applications/packet-sink.h Thu Sep 06 22:47:23 2007 -0700 @@ -0,0 +1,71 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +// +// Copyright (c) 2006 Georgia Tech Research Corporation +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// + +#ifndef __packet_sink_h__ +#define __packet_sink_h__ + +#include "ns3/application.h" +#include "ns3/event-id.h" +#include "ns3/ptr.h" + +namespace ns3 { + +class Address; +class Socket; +class Packet; + +class PacketSink : public Application +{ +public: + /** + * \param n node associated to this application + * \param local local ip address + * \param iid + * \param ontime on time random variable + * \param offtime off time random variable + */ + PacketSink (Ptr n, + const Address &local, + std::string iid); + + virtual ~PacketSink (); + +protected: + virtual void DoDispose (void); +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 + + void Construct (Ptr n, + const Address &local, + std::string iid); + + void Receive (Ptr socket, const Packet& packet, const Address& from); + + Ptr m_socket; // Associated socket + Address m_local; // Local address to bind to + std::string m_iid; // Protocol name (e.g., "Udp") + +}; + +} // namespace ns3 + +#endif + diff -r 4c8b5cc01547 -r ea8d7a1cab1d src/applications/wscript --- a/src/applications/wscript Tue Sep 04 23:59:56 2007 -0700 +++ b/src/applications/wscript Thu Sep 06 22:47:23 2007 -0700 @@ -4,9 +4,11 @@ def build(bld): obj = bld.create_ns3_module('applications', ['node']) obj.source = [ 'onoff-application.cc', + 'packet-sink.cc', ] headers = bld.create_obj('ns3header') headers.source = [ 'onoff-application.h', + 'packet-sink.h', ] diff -r 4c8b5cc01547 -r ea8d7a1cab1d src/node/ipv4-address.h --- a/src/node/ipv4-address.h Tue Sep 04 23:59:56 2007 -0700 +++ b/src/node/ipv4-address.h Thu Sep 06 22:47:23 2007 -0700 @@ -135,8 +135,20 @@ public: static Ipv4Address ConvertFrom (const Address &address); static Ipv4Address GetZero (void); + /** + * \brief Get Ipv4Address corresponding to INADDR_ANY constant + * (0.0.0.0) + */ static Ipv4Address GetAny (void); + /** + * \brief Get Ipv4Address corresponding to INADDR_BROADCAST constant + * (255.255.255.255) + */ static Ipv4Address GetBroadcast (void); + /** + * \brief Get Ipv4Address corresponding to INADDR_LOOPBACK constant + * (127.0.0.1) + */ static Ipv4Address GetLoopback (void); private: Address ConvertTo (void) const;