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

An example request-reply based client application. More...

#include <TCPBasicClientApp.h>

Inheritance diagram for inet::TCPBasicClientApp:
inet::TCPAppBase inet::ILifecycle inet::TCPSocket::CallbackInterface

Public Member Functions

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

Protected Member Functions

virtual void sendRequest ()
 
virtual void rescheduleOrDeleteTimer (simtime_t d, short int msgKind)
 
virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
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
 
virtual bool isNodeUp ()
 
virtual bool handleOperationStage (LifecycleOperation *operation, int stage, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 
- Protected Member Functions inherited from inet::TCPAppBase
virtual void handleMessage (cMessage *msg) override
 
virtual void finish () override
 
virtual void refreshDisplay () const 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

cMessage * timeoutMsg = nullptr
 
NodeStatusnodeStatus = nullptr
 
bool earlySend = false
 
int numRequestsToSend = 0
 
simtime_t startTime
 
simtime_t stopTime
 
- 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

An example request-reply based client application.

Constructor & Destructor Documentation

inet::TCPBasicClientApp::TCPBasicClientApp ( )
inline
56 {}
inet::TCPBasicClientApp::~TCPBasicClientApp ( )
virtual
32 {
33  cancelAndDelete(timeoutMsg);
34 }
cMessage * timeoutMsg
Definition: TCPBasicClientApp.h:35

Member Function Documentation

bool inet::TCPBasicClientApp::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.

67 {
68  Enter_Method_Silent();
69  if (dynamic_cast<NodeStartOperation *>(operation)) {
71  simtime_t now = simTime();
72  simtime_t start = std::max(startTime, now);
73  if (timeoutMsg && ((stopTime < SIMTIME_ZERO) || (start < stopTime) || (start == stopTime && startTime == stopTime))) {
74  timeoutMsg->setKind(MSGKIND_CONNECT);
75  scheduleAt(start, timeoutMsg);
76  }
77  }
78  }
79  else if (dynamic_cast<NodeShutdownOperation *>(operation)) {
81  cancelEvent(timeoutMsg);
83  close();
84  // TODO: wait until socket is closed
85  }
86  }
87  else if (dynamic_cast<NodeCrashOperation *>(operation)) {
89  cancelEvent(timeoutMsg);
90  }
91  else
92  throw cRuntimeError("Unsupported lifecycle operation '%s'", operation->getClassName());
93  return true;
94 }
TCPSocket socket
Definition: TCPAppBase.h:35
Definition: TCPSocket.h:148
Definition: TCPSocket.h:148
double max(double a, double b)
Returns the greater of the given parameters.
Definition: INETMath.h:161
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
#define MSGKIND_CONNECT
Definition: TCPBasicClientApp.cc:26
Stage
Definition: NodeOperations.h:46
simtime_t startTime
Definition: TCPBasicClientApp.h:39
simtime_t stopTime
Definition: TCPBasicClientApp.h:40
cMessage * timeoutMsg
Definition: TCPBasicClientApp.h:35
Definition: NodeOperations.h:127
void inet::TCPBasicClientApp::handleTimer ( cMessage *  msg)
overrideprotectedvirtual

Implements inet::TCPAppBase.

117 {
118  switch (msg->getKind()) {
119  case MSGKIND_CONNECT:
120  connect(); // active OPEN
121 
122  // significance of earlySend: if true, data will be sent already
123  // in the ACK of SYN, otherwise only in a separate packet (but still
124  // immediately)
125  if (earlySend)
126  sendRequest();
127  break;
128 
129  case MSGKIND_SEND:
130  sendRequest();
132  // no scheduleAt(): next request will be sent when reply to this one
133  // arrives (see socketDataArrived())
134  break;
135 
136  default:
137  throw cRuntimeError("Invalid timer msg: kind=%d", msg->getKind());
138  }
139 }
#define MSGKIND_SEND
Definition: TCPBasicClientApp.cc:27
int numRequestsToSend
Definition: TCPBasicClientApp.h:38
virtual void sendRequest()
Definition: TCPBasicClientApp.cc:96
bool earlySend
Definition: TCPBasicClientApp.h:37
virtual void connect()
Definition: TCPAppBase.cc:62
#define MSGKIND_CONNECT
Definition: TCPBasicClientApp.cc:26
void inet::TCPBasicClientApp::initialize ( int  stage)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

