INET Framework for OMNeT++/OMNEST
inet::TCPAppBase Class Referenceabstract

Base class for clients app for TCP-based request-reply protocols or apps. More...

#include <TCPAppBase.h>

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

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 void refreshDisplay () const override
 
virtual void connect ()
 
virtual void close ()
 
virtual void sendPacket (cPacket *pkt)
 
virtual void handleTimer (cMessage *msg)=0
 
virtual void socketEstablished (int connId, void *yourPtr) override
 
virtual void socketDataArrived (int connId, void *yourPtr, cPacket *msg, bool urgent) override
 
virtual void socketPeerClosed (int connId, void *yourPtr) override
 
virtual void socketClosed (int connId, void *yourPtr) override
 
virtual void socketFailure (int connId, void *yourPtr, int code) override
 
virtual void socketStatusArrived (int connId, void *yourPtr, TCPStatusInfo *status) override
 

Protected Attributes

TCPSocket socket
 
int numSessions
 
int numBroken
 
int packetsSent
 
int packetsRcvd
 
int bytesSent
 
int bytesRcvd
 

Static Protected Attributes

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

Additional Inherited Members

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

Detailed Description

Base class for clients app for TCP-based request-reply protocols or apps.

Handles a single session (and TCP connection) at a time.

It needs the following NED parameters: localAddress, localPort, connectAddress, connectPort.

Member Function Documentation

void inet::TCPAppBase::close ( )
protectedvirtual

Referenced by inet::TelnetApp::handleOperationStage(), inet::TCPBasicClientApp::handleOperationStage(), inet::TCPSessionApp::handleOperationStage(), inet::TelnetApp::handleTimer(), inet::TCPSessionApp::handleTimer(), inet::TCPBasicClientApp::socketDataArrived(), and socketPeerClosed().

87 {
88  EV_INFO << "issuing CLOSE command\n";
89  socket.close();
90  emit(connectSignal, -1L);
91 }
static simsignal_t connectSignal
Definition: TCPAppBase.h:46
TCPSocket socket
Definition: TCPAppBase.h:35
void close()
Closes the local end of the connection.
Definition: TCPSocket.cc:192
void inet::TCPAppBase::connect ( )
protectedvirtual

Referenced by inet::TelnetApp::handleTimer(), inet::TCPBasicClientApp::handleTimer(), and inet::TCPSessionApp::handleTimer().

63 {
64  // we need a new connId if this is not the first connection
66 
67  // connect
68  const char *connectAddress = par("connectAddress");
69  int connectPort = par("connectPort");
70 
71  L3Address destination;
72  L3AddressResolver().tryResolve(connectAddress, destination);
73  if (destination.isUnspecified()) {
74  EV_ERROR << "Connecting to " << connectAddress << " port=" << connectPort << ": cannot resolve destination address\n";
75  }
76  else {
77  EV_INFO << "Connecting to " << connectAddress << "(" << destination << ") port=" << connectPort << endl;
78 
79  socket.connect(destination, connectPort);
80 
81  numSessions++;
82  emit(connectSignal, 1L);
83  }
84 }
static simsignal_t connectSignal
Definition: TCPAppBase.h:46
TCPSocket socket
Definition: TCPAppBase.h:35
int numSessions
Definition: TCPAppBase.h:38
void connect(L3Address remoteAddr, int remotePort)
Active OPEN to the given remote socket.
Definition: TCPSocket.cc:148
void renewSocket()
Required to re-connect with a "used" TCPSocket object.
Definition: TCPSocket.cc:226
void inet::TCPAppBase::finish ( )
overrideprotectedvirtual

Reimplemented in inet::PacketDrillApp, and inet::TCPSessionApp.

146 {
147  std::string modulePath = getFullPath();
148 
149  EV_INFO << modulePath << ": opened " << numSessions << " sessions\n";
150  EV_INFO << modulePath << ": sent " << bytesSent << " bytes in " << packetsSent << " packets\n";
151  EV_INFO << modulePath << ": received " << bytesRcvd << " bytes in " << packetsRcvd << " packets\n";
152 }
int bytesSent
Definition: TCPAppBase.h:42
int bytesRcvd
Definition: TCPAppBase.h:43
int numSessions
Definition: TCPAppBase.h:38
int packetsRcvd
Definition: TCPAppBase.h:41
int packetsSent
Definition: TCPAppBase.h:40
void inet::TCPAppBase::handleMessage ( cMessage *  msg)
overrideprotectedvirtual

