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

UDP application. More...

#include <UdpBasicApp.h>

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

Public Member Functions

 UdpBasicApp ()
 
 ~UdpBasicApp ()
 
- Public Member Functions inherited from inet::ClockUserModuleMixin< ApplicationBase >
virtual void scheduleClockEventAt (clocktime_t time, ClockEvent *msg)
 
virtual void scheduleClockEventAfter (clocktime_t delay, ClockEvent *msg)
 
virtual cMessage * cancelClockEvent (ClockEvent *msg)
 
virtual void cancelAndDeleteClockEvent (ClockEvent *msg)
 
virtual void rescheduleClockEventAt (clocktime_t time, ClockEvent *msg)
 
virtual void rescheduleClockEventAfter (clocktime_t time, ClockEvent *msg)
 
virtual clocktime_t computeClockTimeFromSimTime (simtime_t time) const
 
virtual simtime_t computeSimTimeFromClockTime (clocktime_t time) const
 
virtual clocktime_t getClockTime () const
 
virtual clocktime_t getArrivalClockTime (ClockEvent *msg) const
 
- 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, SEND, STOP }
 
- Protected Types inherited from inet::OperationalMixin< cSimpleModule >
enum  State
 

Protected Member Functions

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 L3Address chooseDestAddr ()
 
virtual void sendPacket ()
 
virtual void processPacket (Packet *msg)
 
virtual void setSocketOptions ()
 
virtual void processStart ()
 
virtual void processSend ()
 
virtual void processStop ()
 
virtual void handleStartOperation (LifecycleOperation *operation) override
 
virtual void handleStopOperation (LifecycleOperation *operation) override
 
virtual void handleCrashOperation (LifecycleOperation *operation) 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...
 
- 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

std::vector< L3AddressdestAddresses
 
std::vector< std::string > destAddressStr
 
int localPort = -1
 
int destPort = -1
 
clocktime_t startTime
 
clocktime_t stopTime
 
bool dontFragment = false
 
const char * packetName = nullptr
 
UdpSocket socket
 
ClockEventselfMsg = nullptr
 
int numSent = 0
 
int numReceived = 0
 
- Protected Attributes inherited from inet::OperationalMixin< cSimpleModule >
State operationalState
 
simtime_t lastChange
 
Operation activeOperation
 
cMessage * activeOperationTimeout
 
cMessage * activeOperationExtraTimer
 

Detailed Description

UDP application.

See NED for more info.

Member Enumeration Documentation

◆ SelfMsgKinds

Enumerator
START 
SEND 
STOP 
26 { START = 1, SEND, STOP };

Constructor & Destructor Documentation

◆ UdpBasicApp()

inet::UdpBasicApp::UdpBasicApp ( )
inline
71 {}

◆ ~UdpBasicApp()

inet::UdpBasicApp::~UdpBasicApp ( )
26 {
27  cancelAndDelete(selfMsg);
28 }

Member Function Documentation

◆ chooseDestAddr()

L3Address inet::UdpBasicApp::chooseDestAddr ( )
protectedvirtual
95 {
96  int k = intrand(destAddresses.size());
97  if (destAddresses[k].isUnspecified() || destAddresses[k].isLinkLocal()) {
98  L3AddressResolver().tryResolve(destAddressStr[k].c_str(), destAddresses[k]);
99  }
100  return destAddresses[k];
101 }

Referenced by sendPacket().

◆ finish()

void inet::UdpBasicApp::finish ( )
overrideprotectedvirtual
53 {
54  recordScalar("packets sent", numSent);
55  recordScalar("packets received", numReceived);
56  ApplicationBase::finish();
57 }

◆ handleCrashOperation()

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

Implements inet::OperationalMixin< cSimpleModule >.

249 {
251  socket.destroy(); // TODO in real operating systems, program crash detected by OS and OS closes sockets of crashed programs.
252 }

◆ handleMessageWhenUp()

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

Implements inet::OperationalMixin< cSimpleModule >.

173 {
174  if (msg->isSelfMessage()) {
175  ASSERT(msg == selfMsg);
176  switch (selfMsg->getKind()) {
177  case START:
178  processStart();
179  break;
180 
181  case SEND:
182  processSend();
183  break;
184 
185  case STOP:
186  processStop();
187  break;
188 
189  default:
190  throw cRuntimeError("Invalid kind %d in self message", (int)selfMsg->getKind());
191  }
192  }
193  else
194  socket.processMessage(msg);
195 }

◆ handleStartOperation()

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

