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

An example Telnet client application. More...

#include <TelnetApp.h>

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

Public Member Functions

 TelnetApp ()
 
virtual ~TelnetApp ()
 
- 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 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 void checkedScheduleAt (simtime_t t, cMessage *msg)
 
virtual void sendGenericAppMsg (int numBytes, int expectedReplyBytes)
 
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
 
int numLinesToType = 0
 
int numCharsToType = 0
 
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 Telnet client application.

The server app should be TCPGenericSrvApp.

Constructor & Destructor Documentation

inet::TelnetApp::TelnetApp ( )
inline
52 {}
inet::TelnetApp::~TelnetApp ( )
virtual
35 {
36  cancelAndDelete(timeoutMsg);
37 }
cMessage * timeoutMsg
Definition: TelnetApp.h:33

Member Function Documentation

void inet::TelnetApp::checkedScheduleAt ( simtime_t  t,
cMessage *  msg 
)
protectedvirtual

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

40 {
41  if (stopTime < SIMTIME_ZERO || t < stopTime)
42  scheduleAt(t, msg);
43 }
simtime_t stopTime
Definition: TelnetApp.h:36
bool inet::TelnetApp::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.

73 {
74  Enter_Method_Silent();
75  if (dynamic_cast<NodeStartOperation *>(operation)) {
77  simtime_t now = simTime();
78  simtime_t startTime = par("startTime");
79  simtime_t start = std::max(startTime, now);
80  if (timeoutMsg && ((stopTime < SIMTIME_ZERO) || (start < stopTime) || (start == stopTime && startTime == stopTime))) {
81  timeoutMsg->setKind(MSGKIND_CONNECT);
82  scheduleAt(start, timeoutMsg);
83  }
84  }
85  }
86  else if (dynamic_cast<NodeShutdownOperation *>(operation)) {
88  cancelEvent(timeoutMsg);
90  close();
91  // TODO: wait until socket is closed
92  }
93  }
94  else if (dynamic_cast<NodeCrashOperation *>(operation)) {
96  cancelEvent(timeoutMsg);
97  }
98  else
99  throw cRuntimeError("Unsupported lifecycle operation '%s'", operation->getClassName());
100  return true;
101 }
TCPSocket socket
Definition: TCPAppBase.h:35
cMessage * timeoutMsg
Definition: TelnetApp.h:33
Definition: TCPSocket.h:148
#define MSGKIND_CONNECT
Definition: TelnetApp.cc:27
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
Stage
Definition: NodeOperations.h:46
simtime_t stopTime
Definition: TelnetApp.h:36
Definition: NodeOperations.h:127
void inet::TelnetApp::handleTimer ( cMessage *  msg)
overrideprotectedvirtual

Implements inet::TCPAppBase.

104 {
105  switch (msg->getKind()) {
106  case MSGKIND_CONNECT:
107  EV_INFO << "user fires up telnet program\n";
108  connect();
109  break;
110 
111  case MSGKIND_SEND:
112  if (numCharsToType > 0) {
113  // user types a character and expects it to be echoed
114  EV_INFO << "user types one character, " << numCharsToType - 1 << " more to go\n";
115  sendGenericAppMsg(1, 1);
116  checkedScheduleAt(simTime() + (simtime_t)par("keyPressDelay"), timeoutMsg);
117  numCharsToType--;
118  }
119  else {
120  EV_INFO << "user hits Enter key\n";
121  // Note: reply length must be at least 2, otherwise we'll think
122  // it's an echo when it comes back!
123  sendGenericAppMsg(1, 2 + (long)par("commandOutputLength"));
124  numCharsToType = (long)par("commandLength");
125 
126  // Note: no checkedScheduleAt(), because user only starts typing next command
127  // when output from previous one has arrived (see socketDataArrived())
128  }
129  break;
130 
131  case MSGKIND_CLOSE:
132  EV_INFO << "user exits telnet program\n";
133  close();
134  break;
135  }
136 }
virtual void sendGenericAppMsg(int numBytes, int expectedReplyBytes)
Definition: TelnetApp.cc:138
cMessage * timeoutMsg
Definition: TelnetApp.h:33
#define MSGKIND_CONNECT
Definition: TelnetApp.cc:27
int numCharsToType
Definition: TelnetApp.h:35
virtual void close()
Definition: TCPAppBase.cc:86
#define MSGKIND_SEND
Definition: TelnetApp.cc:28
virtual void connect()
Definition: TCPAppBase.cc:62
#define MSGKIND_CLOSE
Definition: TelnetApp.cc:29
virtual void checkedScheduleAt(simtime_t t, cMessage *msg)
Definition: TelnetApp.cc:39
void inet::TelnetApp::initialize ( int  stage)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

