finish up basic echo apps
authorCraig Dowell <craigdo@ee.washington.edu>
Thu, 13 Sep 2007 10:52:41 -0700
changeset 1500 895ed42278d3
parent 1499 93d51d757afa
child 1501 665e07dbd919
finish up basic echo apps
examples/udp-echo.cc
src/applications/udp-echo/udp-echo-client.cc
src/applications/udp-echo/udp-echo-client.h
src/applications/udp-echo/udp-echo-server.cc
src/applications/udp-echo/udp-echo-server.h
--- a/examples/udp-echo.cc	Wed Sep 12 16:54:21 2007 -0700
+++ b/examples/udp-echo.cc	Thu Sep 13 10:52:41 2007 -0700
@@ -21,7 +21,7 @@
 //       =================
 //              LAN
 //
-// - CBR/UDP flows from n0 to n1 and from n3 to n0
+// - UDP flows from n0 to n1 and back
 // - DropTail queues 
 // - Tracing of queues and packet receptions to file "udp-echo.tr"
 
@@ -136,11 +136,6 @@
 
   uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
     Eui48Address("08:00:2e:00:00:03"));
-
-  NS_DEBUG ("nd0 = " << nd0);
-  NS_DEBUG ("nd1 = " << nd1);
-  NS_DEBUG ("nd2 = " << nd2);
-  NS_DEBUG ("nd3 = " << nd3);
 //
 // We've got the "hardware" in place.  Now we need to add IP addresses.
 //
@@ -171,25 +166,29 @@
 
   NS_DEBUG("Create Applications.");
 //
-// Create a UdpEchoServer application on node 1.
-//
-  Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, "0.0.0.0", 80);
+// Create a UdpEchoServer application on node one.
 //
-// Tell the application when to start and stop.
-//
-  server->Start(Seconds(1.));
-  server->Stop (Seconds(10.0));
+  uint16_t port = 80;
+
+  Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
 //
 // Create a UdpEchoClient application to send UDP datagrams from node zero to
-// node 1.
-//
-  Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, "10.1.1.2", 80, 
-    1, Seconds(1.), 1024);
+// node one.
 //
-// Tell the application when to start and stop.
+  uint32_t packetSize = 1024;
+  uint32_t maxPacketCount = 1;
+  Time interPacketInterval = Seconds (1.);
+
+  Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, "10.1.1.2", port, 
+    maxPacketCount, interPacketInterval, packetSize);
 //
-  client->Start(Seconds(2.0));
-  client->Stop (Seconds(10.0));
+// Tell the applications when to start and stop.
+//
+  server->Start(Seconds(1.));
+  client->Start(Seconds(2.));
+
+  server->Stop (Seconds(10.));
+  client->Stop (Seconds(10.));
 //
 // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
 // Trace output will be sent to the file "udp-echo.tr"
--- a/src/applications/udp-echo/udp-echo-client.cc	Wed Sep 12 16:54:21 2007 -0700
+++ b/src/applications/udp-echo/udp-echo-client.cc	Thu Sep 13 10:52:41 2007 -0700
@@ -99,7 +99,9 @@
       m_socket->Connect (m_peer);
     }
 
-  StopApplication ();
+  m_socket->SetRecvCallback((Callback<void, Ptr<Socket>, const Packet &,
+    const Address &>) MakeCallback(&UdpEchoClient::Receive, this));
+
   ScheduleTransmit (Seconds(0.));
 }
 
@@ -107,6 +109,14 @@
 UdpEchoClient::StopApplication ()
 {
   NS_DEBUG ("UdpEchoClient::StopApplication ()");
+
+  if (!m_socket) 
+    {
+      m_socket->SetRecvCallback((Callback<void, Ptr<Socket>, const Packet &,
+        const Address &>) NULL);
+    }
+
+  Simulator::Cancel(m_sendEvent);
 }
 
 void 
@@ -136,4 +146,22 @@
     }
 }
 
+void
+UdpEchoClient::Receive(
+  Ptr<Socket> socket, 
+  const Packet &packet,
+  const Address &from) 
+{
+  NS_DEBUG ("UdpEchoClient::Receive (" << socket << ", " << packet <<
+    ", " << from << ")");
+
+  if (InetSocketAddress::IsMatchingType (from))
+    {
+      InetSocketAddress address = InetSocketAddress::ConvertFrom (from);
+      NS_DEBUG ("UdpEchoClient::Receive(): Received " << 
+        packet.GetSize() << " bytes from " << address.GetIpv4());
+    }
+}
+
+
 } // Namespace ns3
