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::ApplicationBase inet::TcpSocket::ICallback inet::OperationalBase inet::OperationalMixin< cSimpleModule > inet::ILifecycle

Public Member Functions

 TelnetApp ()
 
virtual ~TelnetApp ()
 
- Public Member Functions inherited from inet::ApplicationBase
 ApplicationBase ()
 
- Public Member Functions inherited from inet::OperationalMixin< cSimpleModule >
virtual ~OperationalMixin ()
 }@ More...
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 
- Public Member Functions inherited from inet::TcpSocket::ICallback
virtual ~ICallback ()
 

Protected Member Functions

virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleTimer (cMessage *msg) override
 
virtual void socketEstablished (TcpSocket *socket) override
 
virtual void socketDataArrived (TcpSocket *socket, Packet *msg, bool urgent) override
 Notifies about data arrival, packet ownership is transferred to the callee. More...
 
virtual void socketClosed (TcpSocket *socket) override
 
virtual void socketFailure (TcpSocket *socket, int code) override
 
virtual void checkedScheduleAt (simtime_t t, cMessage *msg)
 
virtual void sendGenericAppMsg (int numBytes, int expectedReplyBytes)
 
virtual void handleStartOperation (LifecycleOperation *operation) override
 
virtual void handleStopOperation (LifecycleOperation *operation) override
 
virtual void handleCrashOperation (LifecycleOperation *operation) override
 
- Protected Member Functions inherited from inet::TcpAppBase
virtual void handleMessageWhenUp (cMessage *msg) override
 
virtual void finish () override
 
virtual void refreshDisplay () const override
 
virtual void connect ()
 
virtual void close ()
 
virtual void sendPacket (Packet *pkt)
 
virtual void socketAvailable (TcpSocket *socket, TcpAvailableInfo *availableInfo) override
 
virtual void socketPeerClosed (TcpSocket *socket) override
 
virtual void socketStatusArrived (TcpSocket *socket, TcpStatusInfo *status) override
 
virtual void socketDeleted (TcpSocket *socket) override
 
- Protected Member Functions inherited from inet::ApplicationBase
virtual bool isInitializeStage (int stage) const override
 
virtual bool isModuleStartStage (int stage) const override
 
virtual bool isModuleStopStage (int stage) const override
 
- Protected Member Functions inherited from inet::OperationalMixin< cSimpleModule >
virtual int numInitStages () const override
 
virtual void refreshDisplay () const override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void handleMessageWhenDown (cMessage *msg)
 