46 {
48 
49  if (stage == INITSTAGE_LOCAL) {
51  WATCH(numCharsToType);
52  WATCH(numLinesToType);
53  }
54  else if (stage == INITSTAGE_APPLICATION_LAYER) {
55  bool isOperational;
56  NodeStatus *nodeStatus = dynamic_cast<NodeStatus *>(findContainingNode(this)->getSubmodule("status"));
57  isOperational = (!nodeStatus) || nodeStatus->getState() == NodeStatus::UP;
58  if (!isOperational)
59  throw cRuntimeError("This module doesn't support starting in node DOWN state");
60 
61  simtime_t startTime = par("startTime");
62  stopTime = par("stopTime");
63  if (stopTime >= SIMTIME_ZERO && stopTime < startTime)
64  throw cRuntimeError("Invalid startTime/stopTime parameters");
65 
66  timeoutMsg = new cMessage("timer");
67  timeoutMsg->setKind(MSGKIND_CONNECT);
68  scheduleAt(startTime, timeoutMsg);
69  }
70 }
cMessage * timeoutMsg
Definition: TelnetApp.h:33
#define MSGKIND_CONNECT
Definition: TelnetApp.cc:27
int numLinesToType
Definition: TelnetApp.h:34
int numCharsToType
Definition: TelnetApp.h:35
cModule * findContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:56
Local initializations.
Definition: InitStages.h:35
virtual void initialize(int stage) override
Definition: TCPAppBase.cc:28
simtime_t stopTime
Definition: TelnetApp.h:36
Initialization of applications.
Definition: InitStages.h:106
Definition: NodeStatus.h:40
virtual int inet::TelnetApp::numInitStages ( ) const
inlineoverrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

39 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::TelnetApp::sendGenericAppMsg ( int  numBytes,
int  expectedReplyBytes 
)
protectedvirtual

Referenced by handleTimer().

139 {
140  EV_INFO << "sending " << numBytes << " bytes, expecting " << expectedReplyBytes << endl;
141 
142  GenericAppMsg *msg = new GenericAppMsg("data");
143  msg->setByteLength(numBytes);
144  msg->setExpectedReplyLength(expectedReplyBytes);
145  msg->setServerClose(false);
146  sendPacket(msg);
147 }
virtual void sendPacket(cPacket *pkt)
Definition: TCPAppBase.cc:93
void inet::TelnetApp::socketClosed ( int  connId,
void *  yourPtr 
)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

