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

UDP application. More...

#include <UDPBasicApp.h>

Inheritance diagram for inet::UDPBasicApp:
inet::ApplicationBase inet::OperationalBase inet::ILifecycle

Public Member Functions

 UDPBasicApp ()
 
 ~UDPBasicApp ()
 
- 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, SEND, STOP }
 

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 (cPacket *msg)
 
virtual void setSocketOptions ()
 
virtual void processStart ()
 
virtual void processSend ()
 
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

std::vector< L3AddressdestAddresses
 
int localPort = -1
 
int destPort = -1
 
simtime_t startTime
 
simtime_t stopTime
 
const char * packetName = nullptr
 
UDPSocket socket
 
cMessage * selfMsg = nullptr
 
int numSent = 0
 
int numReceived = 0
 
- Protected Attributes inherited from inet::OperationalBase
bool isOperational
 
simtime_t lastChange
 

Static Protected Attributes

static simsignal_t sentPkSignal = registerSignal("sentPk")
 
static simsignal_t rcvdPkSignal = registerSignal("rcvdPk")
 

Detailed Description

UDP application.

See NED for more info.

Member Enumeration Documentation

Enumerator
START 
SEND 
STOP 
37 { START = 1, SEND, STOP };
Definition: UDPBasicApp.h:37
Definition: UDPBasicApp.h:37
Definition: UDPBasicApp.h:37

Constructor & Destructor Documentation

inet::UDPBasicApp::UDPBasicApp ( )
inline
79 {}
inet::UDPBasicApp::~UDPBasicApp ( )
36 {
37  cancelAndDelete(selfMsg);
38 }
cMessage * selfMsg
Definition: UDPBasicApp.h:48

Member Function Documentation

L3Address inet::UDPBasicApp::chooseDestAddr ( )
protectedvirtual

Referenced by sendPacket().

99 {
100  int k = intrand(destAddresses.size());
101  if (destAddresses[k].isLinkLocal()) { // KLUDGE for IPv6
102  const char *destAddrs = par("destAddresses");
103  cStringTokenizer tokenizer(destAddrs);
104  const char *token = nullptr;
105 
106  for (int i = 0; i <= k; ++i)
107  token = tokenizer.nextToken();
108  destAddresses[k] = L3AddressResolver().resolve(token);
109  }
110  return destAddresses[k];
111 }
std::vector< L3Address > destAddresses
Definition: UDPBasicApp.h:40
const double k
Definition: QAM16Modulation.cc:24
void inet::UDPBasicApp::finish ( )
overrideprotectedvirtual
62 {
63  recordScalar("packets sent", numSent);
64  recordScalar("packets received", numReceived);
65  ApplicationBase::finish();
66 }
int numSent
Definition: UDPBasicApp.h:51
int numReceived
Definition: UDPBasicApp.h:52
void inet::UDPBasicApp::handleMessageWhenUp ( cMessage *  msg)
overrideprotectedvirtual

Implements inet::OperationalBase.

180 {
181  if (msg->isSelfMessage()) {
182  ASSERT(msg == selfMsg);
183  switch (selfMsg->getKind()) {
184  case START:
185  processStart();
186  break;
187 
188  case SEND:
189  processSend();
190  break;
191 
192  case STOP:
193  processStop();
194  break;
195 
196  default:
197  throw cRuntimeError("Invalid kind %d in self message", (int)selfMsg->getKind());
198  }
199  }
200  else if (msg->getKind() == UDP_I_DATA) {
201  // process incoming packet
202  processPacket(PK(msg));
203  }
204  else if (msg->getKind() == UDP_I_ERROR) {
205  EV_WARN << "Ignoring UDP error report\n";
206  delete msg;
207  }
208  else {
209  throw cRuntimeError("Unrecognized message (%s)%s", msg->getClassName(), msg->getName());
210  }
211 }
Definition: UDPBasicApp.h:37
Definition: UDPControlInfo_m.h:83
virtual void processStart()
Definition: UDPBasicApp.cc:128
Definition: UDPControlInfo_m.h:84
virtual void processSend()
Definition: UDPBasicApp.cc:160
virtual void processStop()
Definition: UDPBasicApp.cc:174
virtual void processPacket(cPacket *msg)
Definition: UDPBasicApp.cc:220
#define PK(msg)
Definition: INETDefs.h:92
Definition: UDPBasicApp.h:37
cMessage * selfMsg
Definition: UDPBasicApp.h:48
Definition: UDPBasicApp.h:37
void inet::UDPBasicApp::handleNodeCrash ( )
overrideprotectedvirtual

