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

Consumes and prints packets received from the Udp module. More...

#include <UdpSink.h>

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

Public Member Functions

 UdpSink ()
 
virtual ~UdpSink ()
 
- 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

enum  SelfMsgKinds { START = 1, STOP }
 
- Protected Types inherited from inet::OperationalMixin< cSimpleModule >
enum  State
 

Protected Member Functions

virtual void processPacket (Packet *msg)
 
virtual void setSocketOptions ()
 
virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessageWhenUp (cMessage *msg) override
 
virtual void finish () override
 
virtual void refreshDisplay () const override
 
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 processStart ()
 
virtual void processStop ()
 
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

UdpSocket socket
 
int localPort = -1
 
L3Address multicastGroup
 
simtime_t startTime
 
simtime_t stopTime
 
cMessage * selfMsg = nullptr
 
int numReceived = 0
 
- Protected Attributes inherited from inet::OperationalMixin< cSimpleModule >
State operationalState
 
simtime_t lastChange
 
Operation activeOperation
 
cMessage * activeOperationTimeout
 
cMessage * activeOperationExtraTimer
 

Detailed Description

Consumes and prints packets received from the Udp module.

See NED for more info.

Member Enumeration Documentation

◆ SelfMsgKinds

Enumerator
START 
STOP 
22 { START = 1, STOP };

Constructor & Destructor Documentation

◆ UdpSink()

inet::UdpSink::UdpSink ( )
inline
33 {}

◆ ~UdpSink()

inet::UdpSink::~UdpSink ( )
virtual
19 {
20  cancelAndDelete(selfMsg);
21 }

Member Function Documentation

◆ finish()

void inet::UdpSink::finish ( )
overrideprotectedvirtual
91 {
92  ApplicationBase::finish();
93  EV_INFO << getFullPath() << ": received " << numReceived << " packets\n";
94 }

◆ handleCrashOperation()

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

Implements inet::OperationalMixin< cSimpleModule >.

163 {
164  cancelEvent(selfMsg);
165  if (operation->getRootModule() != getContainingNode(this)) { // closes socket when the application crashed only
167  socket.leaveMulticastGroup(multicastGroup); // FIXME should be done by socket.close()
168  socket.destroy(); // TODO in real operating systems, program crash detected by OS and OS closes sockets of crashed programs.
169  }
170 }

◆ handleMessageWhenUp()

void inet::UdpSink::handleMessageWhenUp ( cMessage *  msg)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

41 {
42  if (msg->isSelfMessage()) {
43  ASSERT(msg == selfMsg);
44  switch (selfMsg->getKind()) {
45  case START:
46  processStart();
47  break;
48 
49  case STOP:
50  processStop();
51  break;
52 
53  default:
54  throw cRuntimeError("Invalid kind %d in self message", (int)selfMsg->getKind());
55  }
56  }
57  else if (msg->arrivedOn("socketIn"))
59  else
60  throw cRuntimeError("Unknown incoming gate: '%s'", msg->getArrivalGate()->getFullName());
61 }

◆ handleStartOperation()

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

Implements inet::OperationalMixin< cSimpleModule >.

145 {
146  simtime_t start = std::max(startTime, simTime());
147  if ((stopTime < SIMTIME_ZERO) || (start < stopTime) || (start == stopTime && startTime == stopTime)) {
148  selfMsg->setKind(START);
149  scheduleAt(start, selfMsg);
150  }
151 }

◆ handleStopOperation()

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

Implements inet::OperationalMixin< cSimpleModule >.

154 {
155  cancelEvent(selfMsg);
157  socket.leaveMulticastGroup(multicastGroup); // FIXME should be done by socket.close()
158  socket.close();
159  delayActiveOperationFinish(par("stopOperationTimeout"));
160 }

◆ initialize()

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

Reimplemented from inet::OperationalMixin< cSimpleModule >.

24 {
26 
27  if (stage == INITSTAGE_LOCAL) {
28  numReceived = 0;
29  WATCH(numReceived);
30 
31  localPort = par("localPort");
32  startTime = par("startTime");
33  stopTime = par("stopTime");
34  if (stopTime >= SIMTIME_ZERO && stopTime < startTime)
35  throw cRuntimeError("Invalid startTime/stopTime parameters");
36  selfMsg = new cMessage("UDPSinkTimer");
37  }
38 }

◆ numInitStages()

virtual int inet::UdpSink::numInitStages ( ) const
inlineoverrideprotectedvirtual
41 { return NUM_INIT_STAGES; }

◆ processPacket()

void inet::UdpSink::processPacket ( Packet msg)
protectedvirtual
136 {
137  EV_INFO << "Received packet: " << UdpSocket::getReceivedPacketInfo(pk) << endl;
138  emit(packetReceivedSignal, pk);
139  delete pk;
140 
141  numReceived++;
142 }

Referenced by socketDataArrived().

◆ processStart()

