A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sink-application.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 DERONNE SOFTWARE ENGINEERING
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Sébastien Deronne <sebastien.deronne@gmail.com>
7 */
8
9#ifndef SINK_APPLICATION_H
10#define SINK_APPLICATION_H
11
12#include "ns3/address.h"
13#include "ns3/application.h"
14#include "ns3/traced-callback.h"
15
16#include <limits>
17
18namespace ns3
19{
20
21class Packet;
22class Address;
23class Socket;
24
25/**
26 * @ingroup applications
27 * @brief Base class for sink applications.
28 *
29 * This class can be used as a base class for sink applications.
30 * A sink application is an application that is primarily used to only receive or echo packets.
31 *
32 * The main purpose of this base class application public API is to hold attributes for the local
33 * (IPv4 or IPv6) address and port to bind to.
34 *
35 * There are three ways that the port value can be configured. First, and most typically, through
36 * the use of a socket address (InetSocketAddress or Inet6SocketAddress) that is configured as the
37 * Local address to bind to. Second, through direct configuration of the Port attribute. Third,
38 * through the use of an optional constructor argument. If multiple of these port configuration
39 * methods are used, it is up to subclass definition which one takes precedence; in the existing
40 * subclasses in this directory, the port value configured in the Local socket address (if a socket
41 * address is configured there) will take precedence.
42 */
44{
45 public:
46 /**
47 * @brief Get the type ID.
48 * @return the object TypeId
49 */
50 static TypeId GetTypeId();
51
52 /**
53 * Constructor
54 *
55 * @param defaultPort the default port number
56 */
57 SinkApplication(uint16_t defaultPort = 0);
58 ~SinkApplication() override;
59
60 static constexpr uint32_t INVALID_PORT{std::numeric_limits<uint32_t>::max()}; //!< invalid port
61
62 protected:
63 void DoDispose() override;
64
65 /**
66 * @brief Close all the sockets
67 * @return true if all sockets closed successfully, false otherwise
68 */
69 bool CloseAllSockets();
70
71 /// Callbacks for tracing the packet Rx events
73
74 /// Traced Callback: received packets, source address.
76
77 Ptr<Socket> m_socket; //!< Socket (IPv4 or IPv6, depending on local address)
78 Ptr<Socket> m_socket6; //!< IPv6 Socket (used if only port is specified)
79
80 TypeId m_protocolTid; //!< Protocol TypeId value
81
82 Address m_local; //!< Local address to bind to (address and port)
83 uint32_t m_port; //!< Local port to bind to
84
85 private:
86 void StartApplication() override;
87 void StopApplication() override;
88
89 /**
90 * @brief set the local address
91 * @param addr local address
92 */
93 virtual void SetLocal(const Address& addr);
94
95 /**
96 * @brief get the local address
97 * @return the local address
98 */
99 Address GetLocal() const;
100
101 /**
102 * @brief set the server port
103 * @param port server port
104 */
105 virtual void SetPort(uint32_t port);
106
107 /**
108 * @brief get the server port
109 * @return the server port
110 */
111 uint32_t GetPort() const;
112
113 /**
114 * @brief Close the socket
115 * @param socket the socket to close
116 * @return true if the socket closed successfully, false otherwise
117 */
118 bool CloseSocket(Ptr<Socket> socket);
119
120 /**
121 * @brief Application specific startup code for child subclasses
122 */
123 virtual void DoStartApplication();
124
125 /**
126 * @brief Application specific shutdown code for child subclasses
127 */
128 virtual void DoStopApplication();
129};
130
131} // namespace ns3
132
133#endif /* SINK_APPLICATION_H */
a polymophic address class
Definition address.h:90
network packets
Definition packet.h:228
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
static constexpr uint32_t INVALID_PORT
invalid port
virtual void SetLocal(const Address &addr)
set the local address
ns3::TracedCallback< Ptr< const Packet > > m_rxTraceWithoutAddress
Callbacks for tracing the packet Rx events.
Ptr< Socket > m_socket6
IPv6 Socket (used if only port is specified).
void StopApplication() override
Application specific shutdown code.
void DoDispose() override
Destructor implementation.
TypeId m_protocolTid
Protocol TypeId value.
SinkApplication(uint16_t defaultPort=0)
Constructor.
virtual void DoStartApplication()
Application specific startup code for child subclasses.
Address GetLocal() const
get the local address
uint32_t GetPort() const
get the server port
virtual void DoStopApplication()
Application specific shutdown code for child subclasses.
void StartApplication() override
Application specific startup code.
static TypeId GetTypeId()
Get the type ID.
Address m_local
Local address to bind to (address and port).
Ptr< Socket > m_socket
Socket (IPv4 or IPv6, depending on local address).
bool CloseSocket(Ptr< Socket > socket)
Close the socket.
uint32_t m_port
Local port to bind to.
virtual void SetPort(uint32_t port)
set the server port
bool CloseAllSockets()
Close all the sockets.
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
A low-level Socket API based loosely on the BSD Socket API.
Definition socket.h:57
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition type-id.h:49
uint16_t port
Definition dsdv-manet.cc:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.