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

Simple traffic generator for the Ethernet model. More...

#include <EtherTrafGen.h>

Inheritance diagram for inet::EtherTrafGen:
inet::ILifecycle

Public Member Functions

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

Protected Types

enum  Kinds { START = 100, NEXT }
 

Protected Member Functions

virtual void initialize (int stage) override
 
virtual int numInitStages () const override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void finish () override
 
virtual bool isNodeUp ()
 
virtual bool isGenerator ()
 
virtual void scheduleNextPacket (simtime_t previous)
 
virtual void cancelNextPacket ()
 
virtual MACAddress resolveDestMACAddress ()
 
virtual void sendBurstPackets ()
 
virtual void receivePacket (cPacket *msg)
 
virtual bool handleOperationStage (LifecycleOperation *operation, int stage, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 

Protected Attributes

long seqNum = 0
 
cPar * sendInterval = nullptr
 
cPar * numPacketsPerBurst = nullptr
 
cPar * packetLength = nullptr
 
int etherType = 0
 
MACAddress destMACAddress
 
NodeStatusnodeStatus = nullptr
 
cMessage * timerMsg = nullptr
 
simtime_t startTime
 
simtime_t stopTime
 
long packetsSent = 0
 
long packetsReceived = 0
 

Static Protected Attributes

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

Detailed Description

Simple traffic generator for the Ethernet model.

Member Enumeration Documentation

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

Constructor & Destructor Documentation

inet::EtherTrafGen::EtherTrafGen ( )
36 {
37  sendInterval = nullptr;
38  numPacketsPerBurst = nullptr;
39  packetLength = nullptr;
40  timerMsg = nullptr;
41  nodeStatus = nullptr;
42 }
cPar * sendInterval
Definition: EtherTrafGen.h:40
cMessage * timerMsg
Definition: EtherTrafGen.h:48
NodeStatus * nodeStatus
Definition: EtherTrafGen.h:45
cPar * numPacketsPerBurst
Definition: EtherTrafGen.h:41
cPar * packetLength
Definition: EtherTrafGen.h:42
inet::EtherTrafGen::~EtherTrafGen ( )
virtual
45 {
46  cancelAndDelete(timerMsg);
47 }
cMessage * timerMsg
Definition: EtherTrafGen.h:48

Member Function Documentation

void inet::EtherTrafGen::cancelNextPacket ( )
protectedvirtual

Referenced by handleOperationStage().

146 {
147  cancelEvent(timerMsg);
148 }
cMessage * timerMsg
Definition: EtherTrafGen.h:48
void inet::EtherTrafGen::finish ( )
overrideprotectedvirtual
207 {
208  cancelAndDelete(timerMsg);
209  timerMsg = nullptr;
210 }
cMessage * timerMsg
Definition: EtherTrafGen.h:48
void inet::EtherTrafGen::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
83 {
84  if (!isNodeUp())
85  throw cRuntimeError("Application is not running");
86  if (msg->isSelfMessage()) {
87  if (msg->getKind() == START) {
89  // if no dest address given, nothing to do
91  return;
92  }
94  scheduleNextPacket(simTime());
95  }
96  else
97  receivePacket(check_and_cast<cPacket *>(msg));
98 }
virtual MACAddress resolveDestMACAddress()
Definition: EtherTrafGen.cc:150
MACAddress destMACAddress
Definition: EtherTrafGen.h:44
bool isUnspecified() const
Returns true if all address bytes are zero.
Definition: MACAddress.h:151
Definition: EtherTrafGen.h:35
virtual void sendBurstPackets()
Definition: EtherTrafGen.cc:171
virtual void scheduleNextPacket(simtime_t previous)
Definition: EtherTrafGen.cc:130
virtual bool isNodeUp()
Definition: EtherTrafGen.cc:120
virtual void receivePacket(cPacket *msg)
Definition: EtherTrafGen.cc:197
bool inet::EtherTrafGen::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.

101 {
102  Enter_Method_Silent();
103  if (dynamic_cast<NodeStartOperation *>(operation)) {
105  scheduleNextPacket(-1);
106  }
107  else if (dynamic_cast<NodeShutdownOperation *>(operation)) {
110  }
111  else if (dynamic_cast<NodeCrashOperation *>(operation)) {
114  }
115  else
116  throw cRuntimeError("Unsupported lifecycle operation '%s'", operation->getClassName());
117  return true;
118 }
Stage
Definition: NodeOperations.h:71
Stage
Definition: NodeOperations.h:126
virtual void cancelNextPacket()
Definition: EtherTrafGen.cc:145
virtual bool isGenerator()
Definition: EtherTrafGen.cc:125
virtual void scheduleNextPacket(simtime_t previous)
Definition: EtherTrafGen.cc:130
Stage
Definition: NodeOperations.h:46
Definition: NodeOperations.h:127
void inet::EtherTrafGen::initialize ( int  stage)
overrideprotectedvirtual
50 {
51  cSimpleModule::initialize(stage);
52 
53  if (stage == INITSTAGE_LOCAL) {
54  sendInterval = &par("sendInterval");
55  numPacketsPerBurst = &par("numPacketsPerBurst");
56  packetLength = &par("packetLength");
57  etherType = par("etherType");
58 
59  seqNum = 0;
60  WATCH(seqNum);
61 
62  // statistics
64  WATCH(packetsSent);
65  WATCH(packetsReceived);
66 
67  startTime = par("startTime");
68  stopTime = par("stopTime");
69  if (stopTime >= SIMTIME_ZERO && stopTime < startTime)
70  throw cRuntimeError("Invalid startTime/stopTime parameters");
71  }
72  else if (stage == INITSTAGE_APPLICATION_LAYER) {
73  if (isGenerator())
74  timerMsg = new cMessage("generateNextPacket");
75 
76  nodeStatus = dynamic_cast<NodeStatus *>(findContainingNode(this)->getSubmodule("status"));
77  if (isNodeUp() && isGenerator())
79  }
80 }
cPar * sendInterval
Definition: EtherTrafGen.h:40
cMessage * timerMsg
Definition: EtherTrafGen.h:48
long packetsReceived
Definition: EtherTrafGen.h:54
NodeStatus * nodeStatus
Definition: EtherTrafGen.h:45
int etherType
Definition: EtherTrafGen.h:43
cModule * findContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:56
Local initializations.
Definition: InitStages.h:35
long packetsSent
Definition: EtherTrafGen.h:53
virtual bool isGenerator()
Definition: EtherTrafGen.cc:125
long seqNum
Definition: EtherTrafGen.h:37
simtime_t stopTime
Definition: EtherTrafGen.h:50
virtual void scheduleNextPacket(simtime_t previous)
Definition: EtherTrafGen.cc:130
simtime_t startTime
Definition: EtherTrafGen.h:49
virtual bool isNodeUp()
Definition: EtherTrafGen.cc:120
cPar * numPacketsPerBurst
Definition: EtherTrafGen.h:41
cPar * packetLength
Definition: EtherTrafGen.h:42
Initialization of applications.
Definition: InitStages.h:106
bool inet::EtherTrafGen::isGenerator ( )
protectedvirtual

Referenced by handleOperationStage(), and initialize().

126 {
127  return par("destAddress").stringValue()[0];
128 }
bool inet::EtherTrafGen::isNodeUp ( )
protectedvirtual

Referenced by handleMessage(), and initialize().

121 {
122  return !nodeStatus || nodeStatus->getState() == NodeStatus::UP;
123 }
NodeStatus * nodeStatus
Definition: EtherTrafGen.h:45
virtual State getState() const
Definition: NodeStatus.h:48
Definition: NodeStatus.h:40
virtual int inet::EtherTrafGen::numInitStages ( ) const
inlineoverrideprotectedvirtual
60 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::EtherTrafGen::receivePacket ( cPacket *  msg)
protectedvirtual

Referenced by handleMessage().

198 {
199  EV_INFO << "Received packet `" << msg->getName() << "' length= " << msg->getByteLength() << "B\n";
200 
201  packetsReceived++;
202  emit(rcvdPkSignal, msg);
203  delete msg;
204 }
long packetsReceived
Definition: EtherTrafGen.h:54
static simsignal_t rcvdPkSignal
Definition: EtherTrafGen.h:56
MACAddress inet::EtherTrafGen::resolveDestMACAddress ( )
protectedvirtual

Referenced by handleMessage().

151 {
152  MACAddress destMACAddress;
153  const char *destAddress = par("destAddress");
154  if (destAddress[0]) {
155  // try as mac address first, then as a module
156  if (!destMACAddress.tryParse(destAddress)) {
157  cModule *destStation = getModuleByPath(destAddress);
158  if (!destStation)
159  throw cRuntimeError("cannot resolve MAC address '%s': not a 12-hex-digit MAC address or a valid module path name", destAddress);
160 
161  cModule *destMAC = destStation->getSubmodule("mac");
162  if (!destMAC)
163  throw cRuntimeError("module '%s' has no 'mac' submodule", destAddress);
164 
165  destMACAddress.setAddress(destMAC->par("address"));
166  }
167  }
168  return destMACAddress;
169 }
MACAddress destMACAddress
Definition: EtherTrafGen.h:44
void inet::EtherTrafGen::scheduleNextPacket ( simtime_t  previous)
protectedvirtual

Referenced by handleMessage(), handleOperationStage(), and initialize().

131 {
132  simtime_t next;
133  if (previous == -1) {
134  next = simTime() <= startTime ? startTime : simTime();
135  timerMsg->setKind(START);
136  }
137  else {
138  next = previous + sendInterval->doubleValue();
139  timerMsg->setKind(NEXT);
140  }
141  if (stopTime < SIMTIME_ZERO || next < stopTime)
142  scheduleAt(next, timerMsg);
143 }
cPar * sendInterval
Definition: EtherTrafGen.h:40
cMessage * timerMsg
Definition: EtherTrafGen.h:48
Definition: EtherTrafGen.h:35
Definition: EtherTrafGen.h:35
simtime_t stopTime
Definition: EtherTrafGen.h:50
simtime_t startTime
Definition: EtherTrafGen.h:49
void inet::EtherTrafGen::sendBurstPackets ( )
protectedvirtual

Referenced by handleMessage().

172 {
173  int n = numPacketsPerBurst->longValue();
174  for (int i = 0; i < n; i++) {
175  seqNum++;
176 
177  char msgname[40];
178  sprintf(msgname, "pk-%d-%ld", getId(), seqNum);
179 
180  cPacket *datapacket = new cPacket(msgname, IEEE802CTRL_DATA);
181 
182  long len = packetLength->longValue();
183  datapacket->setByteLength(len);
184 
185  Ieee802Ctrl *etherctrl = new Ieee802Ctrl();
186  etherctrl->setEtherType(etherType);
187  etherctrl->setDest(destMACAddress);
188  datapacket->setControlInfo(etherctrl);
189 
190  EV_INFO << "Send packet `" << msgname << "' dest=" << destMACAddress << " length=" << len << "B type=" << etherType << "\n";
191  emit(sentPkSignal, datapacket);
192  send(datapacket, "out");
193  packetsSent++;
194  }
195 }
Definition: Ieee802Ctrl_m.h:54
MACAddress destMACAddress
Definition: EtherTrafGen.h:44
uint16_t len
Definition: TCP_NSC.cc:85
int etherType
Definition: EtherTrafGen.h:43
static simsignal_t sentPkSignal
Definition: EtherTrafGen.h:55
long packetsSent
Definition: EtherTrafGen.h:53
long seqNum
Definition: EtherTrafGen.h:37
cPar * numPacketsPerBurst
Definition: EtherTrafGen.h:41
cPar * packetLength
Definition: EtherTrafGen.h:42

Member Data Documentation

MACAddress inet::EtherTrafGen::destMACAddress
protected
int inet::EtherTrafGen::etherType = 0
protected

Referenced by initialize(), and sendBurstPackets().

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

Referenced by EtherTrafGen(), initialize(), and isNodeUp().

cPar* inet::EtherTrafGen::numPacketsPerBurst = nullptr
protected
cPar* inet::EtherTrafGen::packetLength = nullptr
protected
long inet::EtherTrafGen::packetsReceived = 0
protected

Referenced by initialize(), and receivePacket().

long inet::EtherTrafGen::packetsSent = 0
protected

Referenced by initialize(), and sendBurstPackets().

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

Referenced by receivePacket().

cPar* inet::EtherTrafGen::sendInterval = nullptr
protected
simsignal_t inet::EtherTrafGen::sentPkSignal = registerSignal("sentPk")
staticprotected

Referenced by sendBurstPackets().

long inet::EtherTrafGen::seqNum = 0
protected

Referenced by initialize(), and sendBurstPackets().

simtime_t inet::EtherTrafGen::startTime
protected

Referenced by initialize(), and scheduleNextPacket().

simtime_t inet::EtherTrafGen::stopTime
protected

Referenced by initialize(), and scheduleNextPacket().

cMessage* inet::EtherTrafGen::timerMsg = nullptr
protected

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