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 TelnetPrintAvailableCommands (const SP_TelnetSession &session)
 
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 770 of file TelnetServer.cpp.

771{
772 session->sendLine("\r\n"
773 "𝑲𝒆𝒆𝒑 𝒚𝒐𝒖𝒓 𝒆𝒚𝒆𝒔 𝒐𝒏 𝒕𝒉𝒆 𝒔𝒕𝒂𝒓𝒔 "
774 "𝒂𝒏𝒅 𝒚𝒐𝒖𝒓 𝒇𝒆𝒆𝒕 𝒐𝒏 𝒕𝒉𝒆 𝒈𝒓𝒐𝒖𝒏𝒅 "
775 "\r\n");
777}
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 779 of file TelnetServer.cpp.

780{
781 spdlog::trace("Received message {}", line);
782
783 // Send received message for user terminal
784 session->sendLine(line);
785
786 if (line.empty())
787 {
788 return true;
789 }
790
791 // Process received message
792 switch (constHasher(line.c_str()))
793 {
794 case constHasher("Test Message"):
795 session->sendLine("OK");
796 return true;
797 case constHasher("help"):
799 return true;
800 case constHasher("disable log"):
801 session->sendLine("Default log mode enabled");
802#ifdef NDEBUG
803 spdlog::set_level(spdlog::level::warn);
804#else
805 spdlog::set_level(spdlog::level::info);
806#endif
807 return true;
808 case constHasher("disable log all"): // Internal use only
809 session->sendLine("Disabling all logs");
810 spdlog::set_level(spdlog::level::off);
811 return true;
812 case constHasher("enable log v"):
813 session->sendLine("Info log mode enabled");
814 spdlog::set_level(spdlog::level::info);
815 return true;
816 case constHasher("enable log vv"):
817 session->sendLine("Debug log mode enabled");
818 spdlog::set_level(spdlog::level::debug);
819 return true;
820 case constHasher("enable log vvv"):
821 session->sendLine("Trace log mode enabled");
822 spdlog::set_level(spdlog::level::trace);
823 return true;
824 case constHasher("ping"):
825 session->sendLine("pong");
826 return true;
827 case constHasher("version"):
828 session->sendLine(PROJECT_FULL_VERSION_STRING);
829 return true;
830 case constHasher("clear"):
831 session->sendLine(TELNET_CLEAR_SCREEN);
832 return true;
833 case constHasher("status"):
834 for (const auto &[service, statusFlag] : vCheckFlag)
835 {
836 session->sendLine(std::format("{:.<{}}{:.>{}} ", service + " ", KEY_WIDTH,
837 (statusFlag->_M_i ? " OK" : " Not Active"), VAL_WIDTH));
838 }
839 return true;
840 /* ################################################################################### */
841 /* ############################# MAKE MODIFICATIONS HERE ############################# */
842 /* ################################################################################### */
843
844 /* ################################################################################### */
845 /* ################################ END MODIFICATIONS ################################ */
846 /* ################################################################################### */
847 case constHasher("quit"):
848 session->sendLine("Closing connection");
849 session->sendLine("Goodbye!");
850 session->markTimeout();
851 return true;
852 default:
853 session->sendLine("Unknown command received");
854 return false;
855 }
856}
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:

◆ TelnetPrintAvailableCommands()

void TelnetPrintAvailableCommands ( const SP_TelnetSession & session)

Print available commands to the session

Parameters
[in]sessionHandle to session

Definition at line 754 of file TelnetServer.cpp.

755{
756 // Print available commands
757 session->sendLine("");
758 session->sendLine("Available commands:");
759 session->sendLine("");
760 for (const auto &[command, info] : telnetCommands)
761 {
762 std::array<char, BUFSIZ> buffer{'\0'};
763 if (std::format_to_n(buffer.data(), BUFSIZ, "{:<25} : {}", command, info).size > 0)
764 {
765 session->sendLine(buffer.data());
766 }
767 }
768}
const std::vector< std::pair< std::string, std::string > > telnetCommands
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 858 of file TelnetServer.cpp.

859{
860 std::string retval;
861
862 size_t ctr = 0;
863 std::ostringstream sStream;
864 for (const auto &[command, info] : telnetCommands)
865 {
866 if (command.starts_with(line))
867 {
868 ++ctr;
869 retval = command;
870 sStream << command << std::setw(KEY_WIDTH);
871 }
872 }
873 // Send suggestions if found any. If there is only one command retval will invoke completion
874 if (ctr != 1 && (!sStream.str().empty()))
875 {
876 session->sendLine(sStream.str());
877 retval = "";
878 }
879
880 return retval;
881}
Here is the caller graph for this function: