A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
21
22#include "ns3/abort.h"
23#include "ns3/log.h"
24#include "ns3/nstime.h"
25#include "ns3/packet-socket-address.h"
26#include "ns3/packet-socket-factory.h"
27#include "ns3/packet-socket.h"
28#include "ns3/packet.h"
29#include "ns3/simulator.h"
30#include "ns3/socket-factory.h"
31#include "ns3/socket.h"
32#include "ns3/uinteger.h"
33
34#include <cstdio>
35#include <cstdlib>
36
37namespace ns3
38{
39
40NS_LOG_COMPONENT_DEFINE("PacketSocketServer");
41
42NS_OBJECT_ENSURE_REGISTERED(PacketSocketServer);
43
44TypeId
46{
47 static TypeId tid = TypeId("ns3::PacketSocketServer")
49 .SetGroupName("Network")
50 .AddConstructor<PacketSocketServer>()
51 .AddTraceSource("Rx",
52 "A packet has been received",
54 "ns3::Packet::AddressTracedCallback");
55 return tid;
56}
57
59{
60 NS_LOG_FUNCTION(this);
61 m_pktRx = 0;
62 m_bytesRx = 0;
63 m_socket = nullptr;
64 m_localAddressSet = false;
65}
66
68{
69 NS_LOG_FUNCTION(this);
70}
71
72void
74{
75 NS_LOG_FUNCTION(this);
77}
78
79void
81{
82 NS_LOG_FUNCTION(this);
83 NS_ASSERT_MSG(m_localAddressSet, "Local address not set");
84
85 if (!m_socket)
86 {
87 TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
90 }
91
93}
94
95void
97{
98 NS_LOG_FUNCTION(this);
100 m_socket->Close();
101}
102
103void
105{
106 NS_LOG_FUNCTION(this << addr);
107 m_localAddress = addr;
108 m_localAddressSet = true;
109}
110
111void
113{
114 NS_LOG_FUNCTION(this << socket);
115 Ptr<Packet> packet;
116 Address from;
117 while ((packet = socket->RecvFrom(from)))
118 {
120 {
121 m_pktRx++;
122 m_bytesRx += packet->GetSize();
123 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " packet sink received "
124 << packet->GetSize() << " bytes from "
125 << PacketSocketAddress::ConvertFrom(from) << " total Rx "
126 << m_pktRx << " packets"
127 << " and " << m_bytesRx << " bytes");
128 m_rxTrace(packet, from);
129 }
130 }
131}
132
133} // Namespace ns3
a polymophic address class
Definition: address.h:100
The base class for all ns3 applications.
Definition: application.h:61
void DoDispose() override
Destructor implementation.
Definition: application.cc:85
Ptr< Node > GetNode() const
Definition: application.cc:107
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:126
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.
@ 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:840
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:936
#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:745
#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:702