Implements inet::OperationalMixin< cSimpleModule >.

233 {
235  if ((stopTime < CLOCKTIME_ZERO) || (start < stopTime) || (start == stopTime && startTime == stopTime)) {
236  selfMsg->setKind(START);
238  }
239 }

◆ handleStopOperation()

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

Implements inet::OperationalMixin< cSimpleModule >.

242 {
243  cancelEvent(selfMsg);
244  socket.close();
245  delayActiveOperationFinish(par("stopOperationTimeout"));
246 }

◆ initialize()

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

Reimplemented from inet::OperationalMixin< cSimpleModule >.

31 {
32  ClockUserModuleMixin::initialize(stage);
33 
34  if (stage == INITSTAGE_LOCAL) {
35  numSent = 0;
36  numReceived = 0;
37  WATCH(numSent);
38  WATCH(numReceived);
39 
40  localPort = par("localPort");
41  destPort = par("destPort");
42  startTime = par("startTime");
43  stopTime = par("stopTime");
44  packetName = par("packetName");
45  dontFragment = par("dontFragment");
47  throw cRuntimeError("Invalid startTime/stopTime parameters");
48  selfMsg = new ClockEvent("sendTimer");
49  }
50 }

◆ numInitStages()

virtual int inet::UdpBasicApp::numInitStages ( ) const
inlineoverrideprotectedvirtual
46 { return NUM_INIT_STAGES; }

◆ processPacket()

void inet::UdpBasicApp::processPacket ( Packet msg)
protectedvirtual
225 {
226  emit(packetReceivedSignal, pk);
227  EV_INFO << "Received packet: " << UdpSocket::getReceivedPacketInfo(pk) << endl;
228  delete pk;
229  numReceived++;
230 }

Referenced by socketDataArrived().

◆ processSend()

void inet::UdpBasicApp::processSend ( )
protectedvirtual
154 {
155  sendPacket();
156  clocktime_t d = par("sendInterval");
157  if (stopTime < CLOCKTIME_ZERO || getClockTime() + d < stopTime) {
158  selfMsg->setKind(SEND);
160  }
161  else {
162  selfMsg->setKind(STOP);
164  }
165 }

Referenced by handleMessageWhenUp(), and processStart().

◆ processStart()

void inet::UdpBasicApp::processStart ( )
protectedvirtual
122 {
123  socket.setOutputGate(gate("socketOut"));
124  const char *localAddress = par("localAddress");
125  socket.bind(*localAddress ? L3AddressResolver().resolve(localAddress) : L3Address(), localPort);
127 
128  const char *destAddrs = par("destAddresses");
129  cStringTokenizer tokenizer(destAddrs);
130  const char *token;
131 
132  while ((token = tokenizer.nextToken()) != nullptr) {
133  destAddressStr.push_back(token);
134  L3Address result;
135  L3AddressResolver().tryResolve(token, result);
136  if (result.isUnspecified())
137  EV_ERROR << "cannot resolve destination address: " << token << endl;
138  destAddresses.push_back(result);
139  }
140 
141  if (!destAddresses.empty()) {
142  selfMsg->setKind(SEND);
143  processSend();
144  }
145  else {
146  if (stopTime >= CLOCKTIME_ZERO) {
147  selfMsg->setKind(STOP);
149  }
150  }
151 }

Referenced by handleMessageWhenUp().

◆ processStop()

void inet::UdpBasicApp::processStop ( )
protectedvirtual
168 {
169  socket.close();
170 }

Referenced by handleMessageWhenUp().

◆ refreshDisplay()

void inet::UdpBasicApp::refreshDisplay ( ) const
overrideprotectedvirtual
216 {
218 
219  char buf[100];
220  sprintf(buf, "rcvd: %d pks\nsent: %d pks", numReceived, numSent);
221  getDisplayString().setTagArg("t", 0, buf);
222 }

◆ sendPacket()

void inet::UdpBasicApp::sendPacket ( )
protectedvirtual
104 {
105  std::ostringstream str;
106  str << packetName << "-" << numSent;
107  Packet *packet = new Packet(str.str().c_str());
108  if (dontFragment)
109  packet->addTag<FragmentationReq>()->setDontFragment(true);
110  const auto& payload = makeShared<ApplicationPacket>();
111  payload->setChunkLength(B(par("messageLength")));
112  payload->setSequenceNumber(numSent);
113  payload->addTag<CreationTimeTag>()->setCreationTime(simTime());
114  packet->insertAtBack(payload);
115  L3Address destAddr = chooseDestAddr();
116  emit(packetSentSignal, packet);
117  socket.sendTo(packet, destAddr, destPort);
118  numSent++;
119 }