--- a/src/applications/udp-echo/udp-echo-client.h	Wed Sep 12 16:54:21 2007 -0700
+++ b/src/applications/udp-echo/udp-echo-client.h	Thu Sep 13 10:52:41 2007 -0700
@@ -50,6 +50,8 @@
   void ScheduleTransmit (Time dt);
   void Send (void);
 
+  void Receive(Ptr<Socket> socket, const Packet &packet, const Address &from);
+
   Ptr<Node> m_node;
   Ipv4Address m_serverAddress;
   uint16_t m_serverPort;
--- a/src/applications/udp-echo/udp-echo-server.cc	Wed Sep 12 16:54:21 2007 -0700
+++ b/src/applications/udp-echo/udp-echo-server.cc	Thu Sep 13 10:52:41 2007 -0700
@@ -15,6 +15,7 @@
  * 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/debug.h"
 #include "ns3/ipv4-address.h"
 #include "ns3/nstime.h"
@@ -32,15 +33,14 @@
 
 UdpEchoServer::UdpEchoServer (
   Ptr<Node> n,
-  Ipv4Address clientAddress,
-  uint16_t clientPort)
+  uint16_t port)
 : 
   Application(n)
 {
-  NS_DEBUG ("UdpEchoServer::UdpEchoServer (" << n << ", " << clientAddress <<
-    ", " << clientPort << ")");
+  NS_DEBUG ("UdpEchoServer::UdpEchoServer (" << n << ", " << 
+    port << ")");
 
-  Construct (n, clientAddress, clientPort);
+  Construct (n, port);
 }
 
 UdpEchoServer::~UdpEchoServer()
@@ -51,18 +51,15 @@
 void
 UdpEchoServer::Construct (
   Ptr<Node> n,
-  Ipv4Address clientAddress,
-  uint16_t clientPort)
+  uint16_t port)
 {
-  NS_DEBUG ("UdpEchoServer::Construct (" << n << ", " << clientAddress <<
-    ", " << clientPort << ")");
+  NS_DEBUG ("UdpEchoServer::Construct (" << n << ", " << port << ")");
 
   m_node = n;
-  m_clientAddress = clientAddress;
-  m_clientPort = clientPort;
+  m_port = port;
 
   m_socket = 0;
-  m_client = InetSocketAddress (clientAddress, clientPort);
+  m_local = InetSocketAddress (Ipv4Address::GetAny (), port);
 }
 
 void
@@ -83,7 +80,7 @@
       Ptr<SocketFactory> socketFactory = 
         GetNode ()->QueryInterface<SocketFactory> (iid);
       m_socket = socketFactory->CreateSocket ();
-      m_socket->Bind (m_client);
+      m_socket->Bind (m_local);
     }
 
   m_socket->SetRecvCallback((Callback<void, Ptr<Socket>, const Packet &,
@@ -101,7 +98,8 @@
     }
 }
 
-void UdpEchoServer::Receive(
+void
+UdpEchoServer::Receive(
   Ptr<Socket> socket, 
   const Packet &packet,
   const Address &from) 
@@ -114,6 +112,9 @@
       InetSocketAddress address = InetSocketAddress::ConvertFrom (from);
       NS_DEBUG ("UdpEchoServer::Receive(): Received " << 
         packet.GetSize() << " bytes from " << address.GetIpv4());
+
+      NS_DEBUG ("UdpEchoServer::Receive (): Echoing packet");
+      socket->SendTo (from, packet);
     }
 }
 
--- a/src/applications/udp-echo/udp-echo-server.h	Wed Sep 12 16:54:21 2007 -0700
+++ b/src/applications/udp-echo/udp-echo-server.h	Thu Sep 13 10:52:41 2007 -0700
@@ -32,27 +32,25 @@
 class UdpEchoServer : public Application 
 {
 public:
-  UdpEchoServer (Ptr<Node> n, Ipv4Address clientAddr, uint16_t clientPort);
+  UdpEchoServer (Ptr<Node> n, uint16_t clientPort);
   virtual ~UdpEchoServer ();
 
 protected:
   virtual void DoDispose (void);
 
 private:
-  void Construct (Ptr<Node> n, Ipv4Address clientAddr, uint16_t clientPort);
+  void Construct (Ptr<Node> n, uint16_t clientPort);
 
   virtual void StartApplication (void);
   virtual void StopApplication (void);
 
-  void Receive(Ptr<Socket> socket, const Packet &packet,
-    const Address &from);
+  void Receive(Ptr<Socket> socket, const Packet &packet, const Address &from);
 
   Ptr<Node> m_node;
-  Ipv4Address m_clientAddress;
-  uint16_t m_clientPort;
+  uint16_t m_port;
 
   Ptr<Socket> m_socket;
-  Address m_client;
+  Address m_local;
 };
 
 } // namespace ns3