Reimplemented from inet::OperationalBase.

247 {
248  if (selfMsg)
249  cancelEvent(selfMsg);
250 }
cMessage * selfMsg
Definition: UDPBasicApp.h:48
bool inet::UDPBasicApp::handleNodeShutdown ( IDoneCallback doneCallback)
overrideprotectedvirtual

Reimplemented from inet::OperationalBase.

239 {
240  if (selfMsg)
241  cancelEvent(selfMsg);
242  //TODO if(socket.isOpened()) socket.close();
243  return true;
244 }
cMessage * selfMsg
Definition: UDPBasicApp.h:48
bool inet::UDPBasicApp::handleNodeStart ( IDoneCallback doneCallback)
overrideprotectedvirtual

Reimplemented from inet::OperationalBase.

229 {
230  simtime_t start = std::max(startTime, simTime());
231  if ((stopTime < SIMTIME_ZERO) || (start < stopTime) || (start == stopTime && startTime == stopTime)) {
232  selfMsg->setKind(START);
233  scheduleAt(start, selfMsg);
234  }
235  return true;
236 }
double max(double a, double b)
Returns the greater of the given parameters.
Definition: INETMath.h:161
simtime_t stopTime
Definition: UDPBasicApp.h:43
simtime_t startTime
Definition: UDPBasicApp.h:42
cMessage * selfMsg
Definition: UDPBasicApp.h:48
Definition: UDPBasicApp.h:37
void inet::UDPBasicApp::initialize ( int  stage)
overrideprotectedvirtual

Reimplemented from inet::OperationalBase.

41 {
43 
44  if (stage == INITSTAGE_LOCAL) {
45  numSent = 0;
46  numReceived = 0;
47  WATCH(numSent);
48  WATCH(numReceived);
49 
50  localPort = par("localPort");
51  destPort = par("destPort");
52  startTime = par("startTime").doubleValue();
53  stopTime = par("stopTime").doubleValue();
54  packetName = par("packetName");
55  if (stopTime >= SIMTIME_ZERO && stopTime < startTime)
56  throw cRuntimeError("Invalid startTime/stopTime parameters");
57  selfMsg = new cMessage("sendTimer");
58  }
59 }
int localPort
Definition: UDPBasicApp.h:41
int destPort
Definition: UDPBasicApp.h:41
simtime_t stopTime
Definition: UDPBasicApp.h:43
int numSent
Definition: UDPBasicApp.h:51
int numReceived
Definition: UDPBasicApp.h:52
Local initializations.
Definition: InitStages.h:35
virtual void initialize(int stage) override
Definition: OperationalBase.cc:32
const char * packetName
Definition: UDPBasicApp.h:44
simtime_t startTime
Definition: UDPBasicApp.h:42
cMessage * selfMsg
Definition: UDPBasicApp.h:48
virtual int inet::UDPBasicApp::numInitStages ( ) const
inlineoverrideprotectedvirtual

Reimplemented from inet::OperationalBase.

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

Referenced by handleMessageWhenUp().

221 {
222  emit(rcvdPkSignal, pk);
223  EV_INFO << "Received packet: " << UDPSocket::getReceivedPacketInfo(pk) << endl;
224  delete pk;
225  numReceived++;
226 }
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: UDPBasicApp.h:52
static simsignal_t rcvdPkSignal
Definition: UDPBasicApp.h:55
void inet::UDPBasicApp::processSend ( )
protectedvirtual

