finish up basic echo apps

This commit is contained in:
Craig Dowell
2007-09-13 10:52:41 -07:00
parent 61a105af21
commit 9bc6fe4e7a
5 changed files with 69 additions and 41 deletions

View File

@@ -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 @@ main (int argc, char *argv[])
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 @@ main (int argc, char *argv[])
NS_DEBUG("Create Applications.");
//
// Create a UdpEchoServer application on node 1.
// Create a UdpEchoServer application on node one.
//
Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, "0.0.0.0", 80);
//
// 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.
// node one.
//
Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, "10.1.1.2", 80,
1, Seconds(1.), 1024);
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);
//
// Tell the application when to start and stop.
// Tell the applications when to start and stop.
//
client->Start(Seconds(2.0));
client->Stop (Seconds(10.0));
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"

View File

@@ -99,7 +99,9 @@ UdpEchoClient::StartApplication (void)
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 @@ void
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 @@ UdpEchoClient::Send (void)
}
}
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

View File

@@ -50,6 +50,8 @@ private:
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;

View File

@@ -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 @@ NS_DEBUG_COMPONENT_DEFINE ("UdpEchoServer");
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 @@ UdpEchoServer::~UdpEchoServer()
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 @@ UdpEchoServer::StartApplication (void)
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 @@ UdpEchoServer::StopApplication ()
}
}
void UdpEchoServer::Receive(
void
UdpEchoServer::Receive(
Ptr<Socket> socket,
const Packet &packet,
const Address &from)
@@ -114,6 +112,9 @@ void UdpEchoServer::Receive(
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);
}
}

View File

@@ -32,27 +32,25 @@ class Packet;
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