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

Accepts any number of incoming connections, and sends back whatever arrives on them. More...

#include <TCPEchoApp.h>

Inheritance diagram for inet::TCPEchoApp:
inet::ILifecycle

Public Member Functions

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

Protected Member Functions

virtual bool isNodeUp ()
 
virtual void sendDown (cMessage *msg)
 
virtual void startListening ()
 
virtual void stopListening ()
 
virtual void initialize (int stage) override
 
virtual int numInitStages () const override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void finish () override
 
virtual void refreshDisplay () const override
 
virtual bool handleOperationStage (LifecycleOperation *operation, int stage, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 

Protected Attributes

simtime_t delay
 
double echoFactor = NaN
 
TCPSocket socket
 
NodeStatusnodeStatus = nullptr
 
long bytesRcvd = 0
 
long bytesSent = 0
 

Static Protected Attributes

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

Detailed Description

Accepts any number of incoming connections, and sends back whatever arrives on them.

Constructor & Destructor Documentation

inet::TCPEchoApp::TCPEchoApp ( )
inline
62 {}

Member Function Documentation

void inet::TCPEchoApp::finish ( )
overrideprotectedvirtual
176 {
177  recordScalar("bytesRcvd", bytesRcvd);
178  recordScalar("bytesSent", bytesSent);
179 }
long bytesSent
Definition: TCPEchoApp.h:43
long bytesRcvd
Definition: TCPEchoApp.h:42
void inet::TCPEchoApp::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
85 {
86  if (!isNodeUp())
87  throw cRuntimeError("Application is not running");
88  if (msg->isSelfMessage()) {
89  sendDown(msg);
90  }
91  else if (msg->getKind() == TCP_I_PEER_CLOSED) {
92  // we'll close too
93  msg->setName("close");
94  msg->setKind(TCP_C_CLOSE);
95 
96  if (delay == 0)
97  sendDown(msg);
98  else
99  scheduleAt(simTime() + delay, msg); // send after a delay
100  }
101  else if (msg->getKind() == TCP_I_DATA || msg->getKind() == TCP_I_URGENT_DATA) {
102  cPacket *pkt = check_and_cast<cPacket *>(msg);
103  emit(rcvdPkSignal, pkt);
104  bytesRcvd += pkt->getByteLength();
105 
106  if (echoFactor == 0) {
107  delete pkt;
108  }
109  else {
110  // reverse direction, modify length, and send it back
111  pkt->setKind(TCP_C_SEND);
112  TCPCommand *ind = check_and_cast<TCPCommand *>(pkt->removeControlInfo());
113  TCPSendCommand *cmd = new TCPSendCommand();
114  cmd->setConnId(ind->getConnId());
115  pkt->setControlInfo(cmd);
116  delete ind;
117 
118  long byteLen = pkt->getByteLength() * echoFactor;
119 
120  if (byteLen < 1)
121  byteLen = 1;
122 
123  pkt->setByteLength(byteLen);
124 
125  RawPacket *baMsg = dynamic_cast<RawPacket *>(pkt);
126 
127  // if (dataTransferMode == TCP_TRANSFER_BYTESTREAM)
128  if (baMsg) {
129  ByteArray& outdata = baMsg->getByteArray();
130  ByteArray indata = outdata;
131  outdata.setDataArraySize(byteLen);
132 
133  for (long i = 0; i < byteLen; i++)
134  outdata.setData(i, indata.getData(i / echoFactor));
135  }
136 
137  if (delay == 0)
138  sendDown(pkt);
139  else
140  scheduleAt(simTime() + delay, pkt); // send after a delay
141  }
142  }
143  else {
144  // some indication -- ignore
145  delete msg;
146  }
147 }
Definition: TCPCommand_m.h:101
simtime_t delay
Definition: TCPEchoApp.h:36
Definition: TCPCommand_m.h:99
Definition: TCPCommand_m.h:63
static simsignal_t rcvdPkSignal
Definition: TCPEchoApp.h:45
long bytesRcvd
Definition: TCPEchoApp.h:42
Definition: TCPCommand_m.h:64
virtual void sendDown(cMessage *msg)
Definition: TCPEchoApp.cc:74
Definition: TCPCommand_m.h:98
double echoFactor
Definition: TCPEchoApp.h:37
virtual bool isNodeUp()
Definition: TCPEchoApp.cc:55
bool inet::TCPEchoApp::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.

157 {
158  Enter_Method_Silent();
159  if (dynamic_cast<NodeStartOperation *>(operation)) {
161  startListening();
162  }
163  else if (dynamic_cast<NodeShutdownOperation *>(operation)) {
165  // TODO: wait until socket is closed
166  stopListening();
167  }
168  else if (dynamic_cast<NodeCrashOperation *>(operation))
169  ;
170  else
171  throw cRuntimeError("Unsupported lifecycle operation '%s'", operation->getClassName());
172  return true;
173 }
virtual void stopListening()
Definition: TCPEchoApp.cc:69
virtual void startListening()
Definition: TCPEchoApp.cc:60
Stage
Definition: NodeOperations.h:71
Stage
Definition: NodeOperations.h:46
void inet::TCPEchoApp::initialize ( int  stage)
overrideprotectedvirtual
33 {
34  cSimpleModule::initialize(stage);
35 
36  if (stage == INITSTAGE_LOCAL) {
37  delay = par("echoDelay");
38  echoFactor = par("echoFactor");
39 
40  bytesRcvd = bytesSent = 0;
41  WATCH(bytesRcvd);
42  WATCH(bytesSent);
43 
44  socket.setOutputGate(gate("tcpOut"));
46 
47  nodeStatus = dynamic_cast<NodeStatus *>(findContainingNode(this)->getSubmodule("status"));
48  }
49  else if (stage == INITSTAGE_APPLICATION_LAYER) {
50  if (isNodeUp())
52  }
53 }
TCPSocket socket
Definition: TCPEchoApp.h:39
virtual void startListening()
Definition: TCPEchoApp.cc:60
long bytesSent
Definition: TCPEchoApp.h:43
simtime_t delay
Definition: TCPEchoApp.h:36
long bytesRcvd
Definition: TCPEchoApp.h:42
cModule * findContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:56
Local initializations.
Definition: InitStages.h:35
void setOutputGate(cGate *toTcp)
Sets the gate on which to send to TCP.
Definition: TCPSocket.h:228
void readDataTransferModePar(cComponent &component)
Read "dataTransferMode" parameter from ini/ned, and set dataTransferMode member value.
Definition: TCPSocket.cc:355
double echoFactor
Definition: TCPEchoApp.h:37
NodeStatus * nodeStatus
Definition: TCPEchoApp.h:40
Initialization of applications.
Definition: InitStages.h:106
virtual bool isNodeUp()
Definition: TCPEchoApp.cc:55
bool inet::TCPEchoApp::isNodeUp ( )
protectedvirtual

Referenced by handleMessage(), and initialize().

56 {
58 }
NodeStatus * nodeStatus
Definition: TCPEchoApp.h:40
virtual State getState() const
Definition: NodeStatus.h:48
Definition: NodeStatus.h:40
virtual int inet::TCPEchoApp::numInitStages ( ) const
inlineoverrideprotectedvirtual
55 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::TCPEchoApp::refreshDisplay ( ) const
overrideprotectedvirtual
150 {
151  char buf[80];
152  sprintf(buf, "rcvd: %ld bytes\nsent: %ld bytes", bytesRcvd, bytesSent);
153  getDisplayString().setTagArg("t", 0, buf);
154 }
long bytesSent
Definition: TCPEchoApp.h:43
long bytesRcvd
Definition: TCPEchoApp.h:42
void inet::TCPEchoApp::sendDown ( cMessage *  msg)
protectedvirtual

Referenced by handleMessage().

75 {
76  if (msg->isPacket()) {
77  bytesSent += ((cPacket *)msg)->getByteLength();
78  emit(sentPkSignal, (cPacket *)msg);
79  }
80 
81  send(msg, "tcpOut");
82 }
long bytesSent
Definition: TCPEchoApp.h:43
static simsignal_t sentPkSignal
Definition: TCPEchoApp.h:46
void inet::TCPEchoApp::startListening ( )
protectedvirtual

Referenced by handleOperationStage(), and initialize().

61 {
62  const char *localAddress = par("localAddress");
63  int localPort = par("localPort");
65  socket.bind(localAddress[0] ? L3Address(localAddress) : L3Address(), localPort);
66  socket.listen();
67 }
TCPSocket socket
Definition: TCPEchoApp.h:39
void bind(int localPort)
Bind the socket to a local port number.
Definition: TCPSocket.cc:101
void listen(bool fork)
Definition: TCPSocket.cc:127
void renewSocket()
Required to re-connect with a "used" TCPSocket object.
Definition: TCPSocket.cc:226
void inet::TCPEchoApp::stopListening ( )
protectedvirtual

Referenced by handleOperationStage().

70 {
71  socket.close();
72 }
TCPSocket socket
Definition: TCPEchoApp.h:39
void close()
Closes the local end of the connection.
Definition: TCPSocket.cc:192

Member Data Documentation

long inet::TCPEchoApp::bytesRcvd = 0
protected
long inet::TCPEchoApp::bytesSent = 0
protected
simtime_t inet::TCPEchoApp::delay
protected

Referenced by handleMessage(), and initialize().

double inet::TCPEchoApp::echoFactor = NaN
protected

Referenced by handleMessage(), and initialize().

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

Referenced by initialize(), and isNodeUp().

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

Referenced by handleMessage().

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

Referenced by sendDown().

TCPSocket inet::TCPEchoApp::socket
protected

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