From 46f98dff697bbd46907966e700bb3ebc25bcb031 Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Thu, 5 Aug 2021 13:07:53 -0700 Subject: [PATCH] Many changes --- Command.cpp | 22 +++++++++------------- Command.h | 4 ++-- CommandList.cpp | 46 +++++++++++++++++++++++----------------------- CommandList.h | 25 ++++++++++++++----------- ConsoleServer.h | 20 ++++++++++---------- ConsoleSession.cpp | 6 ++---- EPoll.cpp | 8 ++++---- EPoll.h | 2 +- TCPServer.cpp | 31 +++++++++++++++++++------------ TCPServer.h | 40 +++++++++++++++++++++++----------------- TCPSession.cpp | 6 +++--- 11 files changed, 110 insertions(+), 100 deletions(-) diff --git a/Command.cpp b/Command.cpp index 5a349e7..206976e 100644 --- a/Command.cpp +++ b/Command.cpp @@ -3,28 +3,24 @@ #include "CommandList.h" namespace core { - - int Command::processCommand(coreutils::ZString request, TCPSession *session, std::stringstream &data) { + + int Command::processCommand(coreutils::ZString &request, TCPSession &session) { return 0; } - + void Command::output(Session *session) {} - + bool Command::check(coreutils::ZString request) { - if(request.getLength() > 0) - if(request.getLength() == name.length()) - if(strncmp(request.getData(), name.c_str(), name.length()) == 0) - if(name.length() > 0) - return true; - return false; + return request[0].equals(name); + } - + void Command::setName(std::string name) { this->name = name; } - + std::string Command::getName() { return name; } - + } diff --git a/Command.h b/Command.h index 3680c1f..d82e8da 100644 --- a/Command.h +++ b/Command.h @@ -19,7 +19,7 @@ namespace core { /// a list of functions that can be invoked as a result of processing a request. /// - class Command : public Object { + class Command { public: @@ -50,7 +50,7 @@ namespace core { /// a non-zero value indicating an error condition. /// - virtual int processCommand(coreutils::ZString request, TCPSession *session, std::stringstream &data); + virtual int processCommand(coreutils::ZString &request, TCPSession &session); /// /// Specify the output that will occur to the specified session. diff --git a/CommandList.cpp b/CommandList.cpp index 1a40c5f..750cf84 100644 --- a/CommandList.cpp +++ b/CommandList.cpp @@ -2,43 +2,43 @@ #include "Log.h" namespace core { - + + CommandList::CommandList(std::string delimiter) : delimiter(delimiter) {} + void CommandList::add(Command &command, std::string name) { command.setName(name); commands.push_back(&command); } - - void CommandList::remove(Command &command) { - - } - - bool CommandList::processRequest(coreutils::ZString request, TCPSession *session, std::stringstream &data) { - if(session->grab != NULL) - return session->grab->processCommand(request, session, data); + + void CommandList::remove(Command &command) {} + + bool CommandList::processRequest(coreutils::ZString &request, TCPSession &session) { + if(session.grab != NULL) + return session.grab->processCommand(request, session); else { - coreutils::ZString function = request.getTokenExclude((char *)" "); - for(auto *command : commands) - if(command->check(function)) - return command->processCommand(request, session, data); + request.split(delimiter); + for(auto *command : commands) + if(command->check(request)) + return command->processCommand(request, session); } return false; } - - bool CommandList::grabInput(TCPSession *session, Command &command) { - session->grab = &command; + + bool CommandList::grabInput(TCPSession &session, Command &command) { + session.grab = &command; return true; } - void CommandList::clearGrab(TCPSession *session) { - session->grab = NULL; + void CommandList::clearGrab(TCPSession &session) { + session.grab = NULL; } - - int CommandList::processCommand(std::string request, TCPSession *session, std::stringstream &data) { - for(Command *command : commands) - data << command->getName() << std::endl; + + int CommandList::processCommand(coreutils::ZString &request, TCPSession &session) { + for(Command *command : commands) + session.out << command->getName() << std::endl; return true; } - + } diff --git a/CommandList.h b/CommandList.h index d57a09f..c541a43 100644 --- a/CommandList.h +++ b/CommandList.h @@ -11,18 +11,20 @@ namespace core { /// CommandList /// /// This object organizes Command objects into a list that is used - /// to parse an input and run the process associated with the - /// selected command. + /// to parse an input and run the process associated with the + /// selected command. /// - + class CommandList : public Command { - + public: - + + CommandList(std::string delimiter = ""); + /// /// Add a new command to the command list and assign a default search value. /// - + void add(Command &command, std::string name = ""); /// @@ -32,13 +34,13 @@ namespace core { void remove(Command &command); /// - /// Use this method to apply a parsed PString to the command set and execute + /// Use this method to apply a parsed ZString to the command set and execute /// the matching parameter. The selected command will return a true on a call /// to check(). If there is a handler that has a grab on the process handler /// then control is given to the process handler holding the grab on the input. /// - bool processRequest(coreutils::ZString request, TCPSession *session, std::stringstream &data); + bool processRequest(coreutils::ZString &request, TCPSession &session); /// /// Use grabInput() within a Command object to force the requesting handler to receive @@ -46,19 +48,19 @@ namespace core { /// back to normal command processing. /// - bool grabInput(TCPSession *session, Command &command); + bool grabInput(TCPSession &session, Command &command); /// /// /// - void clearGrab(TCPSession *session); + void clearGrab(TCPSession &session); /// /// /// - int processCommand(std::string request, TCPSession *session, std::stringstream &data); + int processCommand(coreutils::ZString &request, TCPSession &session); protected: @@ -67,6 +69,7 @@ namespace core { /// std::vector commands; + std::string delimiter; }; diff --git a/ConsoleServer.h b/ConsoleServer.h index 83349ad..dd21859 100644 --- a/ConsoleServer.h +++ b/ConsoleServer.h @@ -8,30 +8,30 @@ #include "LogListener.h" namespace core { - + class TCPSocket; class TCPSession; - + /// - /// /// - + /// + class ConsoleServer : public TCPServer, public coreutils::LogListener { - + public: - + // // // - + ConsoleServer(EPoll &ePoll, IPAddress address); - + // // // - + void logSend(std::string out) override; - + TCPSession * getSocketAccept(EPoll &ePoll) override; }; diff --git a/ConsoleSession.cpp b/ConsoleSession.cpp index 1ec787e..6ac48e8 100644 --- a/ConsoleSession.cpp +++ b/ConsoleSession.cpp @@ -52,7 +52,7 @@ namespace core { clearEOL(); out << "ConsoleSession"; setCursorLocation(2, 1); - setBackColor(BG_BLACK); + setBackColor(BG_BLACK); scrollArea(2, 16); status = PROMPT; protocol((char*)""); @@ -91,16 +91,14 @@ namespace core { saveCursor(); setCursorLocation(16, 1); restoreCursor(); - send(); } void ConsoleSession::doCommand(coreutils::ZString request) { saveCursor(); setCursorLocation(16, 1); out << "--> " << request << std::endl; - server.commands.processRequest(request, this, out); + server.commands.processRequest(request, *this); restoreCursor(); - send(); } } diff --git a/EPoll.cpp b/EPoll.cpp index e170b8f..69e22f9 100644 --- a/EPoll.cpp +++ b/EPoll.cpp @@ -84,12 +84,12 @@ namespace core { return epfd; } - int EPoll::processCommand(coreutils::ZString command, TCPSession *session, std::stringstream &data) { + int EPoll::processCommand(coreutils::ZString &request, TCPSession &session) { int sequence = 0; for(auto threadx : threads) { - data << "|" << ++sequence; - threadx.output(data); - data << "|" << std::endl; + session.out << "|" << ++sequence; + threadx.output(session.out); + session.out << "|" << std::endl; } return 1; } diff --git a/EPoll.h b/EPoll.h index 82d8846..047b698 100644 --- a/EPoll.h +++ b/EPoll.h @@ -110,7 +110,7 @@ namespace core { /// @param session the session to write the requested data to. /// - int processCommand(coreutils::ZString command, TCPSession *session, std::stringstream &data) override; ///send(); } - int TCPServer::processCommand(coreutils::ZString command, TCPSession *session, std::stringstream &data) { + int TCPServer::processCommand(coreutils::ZString &request, TCPSession &session) { int sequence = 0; for(auto *sessionx : sessions) { - data << "|" << ++sequence; - sessionx->output(data); - data << "|" << std::endl; + session.out << "|" << ++sequence; + sessionx->output(session.out); + session.out << "|" << std::endl; } return 1; } - - void TCPServer::sendToAll(std::stringstream &data, TCPSession *sender) { + + void TCPServer::sendToAll(std::stringstream &data) { for(auto session : sessions) - if(session != sender) + session->write(data.str()); + data.str(""); + } + + void TCPServer::sendToAll(std::stringstream &data, TCPSession &sender) { + for(auto session : sessions) + if(session != &sender) session->write(data.str()); data.str(""); } - - void TCPServer::sendToAll(std::stringstream &data, TCPSession *sender, SessionFilter &filter) { + + void TCPServer::sendToAll(std::stringstream &data, TCPSession &sender, SessionFilter filter) { for(auto session : sessions) if(filter.test(*session)) - if(session != sender) + if(session != &sender) session->write(data.str()); data.str(""); } diff --git a/TCPServer.h b/TCPServer.h index ce1b42b..24114e1 100644 --- a/TCPServer.h +++ b/TCPServer.h @@ -34,7 +34,7 @@ namespace core { /// @param commandName the name of the command used to invoke the status display for this object. /// - TCPServer(EPoll &ePoll, IPAddress address, std::string text = ""); + TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter = " ", std::string text = ""); /// /// The destructor for this object. @@ -87,6 +87,27 @@ namespace core { void output(TCPSession *session); ///