Reimplemented in inet::PacketDrillApp.

55 {
56  if (msg->isSelfMessage())
57  handleTimer(msg);
58  else
60 }
TCPSocket socket
Definition: TCPAppBase.h:35
virtual void handleTimer(cMessage *msg)=0
void processMessage(cMessage *msg)
Examines the message (which should have arrived from TCP), updates socket state, and if there is a ca...
Definition: TCPSocket.cc:251
virtual void inet::TCPAppBase::handleTimer ( cMessage *  msg)
protectedpure virtual
void inet::TCPAppBase::initialize ( int  stage)
overrideprotectedvirtual

Reimplemented in inet::PacketDrillApp, inet::TCPSessionApp, inet::TCPBasicClientApp, and inet::TelnetApp.

Referenced by inet::TelnetApp::initialize(), inet::TCPBasicClientApp::initialize(), and inet::TCPSessionApp::initialize().

29 {
30  cSimpleModule::initialize(stage);
31 
32  if (stage == INITSTAGE_LOCAL) {
34 
35  WATCH(numSessions);
36  WATCH(numBroken);
37  WATCH(packetsSent);
38  WATCH(packetsRcvd);
39  WATCH(bytesSent);
40  WATCH(bytesRcvd);
41  }
42  else if (stage == INITSTAGE_APPLICATION_LAYER) {
43  // parameters
44  const char *localAddress = par("localAddress");
45  int localPort = par("localPort");
47  socket.bind(*localAddress ? L3AddressResolver().resolve(localAddress) : L3Address(), localPort);
48 
50  socket.setOutputGate(gate("tcpOut"));
51  }
52 }
TCPSocket socket
Definition: TCPAppBase.h:35
int bytesSent
Definition: TCPAppBase.h:42
int bytesRcvd
Definition: TCPAppBase.h:43
int numSessions
Definition: TCPAppBase.h:38
int packetsRcvd
Definition: TCPAppBase.h:41
void bind(int localPort)
Bind the socket to a local port number.
Definition: TCPSocket.cc:101
Local initializations.
Definition: InitStages.h:35
int packetsSent
Definition: TCPAppBase.h:40
int numBroken
Definition: TCPAppBase.h:39
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
void setCallbackObject(CallbackInterface *cb, void *yourPtr=nullptr)
Sets a callback object, to be used with processMessage().
Definition: TCPSocket.cc:245
Initialization of applications.
Definition: InitStages.h:106
virtual int inet::TCPAppBase::numInitStages ( ) const
inlineoverrideprotectedvirtual

Reimplemented in inet::PacketDrillApp, inet::TCPSessionApp, inet::TCPBasicClientApp, and inet::TelnetApp.

52 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::TCPAppBase::refreshDisplay ( ) const
overrideprotectedvirtual

Reimplemented in inet::TCPSessionApp.

104 {
105  getDisplayString().setTagArg("t", 0, TCPSocket::stateName(socket.getState()));
106 }
TCPSocket socket
Definition: TCPAppBase.h:35
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::TCPAppBase::sendPacket ( cPacket *  pkt)
protectedvirtual

Referenced by inet::TCPSessionApp::sendData(), inet::TelnetApp::sendGenericAppMsg(), and inet::TCPBasicClientApp::sendRequest().

94 {
95  int numBytes = msg->getByteLength();
96  emit(sentPkSignal, msg);
97  socket.send(msg);
98 
99  packetsSent++;
100  bytesSent += numBytes;
101 }
TCPSocket socket
Definition: TCPAppBase.h:35
int bytesSent
Definition: TCPAppBase.h:42
static simsignal_t sentPkSignal
Definition: TCPAppBase.h:48
void send(cMessage *msg)
Sends data packet.
Definition: TCPSocket.cc:175
int packetsSent
Definition: TCPAppBase.h:40
void inet::TCPAppBase::socketClosed ( int  connId,
void *  yourPtr 
)
overrideprotectedvirtual

