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 771 of file TelnetServer.cpp.

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

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

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

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