Referenced by handleMessageWhenUp(), and processStart().

161 {
162  sendPacket();
163  simtime_t d = simTime() + par("sendInterval").doubleValue();
164  if (stopTime < SIMTIME_ZERO || d < stopTime) {
165  selfMsg->setKind(SEND);
166  scheduleAt(d, selfMsg);
167  }
168  else {
169  selfMsg->setKind(STOP);
170  scheduleAt(stopTime, selfMsg);
171  }
172 }
Definition: UDPBasicApp.h:37
virtual void sendPacket()
Definition: UDPBasicApp.cc:113
simtime_t stopTime
Definition: UDPBasicApp.h:43
Definition: UDPBasicApp.h:37
cMessage * selfMsg
Definition: UDPBasicApp.h:48
void inet::UDPBasicApp::processStart ( )
protectedvirtual

Referenced by handleMessageWhenUp().

129 {
130  socket.setOutputGate(gate("udpOut"));
131  const char *localAddress = par("localAddress");
132  socket.bind(*localAddress ? L3AddressResolver().resolve(localAddress) : L3Address(), localPort);
134 
135  const char *destAddrs = par("destAddresses");
136  cStringTokenizer tokenizer(destAddrs);
137  const char *token;
138 
139  while ((token = tokenizer.nextToken()) != nullptr) {
140  L3Address result;
141  L3AddressResolver().tryResolve(token, result);
142  if (result.isUnspecified())
143  EV_ERROR << "cannot resolve destination address: " << token << endl;
144  else
145  destAddresses.push_back(result);
146  }
147 
148  if (!destAddresses.empty()) {
149  selfMsg->setKind(SEND);
150  processSend();
151  }
152  else {
153  if (stopTime >= SIMTIME_ZERO) {
154  selfMsg->setKind(STOP);
155  scheduleAt(stopTime, selfMsg);
156  }
157  }
158 }
Definition: UDPBasicApp.h:37
int localPort
Definition: UDPBasicApp.h:41
simtime_t stopTime
Definition: UDPBasicApp.h:43
void setOutputGate(cGate *toUdp)
Sets the gate on which to send to UDP.
Definition: UDPSocket.h:110
UDPSocket socket
Definition: UDPBasicApp.h:47
virtual void processSend()
Definition: UDPBasicApp.cc:160
virtual void setSocketOptions()
Definition: UDPBasicApp.cc:68
std::vector< L3Address > destAddresses
Definition: UDPBasicApp.h:40
Definition: UDPBasicApp.h:37
cMessage * selfMsg
Definition: UDPBasicApp.h:48
void bind(int localPort)
Bind the socket to a local port number.
Definition: UDPSocket.cc:53
void inet::UDPBasicApp::processStop ( )
protectedvirtual

Referenced by handleMessageWhenUp().

175 {
176  socket.close();
177 }
UDPSocket socket
Definition: UDPBasicApp.h:47
void close()
Unbinds the socket.
Definition: UDPSocket.cc:112
void inet::UDPBasicApp::refreshDisplay ( ) const
overrideprotectedvirtual
214 {
215  char buf[100];
216  sprintf(buf, "rcvd: %d pks\nsent: %d pks", numReceived, numSent);
217  getDisplayString().setTagArg("t", 0, buf);
218 }
int numSent
Definition: UDPBasicApp.h:51
int numReceived
Definition: UDPBasicApp.h:52
void inet::UDPBasicApp::sendPacket ( )
protectedvirtual

Referenced by processSend().

