A Discrete-Event Network Simulator
API
packet-socket-server.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Universita' di Firenze
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18  */
19 
20 #include "packet-socket-server.h"
21 
22 #include "packet-socket-address.h"
23 #include "packet-socket-factory.h"
24 #include "packet-socket.h"
25 
26 #include "ns3/abort.h"
27 #include "ns3/log.h"
28 #include "ns3/nstime.h"
29 #include "ns3/packet.h"
30 #include "ns3/simulator.h"
31 #include "ns3/socket-factory.h"
32 #include "ns3/socket.h"
33 #include "ns3/uinteger.h"
34 
35 #include <cstdio>
36 #include <cstdlib>
37 
38 namespace ns3
39 {
40 
41 NS_LOG_COMPONENT_DEFINE("PacketSocketServer");
42 
43 NS_OBJECT_ENSURE_REGISTERED(PacketSocketServer);
44 
45 TypeId
47 {
48  static TypeId tid = TypeId("ns3::PacketSocketServer")
50  .SetGroupName("Network")
51  .AddConstructor<PacketSocketServer>()
52  .AddTraceSource("Rx",
53  "A packet has been received",
55  "ns3::Packet::AddressTracedCallback");
56  return tid;
57 }
58 
60 {
61  NS_LOG_FUNCTION(this);
62  m_pktRx = 0;
63  m_bytesRx = 0;
64  m_socket = nullptr;
65  m_localAddressSet = false;
66 }
67 
69 {
70  NS_LOG_FUNCTION(this);
71 }
72 
73 void
75 {
76  NS_LOG_FUNCTION(this);
78 }
79 
80 void
82 {
83  NS_LOG_FUNCTION(this);
84  NS_ASSERT_MSG(m_localAddressSet, "Local address not set");
85 
86  if (!m_socket)
87  {
88  TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
91  }
92 
94 }
95 
96 void
98 {
99  NS_LOG_FUNCTION(this);
101  m_socket->Close();
102 }
103 
104 void
106 {
107  NS_LOG_FUNCTION(this << addr);
108  m_localAddress = addr;
109  m_localAddressSet = true;
110 }
111 
112 void
114 {
115  NS_LOG_FUNCTION(this << socket);
116  Ptr<Packet> packet;
117  Address from;
118  while ((packet = socket->RecvFrom(from)))
119  {
121  {
122  m_pktRx++;
123  m_bytesRx += packet->GetSize();
124  NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " packet sink received "
125  << packet->GetSize() << " bytes from "
126  << PacketSocketAddress::ConvertFrom(from) << " total Rx "
127  << m_pktRx << " packets"
128  << " and " << m_bytesRx << " bytes");
129  m_rxTrace(packet, from);
130  }
131  }
132 }
133 
134 } // Namespace ns3
a polymophic address class
Definition: address.h:101
The base class for all ns3 applications.
Definition: application.h:62
void DoDispose() override
Destructor implementation.
Definition: application.cc:86
Ptr< Node > GetNode() const
Definition: application.cc:108
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
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.
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the application.
Ptr< Socket > m_socket
Socket.
void StopApplication() override
Application specific shutdown code.
bool m_localAddressSet
Sanity check.
uint32_t m_pktRx
The number of received packets.
static TypeId GetTypeId()
Get the type ID.
void StartApplication() override
Application specific startup code.
void SetLocal(PacketSocketAddress addr)
set the local address and protocol to be used
void DoDispose() override
Destructor implementation.
PacketSocketAddress m_localAddress
Local address.
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
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:72
virtual int Close()=0
Close a socket.
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:116
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:835
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#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:86
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:747
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
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, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704