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

Single-connection TCP application. More...

#include <TCPSessionApp.h>

Inheritance diagram for inet::TCPSessionApp:
inet::TCPAppBase inet::TCPSocket::CallbackInterface inet::PacketDrillApp

Classes

struct  Command
 

Public Member Functions

 TCPSessionApp ()
 
virtual ~TCPSessionApp ()
 
- Public Member Functions inherited from inet::TCPSocket::CallbackInterface
virtual ~CallbackInterface ()
 
virtual void socketDeleted (int connId, void *yourPtr)
 

Protected Types

typedef std::vector< CommandCommandVector
 

Protected Member Functions

virtual bool isNodeUp ()
 
virtual bool handleOperationStage (LifecycleOperation *operation, int stage, IDoneCallback *doneCallback)
 
virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void finish () override
 
virtual void refreshDisplay () const override
 
virtual void parseScript (const char *script)
 
virtual cPacket * createDataPacket (long sendBytes)
 
virtual void sendData ()
 
virtual void handleTimer (cMessage *msg) override
 
virtual void socketEstablished (int connId, void *yourPtr) override
 
virtual void socketDataArrived (int connId, void *yourPtr, cPacket *msg, bool urgent) override
 
virtual void socketClosed (int connId, void *yourPtr) override
 
virtual void socketFailure (int connId, void *yourPtr, int code) override
 
- Protected Member Functions inherited from inet::TCPAppBase
virtual void handleMessage (cMessage *msg) override
 
virtual void connect ()
 
virtual void close ()
 
virtual void sendPacket (cPacket *pkt)
 
virtual void socketPeerClosed (int connId, void *yourPtr) override
 
virtual void socketStatusArrived (int connId, void *yourPtr, TCPStatusInfo *status) override
 

Protected Attributes

CommandVector commands
 
bool activeOpen = false
 
simtime_t tOpen
 
simtime_t tSend
 
simtime_t tClose
 
int sendBytes = 0
 
int commandIndex = -1
 
cMessage * timeoutMsg = nullptr
 
NodeStatusnodeStatus = nullptr
 
- Protected Attributes inherited from inet::TCPAppBase
TCPSocket socket
 
int numSessions
 
int numBroken
 
int packetsSent
 
int packetsRcvd
 
int bytesSent
 
int bytesRcvd
 

Additional Inherited Members

- Static Protected Attributes inherited from inet::TCPAppBase
static simsignal_t connectSignal = registerSignal("connect")
 
static simsignal_t rcvdPkSignal = registerSignal("rcvdPk")
 
static simsignal_t sentPkSignal = registerSignal("sentPk")
 

Detailed Description

Single-connection TCP application.

Member Typedef Documentation

typedef std::vector<Command> inet::TCPSessionApp::CommandVector
protected

Constructor & Destructor Documentation

inet::TCPSessionApp::TCPSessionApp ( )
inline
76 {}
inet::TCPSessionApp::~TCPSessionApp ( )
virtual
35 {
36  cancelAndDelete(timeoutMsg);
37 }
cMessage * timeoutMsg
Definition: TCPSessionApp.h:53

Member Function Documentation

cPacket * inet::TCPSessionApp::createDataPacket ( long  sendBytes)
protectedvirtual

Referenced by sendData().

146 {
147  switch (socket.getDataTransferMode()) {
149  case TCP_TRANSFER_OBJECT: {
150  cPacket *msg = nullptr;
151  msg = new cPacket("data1");
152  msg->setByteLength(sendBytes);
153  return msg;
154  }
155 
157  RawPacket *msg = new RawPacket("data1");
158  unsigned char *ptr = new unsigned char[sendBytes];
159 
160  for (int i = 0; i < sendBytes; i++)
161  ptr[i] = (bytesSent + i) & 0xFF;
162 
163  msg->getByteArray().assignBuffer(ptr, sendBytes);
164  msg->setByteLength(sendBytes);
165  return msg;
166  }
167 
168  default:
169  throw cRuntimeError("Invalid TCP data transfer mode: %d", socket.getDataTransferMode());
170  }
171 }
Definition: TCPCommand_m.h:255
TCPSocket socket
Definition: TCPAppBase.h:35
int bytesSent
Definition: TCPAppBase.h:42
Definition: TCPCommand_m.h:257
int sendBytes
Definition: TCPSessionApp.h:49
TCPDataTransferMode getDataTransferMode() const
Returns the current dataTransferMode parameter.
Definition: TCPSocket.h:245
Definition: TCPCommand_m.h:256
void inet::TCPSessionApp::finish ( )
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