191 {
193 
194  // start another session after a delay
195  cancelEvent(timeoutMsg);
196  timeoutMsg->setKind(MSGKIND_CONNECT);
197  checkedScheduleAt(simTime() + (simtime_t)par("idleInterval"), timeoutMsg);
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
cMessage * timeoutMsg
Definition: TelnetApp.h:33
#define MSGKIND_CONNECT
Definition: TelnetApp.cc:27
virtual void checkedScheduleAt(simtime_t t, cMessage *msg)
Definition: TelnetApp.cc:39
void inet::TelnetApp::socketDataArrived ( int  connId,
void *  yourPtr,
cPacket *  msg,
bool  urgent 
)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

161 {
162  int len = msg->getByteLength();
163  TCPAppBase::socketDataArrived(connId, ptr, msg, urgent);
164 
165  if (len == 1) {
166  // this is an echo, ignore
167  EV_INFO << "received echo\n";
168  }
169  else {
170  // output from last typed command arrived.
171  EV_INFO << "received output of command typed\n";
172 
173  // If user has finished working, she closes the connection, otherwise
174  // starts typing again after a delay
175  numLinesToType--;
176 
177  if (numLinesToType == 0) {
178  EV_INFO << "user has no more commands to type\n";
179  timeoutMsg->setKind(MSGKIND_CLOSE);
180  checkedScheduleAt(simTime() + (simtime_t)par("thinkTime"), timeoutMsg);
181  }
182  else {
183  EV_INFO << "user looks at output, then starts typing next command\n";
184  timeoutMsg->setKind(MSGKIND_SEND);
185  checkedScheduleAt(simTime() + (simtime_t)par("thinkTime"), timeoutMsg);
186  }
187  }
188 }
az accept haszálja pcb új connId
Definition: lwip_tcp.txt:38
cMessage * timeoutMsg
Definition: TelnetApp.h:33
uint16_t len
Definition: TCP_NSC.cc:85
int numLinesToType
Definition: TelnetApp.h:34
virtual void socketDataArrived(int connId, void *yourPtr, cPacket *msg, bool urgent) override
Definition: TCPAppBase.cc:114
#define MSGKIND_SEND
Definition: TelnetApp.cc:28
#define MSGKIND_CLOSE
Definition: TelnetApp.cc:29
virtual void checkedScheduleAt(simtime_t t, cMessage *msg)
Definition: TelnetApp.cc:39
void inet::TelnetApp::socketEstablished ( int  connId,
void *  yourPtr 
)
overrideprotectedvirtual

Reimplemented from inet::TCPAppBase.

150 {
152 
153  // schedule first sending
154  numLinesToType = (long)par("numCommands");
155  numCharsToType = (long)par("commandLength");
157  checkedScheduleAt(simTime() + (simtime_t)par("thinkTime"), timeoutMsg);
158 }
az accept haszálja pcb új connId
Definition: lwip_tcp.txt:38
cMessage * timeoutMsg
Definition: TelnetApp.h:33
virtual void socketEstablished(int connId, void *yourPtr) override
Definition: TCPAppBase.cc:108
int numLinesToType
Definition: TelnetApp.h:34
int numCharsToType
Definition: TelnetApp.h:35
#define MSGKIND_SEND
Definition: TelnetApp.cc:28
#define MSGKIND_CLOSE
Definition: TelnetApp.cc:29
virtual void checkedScheduleAt(simtime_t t, cMessage *msg)
Definition: TelnetApp.cc:39
void inet::TelnetApp::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  cancelEvent(timeoutMsg);
206  timeoutMsg->setKind(MSGKIND_CONNECT);
207  checkedScheduleAt(simTime() + (simtime_t)par("reconnectInterval"), timeoutMsg);
208 }
az accept haszálja pcb új connId
Definition: lwip_tcp.txt:38
cMessage * timeoutMsg
Definition: TelnetApp.h:33
#define MSGKIND_CONNECT
Definition: TelnetApp.cc:27
virtual void socketFailure(int connId, void *yourPtr, int code) override
Definition: TCPAppBase.cc:138
virtual void checkedScheduleAt(simtime_t t, cMessage *msg)
Definition: TelnetApp.cc:39

Member Data Documentation

int inet::TelnetApp::numCharsToType = 0
protected
int inet::TelnetApp::numLinesToType = 0
protected
simtime_t inet::TelnetApp::stopTime
protected
cMessage* inet::TelnetApp::timeoutMsg = nullptr
protected

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