void inet::UdpSink::processStart ( )
protectedvirtual
117 {
118  socket.setOutputGate(gate("socketOut"));
121 
122  if (stopTime >= SIMTIME_ZERO) {
123  selfMsg->setKind(STOP);
124  scheduleAt(stopTime, selfMsg);
125  }
126 }

Referenced by handleMessageWhenUp().

◆ processStop()

void inet::UdpSink::processStop ( )
protectedvirtual
129 {
131  socket.leaveMulticastGroup(multicastGroup); // FIXME should be done by socket.close()
132  socket.close();
133 }

Referenced by handleMessageWhenUp().

◆ refreshDisplay()

void inet::UdpSink::refreshDisplay ( ) const
overrideprotectedvirtual
82 {
84 
85  char buf[50];
86  sprintf(buf, "rcvd: %d pks", numReceived);
87  getDisplayString().setTagArg("t", 0, buf);
88 }

◆ setSocketOptions()

void inet::UdpSink::setSocketOptions ( )
protectedvirtual
97 {
98  bool receiveBroadcast = par("receiveBroadcast");
99  if (receiveBroadcast)
100  socket.setBroadcast(true);
101 
102  MulticastGroupList mgl = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this)->collectMulticastGroups();
104 
105  // join multicastGroup
106  const char *groupAddr = par("multicastGroup");
107  multicastGroup = L3AddressResolver().resolve(groupAddr);
108  if (!multicastGroup.isUnspecified()) {
110  throw cRuntimeError("Wrong multicastGroup setting: not a multicast address: %s", groupAddr);
112  }
113  socket.setCallback(this);
114 }

Referenced by processStart().

◆ socketClosed()

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

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

Implements inet::UdpSocket::ICallback.

76 {
77  if (operationalState == State::STOPPING_OPERATION)
78  startActiveOperationExtraTimeOrFinish(par("stopOperationExtraTime"));
79 }

◆ socketDataArrived()

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

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

Implements inet::UdpSocket::ICallback.

64 {
65  // process incoming packet
66  processPacket(packet);
67 }

◆ socketErrorArrived()

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

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

Implements inet::UdpSocket::ICallback.

70 {
71  EV_WARN << "Ignoring UDP error report " << indication->getName() << endl;
72  delete indication;
73 }

Member Data Documentation

◆ localPort

int inet::UdpSink::localPort = -1
protected

Referenced by initialize(), and processStart().

◆ multicastGroup

L3Address inet::UdpSink::multicastGroup
protected

◆ numReceived

int inet::UdpSink::numReceived = 0
protected

◆ selfMsg

cMessage* inet::UdpSink::selfMsg = nullptr
protected

◆ socket

◆ startTime

simtime_t inet::UdpSink::startTime
protected

Referenced by handleStartOperation(), and initialize().

◆ stopTime

simtime_t inet::UdpSink::stopTime
protected

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::L3Address::isMulticast
bool isMulticast() const
Definition: L3Address.cc:190
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::getContainingNode
cModule * getContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:40
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::UdpSink::processStart
virtual void processStart()
Definition: UdpSink.cc:116
inet::UdpSocket::setCallback
void setCallback(ICallback *cb)
Sets a callback object, to be used with processMessage().
Definition: UdpSocket.cc:338
inet::UdpSink::STOP
@ STOP
Definition: UdpSink.h:22
inet::L3Address::isUnspecified
bool isUnspecified() const
Definition: L3Address.cc:138
inet::UdpSink::localPort
int localPort
Definition: UdpSink.h:25
inet::UdpSink::socket
UdpSocket socket
Definition: UdpSink.h:24
inet::UdpSink::multicastGroup
L3Address multicastGroup
Definition: UdpSink.h:26
inet::UdpSink::startTime
simtime_t startTime
Definition: UdpSink.h:27
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::UdpSink::stopTime
simtime_t stopTime
Definition: UdpSink.h:28
inet::UdpSink::numReceived
int numReceived
Definition: UdpSink.h:30
inet::UdpSocket::leaveMulticastGroup
void leaveMulticastGroup(const L3Address &multicastAddr)
Causes the socket to leave the given multicast group, i.e.
Definition: UdpSocket.cc:187
inet::UdpSink::processStop
virtual void processStop()
Definition: UdpSink.cc:128
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::sctp::max
double max(const double a, const double b)
Returns the maximum of a and b.
Definition: SctpAssociation.h:266
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::UdpSink::setSocketOptions
virtual void setSocketOptions()
Definition: UdpSink.cc:96
inet::UdpSink::selfMsg
cMessage * selfMsg
Definition: UdpSink.h:29
inet::OperationalMixin< cSimpleModule >::refreshDisplay
virtual void refreshDisplay() const override
Definition: OperationalMixinImpl.h:200
inet::UdpSink::processPacket
virtual void processPacket(Packet *msg)
Definition: UdpSink.cc:135
inet::UdpSink::START
@ START
Definition: UdpSink.h:22