Repo-Init
 
Loading...
Searching...
No Matches
TelnetServer.hpp File Reference
#include "telnet/TelnetStats.hpp"
#include <array>
#include <functional>
#include <list>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
Include dependency graph for TelnetServer.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  TelnetSession
 
class  TelnetServer
 

Typedefs

using Socket = int
 
using SP_TelnetSession = std::shared_ptr<TelnetSession>
 
using VEC_SP_TelnetSession = std::vector<SP_TelnetSession>
 
using FPTR_ConnectedCallback = std::function<void(SP_TelnetSession)>
 
using FPTR_NewLineCallback = std::function<bool(SP_TelnetSession, std::string)>
 
using FPTR_TabCallback = std::function<std::string(SP_TelnetSession, std::string)>
 

Functions

void TelnetConnectedCallback (const SP_TelnetSession &session)
 
bool TelnetMessageCallback (const SP_TelnetSession &session, const std::string &line)
 
std::string TelnetTabCallback (const SP_TelnetSession &session, std::string_view line)
 

Typedef Documentation

◆ FPTR_ConnectedCallback

using FPTR_ConnectedCallback = std::function<void(SP_TelnetSession)>

Definition at line 126 of file TelnetServer.hpp.

◆ FPTR_NewLineCallback

using FPTR_NewLineCallback = std::function<bool(SP_TelnetSession, std::string)>

Definition at line 127 of file TelnetServer.hpp.

◆ FPTR_TabCallback

using FPTR_TabCallback = std::function<std::string(SP_TelnetSession, std::string)>

Definition at line 128 of file TelnetServer.hpp.

◆ Socket

using Socket = int

Definition at line 51 of file TelnetServer.hpp.

◆ SP_TelnetSession

using SP_TelnetSession = std::shared_ptr<TelnetSession>

Definition at line 123 of file TelnetServer.hpp.

◆ VEC_SP_TelnetSession

using VEC_SP_TelnetSession = std::vector<SP_TelnetSession>

Definition at line 124 of file TelnetServer.hpp.

Function Documentation

◆ TelnetConnectedCallback()

void TelnetConnectedCallback ( const SP_TelnetSession & session)

Telnet session connection start callback

Parameters
[in]sessionHandle to session

Definition at line 780 of file TelnetServer.cpp.

781{
782 session->sendLine("\r\n"
783 "𝑲𝒆𝒆𝒑 𝒚𝒐𝒖𝒓 𝒆𝒚𝒆𝒔 𝒐𝒏 𝒕𝒉𝒆 𝒔𝒕𝒂𝒓𝒔 "
784 "𝒂𝒏𝒅 𝒚𝒐𝒖𝒓 𝒇𝒆𝒆𝒕 𝒐𝒏 𝒕𝒉𝒆 𝒈𝒓𝒐𝒖𝒏𝒅 "
785 "\r\n");
787}
void TelnetPrintAvailableCommands(const SP_TelnetSession &session)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TelnetMessageCallback()

bool TelnetMessageCallback ( const SP_TelnetSession & session,
const std::string & line )

Telnet session message received callback

Parameters
[in]sessionHandle to session
[in]lineReceived message

Definition at line 789 of file TelnetServer.cpp.

790{
791 spdlog::trace("Received message {}", line);
792
793 // Send received message for user terminal
794 session->sendLine(line);
795
796 if (line.empty())
797 {
798 return true;
799 }
800
801 // Process received message
802 switch (constHasher(line.c_str()))
803 {
804 case constHasher("Test Message"):
805 session->sendLine("OK");
806 return true;
807 case constHasher("help"):
809 return true;
810 case constHasher("disable log"):
811 session->sendLine("Default log mode enabled");
812 spdlog::set_level(spdlog::level::info);
813 return true;
814 case constHasher("disable log all"): // Internal use only
815 session->sendLine("Disabling all logs");
816 spdlog::set_level(spdlog::level::off);
817 return true;
818 case constHasher("enable log v"):
819 session->sendLine("Info log mode enabled");
820 spdlog::set_level(spdlog::level::info);
821 return true;
822 case constHasher("enable log vv"):
823 session->sendLine("Debug log mode enabled");
824 spdlog::set_level(spdlog::level::debug);
825 return true;
826 case constHasher("enable log vvv"):
827 session->sendLine("Trace log mode enabled");
828 spdlog::set_level(spdlog::level::trace);
829 return true;
830 case constHasher("ping"):
831 session->sendLine("pong");
832 return true;
833 case constHasher("version"):
834 session->sendLine(PROJECT_FULL_VERSION_STRING);
835 return true;
836 case constHasher("clear"):
837 session->sendLine(TELNET_CLEAR_SCREEN);
838 return true;
839 case constHasher("status"):
840 for (const auto &[service, statusFlag] : vCheckFlag)
841 {
842 std::ostringstream oss;
843 oss << std::left << std::setfill('.') << std::setw(KEY_WIDTH) << service + " " << std::setw(VAL_WIDTH)
844 << std::right << (statusFlag->_M_i ? " OK" : " Not Active");
845 session->sendLine(oss.str());
846 }
847 return true;
848 /* ################################################################################### */
849 /* ############################# MAKE MODIFICATIONS HERE ############################# */
850 /* ################################################################################### */
851
852 /* ################################################################################### */
853 /* ################################ END MODIFICATIONS ################################ */
854 /* ################################################################################### */
855 case constHasher("quit"):
856 session->sendLine("Closing connection");
857 session->sendLine("Goodbye!");
858 session->markTimeout();
859 return true;
860 default:
861 session->sendLine("Unknown command received");
862 return false;
863 }
864}
std::vector< std::pair< std::string, std::shared_ptr< std::atomic_flag > > > vCheckFlag
Global variable to check if the servers are running.
constexpr size_t constHasher(const char *s)
Definition Hasher.hpp:13
constexpr int VAL_WIDTH
constexpr int KEY_WIDTH
const std::string TELNET_CLEAR_SCREEN("\033[2J")
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TelnetTabCallback()

std::string TelnetTabCallback ( const SP_TelnetSession & session,
std::string_view line )

Telnet session TAB received callback

Parameters
[in]sessionHandle to session
[in]lineReceived message
Returns
std::string Command to complete

Definition at line 866 of file TelnetServer.cpp.

867{
868 std::string retval;
869
870 size_t ctr = 0;
871 std::ostringstream sStream;
872 for (const auto &[command, info] : telnetCommands)
873 {
874 if (command.rfind(line, 0) == 0)
875 {
876 ++ctr;
877 retval = command;
878 sStream << command << std::setw(KEY_WIDTH);
879 }
880 }
881 // Send suggestions if found any. If there is only one command retval will invoke completion
882 if (ctr != 1 && (!sStream.str().empty()))
883 {
884 session->sendLine(sStream.str());
885 retval = "";
886 }
887
888 return retval;
889}
const std::vector< std::pair< std::string, std::string > > telnetCommands
Here is the caller graph for this function: