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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
10
13#include "packet-socket.h"
14
15#include "ns3/abort.h"
16#include "ns3/log.h"
17#include "ns3/nstime.h"
18#include "ns3/packet.h"
19#include "ns3/simulator.h"
20#include "ns3/socket-factory.h"
21#include "ns3/socket.h"
22#include "ns3/uinteger.h"
23
24#include <cstdio>
25#include <cstdlib>
26
27namespace ns3
28{
29
30NS_LOG_COMPONENT_DEFINE("PacketSocketServer");
31
32NS_OBJECT_ENSURE_REGISTERED(PacketSocketServer);
33
34TypeId
36{
37 static TypeId tid = TypeId("ns3::PacketSocketServer")
39 .SetGroupName("Network")
40 .AddConstructor<PacketSocketServer>()
41 .AddTraceSource("Rx",
42 "A packet has been received",
44 "ns3::Packet::AddressTracedCallback");
45 return tid;
46}
47
49{
50 NS_LOG_FUNCTION(this);
51 m_pktRx = 0;
52 m_bytesRx = 0;
53 m_socket = nullptr;
54 m_localAddressSet = false;
55}
56
61
62void
68
69void
71{
72 NS_LOG_FUNCTION(this);
73 NS_ASSERT_MSG(m_localAddressSet, "Local address not set");
74
75 if (!m_socket)
76 {
77 TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
80 }
81
83}
84
85void
92
93void
100
101void
103{
104 NS_LOG_FUNCTION(this << socket);
105 Ptr<Packet> packet;
106 Address from;
107 while ((packet = socket->RecvFrom(from)))
108 {
110 {
111 m_pktRx++;
112 m_bytesRx += packet->GetSize();
113 NS_LOG_INFO("At time " << Simulator::Now().As(Time::S) << " packet sink received "
114 << packet->GetSize() << " bytes from "
115 << PacketSocketAddress::ConvertFrom(from) << " total Rx "
116 << m_pktRx << " packets"
117 << " and " << m_bytesRx << " bytes");
118 m_rxTrace(packet, from);
119 }
120 }
121}
122
123} // Namespace ns3
a polymophic address class
Definition address.h:90
The base class for all ns3 applications.
Definition application.h:51
void DoDispose() override
Destructor implementation.
Ptr< Node > GetNode() const
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.
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition socket.cc:117
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:61
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:105
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#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:75
Callback< R, Args... > MakeNullCallback()
Definition callback.h:727
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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:264
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
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:684