37 {
39  if (stage == INITSTAGE_LOCAL) {
41  earlySend = false; // TBD make it parameter
42  WATCH(numRequestsToSend);
43  WATCH(earlySend);
44 
45  startTime = par("startTime");
46  stopTime = par("stopTime");
47  if (stopTime >= SIMTIME_ZERO && stopTime < startTime)
48  throw cRuntimeError("Invalid startTime/stopTime parameters");
49  }
50  else if (stage == INITSTAGE_APPLICATION_LAYER) {
51  timeoutMsg = new cMessage("timer");
52  nodeStatus = dynamic_cast<NodeStatus *>(findContainingNode(this)->getSubmodule("status"));
53 
54  if (isNodeUp()) {
55  timeoutMsg->setKind(MSGKIND_CONNECT);
56  scheduleAt(startTime, timeoutMsg);
57  }
58  }
59 }
int numRequestsToSend
Definition: TCPBasicClientApp.h:38
cModule * findContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:56
bool earlySend
Definition: TCPBasicClientApp.h:37
Local initializations.
Definition: InitStages.h:35
#define MSGKIND_CONNECT
Definition: TCPBasicClientApp.cc:26
simtime_t startTime
Definition: TCPBasicClientApp.h:39
virtual void initialize(int stage) override
Definition: TCPAppBase.cc:28
simtime_t stopTime
Definition: TCPBasicClientApp.h:40
NodeStatus * nodeStatus
Definition: TCPBasicClientApp.h:36
cMessage * timeoutMsg
Definition: TCPBasicClientApp.h:35
virtual bool isNodeUp()
Definition: TCPBasicClientApp.cc:61
Initialization of applications.
Definition: InitStages.h:106
bool inet::TCPBasicClientApp::isNodeUp ( )
protectedvirtual

Referenced by initialize().

62 {
64 }
NodeStatus * nodeStatus
Definition: TCPBasicClientApp.h:36
virtual State getState() const
Definition: NodeStatus.h:48
Definition: NodeStatus.h:40
virtual int inet::TCPBasicClientApp::numInitStages ( ) const
inlineoverrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

45 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::TCPBasicClientApp::rescheduleOrDeleteTimer ( simtime_t  d,
short int  msgKind 
)
protectedvirtual

Referenced by socketClosed(), socketDataArrived(), and socketFailure().

158 {
159  cancelEvent(timeoutMsg);
160 
161  if (stopTime < SIMTIME_ZERO || d < stopTime) {
162  timeoutMsg->setKind(msgKind);
163  scheduleAt(d, timeoutMsg);
164  }
165  else {
166  delete timeoutMsg;
167  timeoutMsg = nullptr;
168  }
169 }
simtime_t stopTime
Definition: TCPBasicClientApp.h:40
cMessage * timeoutMsg
Definition: TCPBasicClientApp.h:35
void inet::TCPBasicClientApp::sendRequest ( )
protectedvirtual

Referenced by handleTimer(), and socketEstablished().

97 {
98  long requestLength = par("requestLength");
99  long replyLength = par("replyLength");
100  if (requestLength < 1)
101  requestLength = 1;
102  if (replyLength < 1)
103  replyLength = 1;
104 
105  GenericAppMsg *msg = new GenericAppMsg("data");
106  msg->setByteLength(requestLength);
107  msg->setExpectedReplyLength(replyLength);
108  msg->setServerClose(false);
109 
110  EV_INFO << "sending request with " << requestLength << " bytes, expected reply length " << replyLength << " bytes,"
111  << "remaining " << numRequestsToSend - 1 << " request\n";
112 
113  sendPacket(msg);
114 }
int numRequestsToSend
Definition: TCPBasicClientApp.h:38
virtual void sendPacket(cPacket *pkt)
Definition: TCPAppBase.cc:93
void inet::TCPBasicClientApp::socketClosed ( int  connId,
void *  yourPtr 
)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