virtual bool handleOperationStage (LifecycleOperation *operation, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 
virtual State getInitialOperationalState () const
 Returns initial operational state: OPERATING or NOT_OPERATING. More...
 
virtual void handleActiveOperationTimeout (cMessage *message)
 
virtual bool isUp () const
 utility functions More...
 
virtual bool isDown () const
 
virtual void setOperationalState (State newState)
 
virtual void scheduleOperationTimeout (simtime_t timeout)
 
virtual void setupActiveOperation (LifecycleOperation *operation, IDoneCallback *doneCallback, State)
 
virtual void delayActiveOperationFinish (simtime_t timeout)
 
virtual void startActiveOperationExtraTime (simtime_t delay=SIMTIME_ZERO)
 
virtual void startActiveOperationExtraTimeOrFinish (simtime_t extraTime)
 
virtual void finishActiveOperation ()
 

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
 
- Protected Attributes inherited from inet::OperationalMixin< cSimpleModule >
State operationalState
 
simtime_t lastChange
 
Operation activeOperation
 
cMessage * activeOperationTimeout
 
cMessage * activeOperationExtraTimer
 

Additional Inherited Members

- Protected Types inherited from inet::OperationalMixin< cSimpleModule >
enum  State
 
- Static Protected Attributes inherited from inet::TcpAppBase
static simsignal_t connectSignal = registerSignal("connect")
 

Detailed Description

An example Telnet client application.

The server app should be TcpGenericServerApp.

Constructor & Destructor Documentation

◆ TelnetApp()

inet::TelnetApp::TelnetApp ( )
inline
43 {}

◆ ~TelnetApp()

inet::TelnetApp::~TelnetApp ( )
virtual
26 {
27  cancelAndDelete(timeoutMsg);
28 }

Member Function Documentation

◆ checkedScheduleAt()

void inet::TelnetApp::checkedScheduleAt ( simtime_t  t,
cMessage *  msg 
)
protectedvirtual
31 {
32  if (stopTime < SIMTIME_ZERO || t < stopTime)
33  scheduleAt(t, msg);
34 }

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

◆ handleCrashOperation()

void inet::TelnetApp::handleCrashOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

73 {
74  cancelEvent(timeoutMsg);
75  if (operation->getRootModule() != getContainingNode(this))
76  socket.destroy();
77 }

◆ handleStartOperation()

void inet::TelnetApp::handleStartOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

53 {
54  simtime_t now = simTime();
55  simtime_t startTime = par("startTime");
56  simtime_t start = std::max(startTime, now);
57  if (timeoutMsg && ((stopTime < SIMTIME_ZERO) || (start < stopTime) || (start == stopTime && startTime == stopTime))) {
58  timeoutMsg->setKind(MSGKIND_CONNECT);
59  scheduleAt(start, timeoutMsg);
60  }
61 }

◆ handleStopOperation()

void inet::TelnetApp::handleStopOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

64 {
65  cancelEvent(timeoutMsg);
66  if (socket.isOpen()) {
67  close();
68  delayActiveOperationFinish(par("stopOperationTimeout"));
69  }
70 }

◆ handleTimer()

void inet::TelnetApp::handleTimer ( cMessage *  msg)
overrideprotectedvirtual

Implements inet::TcpAppBase.

80 {
81  switch (msg->getKind()) {
82  case MSGKIND_CONNECT:
83  EV_INFO << "user fires up telnet program\n";
84  connect();
85  break;
86 
87  case MSGKIND_SEND:
88  if (numCharsToType > 0) {
89  // user types a character and expects it to be echoed
90  EV_INFO << "user types one character, " << numCharsToType - 1 << " more to go\n";
91  sendGenericAppMsg(1, 1);
92  checkedScheduleAt(simTime() + par("keyPressDelay"), timeoutMsg);
94  }
95  else {
96  EV_INFO << "user hits Enter key\n";
97  // Note: reply length must be at least 2, otherwise we'll think
98  // it's an echo when it comes back!
99  sendGenericAppMsg(1, 2 + par("commandOutputLength").intValue());
100  numCharsToType = par("commandLength");
101 
102  // Note: no checkedScheduleAt(), because user only starts typing next command
103  // when output from previous one has arrived (see socketDataArrived())
104  }
105  break;
106 
107  case MSGKIND_CLOSE:
108  EV_INFO << "user exits telnet program\n";
109  close();
110  break;
111  }
112 }

◆ initialize()

void inet::TelnetApp::initialize ( int  stage)
overrideprotectedvirtual

Reimplemented from inet::TcpAppBase.

37 {
39 
40  if (stage == INITSTAGE_LOCAL) {
42  WATCH(numCharsToType);
43  WATCH(numLinesToType);
44  simtime_t startTime = par("startTime");
45  stopTime = par("stopTime");
46  if (stopTime >= SIMTIME_ZERO && stopTime < startTime)
47  throw cRuntimeError("Invalid startTime/stopTime parameters");
48  timeoutMsg = new cMessage("timer");
49  }
50 }

◆ numInitStages()

virtual int inet::TelnetApp::numInitStages ( ) const
inlineoverrideprotectedvirtual

Reimplemented from inet::TcpAppBase.

28 { return NUM_INIT_STAGES; }

◆ sendGenericAppMsg()

void inet::TelnetApp::sendGenericAppMsg ( int  numBytes,
int  expectedReplyBytes 
)
protectedvirtual
115 {
116  EV_INFO << "sending " << numBytes << " bytes, expecting " << expectedReplyBytes << endl;
117 
118  const auto& payload = makeShared<GenericAppMsg>();
119  Packet *packet = new Packet("data");
120  payload->setChunkLength(B(numBytes));
121  payload->setExpectedReplyLength(B(expectedReplyBytes));
122  payload->setServerClose(false);
123  payload->addTag<CreationTimeTag>()->setCreationTime(simTime());
124  packet->insertAtBack(payload);
125 
126  sendPacket(packet);
127 }

Referenced by handleTimer().

◆ socketClosed()

void inet::TelnetApp::socketClosed ( TcpSocket socket)
overrideprotectedvirtual

Reimplemented from inet::TcpAppBase.

175 {
177  cancelEvent(timeoutMsg);
178  if (operationalState == State::OPERATING) {
179  // start another session after a delay
180  timeoutMsg->setKind(MSGKIND_CONNECT);
181  checkedScheduleAt(simTime() + par("idleInterval"), timeoutMsg);
182  }
183  else if (operationalState == State::STOPPING_OPERATION) {
184  if (!this->socket.isOpen())
185  startActiveOperationExtraTimeOrFinish(par("stopOperationExtraTime"));
186  }
187 }

◆ socketDataArrived()

void inet::TelnetApp::socketDataArrived ( TcpSocket socket,
Packet packet,
bool  urgent 
)
overrideprotectedvirtual

Notifies about data arrival, packet ownership is transferred to the callee.

Reimplemented from inet::TcpAppBase.

141 {
142  int len = msg->getByteLength();
144 
145  if (len == 1) {
146  // this is an echo, ignore
147  EV_INFO << "received echo\n";
148  }
149  else {
150  // output from last typed command arrived.
151  EV_INFO << "received output of command typed\n";
152 
153  // If user has finished working, she closes the connection, otherwise
154  // starts typing again after a delay
155  numLinesToType--;
156 
157  if (numLinesToType == 0) {
158  EV_INFO << "user has no more commands to type\n";
159  if (timeoutMsg->isScheduled())
160  cancelEvent(timeoutMsg);
161  timeoutMsg->setKind(MSGKIND_CLOSE);
162  checkedScheduleAt(simTime() + par("thinkTime"), timeoutMsg);
163  }
164  else {
165  EV_INFO << "user looks at output, then starts typing next command\n";
166  if (!timeoutMsg->isScheduled()) {
167  timeoutMsg->setKind(MSGKIND_SEND);
168  checkedScheduleAt(simTime() + par("thinkTime"), timeoutMsg);
169  }
170  }
171  }
172 }

◆ socketEstablished()

void inet::TelnetApp::socketEstablished ( TcpSocket socket)
overrideprotectedvirtual

Reimplemented from inet::TcpAppBase.

130 {
132 
133  // schedule first sending
134  numLinesToType = par("numCommands");
135  numCharsToType = par("commandLength");
137  checkedScheduleAt(simTime() + par("thinkTime"), timeoutMsg);
138 }

◆ socketFailure()

void inet::TelnetApp::socketFailure ( TcpSocket socket,
int  code 
)
overrideprotectedvirtual

Reimplemented from inet::TcpAppBase.

190 {
192 
193  // reconnect after a delay
194  cancelEvent(timeoutMsg);
195  timeoutMsg->setKind(MSGKIND_CONNECT);
196  checkedScheduleAt(simTime() + par("reconnectInterval"), timeoutMsg);
197 }

Member Data Documentation

◆ numCharsToType

int inet::TelnetApp::numCharsToType = 0
protected

◆ numLinesToType

int inet::TelnetApp::numLinesToType = 0
protected

◆ stopTime

simtime_t inet::TelnetApp::stopTime
protected

◆ timeoutMsg


The documentation for this class was generated from the following files:
inet::TelnetApp::sendGenericAppMsg
virtual void sendGenericAppMsg(int numBytes, int expectedReplyBytes)
Definition: TelnetApp.cc:114
inet::OperationalMixin< cSimpleModule >::operationalState
State operationalState
Definition: OperationalMixin.h:23
inet::TelnetApp::timeoutMsg
cMessage * timeoutMsg
Definition: TelnetApp.h:22
MSGKIND_SEND
#define MSGKIND_SEND
Definition: TelnetApp.cc:20
inet::getContainingNode
cModule * getContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:40
inet::TcpSocket::isOpen
virtual bool isOpen() const override
Definition: TcpSocket.cc:257
inet::TcpAppBase::initialize
virtual void initialize(int stage) override
Definition: TcpAppBase.cc:17
inet::TcpAppBase::socketEstablished
virtual void socketEstablished(TcpSocket *socket) override
Definition: TcpAppBase.cc:113
inet::TcpAppBase::sendPacket
virtual void sendPacket(Packet *pkt)
Definition: TcpAppBase.cc:97
inet::units::units::B
intscale< b, 1, 8 > B
Definition: Units.h:1168
inet::TcpAppBase::connect
virtual void connect()
Definition: TcpAppBase.cc:50
inet::TelnetApp::checkedScheduleAt
virtual void checkedScheduleAt(simtime_t t, cMessage *msg)
Definition: TelnetApp.cc:30
inet::TcpAppBase::socket
TcpSocket socket
Definition: TcpAppBase.h:25
inet::TcpAppBase::socketDataArrived
virtual void socketDataArrived(TcpSocket *socket, Packet *msg, bool urgent) override
Notifies about data arrival, packet ownership is transferred to the callee.
Definition: TcpAppBase.cc:119
MSGKIND_CLOSE
#define MSGKIND_CLOSE
Definition: TelnetApp.cc:21
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::TelnetApp::stopTime
simtime_t stopTime
Definition: TelnetApp.h:25
inet::TcpSocket::destroy
virtual void destroy() override
Destroy the connection.
Definition: TcpSocket.cc:186
inet::TcpAppBase::socketFailure
virtual void socketFailure(TcpSocket *socket, int code) override
Definition: TcpAppBase.cc:144
inet::sctp::max
double max(const double a, const double b)
Returns the maximum of a and b.
Definition: SctpAssociation.h:266
inet::TelnetApp::numCharsToType
int numCharsToType
Definition: TelnetApp.h:24
inet::OperationalMixin< cSimpleModule >::delayActiveOperationFinish
virtual void delayActiveOperationFinish(simtime_t timeout)
Definition: OperationalMixinImpl.h:161
inet::OperationalMixin< cSimpleModule >::startActiveOperationExtraTimeOrFinish
virtual void startActiveOperationExtraTimeOrFinish(simtime_t extraTime)
Definition: OperationalMixinImpl.h:179
inet::TcpAppBase::socketClosed
virtual void socketClosed(TcpSocket *socket) override
Definition: TcpAppBase.cc:138
MSGKIND_CONNECT
#define MSGKIND_CONNECT
Definition: TelnetApp.cc:19
inet::TcpAppBase::close
virtual void close()
Definition: TcpAppBase.cc:90
inet::TelnetApp::numLinesToType
int numLinesToType
Definition: TelnetApp.h:23