A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
packet-socket-server.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Universita' di Firenze
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/nstime.h"
23 #include "ns3/packet-socket-address.h"
24 #include "ns3/packet-socket.h"
25 #include "ns3/packet-socket-factory.h"
26 #include "ns3/socket.h"
27 #include "ns3/simulator.h"
28 #include "ns3/socket-factory.h"
29 #include "ns3/packet.h"
30 #include "ns3/uinteger.h"
31 #include "ns3/abort.h"
32 #include "packet-socket-server.h"
33 #include <cstdlib>
34 #include <cstdio>
35 
36 namespace ns3 {
37 
38 NS_LOG_COMPONENT_DEFINE ("PacketSocketServer");
39 NS_OBJECT_ENSURE_REGISTERED (PacketSocketServer);
40 
41 TypeId
43 {
44  static TypeId tid = TypeId ("ns3::PacketSocketServer")
46  .AddConstructor<PacketSocketServer> ()
47  .AddTraceSource ("Rx", "A packet has been received",
49  ;
50  return tid;
51 }
52 
54 {
55  NS_LOG_FUNCTION (this);
56  m_pktRx = 0;
57  m_bytesRx = 0;
58  m_socket = 0;
59  m_localAddressSet = false;
60 }
61 
63 {
64  NS_LOG_FUNCTION (this);
65 }
66 
67 void
69 {
70  NS_LOG_FUNCTION (this);
72 }
73 
74 void
76 {
77  NS_LOG_FUNCTION (this);
78  NS_ASSERT_MSG (m_localAddressSet, "Local address not set");
79 
80  if (m_socket == 0)
81  {
82  TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
85  }
86 
88 }
89 
90 void
92 {
93  NS_LOG_FUNCTION (this);
95  m_socket->Close ();
96 }
97 
98 void
100 {
101  NS_LOG_FUNCTION (this << addr);
102  m_localAddress = addr;
103  m_localAddressSet = true;
104 }
105 
106 void
108 {
109  NS_LOG_FUNCTION (this << socket);
110  Ptr<Packet> packet;
111  Address from;
112  while ((packet = socket->RecvFrom (from)))
113  {
115  {
116  m_pktRx ++;
117  m_bytesRx += packet->GetSize ();
118  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds ()
119  << "s packet sink received "
120  << packet->GetSize () << " bytes from "
122  << " total Rx " << m_pktRx << " packets"
123  << " and " << m_bytesRx << " bytes");
124  m_rxTrace (packet, from);
125  }
126  }
127 }
128 
129 } // Namespace ns3
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
bool m_localAddressSet
Sanity check.
void SetLocal(PacketSocketAddress addr)
set the local address and protocol to be used
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
PacketSocketAddress m_localAddress
Local address.
uint32_t m_bytesRx
Total bytes received.
uint32_t m_pktRx
The number of received packets.
an address for a packet socket
static bool IsMatchingType(const Address &address)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
Callback< R > MakeNullCallback(void)
Definition: callback.h:1429
virtual void StartApplication(void)
Application specific startup code.
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the application.
a polymophic address class
Definition: address.h:86
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
The base class for all ns3 applications.
Definition: application.h:60
Ptr< Node > GetNode() const
Definition: application.cc:103
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1283
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:70
Ptr< Socket > m_socket
Socket.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: application.cc:82
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:84
static TypeId GetTypeId(void)
Get the type ID.
virtual void StopApplication(void)
Application specific shutdown code.
static PacketSocketAddress ConvertFrom(const Address &address)
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
virtual int Close(void)=0
Close a socket.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
static TypeId LookupByName(std::string name)
Definition: type-id.cc:535