INET Framework for OMNeT++/OMNEST
inet::UdpSocketIo Class Reference

#include <UdpSocketIo.h>

Inheritance diagram for inet::UdpSocketIo:
inet::ApplicationBase inet::UdpSocket::ICallback inet::OperationalBase inet::OperationalMixin< cSimpleModule > inet::ILifecycle

Protected Member Functions

virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessageWhenUp (cMessage *message) override
 
virtual void finish () override
 
virtual void refreshDisplay () const override
 
virtual void setSocketOptions ()
 
virtual void socketDataArrived (UdpSocket *socket, Packet *packet) override
 Notifies about data arrival, packet ownership is transferred to the callee. More...
 
virtual void socketErrorArrived (UdpSocket *socket, Indication *indication) override
 Notifies about error indication arrival, indication ownership is transferred to the callee. More...
 
virtual void socketClosed (UdpSocket *socket) override
 Notifies about socket closed, indication ownership is transferred to the callee. More...
 
virtual void handleStartOperation (LifecycleOperation *operation) override
 
virtual void handleStopOperation (LifecycleOperation *operation) override
 
virtual void handleCrashOperation (LifecycleOperation *operation) override
 
- Protected Member Functions inherited from inet::ApplicationBase
virtual bool isInitializeStage (int stage) const override
 
virtual bool isModuleStartStage (int stage) const override
 
virtual bool isModuleStopStage (int stage) const override
 
- Protected Member Functions inherited from inet::OperationalMixin< cSimpleModule >
virtual int numInitStages () const override
 
virtual void refreshDisplay () const override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void handleMessageWhenDown (cMessage *msg)
 
virtual bool handleOperationStage (LifecycleOperation *operation, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 
virtual State getInitialOperationalState () const
 Returns initial operational state: OPERATING or NOT_OPERATING. More...
 
virtual void handleActiveOperationTimeout (cMessage *message)
 
virtual bool isUp () const
 utility functions More...
 
virtual bool isDown () const
 
virtual void setOperationalState (State newState)
 
virtual void scheduleOperationTimeout (simtime_t timeout)
 
virtual void setupActiveOperation (LifecycleOperation *operation, IDoneCallback *doneCallback, State)
 
virtual void delayActiveOperationFinish (simtime_t timeout)
 
virtual void startActiveOperationExtraTime (simtime_t delay=SIMTIME_ZERO)
 
virtual void startActiveOperationExtraTimeOrFinish (simtime_t extraTime)
 
virtual void finishActiveOperation ()
 

Protected Attributes

bool dontFragment = false
 
UdpSocket socket
 
int numSent = 0
 
int numReceived = 0
 
- Protected Attributes inherited from inet::OperationalMixin< cSimpleModule >
State operationalState
 
simtime_t lastChange
 
Operation activeOperation
 
cMessage * activeOperationTimeout
 
cMessage * activeOperationExtraTimer
 

Additional Inherited Members

- Public Member Functions inherited from inet::ApplicationBase
 ApplicationBase ()
 
- Public Member Functions inherited from inet::OperationalMixin< cSimpleModule >
virtual ~OperationalMixin ()
 }@ More...
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 
- Public Member Functions inherited from inet::UdpSocket::ICallback
virtual ~ICallback ()
 
- Protected Types inherited from inet::OperationalMixin< cSimpleModule >
enum  State
 

Member Function Documentation

◆ finish()

void inet::UdpSocketIo::finish ( )
overrideprotectedvirtual
46 {
47  recordScalar("packets sent", numSent);
48  recordScalar("packets received", numReceived);
49  ApplicationBase::finish();
50 }

◆ handleCrashOperation()

void inet::UdpSocketIo::handleCrashOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

139 {
140  socket.destroy();
141 }

◆ handleMessageWhenUp()

void inet::UdpSocketIo::handleMessageWhenUp ( cMessage *  message)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

32 {
33  if (socket.belongsToSocket(message))
34  socket.processMessage(message);
35  else {
36  auto packet = check_and_cast<Packet *>(message);
37  if (dontFragment)
38  packet->addTagIfAbsent<FragmentationReq>()->setDontFragment(true);
39  socket.send(packet);
40  numSent++;
41  emit(packetSentSignal, packet);
42  }
43 }

◆ handleStartOperation()

void inet::UdpSocketIo::handleStartOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

122 {
123  socket.setOutputGate(gate("socketOut"));
125  const char *localAddress = par("localAddress");
126  socket.bind(*localAddress ? L3AddressResolver().resolve(localAddress) : L3Address(), par("localPort"));
127  const char *destAddrs = par("destAddress");
128  if (*destAddrs)
129  socket.connect(L3AddressResolver().resolve(destAddrs), par("destPort"));
130 }

◆ handleStopOperation()

