CLI  2.9
telnet.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2006-2018, Alexis Royer, http://alexis.royer.free.fr/CLI
3 
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation
10  and/or other materials provided with the distribution.
11  * Neither the name of the CLI library project nor the names of its contributors may be used to endorse or promote products derived from this software
12  without specific prior written permission.
13 
14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 
31 
32 #ifndef _CLI_TELNET_H_
33 #define _CLI_TELNET_H_
34 
35 #include "cli/namespace.h"
36 #include "cli/object.h"
38 #include "cli/tk.h"
39 
40 
42 
43  // Forward declarations.
44  class ExecutionContext;
45  class ExecutionResult;
46  class TelnetConnection;
47 
48 
57  class TelnetServer : public Object
58  {
59  public:
61  explicit TelnetServer(
62  const unsigned int UI_MaxConnections,
63  const unsigned long UL_TcpPort,
64  const ResourceString::LANG E_Lang
65  );
66 
68  virtual ~TelnetServer(void);
69 
70  private:
72  explicit TelnetServer(void);
74  TelnetServer(const TelnetServer&);
76  TelnetServer& operator=(const TelnetServer&);
77 
78  public:
81  void StartServer(void);
82 
84  void StopServer(void);
85 
86  protected:
89  virtual ExecutionContext* const OnNewConnection(
90  const TelnetConnection& CLI_NewConnection
91  ) = 0;
92 
94  virtual void OnCloseConnection(
95  const TelnetConnection& CLI_ConnectionClosed,
96  ExecutionContext* const PCLI_Context
97  ) = 0;
98 
99  private:
102  const bool RunLoop(
103  const int I_Milli
104  );
106 
108  const bool AcceptConnection(void);
109 
111  const bool CloseConnection(
112  const int I_ConnectionSocket
113  );
114 
117  const bool TerminateServer(void);
118 
119  private:
121  int m_iServerSocket;
123  const unsigned long m_ulTcpPort;
125  const ResourceString::LANG m_eLang;
127  typedef struct {
128  int i_Socket;
129  TelnetConnection* pcli_Connection;
130  ExecutionContext* pcli_Context;
131  } ConnectionInfo;
133  tk::Map<int, ConnectionInfo> m_tkConnections;
135  const unsigned int m_uiMaxConnections;
136 
137  friend class TelnetConnection; // Let TelnetConnection call RunLoop().
138  };
139 
140 
143  {
144  public:
146  explicit TelnetConnection(
147  TelnetServer* const PCLI_Server,
148  const int I_Socket,
149  const ResourceString::LANG E_Lang,
150  const bool B_AutoDelete
151  );
152 
154  virtual ~TelnetConnection(void);
155 
156  private:
158  explicit TelnetConnection(void);
162  TelnetConnection& operator=(const TelnetConnection&);
163 
164  protected:
165  // Inherit doxygen comments from cli::OutputDevice interface documentation.
166  virtual const bool OpenDevice(void);
167  // Inherit doxygen comments from cli::OutputDevice interface documentation.
168  virtual const bool CloseDevice(void);
169  protected:
172  const bool ReceiveChars(void) const;
175  const bool ProcessKeys(void) const;
178  const bool CheckUp(void) const;
179  public:
180  // Inherit doxygen comments from cli::IODevice interface documentation.
181  virtual const KEY GetKey(void) const;
182  // Inherit doxygen comments from cli::NonBlockingIODevice interface documentation.
183  virtual void OnKey(const KEY E_Key) const;
184  // Inherit doxygen comments from cli::OutputDevice interface documentation.
185  virtual void PutString(const char* const STR_Out) const;
186  // Inherit doxygen comments from cli::OutputDevice interface documentation.
187  virtual void Beep(void) const;
188  // Inherit doxygen comments from cli::OutputDevice interface documentation.
189  virtual void CleanScreen(void) const;
190 
191  private:
193  TelnetServer* const m_pcliServer;
195  const int m_iSocket;
197  const ResourceString::LANG m_eLang;
199  mutable tk::Queue<int> m_qChars;
201  mutable bool m_bWaitingForKeys;
202 
203  friend class TelnetServer; // Let TelnetServer call ReceiveChars().
204  };
205 
207 
208 #endif // _CLI_TELNET_H_
209 
Generic object.
Definition: object.h:42
virtual void PutString(const char *const STR_Out) const
Output handler.
Main namespace of the CLI library.
virtual const KEY GetKey(void) const
Input key capture handler.
virtual ~TelnetConnection(void)
Destructor.
virtual void OnKey(const KEY E_Key) const
Handler to call when a key is received.
Non-blocking input device.
virtual void CleanScreen(void) const
Clean screen.
Telnet connection input/output device.
Definition: telnet.h:142
#define CLI_NS_END(__ns)
End a namespace definition.
Definition: namespace.h:45
LANG
Languages identifiers.
Result interface for execution contexts.
Definition: exec_context.h:313
virtual const bool CloseDevice(void)
Device closure handler.
const bool ReceiveChars(void) const
Characters received from the socket.
Namespace management.
Telnet server class.
Definition: telnet.h:57
Execution context.
Definition: exec_context.h:149
const bool ProcessKeys(void) const
Processes input chars and converts them into keys one by one.
NonBlockingDevice class definition.
void StartServer(void)
Starts the server.
enum _KEY KEY
Input keys.
#define CLI_NS_BEGIN(__ns)
Begin a namespace definition.
Definition: namespace.h:38
virtual void OnCloseConnection(const TelnetConnection &CLI_ConnectionClosed, ExecutionContext *const PCLI_Context)=0
Execution context release handler.
CLI toolkit definition.
Object class definition.
virtual const bool OpenDevice(void)
Device opening handler.
void StopServer(void)
Stops the server.
virtual ~TelnetServer(void)
Destructor.
const bool CheckUp(void) const
Checks whether the connection should still be up.
virtual void Beep(void) const
Beep handler.
virtual ExecutionContext *const OnNewConnection(const TelnetConnection &CLI_NewConnection)=0
Execution context creation handler.