114 {
115  std::ostringstream str;
116  str << packetName << "-" << numSent;
117  ApplicationPacket *payload = new ApplicationPacket(str.str().c_str());
118  payload->setByteLength(par("messageLength").longValue());
119  payload->setSequenceNumber(numSent);
120 
121  L3Address destAddr = chooseDestAddr();
122 
123  emit(sentPkSignal, payload);
124  socket.sendTo(payload, destAddr, destPort);
125  numSent++;
126 }
void sendTo(cPacket *msg, L3Address destAddr, int destPort, const SendOptions *options=nullptr)
Sends a data packet to the given address and port.
Definition: UDPSocket.cc:88
static simsignal_t sentPkSignal
Definition: UDPBasicApp.h:54
int destPort
Definition: UDPBasicApp.h:41
int numSent
Definition: UDPBasicApp.h:51
UDPSocket socket
Definition: UDPBasicApp.h:47
virtual L3Address chooseDestAddr()
Definition: UDPBasicApp.cc:98
const char * packetName
Definition: UDPBasicApp.h:44
void inet::UDPBasicApp::setSocketOptions ( )
protectedvirtual

Referenced by processStart().

69 {
70  int timeToLive = par("timeToLive");
71  if (timeToLive != -1)
72  socket.setTimeToLive(timeToLive);
73 
74  int typeOfService = par("typeOfService");
75  if (typeOfService != -1)
76  socket.setTypeOfService(typeOfService);
77 
78  const char *multicastInterface = par("multicastInterface");
79  if (multicastInterface[0]) {
80  IInterfaceTable *ift = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this);
81  InterfaceEntry *ie = ift->getInterfaceByName(multicastInterface);
82  if (!ie)
83  throw cRuntimeError("Wrong multicastInterface setting: no interface named \"%s\"", multicastInterface);
84  socket.setMulticastOutputInterface(ie->getInterfaceId());
85  }
86 
87  bool receiveBroadcast = par("receiveBroadcast");
88  if (receiveBroadcast)
89  socket.setBroadcast(true);
90 
91  bool joinLocalMulticastGroups = par("joinLocalMulticastGroups");
92  if (joinLocalMulticastGroups) {
93  MulticastGroupList mgl = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this)->collectMulticastGroups();
95  }
96 }
void setTimeToLive(int ttl)
Set the TTL (IPv6: Hop Limit) field on sent packets.
Definition: UDPSocket.cc:131
void joinLocalMulticastGroups(MulticastGroupList mgl)
Joins the socket to each multicast group that are registered with any of the interfaces.
Definition: UDPSocket.cc:194
void setMulticastOutputInterface(int interfaceId)
Set the output interface for sending multicast packets (like the Unix IP_MULTICAST_IF socket option)...
Definition: UDPSocket.cc:151
UDPSocket socket
Definition: UDPBasicApp.h:47
void setBroadcast(bool broadcast)
Set the Broadcast option on the UDP socket.
Definition: UDPSocket.cc:121
void setTypeOfService(unsigned char tos)
Sets the IPv4 Type of Service / IPv6 Traffic Class fields of packets sent from the UDP socket...
Definition: UDPSocket.cc:141
std::vector< MulticastGroup > MulticastGroupList
Definition: IInterfaceTable.h:36

Member Data Documentation

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

Referenced by chooseDestAddr(), and processStart().

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

Referenced by initialize(), and sendPacket().

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

Referenced by initialize(), and processStart().

int inet::UDPBasicApp::numReceived = 0
protected
int inet::UDPBasicApp::numSent = 0
protected
const char* inet::UDPBasicApp::packetName = nullptr
protected

Referenced by initialize(), and sendPacket().

simsignal_t inet::UDPBasicApp::rcvdPkSignal = registerSignal("rcvdPk")
staticprotected

Referenced by processPacket().

cMessage* inet::UDPBasicApp::selfMsg = nullptr
protected
simsignal_t inet::UDPBasicApp::sentPkSignal = registerSignal("sentPk")
staticprotected

Referenced by sendPacket().

UDPSocket inet::UDPBasicApp::socket
protected
simtime_t inet::UDPBasicApp::startTime
protected

Referenced by handleNodeStart(), and initialize().

simtime_t inet::UDPBasicApp::stopTime
protected

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