Reimplemented from inet::TCPSocket::CallbackInterface.

Reimplemented in inet::TCPSessionApp, inet::TCPBasicClientApp, and inet::TelnetApp.

Referenced by inet::TelnetApp::socketClosed(), inet::TCPBasicClientApp::socketClosed(), and inet::TCPSessionApp::socketClosed().

133 {
134  // *redefine* to start another session etc.
135  EV_INFO << "connection closed\n";
136 }
void inet::TCPAppBase::socketDataArrived ( int  connId,
void *  yourPtr,
cPacket *  msg,
bool  urgent 
)
overrideprotectedvirtual

Implements inet::TCPSocket::CallbackInterface.

Reimplemented in inet::TCPSessionApp, inet::TCPBasicClientApp, and inet::TelnetApp.

Referenced by inet::TelnetApp::socketDataArrived(), inet::TCPBasicClientApp::socketDataArrived(), and inet::TCPSessionApp::socketDataArrived().

115 {
116  // *redefine* to perform or schedule next sending
117  packetsRcvd++;
118  bytesRcvd += msg->getByteLength();
119  emit(rcvdPkSignal, msg);
120  delete msg;
121 }
static simsignal_t rcvdPkSignal
Definition: TCPAppBase.h:47
int bytesRcvd
Definition: TCPAppBase.h:43
int packetsRcvd
Definition: TCPAppBase.h:41
void inet::TCPAppBase::socketEstablished ( int  connId,
void *  yourPtr 
)
overrideprotectedvirtual

Reimplemented from inet::TCPSocket::CallbackInterface.

Reimplemented in inet::TCPSessionApp, inet::TCPBasicClientApp, and inet::TelnetApp.

Referenced by inet::TelnetApp::socketEstablished(), inet::TCPBasicClientApp::socketEstablished(), and inet::TCPSessionApp::socketEstablished().

109 {
110  // *redefine* to perform or schedule first sending
111  EV_INFO << "connected\n";
112 }
void inet::TCPAppBase::socketFailure ( int  connId,
void *  yourPtr,
int  code 
)
overrideprotectedvirtual

Reimplemented from inet::TCPSocket::CallbackInterface.

Reimplemented in inet::TCPSessionApp, inet::TCPBasicClientApp, and inet::TelnetApp.

Referenced by inet::TelnetApp::socketFailure(), inet::TCPBasicClientApp::socketFailure(), and inet::TCPSessionApp::socketFailure().

139 {
140  // subclasses may override this function, and add code try to reconnect after a delay.
141  EV_WARN << "connection broken\n";
142  numBroken++;
143 }
int numBroken
Definition: TCPAppBase.h:39
void inet::TCPAppBase::socketPeerClosed ( int  connId,
void *  yourPtr 
)
overrideprotectedvirtual

Reimplemented from inet::TCPSocket::CallbackInterface.

124 {
125  // close the connection (if not already closed)
127  EV_INFO << "remote TCP closed, closing here as well\n";
128  close();
129  }
130 }
TCPSocket socket
Definition: TCPAppBase.h:35
Definition: TCPSocket.h:148
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 inet::TCPAppBase::socketStatusArrived ( int  connId,
void *  yourPtr,
TCPStatusInfo status 
)
inlineoverrideprotectedvirtual

Reimplemented from inet::TCPSocket::CallbackInterface.

69 { delete status; }

Member Data Documentation

int inet::TCPAppBase::bytesRcvd
protected
simsignal_t inet::TCPAppBase::connectSignal = registerSignal("connect")
staticprotected

Referenced by close(), and connect().

int inet::TCPAppBase::numBroken
protected

Referenced by initialize(), and socketFailure().

int inet::TCPAppBase::numSessions
protected

Referenced by connect(), finish(), and initialize().

int inet::TCPAppBase::packetsRcvd
protected
int inet::TCPAppBase::packetsSent
protected

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

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

Referenced by socketDataArrived().

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

Referenced by sendPacket().


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