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::OperationalBase inet::ILifecycle

Public Member Functions

 UDPSink ()
 
virtual ~UDPSink ()
 
- Public Member Functions inherited from inet::ApplicationBase
 ApplicationBase ()
 
- Public Member Functions inherited from inet::OperationalBase
 OperationalBase ()
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 

Protected Types

enum  SelfMsgKinds { START = 1, STOP }
 

Protected Member Functions

virtual void processPacket (cPacket *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 processStart ()
 
virtual void processStop ()
 
virtual bool handleNodeStart (IDoneCallback *doneCallback) override
 
virtual bool handleNodeShutdown (IDoneCallback *doneCallback) override
 
virtual void handleNodeCrash () override
 
- Protected Member Functions inherited from inet::ApplicationBase
virtual bool isInitializeStage (int stage) override
 
virtual bool isNodeStartStage (int stage) override
 
virtual bool isNodeShutdownStage (int stage) override
 
- Protected Member Functions inherited from inet::OperationalBase
virtual void handleMessage (cMessage *msg) override
 
virtual void handleMessageWhenDown (cMessage *msg)
 
virtual bool handleOperationStage (LifecycleOperation *operation, int stage, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 
virtual void setOperational (bool isOperational)
 

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::OperationalBase
bool isOperational
 
simtime_t lastChange
 

Static Protected Attributes

static simsignal_t rcvdPkSignal = registerSignal("rcvdPk")
 

Detailed Description

Consumes and prints packets received from the UDP module.

See NED for more info.

Member Enumeration Documentation

Enumerator
START 
STOP 
35 { START = 1, STOP };
Definition: UDPSink.h:35
Definition: UDPSink.h:35

Constructor & Destructor Documentation

inet::UDPSink::UDPSink ( )
inline
48 {}
inet::UDPSink::~UDPSink ( )
virtual
31 {
32  cancelAndDelete(selfMsg);
33 }
cMessage * selfMsg
Definition: UDPSink.h:42

Member Function Documentation

void inet::UDPSink::finish ( )
overrideprotectedvirtual
90 {
91  ApplicationBase::finish();
92  EV_INFO << getFullPath() << ": received " << numReceived << " packets\n";
93 }
int numReceived
Definition: UDPSink.h:44
void inet::UDPSink::handleMessageWhenUp ( cMessage *  msg)
overrideprotectedvirtual

Implements inet::OperationalBase.

53 {
54  if (msg->isSelfMessage()) {
55  ASSERT(msg == selfMsg);
56  switch (selfMsg->getKind()) {
57  case START:
58  processStart();
59  break;
60 
61  case STOP:
62  processStop();
63  break;
64 
65  default:
66  throw cRuntimeError("Invalid kind %d in self message", (int)selfMsg->getKind());
67  }
68  }
69  else if (msg->getKind() == UDP_I_DATA) {
70  // process incoming packet
71  processPacket(PK(msg));
72  }
73  else if (msg->getKind() == UDP_I_ERROR) {
74  EV_WARN << "Ignoring UDP error report\n";
75  delete msg;
76  }
77  else {
78  throw cRuntimeError("Unrecognized message (%s)%s", msg->getClassName(), msg->getName());
79  }
80 }
cMessage * selfMsg
Definition: UDPSink.h:42
virtual void processStart()
Definition: UDPSink.cc:114
Definition: UDPSink.h:35
Definition: UDPControlInfo_m.h:83
virtual void processStop()
Definition: UDPSink.cc:126
virtual void processPacket(cPacket *msg)
Definition: UDPSink.cc:133
Definition: UDPSink.h:35
Definition: UDPControlInfo_m.h:84
#define PK(msg)
Definition: INETDefs.h:92
void inet::UDPSink::handleNodeCrash ( )
overrideprotectedvirtual

Reimplemented from inet::OperationalBase.

161 {
162  if (selfMsg)
163  cancelEvent(selfMsg);
164 }
cMessage * selfMsg
Definition: UDPSink.h:42
bool inet::UDPSink::handleNodeShutdown ( IDoneCallback doneCallback)
overrideprotectedvirtual

Reimplemented from inet::OperationalBase.

153 {
154  if (selfMsg)
155  cancelEvent(selfMsg);
156  //TODO if(socket.isOpened()) socket.close();
157  return true;
158 }
cMessage * selfMsg
Definition: UDPSink.h:42
bool inet::UDPSink::handleNodeStart ( IDoneCallback doneCallback)
overrideprotectedvirtual

Reimplemented from inet::OperationalBase.

143 {
144  simtime_t start = std::max(startTime, simTime());
145  if ((stopTime < SIMTIME_ZERO) || (start < stopTime) || (start == stopTime && startTime == stopTime)) {
146  selfMsg->setKind(START);
147  scheduleAt(start, selfMsg);
148  }
149  return true;
150 }
cMessage * selfMsg
Definition: UDPSink.h:42
simtime_t startTime
Definition: UDPSink.h:40
double max(double a, double b)
Returns the greater of the given parameters.
Definition: INETMath.h:161
Definition: UDPSink.h:35
simtime_t stopTime
Definition: UDPSink.h:41
void inet::UDPSink::initialize ( int  stage)
overrideprotectedvirtual

Reimplemented from inet::OperationalBase.

36 {
38 
39  if (stage == INITSTAGE_LOCAL) {
40  numReceived = 0;
41  WATCH(numReceived);
42 
43  localPort = par("localPort");
44  startTime = par("startTime").doubleValue();
45  stopTime = par("stopTime").doubleValue();
46  if (stopTime >= SIMTIME_ZERO && stopTime < startTime)
47  throw cRuntimeError("Invalid startTime/stopTime parameters");
48  selfMsg = new cMessage("UDPSinkTimer");
49  }
50 }
cMessage * selfMsg
Definition: UDPSink.h:42
simtime_t startTime
Definition: UDPSink.h:40
int localPort
Definition: UDPSink.h:38
int numReceived
Definition: UDPSink.h:44
Local initializations.
Definition: InitStages.h:35
virtual void initialize(int stage) override
Definition: OperationalBase.cc:32
simtime_t stopTime
Definition: UDPSink.h:41
virtual int inet::UDPSink::numInitStages ( ) const
inlineoverrideprotectedvirtual

Reimplemented from inet::OperationalBase.

56 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::UDPSink::processPacket ( cPacket *  msg)
protectedvirtual

Referenced by handleMessageWhenUp().

134 {
135  EV_INFO << "Received packet: " << UDPSocket::getReceivedPacketInfo(pk) << endl;
136  emit(rcvdPkSignal, pk);
137  delete pk;
138 
139  numReceived++;
140 }
static std::string getReceivedPacketInfo(cPacket *pk)
Utility function: returns a line of information about a packet received via UDP.
Definition: UDPSocket.cc:324
int numReceived
Definition: UDPSink.h:44
static simsignal_t rcvdPkSignal
Definition: UDPSink.h:45
void inet::UDPSink::processStart ( )
protectedvirtual

Referenced by handleMessageWhenUp().

115 {
116  socket.setOutputGate(gate("udpOut"));
119 
120  if (stopTime >= SIMTIME_ZERO) {
121  selfMsg->setKind(STOP);
122  scheduleAt(stopTime, selfMsg);
123  }
124 }
cMessage * selfMsg
Definition: UDPSink.h:42
UDPSocket socket
Definition: UDPSink.h:37
Definition: UDPSink.h:35
virtual void setSocketOptions()
Definition: UDPSink.cc:95
void setOutputGate(cGate *toUdp)
Sets the gate on which to send to UDP.
Definition: UDPSocket.h:110
int localPort
Definition: UDPSink.h:38
simtime_t stopTime
Definition: UDPSink.h:41
void bind(int localPort)
Bind the socket to a local port number.
Definition: UDPSocket.cc:53
void inet::UDPSink::processStop ( )
protectedvirtual

Referenced by handleMessageWhenUp().

127 {
129  socket.leaveMulticastGroup(multicastGroup); // FIXME should be done by socket.close()
130  socket.close();
131 }
UDPSocket socket
Definition: UDPSink.h:37
L3Address multicastGroup
Definition: UDPSink.h:39
void close()
Unbinds the socket.
Definition: UDPSocket.cc:112
bool isUnspecified() const
Definition: L3Address.cc:133
void leaveMulticastGroup(const L3Address &multicastAddr)
Causes the socket to leave the given multicast group, i.e.
Definition: UDPSocket.cc:213
void inet::UDPSink::refreshDisplay ( ) const
overrideprotectedvirtual
83 {
84  char buf[50];
85  sprintf(buf, "rcvd: %d pks", numReceived);
86  getDisplayString().setTagArg("t", 0, buf);
87 }
int numReceived
Definition: UDPSink.h:44
void inet::UDPSink::setSocketOptions ( )
protectedvirtual

Referenced by processStart().

96 {
97  bool receiveBroadcast = par("receiveBroadcast");
98  if (receiveBroadcast)
99  socket.setBroadcast(true);
100 
101  MulticastGroupList mgl = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this)->collectMulticastGroups();
103 
104  // join multicastGroup
105  const char *groupAddr = par("multicastGroup");
106  multicastGroup = L3AddressResolver().resolve(groupAddr);
107  if (!multicastGroup.isUnspecified()) {
109  throw cRuntimeError("Wrong multicastGroup setting: not a multicast address: %s", groupAddr);
111  }
112 }
UDPSocket socket
Definition: UDPSink.h:37
L3Address multicastGroup
Definition: UDPSink.h:39
void joinLocalMulticastGroups(MulticastGroupList mgl)
Joins the socket to each multicast group that are registered with any of the interfaces.
Definition: UDPSocket.cc:194
bool isMulticast() const
Definition: L3Address.cc:185
void setBroadcast(bool broadcast)
Set the Broadcast option on the UDP socket.
Definition: UDPSocket.cc:121
bool isUnspecified() const
Definition: L3Address.cc:133
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:181
std::vector< MulticastGroup > MulticastGroupList
Definition: IInterfaceTable.h:36

Member Data Documentation

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

Referenced by initialize(), and processStart().

L3Address inet::UDPSink::multicastGroup
protected

Referenced by processStop(), and setSocketOptions().

int inet::UDPSink::numReceived = 0
protected
simsignal_t inet::UDPSink::rcvdPkSignal = registerSignal("rcvdPk")
staticprotected

Referenced by processPacket().

cMessage* inet::UDPSink::selfMsg = nullptr
protected
UDPSocket inet::UDPSink::socket
protected
simtime_t inet::UDPSink::startTime
protected

Referenced by handleNodeStart(), and initialize().

simtime_t inet::UDPSink::stopTime
protected

The documentation for this class was generated from the following files: