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

IP traffic generator application. More...

#include <IPvXTrafGen.h>

Inheritance diagram for inet::IPvXTrafGen:
inet::ILifecycle

Public Member Functions

 IPvXTrafGen ()
 
virtual ~IPvXTrafGen ()
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 

Protected Types

enum  Kinds { START = 100, NEXT }
 

Protected Member Functions

virtual void scheduleNextPacket (simtime_t previous)
 
virtual void cancelNextPacket ()
 
virtual bool isNodeUp ()
 
virtual bool isEnabled ()
 
virtual L3Address chooseDestAddr ()
 
virtual void sendPacket ()
 
virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void refreshDisplay () const override
 
virtual void startApp ()
 
virtual void printPacket (cPacket *msg)
 
virtual void processPacket (cPacket *msg)
 
virtual bool handleOperationStage (LifecycleOperation *operation, int stage, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 

Protected Attributes

simtime_t startTime
 
simtime_t stopTime
 
cPar * sendIntervalPar = nullptr
 
cPar * packetLengthPar = nullptr
 
int protocol = -1
 
std::vector< L3AddressdestAddresses
 
int numPackets = 0
 
NodeStatusnodeStatus = nullptr
 
cMessage * timer = nullptr
 
bool isOperational = false
 
int numSent = 0
 
int numReceived = 0
 

Static Protected Attributes

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

Detailed Description

IP traffic generator application.

See NED for more info.

Member Enumeration Documentation

enum inet::IPvXTrafGen::Kinds
protected
Enumerator
START 
NEXT 
39 { START = 100, NEXT };
Definition: IPvXTrafGen.h:39
Definition: IPvXTrafGen.h:39

Constructor & Destructor Documentation

inet::IPvXTrafGen::IPvXTrafGen ( )
36 {
37 }
inet::IPvXTrafGen::~IPvXTrafGen ( )
virtual
40 {
41  cancelAndDelete(timer);
42 }
cMessage * timer
Definition: IPvXTrafGen.h:52

Member Function Documentation

void inet::IPvXTrafGen::cancelNextPacket ( )
protectedvirtual

Referenced by handleOperationStage().

157 {
158  cancelEvent(timer);
159 }
cMessage * timer
Definition: IPvXTrafGen.h:52
L3Address inet::IPvXTrafGen::chooseDestAddr ( )
protectedvirtual

Referenced by sendPacket().

172 {
173  int k = intrand(destAddresses.size());
174  return destAddresses[k];
175 }
std::vector< L3Address > destAddresses
Definition: IPvXTrafGen.h:47
const double k
Definition: QAM16Modulation.cc:24
void inet::IPvXTrafGen::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
86 {
87  if (!isNodeUp())
88  throw cRuntimeError("Application is not running");
89  if (msg == timer) {
90  if (msg->getKind() == START) {
91  destAddresses.clear();
92  const char *destAddrs = par("destAddresses");
93  cStringTokenizer tokenizer(destAddrs);
94  const char *token;
95  while ((token = tokenizer.nextToken()) != nullptr) {
96  L3Address result;
97  L3AddressResolver().tryResolve(token, result);
98  if (result.isUnspecified())
99  EV_ERROR << "cannot resolve destination address: " << token << endl;
100  else
101  destAddresses.push_back(result);
102  }
103  }
104  if (!destAddresses.empty()) {
105  sendPacket();
106  if (isEnabled())
107  scheduleNextPacket(simTime());
108  }
109  }
110  else
111  processPacket(PK(msg));
112 }
virtual bool isEnabled()
Definition: IPvXTrafGen.cc:166
Definition: IPvXTrafGen.h:39
virtual void sendPacket()
Definition: IPvXTrafGen.cc:177
std::vector< L3Address > destAddresses
Definition: IPvXTrafGen.h:47
virtual bool isNodeUp()
Definition: IPvXTrafGen.cc:161
cMessage * timer
Definition: IPvXTrafGen.h:52
virtual void processPacket(cPacket *msg)
Definition: IPvXTrafGen.cc:220
#define PK(msg)
Definition: INETDefs.h:92
virtual void scheduleNextPacket(simtime_t previous)
Definition: IPvXTrafGen.cc:141
bool inet::IPvXTrafGen::handleOperationStage ( LifecycleOperation operation,
int  stage,
IDoneCallback doneCallback 
)
overrideprotectedvirtual

Perform one stage of a lifecycle operation.

Processing may be done entirely within this method, or may be a longer process that involves nonzero simulation time or several events, and is triggered by this method call.

Return value: true = "done"; false = "not yet done, will invoke doneCallback when done"

Implements inet::ILifecycle.

122 {
123  Enter_Method_Silent();
124  if (dynamic_cast<NodeStartOperation *>(operation)) {
126  startApp();
127  }
128  else if (dynamic_cast<NodeShutdownOperation *>(operation)) {
131  }
132  else if (dynamic_cast<NodeCrashOperation *>(operation)) {
135  }
136  else
137  throw cRuntimeError("Unsupported lifecycle operation '%s'", operation->getClassName());
138  return true;
139 }
virtual void cancelNextPacket()
Definition: IPvXTrafGen.cc:156
Stage
Definition: NodeOperations.h:71
Stage
Definition: NodeOperations.h:126
Stage
Definition: NodeOperations.h:46
virtual void startApp()
Definition: IPvXTrafGen.cc:79
Definition: NodeOperations.h:127
void inet::IPvXTrafGen::initialize ( int  stage)
overrideprotectedvirtual
45 {
46  cSimpleModule::initialize(stage);
47 
48  // because of IPvXAddressResolver, we need to wait until interfaces are registered,
49  // address auto-assignment takes place etc.
50  if (stage == INITSTAGE_LOCAL) {
51  protocol = par("protocol");
52  numPackets = par("numPackets");
53  startTime = par("startTime");
54  stopTime = par("stopTime");
55  if (stopTime >= SIMTIME_ZERO && stopTime < startTime)
56  throw cRuntimeError("Invalid startTime/stopTime parameters");
57 
58  packetLengthPar = &par("packetLength");
59  sendIntervalPar = &par("sendInterval");
60 
61  numSent = 0;
62  numReceived = 0;
63  WATCH(numSent);
64  WATCH(numReceived);
65  }
66  else if (stage == INITSTAGE_APPLICATION_LAYER) {
67  IPSocket ipSocket(gate("ipOut"));
68  ipSocket.registerProtocol(protocol);
69 
70  timer = new cMessage("sendTimer");
71  nodeStatus = dynamic_cast<NodeStatus *>(findContainingNode(this)->getSubmodule("status"));
73 
74  if (isNodeUp())
75  startApp();
76  }
77 }
cPar * sendIntervalPar
Definition: IPvXTrafGen.h:44
int numPackets
Definition: IPvXTrafGen.h:48
virtual bool isNodeUp()
Definition: IPvXTrafGen.cc:161
int numReceived
Definition: IPvXTrafGen.h:57
cModule * findContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:56
int numSent
Definition: IPvXTrafGen.h:56
Local initializations.
Definition: InitStages.h:35
simtime_t startTime
Definition: IPvXTrafGen.h:42
cMessage * timer
Definition: IPvXTrafGen.h:52
int protocol
Definition: IPvXTrafGen.h:46
bool isOperational
Definition: IPvXTrafGen.h:53
virtual void startApp()
Definition: IPvXTrafGen.cc:79
cPar * packetLengthPar
Definition: IPvXTrafGen.h:45
simtime_t stopTime
Definition: IPvXTrafGen.h:43
NodeStatus * nodeStatus
Definition: IPvXTrafGen.h:51
Initialization of applications.
Definition: InitStages.h:106
virtual State getState() const
Definition: NodeStatus.h:48
Definition: NodeStatus.h:40
bool inet::IPvXTrafGen::isEnabled ( )
protectedvirtual

Referenced by handleMessage(), and startApp().

167 {
168  return numPackets == -1 || numSent < numPackets;
169 }
int numPackets
Definition: IPvXTrafGen.h:48
int numSent
Definition: IPvXTrafGen.h:56
bool inet::IPvXTrafGen::isNodeUp ( )
protectedvirtual

Referenced by handleMessage(), and initialize().

162 {
163  return !nodeStatus || nodeStatus->getState() == NodeStatus::UP;
164 }
NodeStatus * nodeStatus
Definition: IPvXTrafGen.h:51
virtual State getState() const
Definition: NodeStatus.h:48
Definition: NodeStatus.h:40
virtual int inet::IPvXTrafGen::numInitStages ( ) const
inlineoverrideprotectedvirtual
70 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::IPvXTrafGen::printPacket ( cPacket *  msg)
protectedvirtual

Referenced by processPacket(), and sendPacket().

202 {
203  L3Address src, dest;
204  int protocol = -1;
205 
206  INetworkProtocolControlInfo *ctrl = dynamic_cast<INetworkProtocolControlInfo *>(msg->getControlInfo());
207  if (ctrl != nullptr) {
208  src = ctrl->getSourceAddress();
209  dest = ctrl->getDestinationAddress();
210  protocol = ctrl->getTransportProtocol();
211  }
212 
213  EV_INFO << msg << endl;
214  EV_INFO << "Payload length: " << msg->getByteLength() << " bytes" << endl;
215 
216  if (protocol != -1)
217  EV_INFO << "src: " << src << " dest: " << dest << " protocol=" << protocol << "\n";
218 }
int protocol
Definition: IPvXTrafGen.h:46
void inet::IPvXTrafGen::processPacket ( cPacket *  msg)
protectedvirtual

Referenced by handleMessage().

221 {
222  emit(rcvdPkSignal, msg);
223  EV_INFO << "Received packet: ";
224  printPacket(msg);
225  delete msg;
226  numReceived++;
227 }
virtual void printPacket(cPacket *msg)
Definition: IPvXTrafGen.cc:201
int numReceived
Definition: IPvXTrafGen.h:57
static simsignal_t rcvdPkSignal
Definition: IPvXTrafGen.h:59
void inet::IPvXTrafGen::refreshDisplay ( ) const
overrideprotectedvirtual
115 {
116  char buf[40];
117  sprintf(buf, "rcvd: %d pks\nsent: %d pks", numReceived, numSent);
118  getDisplayString().setTagArg("t", 0, buf);
119 }
int numReceived
Definition: IPvXTrafGen.h:57
int numSent
Definition: IPvXTrafGen.h:56
void inet::IPvXTrafGen::scheduleNextPacket ( simtime_t  previous)
protectedvirtual

Referenced by handleMessage(), and startApp().

142 {
143  simtime_t next;
144  if (previous == -1) {
145  next = simTime() <= startTime ? startTime : simTime();
146  timer->setKind(START);
147  }
148  else {
149  next = previous + sendIntervalPar->doubleValue();
150  timer->setKind(NEXT);
151  }
152  if (stopTime < SIMTIME_ZERO || next < stopTime)
153  scheduleAt(next, timer);
154 }
Definition: IPvXTrafGen.h:39
cPar * sendIntervalPar
Definition: IPvXTrafGen.h:44
Definition: IPvXTrafGen.h:39
simtime_t startTime
Definition: IPvXTrafGen.h:42
cMessage * timer
Definition: IPvXTrafGen.h:52
simtime_t stopTime
Definition: IPvXTrafGen.h:43
void inet::IPvXTrafGen::sendPacket ( )
protectedvirtual

Referenced by handleMessage().

178 {
179  char msgName[32];
180  sprintf(msgName, "appData-%d", numSent);
181 
182  cPacket *payload = new cPacket(msgName);
183  payload->setByteLength(packetLengthPar->longValue());
184 
185  L3Address destAddr = chooseDestAddr();
186 
187  IL3AddressType *addressType = destAddr.getAddressType();
188  INetworkProtocolControlInfo *controlInfo = addressType->createNetworkProtocolControlInfo();
189  //controlInfo->setSourceAddress();
190  controlInfo->setDestinationAddress(destAddr);
191  controlInfo->setTransportProtocol(protocol);
192  payload->setControlInfo(check_and_cast<cObject *>(controlInfo));
193 
194  EV_INFO << "Sending packet: ";
195  printPacket(payload);
196  emit(sentPkSignal, payload);
197  send(payload, "ipOut");
198  numSent++;
199 }
virtual L3Address chooseDestAddr()
Definition: IPvXTrafGen.cc:171
static simsignal_t sentPkSignal
Definition: IPvXTrafGen.h:58
virtual void printPacket(cPacket *msg)
Definition: IPvXTrafGen.cc:201
int numSent
Definition: IPvXTrafGen.h:56
int protocol
Definition: IPvXTrafGen.h:46
cPar * packetLengthPar
Definition: IPvXTrafGen.h:45
void inet::IPvXTrafGen::startApp ( )
protectedvirtual

Referenced by handleOperationStage(), and initialize().

80 {
81  if (isEnabled())
83 }
virtual bool isEnabled()
Definition: IPvXTrafGen.cc:166
virtual void scheduleNextPacket(simtime_t previous)
Definition: IPvXTrafGen.cc:141

Member Data Documentation

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

Referenced by chooseDestAddr(), and handleMessage().

bool inet::IPvXTrafGen::isOperational = false
protected

Referenced by initialize().

NodeStatus* inet::IPvXTrafGen::nodeStatus = nullptr
protected

Referenced by initialize(), and isNodeUp().

int inet::IPvXTrafGen::numPackets = 0
protected

Referenced by initialize(), and isEnabled().

int inet::IPvXTrafGen::numReceived = 0
protected
int inet::IPvXTrafGen::numSent = 0
protected
cPar* inet::IPvXTrafGen::packetLengthPar = nullptr
protected

Referenced by initialize(), and sendPacket().

int inet::IPvXTrafGen::protocol = -1
protected

Referenced by initialize(), printPacket(), and sendPacket().

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

Referenced by processPacket().

cPar* inet::IPvXTrafGen::sendIntervalPar = nullptr
protected

Referenced by initialize(), and scheduleNextPacket().

simsignal_t inet::IPvXTrafGen::sentPkSignal = registerSignal("sentPk")
staticprotected

Referenced by sendPacket().

simtime_t inet::IPvXTrafGen::startTime
protected

Referenced by initialize(), and scheduleNextPacket().

simtime_t inet::IPvXTrafGen::stopTime
protected

Referenced by initialize(), and scheduleNextPacket().

cMessage* inet::IPvXTrafGen::timer = nullptr
protected

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