Reimplemented in inet::PacketDrillApp.

254 {
255  EV << getFullPath() << ": received " << bytesRcvd << " bytes in " << packetsRcvd << " packets\n";
256  recordScalar("bytesRcvd", bytesRcvd);
257  recordScalar("bytesSent", bytesSent);
258 }
int bytesSent
Definition: TCPAppBase.h:42
int bytesRcvd
Definition: TCPAppBase.h:43
int packetsRcvd
Definition: TCPAppBase.h:41
bool inet::TCPSessionApp::handleOperationStage ( LifecycleOperation operation,
int  stage,
IDoneCallback doneCallback 
)
protectedvirtual

Reimplemented in inet::PacketDrillApp.

79 {
80  Enter_Method_Silent();
81  if (dynamic_cast<NodeStartOperation *>(operation)) {
83  if (simTime() <= tOpen) {
84  timeoutMsg->setKind(MSGKIND_CONNECT);
85  scheduleAt(tOpen, timeoutMsg);
86  }
87  }
88  }
89  else if (dynamic_cast<NodeShutdownOperation *>(operation)) {
91  cancelEvent(timeoutMsg);
93  close();
94  // TODO: wait until socket is closed
95  }
96  }
97  else if (dynamic_cast<NodeCrashOperation *>(operation)) {
99  cancelEvent(timeoutMsg);
100  }
101  else
102  throw cRuntimeError("Unsupported lifecycle operation '%s'", operation->getClassName());
103  return true;
104 }
TCPSocket socket
Definition: TCPAppBase.h:35
cMessage * timeoutMsg
Definition: TCPSessionApp.h:53
simtime_t tOpen
Definition: TCPSessionApp.h:46
Definition: TCPSocket.h:148
Definition: TCPSocket.h:148
#define MSGKIND_CONNECT
Definition: TCPSessionApp.cc:29
Definition: TCPSocket.h:148
virtual void close()
Definition: TCPAppBase.cc:86
Stage
Definition: NodeOperations.h:71
Stage
Definition: NodeOperations.h:126
int getState() const
Returns the socket state, one of NOT_BOUND, CLOSED, LISTENING, CONNECTING, CONNECTED, etc.
Definition: TCPSocket.h:204
Stage
Definition: NodeOperations.h:46
Definition: NodeOperations.h:127
void inet::TCPSessionApp::handleTimer ( cMessage *  msg)
overrideprotectedvirtual

Implements inet::TCPAppBase.

Reimplemented in inet::PacketDrillApp.

107 {
108  switch (msg->getKind()) {
109  case MSGKIND_CONNECT:
110  if (activeOpen)
111  connect(); // sending will be scheduled from socketEstablished()
112  else
113  ; //TODO
114  break;
115 
116  case MSGKIND_SEND:
117  sendData();
118  break;
119 
120  case MSGKIND_CLOSE:
121  close();
122  break;
123 
124  default:
125  throw cRuntimeError("Invalid timer msg: kind=%d", msg->getKind());
126  }
127 }
#define MSGKIND_CLOSE
Definition: TCPSessionApp.cc:31
#define MSGKIND_CONNECT
Definition: TCPSessionApp.cc:29
#define MSGKIND_SEND
Definition: TCPSessionApp.cc:30
virtual void close()
Definition: TCPAppBase.cc:86
bool activeOpen
Definition: TCPSessionApp.h:45
virtual void connect()
Definition: TCPAppBase.cc:62
virtual void sendData()
Definition: TCPSessionApp.cc:129
void inet::TCPSessionApp::initialize ( int  stage)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

Reimplemented in inet::PacketDrillApp.

