Begin checkin of ns-3-gfr; s/UdpSocket/DatagramSocket; bring in Application base class and onoff-application, and base class socket; bring in random number generator files

This commit is contained in:
Tom Henderson committed 2007-03-18 14:30:26 -07:00
commit 080a57c8b5
17 files changed
+2111 -65

No files matched your search

+173
View File
@@ -0,0 +1,173 @@
/* -*- 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
*
* Author: George F. Riley<riley@ece.gatech.edu>
*/
// Implementation for ns3 Application base class.
// George F. Riley, Georgia Tech, Fall 2006
#include "application.h"
#include "node.h"
#include "node-reference.h"
#include "ns3/nstime.h"
#include "ns3/random-variable.h"
#include "ns3/simulator.h"
using namespace std;
namespace ns3 {
#define nil 0
// Application Methods
// \brief Application Constructor
Application::Application() : m_node(nil), m_startVar(nil), m_stopVar(nil),
m_start(false), m_stop(false)
{
}
Application::Application(const Application& o)
: m_node(nil), m_startVar(nil), m_stopVar(nil),
m_start(false), m_stop(false)
{ // Copy constructor
if (o.GetNode())m_node = new NodeReference(*o.GetNode());
// Copy the start and stop random variables if they exist
if (o.m_startVar) m_startVar = o.m_startVar->Copy();
if (o.m_stopVar) m_stopVar = o.m_stopVar->Copy();
if (o.m_start) ScheduleStart();
if (o.m_stop) ScheduleStop();
}
// \brief Application Destructor
Application::~Application()
{
delete m_node;
// Cancel the start/stop events if scheduled
if (m_start) Simulator::Cancel(m_startEvent);
if (m_stop) Simulator::Cancel(m_stopEvent);
// Delete the random variablse
delete m_startVar;
delete m_stopVar;
}
Application& Application::operator=(const Application& rhs)
{
if (this == &rhs) return *this; // Self assignment
delete m_node;
m_node = nil;
if (rhs.GetNode())m_node = new NodeReference(*rhs.GetNode());
delete m_startVar;
m_startVar = nil;
if (rhs.m_startVar) m_startVar = rhs.m_startVar->Copy();
delete m_stopVar;
m_stopVar = nil;
if (rhs.m_stopVar) m_stopVar = rhs.m_stopVar->Copy();
m_start = false;
if (rhs.m_start) ScheduleStart();
if (rhs.m_stop) ScheduleStop();
return *this;
}
// \brief Specify application start time
// The virtual method STartApp will be called at the time
// specified by startTime.
// \param Time to start application (absolute time, from start of simulation)
void Application::Start(const Time& startTime)
{
delete m_startVar;
m_startVar = new ConstantVariable(startTime.GetSeconds());
ScheduleStart();
}
void Application::Start(const RandomVariable& startVar)
{ // Start at random time
delete m_startVar;
m_startVar = startVar.Copy();
ScheduleStart();
}
// \brief Specify application stop time
// The virtual method StopApp will be called at the time
// specified by stopTime.
// \param Time to stop application (absolute time, from start of simulation)
void Application::Stop(const Time& stopTime)
{
delete m_stopVar;
m_stopVar = new ConstantVariable(stopTime.GetSeconds());
ScheduleStop();
}
void Application::Stop(const RandomVariable& stopVar)
{ // Stop at random time
delete m_stopVar;
m_stopVar = stopVar.Copy();
ScheduleStop();
}
// \brief Assign this application to a given node
// Called by the application manager capability when adding
// an application to a node.
void Application::SetNode(const Node& n)
{
delete m_node;
m_node = new NodeReference(n);
}
Node* Application::GetNode() const
{
return m_node->GetNode();
}
// Protected methods
// StartApp and StopApp will likely be overridden by application subclasses
void Application::StartApplication()
{ // Provide null functionality in case subclass is not interested
}
void Application::StopApplication()
{ // Provide null functionality in case subclass is not interested
}
// Private helpers
void Application::ScheduleStart()
{
m_startEvent = Simulator::Schedule(Seconds(m_startVar->GetValue()) -
Simulator::Now(),
&Application::StartApplication, this);
m_start = true;
}
void Application::ScheduleStop()
{
m_stopEvent = Simulator::Schedule(Seconds(m_stopVar->GetValue()) -
Simulator::Now(),
&Application::StopApplication, this);
m_stop = true;
}
} //namespace ns3
+136
View File
@@ -0,0 +1,136 @@
/* -*- 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
*
* Author: George F. Riley<riley@ece.gatech.edu>
*/
#ifndef __APPLICATION_H__
#define __APPLICATION_H__
//
// \brief The base class for all ns3 applicationes
//
// Class Application is the base class for all ns3 applications.
// Applications are associated with individual nodes, and are created
// using the AddApplication method in the ApplicationManager capability.
//
// Conceptually, an application has zero or more Socket
// objects associated with it, that are created using the Socket
// creation API of the Kernel capability. The Socket object
// API is modeled after the
// well-known BSD sockets interface, although it is somewhat
// simplified for use with ns3. Further, any socket call that
// would normally "block" in normal sockets will return immediately
// in ns3. A set of "upcalls" are defined that will be called when
// the previous blocking call would normally exit. THis is documented
// in more detail Socket class in socket.h.
//
// There is a second application class in ns3, called "ThreadedApplication"
// that implements a true sockets interface, which should be used
// when porting existing sockets code to ns3. The true
// sockets approach is significantly
// less memory--efficient using private stacks for each defined application,
// so that approach should be used with care. The design and implementation
// of the ThreadedApplication are still being discussed.
#include "ns3/event-id.h"
#include "ns3/nstime.h"
namespace ns3 {
class Node;
class NodeReference;
class RandomVariable;
class Application {
public:
Application();
Application(const Application&); // Copy constructor
Application& operator=(const Application&); // Assignment operator
virtual ~Application();
virtual Application* Copy() const = 0; // All applications must provide
// \brief Specify application start time
// Applications start at various times in the simulation scenario.
// The Start method specifies when the application should be
// started. The application subclasses should override the
// private "StartApplication" method defined below, which is called at the
// time specified, to cause the application to begin.
// \param Start time for this application, relative to the
// current simulation time.
void Start(const Time&);
// \brief Same as above, but uses a random variable for start time
// The random variable returns the desired start time in units of
// Seconds.
void Start(const RandomVariable&);
// \brief Specify application stop time
// Once an application has started, it is sometimes useful
// to stop the application. The Stop method specifies when an
// application is to stop. The application subclasses should override
// the private StopApplication method defined below, to cause the application
// to stop.
// \param Stop time for this application, relative to the
// current simulation time.
void Stop(const Time&);
// \brief Same as above, but uses a random variable for stop time
// The random variable returns the desired stop time in units of
// Seconds.
void Stop(const RandomVariable&);
// \brief Attaches an application to a specific node
// Specifies which node object this application is associated with.
// \param Node object to associate with this application.
void SetNode(const Node&);
// \brief Returns the pointer to the attached node.
Node* GetNode() const;
// Members
NodeReference* m_node; // All applications have an associated node
RandomVariable* m_startVar; // Random variable for start time
RandomVariable* m_stopVar; // Random variable for stop time
EventId m_startEvent;// Event identifier for start event
EventId m_stopEvent; // Event identifier for the stop event
bool m_start; // True if start event scheduled
bool m_stop; // True if stop event scheduled
protected:
// \brief Application specific startup code
// The StartApplication method is called at the start time specifed by Start
// This method should be overridden by all or most application
// subclasses.
virtual void StartApplication();
// \brief Application specific shutdown code
// The StopApplication method is called at the stop time specifed by Stop
// This method should be overridden by all or most application
// subclasses.
virtual void StopApplication();
private:
// Helpers
void ScheduleStart();
void ScheduleStop();
};
} //namespace ns3
#endif
@@ -18,7 +18,7 @@
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
#include "udp-socket.h"
#include "datagram-socket.h"
#include "udp.h"
#include "udp-end-point.h"
#include "node.h"
@@ -26,19 +26,19 @@
namespace ns3 {
UdpSocket::UdpSocket (Node *node)
DatagramSocket::DatagramSocket (Node *node)
: m_endPoint (0),
m_node (node)
{
NS_ASSERT (GetUdp () != 0);
}
UdpSocket::~UdpSocket ()
DatagramSocket::~DatagramSocket ()
{
delete m_endPoint;
}
int
UdpSocket::Bind (void)
DatagramSocket::Bind (void)
{
m_endPoint = GetUdp ()->Allocate ();
if (m_endPoint == 0)
@@ -49,7 +49,7 @@ UdpSocket::Bind (void)
return 0;
}
int
UdpSocket::Bind (Ipv4Address address)
DatagramSocket::Bind (Ipv4Address address)
{
m_endPoint = GetUdp ()->Allocate (address);
if (m_endPoint == 0)
@@ -60,7 +60,7 @@ UdpSocket::Bind (Ipv4Address address)
return 0;
}
int
UdpSocket::Bind (uint16_t port)
DatagramSocket::Bind (uint16_t port)
{
m_endPoint = GetUdp ()->Allocate (port);
if (m_endPoint == 0)
@@ -71,7 +71,7 @@ UdpSocket::Bind (uint16_t port)
return 0;
}
int
UdpSocket::Bind (Ipv4Address address, uint16_t port)
DatagramSocket::Bind (Ipv4Address address, uint16_t port)
{
m_endPoint = GetUdp ()->Allocate (address, port);
if (m_endPoint == 0)
@@ -83,18 +83,18 @@ UdpSocket::Bind (Ipv4Address address, uint16_t port)
}
void
UdpSocket::SetDefaultDestination (Ipv4Address daddr, uint16_t dport)
DatagramSocket::SetDefaultDestination (Ipv4Address daddr, uint16_t dport)
{
m_defaultAddress = daddr;
m_defaultPort = dport;
}
void
UdpSocket::SendDummy (uint32_t size)
DatagramSocket::SendDummy (uint32_t size)
{
SendDummyTo (size, m_defaultAddress, m_defaultPort);
}
void
UdpSocket::SendDummyTo (uint32_t size, Ipv4Address daddr, uint16_t dport)
DatagramSocket::SendDummyTo (uint32_t size, Ipv4Address daddr, uint16_t dport)
{
if (m_endPoint == 0)
{
@@ -110,12 +110,12 @@ UdpSocket::SendDummyTo (uint32_t size, Ipv4Address daddr, uint16_t dport)
}
void
UdpSocket::Send (uint8_t const*buffer, uint32_t size)
DatagramSocket::Send (uint8_t const*buffer, uint32_t size)
{
SendTo (buffer, size, m_defaultAddress, m_defaultPort);
}
void
UdpSocket::SendTo (uint8_t const*buffer, uint32_t size,
DatagramSocket::SendTo (uint8_t const*buffer, uint32_t size,
Ipv4Address daddr, uint16_t dport)
{
if (m_endPoint == 0)
@@ -131,14 +131,14 @@ UdpSocket::SendTo (uint8_t const*buffer, uint32_t size,
m_endPoint->GetLocalPort (), dport);
}
void
UdpSocket::SetDummyRxCallback (Callback<void,UdpSocket*,
DatagramSocket::SetDummyRxCallback (Callback<void,DatagramSocket*,
uint32_t,
Ipv4Address,uint16_t> cb)
{
m_dummyRxCallback = cb;
}
void
UdpSocket::SetRxCallback (Callback<void,UdpSocket*,
DatagramSocket::SetRxCallback (Callback<void,DatagramSocket*,
uint8_t const*,uint32_t,
Ipv4Address,uint16_t> cb)
{
@@ -146,7 +146,7 @@ UdpSocket::SetRxCallback (Callback<void,UdpSocket*,
}
void
UdpSocket::ForwardUp (Packet &p, Ipv4Address saddr, uint16_t sport)
DatagramSocket::ForwardUp (Packet &p, Ipv4Address saddr, uint16_t sport)
{
if (!m_dummyRxCallback.IsNull ())
{
@@ -159,13 +159,13 @@ UdpSocket::ForwardUp (Packet &p, Ipv4Address saddr, uint16_t sport)
}
Udp *
UdpSocket::GetUdp (void) const
DatagramSocket::GetUdp (void) const
{
return m_node->GetUdp ();
}
Node *
UdpSocket::GetNode (void) const
DatagramSocket::GetNode (void) const
{
return m_node;
}
@@ -39,14 +39,14 @@ class Udp;
* one of the ::Send methods, the socket is implicitely
* bound to a random port and to all interfaces.
*/
class UdpSocket
class DatagramSocket
{
public:
/**
* Create an unbound udp socket.
*/
UdpSocket (Node *node);
~UdpSocket ();
DatagramSocket (Node *node);
~DatagramSocket ();
/**
* Allocate a free port number and
@@ -106,14 +106,14 @@ public:
* forwards to the application the number of bytes received and from who they
* were received.
*/
void SetDummyRxCallback (Callback<void,UdpSocket*,uint32_t,Ipv4Address,uint16_t> cb);
void SetDummyRxCallback (Callback<void,DatagramSocket*,uint32_t,Ipv4Address,uint16_t> cb);
/**
* When a packet is received by this socket, it invokes the "normal callback" which
* forwards to the application the buffer of bytes received and from who they
* were received. The application is responsible for copying that buffer if it wants
* to keep track of it.
*/
void SetRxCallback (Callback<void,UdpSocket*,uint8_t const*,uint32_t,Ipv4Address,uint16_t> cb);
void SetRxCallback (Callback<void,DatagramSocket*,uint8_t const*,uint32_t,Ipv4Address,uint16_t> cb);
/**
* Return pointer to node
*/
@@ -129,8 +129,8 @@ private:
Node *m_node;
Ipv4Address m_defaultAddress;
uint16_t m_defaultPort;
Callback<void,UdpSocket*,uint32_t,Ipv4Address,uint16_t> m_dummyRxCallback;
Callback<void,UdpSocket*,uint8_t const*,uint32_t,Ipv4Address,uint16_t> m_rxCallback;
Callback<void,DatagramSocket*,uint32_t,Ipv4Address,uint16_t> m_dummyRxCallback;
Callback<void,DatagramSocket*,uint8_t const*,uint32_t,Ipv4Address,uint16_t> m_rxCallback;
};
}//namespace ns3
+45
View File
@@ -0,0 +1,45 @@
/* -*- 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
//
// Author: George F. Riley<riley@ece.gatech.edu>
//
// Define a class that wraps a node pointer. This allows numerous ns3
// objects to have access to a node, but without ambiguity of whether
// the owner should "delete" it.
// George F. Riley, Georgia Tech, Spring 2007
#ifndef __NODE_REFERENCE_H__
#define __NODE_REFERENCE_H__
namespace ns3 {
class Node;
class NodeReference {
public:
NodeReference(const Node& n)
: m_node(n) {}
Node* GetNode() const { return (Node*)&m_node;}
public:
const Node& m_node;
};
} // namespace ns3
#endif
+2 -2
View File
@@ -28,11 +28,11 @@ UdpEndPoint::UdpEndPoint (Ipv4Address address, uint16_t port)
{}
void
UdpEndPoint::SetSocket (UdpSocket *socket)
UdpEndPoint::SetSocket (DatagramSocket *socket)
{
m_socket = socket;
}
UdpSocket *
DatagramSocket *
UdpEndPoint::GetSocket (void) const
{
return m_socket;
+4 -4
View File
@@ -27,17 +27,17 @@
namespace ns3 {
class UdpSocket;
class DatagramSocket;
class UdpEndPoint : public Ipv4EndPoint
{
public:
UdpEndPoint (Ipv4Address address, uint16_t port);
void SetSocket (UdpSocket *socket);
UdpSocket *GetSocket (void) const;
void SetSocket (DatagramSocket *socket);
DatagramSocket *GetSocket (void) const;
private:
UdpSocket *m_socket;
DatagramSocket *m_socket;
};
}//namespace ns3
+2 -2
View File
@@ -27,7 +27,7 @@
#include "udp-header.h"
#include "ipv4-end-point-demux.h"
#include "udp-end-point.h"
#include "udp-socket.h"
#include "datagram-socket.h"
#include "node.h"
#include "ipv4.h"
#include "l3-demux.h"
@@ -102,7 +102,7 @@ Udp::Receive(Packet& packet,
{
return;
}
UdpSocket *socket = endPoint->GetSocket ();
DatagramSocket *socket = endPoint->GetSocket ();
socket->ForwardUp (packet, source, udpHeader.GetSource ());
NS_ASSERT (socket != 0);
}