From 20d5c9951707d4875770445f2166fef38ea062c3 Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Sat, 7 Aug 2021 11:47:54 -0700 Subject: [PATCH] Working Version --- Command.cpp | 5 +++-- Command.h | 42 +++++++++++++++++----------------- CommandList.h | 26 ++++++++++----------- ConsoleServer.cpp | 12 +++++----- EPoll.cpp | 2 +- EPoll.h | 12 +++++----- TCPServer.cpp | 6 ++--- TCPServer.h | 2 +- TCPSession.cpp | 2 +- Thread.cpp | 57 +++++++++++++++++++++++------------------------ 10 files changed, 82 insertions(+), 84 deletions(-) diff --git a/Command.cpp b/Command.cpp index 206976e..af3f10d 100644 --- a/Command.cpp +++ b/Command.cpp @@ -8,11 +8,12 @@ namespace core { return 0; } - void Command::output(Session *session) {} + void Command::output(std::stringstream &out) { + out << "Write your own command description here for the help system." << std::endl; + } bool Command::check(coreutils::ZString request) { return request[0].equals(name); - } void Command::setName(std::string name) { diff --git a/Command.h b/Command.h index d82e8da..6915256 100644 --- a/Command.h +++ b/Command.h @@ -9,36 +9,36 @@ namespace core { class CommandList; - + class Session; - + /// /// Command /// /// Use the Command object in combination with a CommandList object to maintain /// a list of functions that can be invoked as a result of processing a request. /// - + class Command { - - public: - + + public: + /// /// Implement check method to provide a special check rule upon the request to see - /// if the command should be processed. + /// if the command should be processed. /// /// The default rule is to verify that the first token in the request string matches /// the name given on the registration of the command to the CommandList. This can /// be overridden by implementing the check() method to perform the test and return /// the condition of the command. /// - /// @param request The request passed to the parser to check the rule. + /// @param request The request passed to the parser to check the rule. /// @return Return true to execute the command. Returning false will cause no action /// on this command. /// - + virtual bool check(coreutils::ZString request); - + /// /// This method is used to implement the functionality of the requested command. /// This pure virtual function must be implemented in your inheriting object. @@ -49,17 +49,17 @@ namespace core { /// @return Returns 0 if execution of the command was successful. Otherwise returns /// a non-zero value indicating an error condition. /// - + virtual int processCommand(coreutils::ZString &request, TCPSession &session); - + /// /// Specify the output that will occur to the specified session. /// /// @param session The session that will receive the output. /// - - virtual void output(Session *session); - + + virtual void output(std::stringstream &out); + /// /// Set the name of this command used in default rule checking during request parsing. /// NOTE: You do not need to call this under normal conditions as adding a Command @@ -68,16 +68,16 @@ namespace core { /// /// @param name Specify the name of this command for default parsing. /// - + void setName(std::string name); - + std::string getName(); - + private: - std::string name; - + std::string name; + }; - + } #endif diff --git a/CommandList.h b/CommandList.h index c541a43..9e506ef 100644 --- a/CommandList.h +++ b/CommandList.h @@ -26,20 +26,20 @@ namespace core { /// void add(Command &command, std::string name = ""); - + /// /// Remove a command object from the command list. /// - + void remove(Command &command); - + /// /// 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); /// @@ -47,32 +47,32 @@ namespace core { /// all further input from the socket. Use releaseGrab() method to release the session /// back to normal command processing. /// - + bool grabInput(TCPSession &session, Command &command); /// /// /// - + void clearGrab(TCPSession &session); - + /// /// /// - + int processCommand(coreutils::ZString &request, TCPSession &session); - + protected: - + /// /// The vector of all registered commands. /// - + std::vector commands; std::string delimiter; - + }; - + } #endif diff --git a/ConsoleServer.cpp b/ConsoleServer.cpp index 68a46c3..3bbacd4 100644 --- a/ConsoleServer.cpp +++ b/ConsoleServer.cpp @@ -4,18 +4,18 @@ #include "Log.h" namespace core { - + ConsoleServer::ConsoleServer(EPoll &ePoll, IPAddress address) : TCPServer(ePoll, address, "Console") { - coreutils::Log(this); + coreutils::Log(this); } - + void ConsoleServer::logSend(std::string out) { - for(auto *session : sessions) + for(auto *session : sessions) ((ConsoleSession *)session)->writeLog(out); } - + TCPSession * ConsoleServer::getSocketAccept(EPoll &ePoll) { return new ConsoleSession(ePoll, *this); } - + } diff --git a/EPoll.cpp b/EPoll.cpp index 69e22f9..5cccb96 100644 --- a/EPoll.cpp +++ b/EPoll.cpp @@ -113,7 +113,7 @@ namespace core { event.data.ptr = socket; event.events = EPOLLIN | EPOLLONESHOT | EPOLLRDHUP | EPOLLET; if(socket->needsToWrite()) - event.events |= EPOLLWRNORM; + event.events |= EPOLLWRNORM; epoll_ctl(epfd, EPOLL_CTL_MOD, socket->getDescriptor(), &event); } diff --git a/EPoll.h b/EPoll.h index 047b698..dcd389b 100644 --- a/EPoll.h +++ b/EPoll.h @@ -27,17 +27,17 @@ namespace core { /// Threads are used to establish a read queue for epoll. The desired number of threads (or /// queues) is established by a parameter on the start method. /// - + class EPoll : public Command { - - public: - + + public: + /// /// The constructor for the BMAEPoll object. /// - + EPoll(); - + /// /// The destructor for the BMAEPoll object. /// diff --git a/TCPServer.cpp b/TCPServer.cpp index 1abc839..edba41d 100644 --- a/TCPServer.cpp +++ b/TCPServer.cpp @@ -63,10 +63,8 @@ namespace core { return new TCPSession(ePoll, *this); } - void TCPServer::output(TCPSession *session) { - std::stringstream out; - out << "|" << session->ipAddress.getClientAddressAndPort(); - session->send(); + void TCPServer::output(std::stringstream &out) { + out << "Use the 'help' command to list the commands for this server." << std::endl; } int TCPServer::processCommand(coreutils::ZString &request, TCPSession &session) { diff --git a/TCPServer.h b/TCPServer.h index 24114e1..21c4560 100644 --- a/TCPServer.h +++ b/TCPServer.h @@ -85,7 +85,7 @@ namespace core { void removeFromSessionList(TCPSession *session); - void output(TCPSession *session); ///join(); } - + std::string Thread::getStatus() { - return status; + return status; } - + pid_t Thread::getThreadId() { return threadId; } @@ -26,44 +26,43 @@ namespace core { int Thread::getCount() { return count; } - - void Thread::output(std::stringstream &data) { - data << "|" << getThreadId(); + + void Thread::output(std::stringstream &data) { + data << "|" << getThreadId(); data << "|" << getStatus(); - data << "|" << getCount(); + data << "|" << getCount(); } - - + void Thread::run() { - + threadId = syscall(SYS_gettid); - + coreutils::Log(coreutils::LOG_DEBUG_1) << "Thread started with thread id " << threadId << "."; - + count = 0; - + struct epoll_event events[50]; - + while(1) { - + if(ePoll.isStopping()) break; - - status = "WAITING"; + + status = "WAITING"; int rc = epoll_wait(ePoll.getDescriptor(), events, 50, -1); status = "RUNNING"; - + if(rc < 0) { // TODO: Make log entry indicating status received and ignore for now. - } else if(rc == 0) { + } else if(rc == 0) { break; - } else if(rc > 0) { - for(int ix = 0; ix < rc; ++ix) { - ++count; + } else if(rc > 0) { + for(int ix = 0; ix < rc; ++ix) { + ++count; if(((Socket *)events[ix].data.ptr)->eventReceived(events[ix])) ePoll.resetSocket((Socket *)events[ix].data.ptr); - } - } + } + } } coreutils::Log(coreutils::LOG_DEBUG_1) << "Thread ending with thread id " << threadId << "."; }