Referenced by processSend().

◆ setSocketOptions()

void inet::UdpBasicApp::setSocketOptions ( )
protectedvirtual
60 {
61  int timeToLive = par("timeToLive");
62  if (timeToLive != -1)
63  socket.setTimeToLive(timeToLive);
64 
65  int dscp = par("dscp");
66  if (dscp != -1)
67  socket.setDscp(dscp);
68 
69  int tos = par("tos");
70  if (tos != -1)
71  socket.setTos(tos);
72 
73  const char *multicastInterface = par("multicastInterface");
74  if (multicastInterface[0]) {
75  IInterfaceTable *ift = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this);
76  NetworkInterface *ie = ift->findInterfaceByName(multicastInterface);
77  if (!ie)
78  throw cRuntimeError("Wrong multicastInterface setting: no interface named \"%s\"", multicastInterface);
79  socket.setMulticastOutputInterface(ie->getInterfaceId());
80  }
81 
82  bool receiveBroadcast = par("receiveBroadcast");
83  if (receiveBroadcast)
84  socket.setBroadcast(true);
85 
86  bool joinLocalMulticastGroups = par("joinLocalMulticastGroups");
87  if (joinLocalMulticastGroups) {
88  MulticastGroupList mgl = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this)->collectMulticastGroups();
90  }
91  socket.setCallback(this);
92 }

Referenced by processStart().

◆ socketClosed()

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

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

Implements inet::UdpSocket::ICallback.

210 {
211  if (operationalState == State::STOPPING_OPERATION)
212  startActiveOperationExtraTimeOrFinish(par("stopOperationExtraTime"));
213 }

◆ socketDataArrived()

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

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

Implements inet::UdpSocket::ICallback.

198 {
199  // process incoming packet
200  processPacket(packet);
201 }

◆ socketErrorArrived()

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

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

Implements inet::UdpSocket::ICallback.

204 {
205  EV_WARN << "Ignoring UDP error report " << indication->getName() << endl;
206  delete indication;
207 }

Member Data Documentation

◆ destAddresses

std::vector<L3Address> inet::UdpBasicApp::destAddresses
protected

Referenced by chooseDestAddr(), and processStart().

◆ destAddressStr

std::vector<std::string> inet::UdpBasicApp::destAddressStr
protected

Referenced by chooseDestAddr(), and processStart().

◆ destPort

int inet::UdpBasicApp::destPort = -1
protected

Referenced by initialize(), and sendPacket().

◆ dontFragment

bool inet::UdpBasicApp::dontFragment = false
protected

Referenced by initialize(), and sendPacket().

◆ localPort

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

Referenced by initialize(), and processStart().

◆ numReceived

int inet::UdpBasicApp::numReceived = 0
protected

◆ numSent

int inet::UdpBasicApp::numSent = 0
protected

◆ packetName

const char* inet::UdpBasicApp::packetName = nullptr
protected

Referenced by initialize(), and sendPacket().

◆ selfMsg

◆ socket

◆ startTime

clocktime_t inet::UdpBasicApp::startTime
protected

Referenced by handleStartOperation(), and initialize().

◆ stopTime