190 {
192 
193  // start another session after a delay
194  if (timeoutMsg) {
195  simtime_t d = simTime() + (simtime_t)par("idleInterval");
197  }
198 }
az accept haszálja pcb új connId
Definition: lwip_tcp.txt:38
virtual void socketClosed(int connId, void *yourPtr) override
Definition: TCPAppBase.cc:132
#define MSGKIND_CONNECT
Definition: TCPBasicClientApp.cc:26
virtual void rescheduleOrDeleteTimer(simtime_t d, short int msgKind)
Definition: TCPBasicClientApp.cc:157
cMessage * timeoutMsg
Definition: TCPBasicClientApp.h:35
void inet::TCPBasicClientApp::socketDataArrived ( int  connId,
void *  yourPtr,
cPacket *  msg,
bool  urgent 
)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

172 {
173  TCPAppBase::socketDataArrived(connId, ptr, msg, urgent);
174 
175  if (numRequestsToSend > 0) {
176  EV_INFO << "reply arrived\n";
177 
178  if (timeoutMsg) {
179  simtime_t d = simTime() + (simtime_t)par("thinkTime");
181  }
182  }
183  else if (socket.getState() != TCPSocket::LOCALLY_CLOSED) {
184  EV_INFO << "reply to last request arrived, closing session\n";
185  close();
186  }
187 }
#define MSGKIND_SEND
Definition: TCPBasicClientApp.cc:27
TCPSocket socket
Definition: TCPAppBase.h:35
az accept haszálja pcb új connId
Definition: lwip_tcp.txt:38
int numRequestsToSend
Definition: TCPBasicClientApp.h:38
Definition: TCPSocket.h:148
virtual void socketDataArrived(int connId, void *yourPtr, cPacket *msg, bool urgent) override
Definition: TCPAppBase.cc:114
virtual void close()
Definition: TCPAppBase.cc:86
int getState() const
Returns the socket state, one of NOT_BOUND, CLOSED, LISTENING, CONNECTING, CONNECTED, etc.
Definition: TCPSocket.h:204
virtual void rescheduleOrDeleteTimer(simtime_t d, short int msgKind)
Definition: TCPBasicClientApp.cc:157
cMessage * timeoutMsg
Definition: TCPBasicClientApp.h:35
void inet::TCPBasicClientApp::socketEstablished ( int  connId,
void *  yourPtr 
)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

142 {
144 
145  // determine number of requests in this session
146  numRequestsToSend = par("numRequestsPerSession").longValue();
147  if (numRequestsToSend < 1)
148  numRequestsToSend = 1;
149 
150  // perform first request if not already done (next one will be sent when reply arrives)
151  if (!earlySend)
152  sendRequest();
153 
155 }
az accept haszálja pcb új connId
Definition: lwip_tcp.txt:38
int numRequestsToSend
Definition: TCPBasicClientApp.h:38
virtual void socketEstablished(int connId, void *yourPtr) override
Definition: TCPAppBase.cc:108
virtual void sendRequest()
Definition: TCPBasicClientApp.cc:96
bool earlySend
Definition: TCPBasicClientApp.h:37
void inet::TCPBasicClientApp::socketFailure ( int  connId,
void *  yourPtr,
int  code 
)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

201 {
202  TCPAppBase::socketFailure(connId, ptr, code);
203 
204  // reconnect after a delay
205  if (timeoutMsg) {
206  simtime_t d = simTime() + (simtime_t)par("reconnectInterval");
208  }
209 }
az accept haszálja pcb új connId
Definition: lwip_tcp.txt:38
#define MSGKIND_CONNECT
Definition: TCPBasicClientApp.cc:26
virtual void rescheduleOrDeleteTimer(simtime_t d, short int msgKind)
Definition: TCPBasicClientApp.cc:157
virtual void socketFailure(int connId, void *yourPtr, int code) override
Definition: TCPAppBase.cc:138
cMessage * timeoutMsg
Definition: TCPBasicClientApp.h:35

Member Data Documentation

bool inet::TCPBasicClientApp::earlySend = false
protected
NodeStatus* inet::TCPBasicClientApp::nodeStatus = nullptr
protected

Referenced by initialize(), and isNodeUp().

int inet::TCPBasicClientApp::numRequestsToSend = 0
protected
simtime_t inet::TCPBasicClientApp::startTime
protected

Referenced by handleOperationStage(), and initialize().

simtime_t inet::TCPBasicClientApp::stopTime
protected
cMessage* inet::TCPBasicClientApp::timeoutMsg = nullptr
protected

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