void inet::UdpSocketIo::handleStopOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

133 {
134  socket.close();
135  delayActiveOperationFinish(par("stopOperationTimeout"));
136 }

◆ initialize()

void inet::UdpSocketIo::initialize ( int  stage)
overrideprotectedvirtual

Reimplemented from inet::OperationalMixin< cSimpleModule >.

20 {
22  if (stage == INITSTAGE_LOCAL) {
23  dontFragment = par("dontFragment");
24  numSent = 0;
25  numReceived = 0;
26  WATCH(numSent);
27  WATCH(numReceived);
28  }
29 }

◆ numInitStages()

virtual int inet::UdpSocketIo::numInitStages ( ) const
inlineoverrideprotectedvirtual
25 { return NUM_INIT_STAGES; }

◆ refreshDisplay()

void inet::UdpSocketIo::refreshDisplay ( ) const
overrideprotectedvirtual
53 {
55  char buf[100];
56  sprintf(buf, "rcvd: %d pks\nsent: %d pks", numReceived, numSent);
57  getDisplayString().setTagArg("t", 0, buf);
58 }

◆ setSocketOptions()

void inet::UdpSocketIo::setSocketOptions ( )
protectedvirtual
61 {
62  int timeToLive = par("timeToLive");
63  if (timeToLive != -1)
64  socket.setTimeToLive(timeToLive);
65 
66  int dscp = par("dscp");
67  if (dscp != -1)
68  socket.setDscp(dscp);
69 
70  int tos = par("tos");
71  if (tos != -1)
72  socket.setTos(tos);
73 
74  NetworkInterface *multicastInterface = nullptr;
75  const char *multicastInterfaceName = par("multicastInterface");
76  if (multicastInterfaceName[0]) {
77  IInterfaceTable *ift = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this);
78  multicastInterface = ift->findInterfaceByName(multicastInterfaceName);
79  if (!multicastInterface)
80  throw cRuntimeError("Wrong multicastInterface setting: no interface named \"%s\"", multicastInterfaceName);
81  socket.setMulticastOutputInterface(multicastInterface->getInterfaceId());
82  }
83 
84  auto multicastAddresses = check_and_cast<cValueArray *>(par("multicastAddresses").objectValue());
85  for (int i = 0; i < multicastAddresses->size(); i++)
86  socket.joinMulticastGroup(Ipv4Address(multicastAddresses->get(0).stringValue()), multicastInterface != nullptr ? multicastInterface->getInterfaceId() : -1);
87 
88  bool receiveBroadcast = par("receiveBroadcast");
89  if (receiveBroadcast)
90  socket.setBroadcast(true);
91 
92  bool joinLocalMulticastGroups = par("joinLocalMulticastGroups");
93  if (joinLocalMulticastGroups) {
94  MulticastGroupList mgl = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this)->collectMulticastGroups();
96  }
97  socket.setCallback(this);
98 }

Referenced by handleStartOperation().

◆ socketClosed()

void inet::UdpSocketIo::socketClosed ( UdpSocket socket)
overrideprotectedvirtual

Notifies about socket closed, indication ownership is transferred to the callee.

Implements inet::UdpSocket::ICallback.

116 {
117  if (operationalState == State::STOPPING_OPERATION)
118  startActiveOperationExtraTimeOrFinish(par("stopOperationExtraTime"));
119 }

◆ socketDataArrived()

void inet::UdpSocketIo::socketDataArrived ( UdpSocket socket,
Packet packet 
)
overrideprotectedvirtual

Notifies about data arrival, packet ownership is transferred to the callee.

Implements inet::UdpSocket::ICallback.

101 {
102  emit(packetReceivedSignal, packet);
103  EV_INFO << "Received packet: " << UdpSocket::getReceivedPacketInfo(packet) << endl;
104  numReceived++;
105  packet->removeTag<SocketInd>();
106  send(packet, "trafficOut");
107 }

◆ socketErrorArrived()

void inet::UdpSocketIo::socketErrorArrived ( UdpSocket socket,
Indication indication 
)
overrideprotectedvirtual

Notifies about error indication arrival, indication ownership is transferred to the callee.

Implements inet::UdpSocket::ICallback.

110 {
111  EV_WARN << "Ignoring UDP error report " << indication->getName() << endl;
112  delete indication;
113 }

Member Data Documentation

◆ dontFragment

bool inet::UdpSocketIo::dontFragment = false
protected

Referenced by handleMessageWhenUp(), and initialize().

◆ numReceived

int inet::UdpSocketIo::numReceived = 0
protected

◆ numSent

int inet::UdpSocketIo::numSent = 0
protected

◆ socket