clocktime_t inet::UdpBasicApp::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::UdpBasicApp::destAddresses
std::vector< L3Address > destAddresses
Definition: UdpBasicApp.h:29
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::UdpBasicApp::STOP
@ STOP
Definition: UdpBasicApp.h:26
inet::OperationalMixin< cSimpleModule >::operationalState
State operationalState
Definition: OperationalMixin.h:23
inet::clocktime_t
ClockTime clocktime_t
Definition: contract/ClockTime.h:25
inet::UdpBasicApp::numReceived
int numReceived
Definition: UdpBasicApp.h:43
inet::UdpSocket::bind
void bind(int localPort)
Bind the socket to a local port number.
Definition: UdpSocket.cc:34
inet::UdpSocket::sendTo
void sendTo(Packet *msg, L3Address destAddr, int destPort)
Sends a data packet to the given address and port.
Definition: UdpSocket.cc:69
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::ClockUserModuleMixin< ApplicationBase >::scheduleClockEventAfter
virtual void scheduleClockEventAfter(clocktime_t delay, ClockEvent *msg)
Definition: ClockUserModuleMixin.h:65
inet::UdpSocket::destroy
virtual void destroy() override
Notify the protocol that the owner of ISocket has destroyed the socket.
Definition: UdpSocket.cc:98
inet::ClockUserModuleMixin< ApplicationBase >::scheduleClockEventAt
virtual void scheduleClockEventAt(clocktime_t time, ClockEvent *msg)
Definition: ClockUserModuleMixin.h:64
inet::packetSentSignal
simsignal_t packetSentSignal
Definition: Simsignals.cc:96
inet::UdpBasicApp::destAddressStr
std::vector< std::string > destAddressStr
Definition: UdpBasicApp.h:30
inet::UdpBasicApp::socket
UdpSocket socket
Definition: UdpBasicApp.h:38
inet::UdpSocket::setCallback
void setCallback(ICallback *cb)
Sets a callback object, to be used with processMessage().
Definition: UdpSocket.cc:338
CLOCKTIME_ZERO
#define CLOCKTIME_ZERO
Constant for zero simulation time.
Definition: contract/ClockTime.h:36
inet::UdpBasicApp::setSocketOptions
virtual void setSocketOptions()
Definition: UdpBasicApp.cc:59
inet::ClockUserModuleMixin< ApplicationBase >::getClockTime
virtual clocktime_t getClockTime() const
Definition: ClockUserModuleMixin.h:72
inet::UdpBasicApp::SEND
@ SEND
Definition: UdpBasicApp.h:26
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::ClockEvent
cMessage ClockEvent
Definition: contract/ClockEvent.h:18
inet::UdpBasicApp::selfMsg
ClockEvent * selfMsg
Definition: UdpBasicApp.h:39
inet::units::units::B
intscale< b, 1, 8 > B
Definition: Units.h:1168
inet::packetReceivedSignal
simsignal_t packetReceivedSignal
Definition: Simsignals.cc:97
inet::UdpBasicApp::destPort
int destPort
Definition: UdpBasicApp.h:31
inet::UdpBasicApp::stopTime
clocktime_t stopTime
Definition: UdpBasicApp.h:33
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::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
inet::UdpBasicApp::processSend
virtual void processSend()
Definition: UdpBasicApp.cc:153
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::UdpBasicApp::localPort
int localPort
Definition: UdpBasicApp.h:31
inet::sctp::max
double max(const double a, const double b)
Returns the maximum of a and b.
Definition: SctpAssociation.h:266
inet::UdpBasicApp::packetName
const char * packetName
Definition: UdpBasicApp.h:35
inet::physicallayer::k
const double k
Definition: Qam1024Modulation.cc:14
inet::UdpBasicApp::START
@ START
Definition: UdpBasicApp.h:26
inet::UdpSocket::setBroadcast
void setBroadcast(bool broadcast)
Set the Broadcast option on the UDP socket.
Definition: UdpSocket.cc:139
inet::UdpBasicApp::chooseDestAddr
virtual L3Address chooseDestAddr()
Definition: UdpBasicApp.cc:94
inet::UdpBasicApp::numSent
int numSent
Definition: UdpBasicApp.h:42
inet::OperationalMixin< cSimpleModule >::delayActiveOperationFinish
virtual void delayActiveOperationFinish(simtime_t timeout)
Definition: OperationalMixinImpl.h:161
inet::UdpBasicApp::processStop
virtual void processStop()
Definition: UdpBasicApp.cc:167
inet::OperationalMixin< cSimpleModule >::startActiveOperationExtraTimeOrFinish
virtual void startActiveOperationExtraTimeOrFinish(simtime_t extraTime)
Definition: OperationalMixinImpl.h:179
inet::UdpBasicApp::dontFragment
bool dontFragment
Definition: UdpBasicApp.h:34
inet::UdpBasicApp::processStart
virtual void processStart()
Definition: UdpBasicApp.cc:121
inet::UdpBasicApp::startTime
clocktime_t startTime
Definition: UdpBasicApp.h:32
inet::UdpBasicApp::sendPacket
virtual void sendPacket()
Definition: UdpBasicApp.cc:103
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::ClockUserModuleMixin< ApplicationBase >::cancelClockEvent
virtual cMessage * cancelClockEvent(ClockEvent *msg)
Definition: ClockUserModuleMixin.h:66
inet::OperationalMixin< cSimpleModule >::refreshDisplay
virtual void refreshDisplay() const override
Definition: OperationalMixinImpl.h:200
inet::UdpBasicApp::processPacket
virtual void processPacket(Packet *msg)
Definition: UdpBasicApp.cc:224