A Discrete-Event Network Simulator
API
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 
40 NS_OBJECT_ENSURE_REGISTERED (PacketSocketServer);
41 
42 TypeId
44 {
45  static TypeId tid = TypeId ("ns3::PacketSocketServer")
47  .SetGroupName("Network")
48  .AddConstructor<PacketSocketServer> ()
49  .AddTraceSource ("Rx", "A packet has been received",
51  "ns3::Packet::AddressTracedCallback")
52  ;
53  return tid;
54 }
55 
57 {
58  NS_LOG_FUNCTION (this);
59  m_pktRx = 0;
60  m_bytesRx = 0;
61  m_socket = 0;
62  m_localAddressSet = false;
63 }
64 
66 {
67  NS_LOG_FUNCTION (this);
68 }
69 
70 void
72 {
73  NS_LOG_FUNCTION (this);
75 }
76 
77 void
79 {
80  NS_LOG_FUNCTION (this);
81  NS_ASSERT_MSG (m_localAddressSet, "Local address not set");
82 
83  if (m_socket == 0)
84  {
85  TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
88  }
89 
91 }
92 
93 void
95 {
96  NS_LOG_FUNCTION (this);
98  m_socket->Close ();
99 }
100 
101 void
103 {
104  NS_LOG_FUNCTION (this << addr);
105  m_localAddress = addr;
106  m_localAddressSet = true;
107 }
108 
109 void
111 {
112  NS_LOG_FUNCTION (this << socket);
113  Ptr<Packet> packet;
114  Address from;
115  while ((packet = socket->RecvFrom (from)))
116  {
118  {
119  m_pktRx ++;
120  m_bytesRx += packet->GetSize ();
121  NS_LOG_INFO ("At time " << Simulator::Now ().As (Time::S)
122  << " packet sink received "
123  << packet->GetSize () << " bytes from "
125  << " total Rx " << m_pktRx << " packets"
126  << " and " << m_bytesRx << " bytes");
127  m_rxTrace (packet, from);
128  }
129  }
130 }
131 
132 } // Namespace ns3
a polymophic address class
Definition: address.h:91
The base class for all ns3 applications.
Definition: application.h:61
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
Ptr< Node > GetNode() const
Definition: application.cc:104
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
an address for a packet socket
static bool IsMatchingType(const Address &address)
static PacketSocketAddress ConvertFrom(const Address &address)
A server using PacketSocket.
uint32_t m_bytesRx
Total bytes received.
virtual void DoDispose(void)
Destructor implementation.
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the application.
Ptr< Socket > m_socket
Socket.
bool m_localAddressSet
Sanity check.
static TypeId GetTypeId(void)
Get the type ID.
uint32_t m_pktRx
The number of received packets.
virtual void StopApplication(void)
Application specific shutdown code.
void SetLocal(PacketSocketAddress addr)
set the local address and protocol to be used
virtual void StartApplication(void)
Application specific startup code.
PacketSocketAddress m_localAddress
Local address.
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
virtual int Close(void)=0
Close a socket.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
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:71
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
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.
@ S
second
Definition: nstime.h:114
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:829
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#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:88
Callback< R, Ts... > MakeNullCallback(void)
Definition: callback.h:1688
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648