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 128 of file TelnetServer.hpp.

◆ FPTR_NewLineCallback

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

Definition at line 129 of file TelnetServer.hpp.

◆ FPTR_TabCallback

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

Definition at line 130 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 125 of file TelnetServer.hpp.

◆ VEC_SP_TelnetSession

using VEC_SP_TelnetSession = std::vector<SP_TelnetSession>

Definition at line 126 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 783 of file TelnetServer.cpp.

784{
785 session->sendLine("\r\n"
786 "𝑲𝒆𝒆𝒑 𝒚𝒐𝒖𝒓 𝒆𝒚𝒆𝒔 𝒐𝒏 𝒕𝒉𝒆 𝒔𝒕𝒂𝒓𝒔 "
787 "𝒂𝒏𝒅 𝒚𝒐𝒖𝒓 𝒇𝒆𝒆𝒕 𝒐𝒏 𝒕𝒉𝒆 𝒈𝒓𝒐𝒖𝒏𝒅 "
788 "\r\n");
790}
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 792 of file TelnetServer.cpp.

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

768{
769 // Print available commands
770 session->sendLine("");
771 session->sendLine("Available commands:");
772 session->sendLine("");
773 for (const auto &[command, info] : telnetCommands)
774 {
775 std::array<char, BUFSIZ> buffer{'\0'};
776 if (std::format_to_n(buffer.data(), BUFSIZ, "{:<25} : {}", command, info).size > 0)
777 {
778 session->sendLine(buffer.data());
779 }
780 }
781}
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 871 of file TelnetServer.cpp.

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