40 {
42  if (stage == INITSTAGE_LOCAL) {
43  activeOpen = par("active");
44  tOpen = par("tOpen");
45  tSend = par("tSend");
46  tClose = par("tClose");
47  sendBytes = par("sendBytes");
48  commandIndex = 0;
49 
51 
52  const char *script = par("sendScript");
53  parseScript(script);
54 
55  if (sendBytes > 0 && commands.size() > 0)
56  throw cRuntimeError("Cannot use both sendScript and tSend+sendBytes");
57  if (sendBytes > 0)
58  commands.push_back(Command(tSend, sendBytes));
59  if (commands.size() == 0)
60  throw cRuntimeError("sendScript is empty");
61  }
62  else if (stage == INITSTAGE_APPLICATION_LAYER) {
63  timeoutMsg = new cMessage("timer");
64  nodeStatus = dynamic_cast<NodeStatus *>(findContainingNode(this)->getSubmodule("status"));
65 
66  if (isNodeUp()) {
67  timeoutMsg->setKind(MSGKIND_CONNECT);
68  scheduleAt(tOpen, timeoutMsg);
69  }
70  }
71 }
int commandIndex
Definition: TCPSessionApp.h:52
TCPSocket socket
Definition: TCPAppBase.h:35
cMessage * timeoutMsg
Definition: TCPSessionApp.h:53
simtime_t tOpen
Definition: TCPSessionApp.h:46
#define MSGKIND_CONNECT
Definition: TCPSessionApp.cc:29
int sendBytes
Definition: TCPSessionApp.h:49
simtime_t tSend
Definition: TCPSessionApp.h:47
cModule * findContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:56
Local initializations.
Definition: InitStages.h:35
simtime_t tClose
Definition: TCPSessionApp.h:48
bool activeOpen
Definition: TCPSessionApp.h:45
virtual void initialize(int stage) override
Definition: TCPAppBase.cc:28
void readDataTransferModePar(cComponent &component)
Read "dataTransferMode" parameter from ini/ned, and set dataTransferMode member value.
Definition: TCPSocket.cc:355
NodeStatus * nodeStatus
Definition: TCPSessionApp.h:54
virtual bool isNodeUp()
Definition: TCPSessionApp.cc:73
CommandVector commands
Definition: TCPSessionApp.h:43
Initialization of applications.
Definition: InitStages.h:106
virtual void parseScript(const char *script)
Definition: TCPSessionApp.cc:200
bool inet::TCPSessionApp::isNodeUp ( )
protectedvirtual

Referenced by initialize().

74 {
76 }
NodeStatus * nodeStatus
Definition: TCPSessionApp.h:54
virtual State getState() const
Definition: NodeStatus.h:48
Definition: NodeStatus.h:40
virtual int inet::TCPSessionApp::numInitStages ( ) const
inlineoverrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

Reimplemented in inet::PacketDrillApp.

60 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::TCPSessionApp::parseScript ( const char *  script)
protectedvirtual

Referenced by initialize().

201 {
202  const char *s = script;
203 
204  EV_DEBUG << "parse script \"" << script << "\"\n";
205  while (*s) {
206  // parse time
207  while (isspace(*s))
208  s++;
209 
210  if (!*s || *s == ';')
211  break;
212 
213  const char *s0 = s;
214  simtime_t tSend = strtod(s, &const_cast<char *&>(s));
215 
216  if (s == s0)
217  throw cRuntimeError("Syntax error in script: simulation time expected");
218 
219  // parse number of bytes
220  while (isspace(*s))
221  s++;
222 
223  if (!isdigit(*s))
224  throw cRuntimeError("Syntax error in script: number of bytes expected");
225 
226  long numBytes = strtol(s, nullptr, 10);
227 
228  while (isdigit(*s))
229  s++;
230 
231  // add command
232  EV_DEBUG << " add command (" << tSend << "s, " << "B)\n";
233  commands.push_back(Command(tSend, numBytes));
234 
235  // skip delimiter
236  while (isspace(*s))
237  s++;
238 
239  if (!*s)
240  break;
241 
242  if (*s != ';')
243  throw cRuntimeError("Syntax error in script: separator ';' missing");
244 
245  s++;
246 
247  while (isspace(*s))
248  s++;
249  }
250  EV_DEBUG << "parser finished\n";
251 }
simtime_t tSend
Definition: TCPSessionApp.h:47
value< double, units::s > s
Definition: Units.h:1049
CommandVector commands
Definition: TCPSessionApp.h:43
void inet::TCPSessionApp::refreshDisplay ( ) const
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

261 {
262  std::ostringstream os;
263  os << TCPSocket::stateName(socket.getState()) << "\nsent: " << bytesSent << " bytes\nrcvd: " << bytesRcvd << " bytes";
264  getDisplayString().setTagArg("t", 0, os.str().c_str());
265 }
TCPSocket socket
Definition: TCPAppBase.h:35
int bytesSent
Definition: TCPAppBase.h:42
int bytesRcvd
Definition: TCPAppBase.h:43
static const char * stateName(int state)
Returns name of socket state code returned by getState().
Definition: TCPSocket.cc:73
int getState() const
Returns the socket state, one of NOT_BOUND, CLOSED, LISTENING, CONNECTING, CONNECTED, etc.
Definition: TCPSocket.h:204
void inet::TCPSessionApp::sendData ( )
protectedvirtual

Referenced by handleTimer().

130 {
131  long numBytes = commands[commandIndex].numBytes;
132  EV_INFO << "sending data with " << numBytes << " bytes\n";
133  sendPacket(createDataPacket(numBytes));
134 
135  if (++commandIndex < (int)commands.size()) {
136  simtime_t tSend = commands[commandIndex].tSend;
137  scheduleAt(std::max(tSend, simTime()), timeoutMsg);
138  }
139  else {
140  timeoutMsg->setKind(MSGKIND_CLOSE);
141  scheduleAt(std::max(tClose, simTime()), timeoutMsg);
142  }
143 }
int commandIndex
Definition: TCPSessionApp.h:52
cMessage * timeoutMsg
Definition: TCPSessionApp.h:53
virtual cPacket * createDataPacket(long sendBytes)
Definition: TCPSessionApp.cc:145
#define MSGKIND_CLOSE
Definition: TCPSessionApp.cc:31
double max(double a, double b)
Returns the greater of the given parameters.
Definition: INETMath.h:161
simtime_t tSend
Definition: TCPSessionApp.h:47
virtual void sendPacket(cPacket *pkt)
Definition: TCPAppBase.cc:93
simtime_t tClose
Definition: TCPSessionApp.h:48
CommandVector commands
Definition: TCPSessionApp.h:43
void inet::TCPSessionApp::socketClosed ( int  connId,
void *  yourPtr 
)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

189 {
191  cancelEvent(timeoutMsg);
192 }
az accept haszálja pcb új connId
Definition: lwip_tcp.txt:38
cMessage * timeoutMsg
Definition: TCPSessionApp.h:53
virtual void socketClosed(int connId, void *yourPtr) override
Definition: TCPAppBase.cc:132
void inet::TCPSessionApp::socketDataArrived ( int  connId,
void *  yourPtr,
cPacket *  msg,
bool  urgent 
)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

184 {
185  TCPAppBase::socketDataArrived(connId, ptr, msg, urgent);
186 }
az accept haszálja pcb új connId
Definition: lwip_tcp.txt:38
virtual void socketDataArrived(int connId, void *yourPtr, cPacket *msg, bool urgent) override
Definition: TCPAppBase.cc:114
void inet::TCPSessionApp::socketEstablished ( int  connId,
void *  yourPtr 
)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

174 {
176 
177  ASSERT(commandIndex == 0);
178  timeoutMsg->setKind(MSGKIND_SEND);
179  simtime_t tSend = commands[commandIndex].tSend;
180  scheduleAt(std::max(tSend, simTime()), timeoutMsg);
181 }
int commandIndex
Definition: TCPSessionApp.h:52
az accept haszálja pcb új connId
Definition: lwip_tcp.txt:38
cMessage * timeoutMsg
Definition: TCPSessionApp.h:53
virtual void socketEstablished(int connId, void *yourPtr) override
Definition: TCPAppBase.cc:108
double max(double a, double b)
Returns the greater of the given parameters.
Definition: INETMath.h:161
#define MSGKIND_SEND
Definition: TCPSessionApp.cc:30
simtime_t tSend
Definition: TCPSessionApp.h:47
CommandVector commands
Definition: TCPSessionApp.h:43
void inet::TCPSessionApp::socketFailure ( int  connId,
void *  yourPtr,
int  code 
)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

195 {
196  TCPAppBase::socketFailure(connId, ptr, code);
197  cancelEvent(timeoutMsg);
198 }
az accept haszálja pcb új connId
Definition: lwip_tcp.txt:38
cMessage * timeoutMsg
Definition: TCPSessionApp.h:53
virtual void socketFailure(int connId, void *yourPtr, int code) override
Definition: TCPAppBase.cc:138

Member Data Documentation

bool inet::TCPSessionApp::activeOpen = false
protected

Referenced by handleTimer(), and initialize().

int inet::TCPSessionApp::commandIndex = -1
protected
CommandVector inet::TCPSessionApp::commands
protected
NodeStatus* inet::TCPSessionApp::nodeStatus = nullptr
protected

Referenced by initialize(), and isNodeUp().

int inet::TCPSessionApp::sendBytes = 0
protected

Referenced by createDataPacket(), and initialize().

simtime_t inet::TCPSessionApp::tClose
protected

Referenced by initialize(), and sendData().

cMessage* inet::TCPSessionApp::timeoutMsg = nullptr
protected
simtime_t inet::TCPSessionApp::tOpen
protected

Referenced by handleOperationStage(), and initialize().

simtime_t inet::TCPSessionApp::tSend
protected

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