A Telnet connection manager, wrapping libtelnet
.
More...
#include <telnetConn.hpp>
Public Member Functions | |
~telnetConn () | |
D'tor, conducts connection cleanup. More... | |
int | connect (const std::string &host, const std::string &port) |
Connect to the device. More... | |
int | login (const std::string &username, const std::string &password) |
Manage the login process on this device. More... | |
int | noLogin () |
Set flags as if we're logged in, used when device doesn't require it. More... | |
int | write (const std::string &buffWrite, int timeoutWrite) |
Write to a telnet connection. More... | |
int | read (const std::string &eot, int timeoutRead, bool clear=true) |
Read from a telnet connection, until end-of-transmission string is read. More... | |
int | read (int timeoutRead, bool clear=true) |
Read from a telnet connection, until m_prompt is read. More... | |
int | writeRead (const std::string &strWrite, bool swallowEcho, int timeoutWrite, int timeoutRead) |
Write to a telnet connection, then get the reply. More... | |
Static Public Member Functions | |
static int | send (int sock, const char *buffer, size_t size) |
Internal send for use by event_handler. More... | |
static void | event_handler (telnet_t *telnet, telnet_event_t *ev, void *user_data) |
Event handler callback for libtelnet processing. More... | |
Public Attributes | |
int | m_sock {0} |
The socket file descriptor. More... | |
telnet_t * | m_telnet {nullptr} |
libtelnet telnet_t structure More... | |
std::string | m_usernamePrompt {"Username:"} |
The device's username entry prompt, used for managing login. More... | |
std::string | m_passwordPrompt {"Password:"} |
The device's password entry prompt, used for managing login. More... | |
std::string | m_prompt {"$> "} |
The device's prompt, used for detecting end of transmission. More... | |
int | m_loggedin {0} |
Flag denoting the login state. More... | |
int | m_EHError {0} |
Used to indicate an error occurred in the event handler callback. More... | |
std::string | m_strRead |
The accumulated string read from the device. More... | |
A Telnet connection manager, wrapping libtelnet
.
Establishes the connection to the server, and initializes the libtelnet
structure, including registering the event handler callback.
Errors encountered during telnet event handling are indicated by an internal flag, which must be checked each time a libtelnet function is called. If it is nonzero an error has occurred.
Responses from the server are accumulated in the m_strRead
member. It is typically cleared before reading, but this can be suppressed when desired.
Because of the way event handling is managed, and the class-global error and response accumulation this is not thread-safe. Any calls to this class methods should be mutex-ed.
Definition at line 80 of file telnetConn.hpp.
MagAOX::tty::telnetConn::~telnetConn | ( | ) |
D'tor, conducts connection cleanup.
Definition at line 24 of file telnetConn.cpp.
int MagAOX::tty::telnetConn::connect | ( | const std::string & | host, |
const std::string & | port | ||
) |
Connect to the device.
[in] | host | The host specification (i.p. address) |
[in] | port | the port on the host. |
Definition at line 31 of file telnetConn.cpp.
Referenced by MagAOX::app::picoMotorCtrl::appLogic(), MagAOX::app::siglentSDG::appLogic(), and MagAOX::app::trippLitePDU::devConnect().
|
static |
Event handler callback for libtelnet processing.
Resets the internal m_EHError value to TTY_E_NOERROR on entry. Will set it to an error flag if an error is encountered, so this flag should be checked after any call to a libtelnet function.
Definition at line 378 of file telnetConn.cpp.
Referenced by connect().
int MagAOX::tty::telnetConn::login | ( | const std::string & | username, |
const std::string & | password | ||
) |
Manage the login process on this device.
[in] | password | The username [in] The password. |
Definition at line 90 of file telnetConn.cpp.
Referenced by MagAOX::app::trippLitePDU::devLogin().
int MagAOX::tty::telnetConn::noLogin | ( | ) |
Set flags as if we're logged in, used when device doesn't require it.
Definition at line 176 of file telnetConn.cpp.
Referenced by MagAOX::app::picoMotorCtrl::appLogic(), and MagAOX::app::siglentSDG::appLogic().
int MagAOX::tty::telnetConn::read | ( | const std::string & | eot, |
int | timeoutRead, | ||
bool | clear = true |
||
) |
Read from a telnet connection, until end-of-transmission string is read.
[in] | eot | the end-of-transmission indicator |
[in] | timeoutRead | The timeout in milliseconds. |
[in] | clear | [optional] whether or not to clear the strRead buffer |
Definition at line 223 of file telnetConn.cpp.
Referenced by MagAOX::app::picoMotorCtrl::appLogic(), MagAOX::app::siglentSDG::appLogic(), MagAOX::app::trippLitePDU::devStatus(), read(), MagAOX::app::siglentSDG::writeCommand(), writeRead(), and MagAOX::app::siglentSDG::writeRead().
int MagAOX::tty::telnetConn::read | ( | int | timeoutRead, |
bool | clear = true |
||
) |
Read from a telnet connection, until m_prompt is read.
[in] | timeoutRead | The timeout in milliseconds. |
[in] | clear | [optional] whether or not to clear the strRead buffer |
Definition at line 287 of file telnetConn.cpp.
|
static |
Internal send for use by event_handler.
Definition at line 353 of file telnetConn.cpp.
Referenced by event_handler().
int MagAOX::tty::telnetConn::write | ( | const std::string & | buffWrite, |
int | timeoutWrite | ||
) |
Write to a telnet connection.
[in] | buffWrite | The characters to write to the telnet. |
[in] | timeoutWrite | The timeout in milliseconds. |
Definition at line 182 of file telnetConn.cpp.
Referenced by MagAOX::app::picoMotorCtrl::appLogic(), MagAOX::app::siglentSDG::appLogic(), MagAOX::app::picoMotorCtrl::channelThreadExec(), login(), MagAOX::app::siglentSDG::writeCommand(), writeRead(), and MagAOX::app::siglentSDG::writeRead().
int MagAOX::tty::telnetConn::writeRead | ( | const std::string & | strWrite, |
bool | swallowEcho, | ||
int | timeoutWrite, | ||
int | timeoutRead | ||
) |
Write to a telnet connection, then get the reply.
The read is conducted until the m_prompt string is received. Echo characters are swallowed if desired.
[in] | strWrite | The characters to write to the telnet. |
[in] | swallowEcho | If true, strWrite.size() characters are read after the write |
[in] | timeoutWrite | The write timeout in milliseconds. |
[in] | timeoutRead | The read timeout in milliseconds. |
Definition at line 294 of file telnetConn.cpp.
Referenced by MagAOX::app::trippLitePDU::devPostLogin(), MagAOX::app::trippLitePDU::devStatus(), MagAOX::app::trippLitePDU::turnOutletOff(), MagAOX::app::trippLitePDU::turnOutletOn(), and MagAOX::app::siglentSDG::writeRead().
int MagAOX::tty::telnetConn::m_EHError {0} |
Used to indicate an error occurred in the event handler callback.
Definition at line 107 of file telnetConn.hpp.
Referenced by event_handler(), login(), read(), and writeRead().
int MagAOX::tty::telnetConn::m_loggedin {0} |
Flag denoting the login state.
Used to manage different behaviors in the libtelnet event handler.
Definition at line 104 of file telnetConn.hpp.
Referenced by connect(), event_handler(), login(), and noLogin().
std::string MagAOX::tty::telnetConn::m_passwordPrompt {"Password:"} |
The device's password entry prompt, used for managing login.
Definition at line 90 of file telnetConn.hpp.
Referenced by event_handler().
std::string MagAOX::tty::telnetConn::m_prompt {"$> "} |
The device's prompt, used for detecting end of transmission.
Definition at line 92 of file telnetConn.hpp.
Referenced by MagAOX::app::picoMotorCtrl::picoMotorCtrl(), MagAOX::app::siglentSDG::siglentSDG(), MagAOX::app::trippLitePDU::devLogin(), MagAOX::app::trippLitePDU::devPostLogin(), event_handler(), read(), and writeRead().
int MagAOX::tty::telnetConn::m_sock {0} |
The socket file descriptor.
Definition at line 82 of file telnetConn.hpp.
Referenced by ~telnetConn(), connect(), event_handler(), login(), read(), write(), and writeRead().
std::string MagAOX::tty::telnetConn::m_strRead |
The accumulated string read from the device.
This needs to be clear()-ed when expecting a new response to start.
Definition at line 113 of file telnetConn.hpp.
Referenced by MagAOX::app::picoMotorCtrl::appLogic(), MagAOX::app::siglentSDG::appLogic(), MagAOX::app::trippLitePDU::devStatus(), event_handler(), read(), writeRead(), and MagAOX::app::siglentSDG::writeRead().
telnet_t* MagAOX::tty::telnetConn::m_telnet {nullptr} |
libtelnet telnet_t structure
Definition at line 84 of file telnetConn.hpp.
Referenced by ~telnetConn(), connect(), login(), read(), write(), and writeRead().
std::string MagAOX::tty::telnetConn::m_usernamePrompt {"Username:"} |
The device's username entry prompt, used for managing login.
Definition at line 87 of file telnetConn.hpp.
Referenced by MagAOX::app::trippLitePDU::devLogin(), and event_handler().