The documentation for this class was generated from the following files:
inet::UdpSocket::setOutputGate
void setOutputGate(cGate *toUdp)
Sets the gate on which to send to UDP.
Definition: UdpSocket.h:117
inet::UdpSocket::setDscp
void setDscp(short dscp)
Sets the Ipv4 / Ipv6 dscp fields of packets sent from the UDP socket.
Definition: UdpSocket.cc:121
inet::UdpSocket::setTimeToLive
void setTimeToLive(int ttl)
Set the TTL (Ipv6: Hop Limit) field on sent packets.
Definition: UdpSocket.cc:112
inet::OperationalMixin< cSimpleModule >::operationalState
State operationalState
Definition: OperationalMixin.h:23
inet::UdpSocket::bind
void bind(int localPort)
Bind the socket to a local port number.
Definition: UdpSocket.cc:34
inet::UdpSocket::getReceivedPacketInfo
static std::string getReceivedPacketInfo(Packet *pk)
Utility function: returns a line of information about a packet received via UDP.
Definition: UdpSocket.cc:315
inet::OperationalMixin< cSimpleModule >::initialize
virtual void initialize(int stage) override
Definition: OperationalMixinImpl.h:26
inet::UdpSocket::destroy
virtual void destroy() override
Notify the protocol that the owner of ISocket has destroyed the socket.
Definition: UdpSocket.cc:98
inet::UdpSocketIo::numReceived
int numReceived
Definition: UdpSocketIo.h:22
inet::packetSentSignal
simsignal_t packetSentSignal
Definition: Simsignals.cc:96
inet::UdpSocket::setCallback
void setCallback(ICallback *cb)
Sets a callback object, to be used with processMessage().
Definition: UdpSocket.cc:338
inet::UdpSocket::setMulticastOutputInterface
void setMulticastOutputInterface(int interfaceId)
Set the output interface for sending multicast packets (like the Unix IP_MULTICAST_IF socket option).
Definition: UdpSocket.cc:148
inet::UdpSocket::send
virtual void send(Packet *msg) override
Sends a data packet to the address and port specified previously in a connect() call.
Definition: UdpSocket.cc:80
inet::UdpSocket::connect
void connect(L3Address remoteAddr, int remotePort)
Connects to a remote UDP socket.
Definition: UdpSocket.cc:53
inet::UdpSocketIo::setSocketOptions
virtual void setSocketOptions()
Definition: UdpSocketIo.cc:60
inet::packetReceivedSignal
simsignal_t packetReceivedSignal
Definition: Simsignals.cc:97
inet::UdpSocket::joinLocalMulticastGroups
void joinLocalMulticastGroups(MulticastGroupList mgl)
Joins the socket to each multicast group that are registered with any of the interfaces.
Definition: UdpSocket.cc:265
inet::UdpSocket::processMessage
virtual void processMessage(cMessage *msg) override
Examines the message, takes ownership, and updates socket state.
Definition: UdpSocket.cc:343
inet::UdpSocketIo::numSent
int numSent
Definition: UdpSocketIo.h:21
inet::UdpSocket::joinMulticastGroup
void joinMulticastGroup(const L3Address &multicastAddr, int interfaceId=-1)
Adds the socket to the given multicast group, that is, UDP packets arriving to the given multicast ad...
Definition: UdpSocket.cc:175
inet::MulticastGroupList
std::vector< MulticastGroup > MulticastGroupList
Definition: IInterfaceTable.h:26
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
inet::UdpSocket::close
virtual void close() override
Unbinds the socket.
Definition: UdpSocket.cc:87
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::UdpSocket::belongsToSocket
virtual bool belongsToSocket(cMessage *msg) const override
Returns true if the message belongs to this socket instance (message has a UdpControlInfo as getContr...
Definition: UdpSocket.cc:373
inet::UdpSocket::setBroadcast
void setBroadcast(bool broadcast)
Set the Broadcast option on the UDP socket.
Definition: UdpSocket.cc:139
inet::OperationalMixin< cSimpleModule >::delayActiveOperationFinish
virtual void delayActiveOperationFinish(simtime_t timeout)
Definition: OperationalMixinImpl.h:161
inet::OperationalMixin< cSimpleModule >::startActiveOperationExtraTimeOrFinish
virtual void startActiveOperationExtraTimeOrFinish(simtime_t extraTime)
Definition: OperationalMixinImpl.h:179
inet::UdpSocketIo::socket
UdpSocket socket
Definition: UdpSocketIo.h:20
inet::UdpSocket::setTos
void setTos(short tos)
Sets the Ipv4 Type of Service / Ipv6 Traffic Class fields of packets sent from the UDP socket.
Definition: UdpSocket.cc:130
inet::OperationalMixin< cSimpleModule >::refreshDisplay
virtual void refreshDisplay() const override
Definition: OperationalMixinImpl.h:200
inet::UdpSocketIo::dontFragment
bool dontFragment
Definition: UdpSocketIo.h:19