Many changes

This commit is contained in:
Brad Arant 2021-08-05 13:07:53 -07:00
parent 50bbfcc3c3
commit 46f98dff69
11 changed files with 110 additions and 100 deletions

View File

@ -4,19 +4,15 @@
namespace core { namespace core {
int Command::processCommand(coreutils::ZString request, TCPSession *session, std::stringstream &data) { int Command::processCommand(coreutils::ZString &request, TCPSession &session) {
return 0; return 0;
} }
void Command::output(Session *session) {} void Command::output(Session *session) {}
bool Command::check(coreutils::ZString request) { bool Command::check(coreutils::ZString request) {
if(request.getLength() > 0) return request[0].equals(name);
if(request.getLength() == name.length())
if(strncmp(request.getData(), name.c_str(), name.length()) == 0)
if(name.length() > 0)
return true;
return false;
} }
void Command::setName(std::string name) { void Command::setName(std::string name) {

View File

@ -19,7 +19,7 @@ namespace core {
/// a list of functions that can be invoked as a result of processing a request. /// a list of functions that can be invoked as a result of processing a request.
/// ///
class Command : public Object { class Command {
public: public:
@ -50,7 +50,7 @@ namespace core {
/// a non-zero value indicating an error condition. /// 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. /// Specify the output that will occur to the specified session.

View File

@ -3,39 +3,39 @@
namespace core { namespace core {
CommandList::CommandList(std::string delimiter) : delimiter(delimiter) {}
void CommandList::add(Command &command, std::string name) { void CommandList::add(Command &command, std::string name) {
command.setName(name); command.setName(name);
commands.push_back(&command); commands.push_back(&command);
} }
void CommandList::remove(Command &command) { void CommandList::remove(Command &command) {}
} bool CommandList::processRequest(coreutils::ZString &request, TCPSession &session) {
if(session.grab != NULL)
bool CommandList::processRequest(coreutils::ZString request, TCPSession *session, std::stringstream &data) { return session.grab->processCommand(request, session);
if(session->grab != NULL)
return session->grab->processCommand(request, session, data);
else { else {
coreutils::ZString function = request.getTokenExclude((char *)" "); request.split(delimiter);
for(auto *command : commands) for(auto *command : commands)
if(command->check(function)) if(command->check(request))
return command->processCommand(request, session, data); return command->processCommand(request, session);
} }
return false; return false;
} }
bool CommandList::grabInput(TCPSession *session, Command &command) { bool CommandList::grabInput(TCPSession &session, Command &command) {
session->grab = &command; session.grab = &command;
return true; return true;
} }
void CommandList::clearGrab(TCPSession *session) { void CommandList::clearGrab(TCPSession &session) {
session->grab = NULL; session.grab = NULL;
} }
int CommandList::processCommand(std::string request, TCPSession *session, std::stringstream &data) { int CommandList::processCommand(coreutils::ZString &request, TCPSession &session) {
for(Command *command : commands) for(Command *command : commands)
data << command->getName() << std::endl; session.out << command->getName() << std::endl;
return true; return true;
} }

View File

@ -19,6 +19,8 @@ namespace core {
public: public:
CommandList(std::string delimiter = "");
/// ///
/// Add a new command to the command list and assign a default search value. /// Add a new command to the command list and assign a default search value.
/// ///
@ -32,13 +34,13 @@ namespace core {
void remove(Command &command); 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 /// 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 /// 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. /// 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 /// Use grabInput() within a Command object to force the requesting handler to receive
@ -46,19 +48,19 @@ namespace core {
/// back to normal command processing. /// 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: protected:
@ -67,6 +69,7 @@ namespace core {
/// ///
std::vector<Command *> commands; std::vector<Command *> commands;
std::string delimiter;
}; };

View File

@ -91,16 +91,14 @@ namespace core {
saveCursor(); saveCursor();
setCursorLocation(16, 1); setCursorLocation(16, 1);
restoreCursor(); restoreCursor();
send();
} }
void ConsoleSession::doCommand(coreutils::ZString request) { void ConsoleSession::doCommand(coreutils::ZString request) {
saveCursor(); saveCursor();
setCursorLocation(16, 1); setCursorLocation(16, 1);
out << "--> " << request << std::endl; out << "--> " << request << std::endl;
server.commands.processRequest(request, this, out); server.commands.processRequest(request, *this);
restoreCursor(); restoreCursor();
send();
} }
} }

View File

@ -84,12 +84,12 @@ namespace core {
return epfd; return epfd;
} }
int EPoll::processCommand(coreutils::ZString command, TCPSession *session, std::stringstream &data) { int EPoll::processCommand(coreutils::ZString &request, TCPSession &session) {
int sequence = 0; int sequence = 0;
for(auto threadx : threads) { for(auto threadx : threads) {
data << "|" << ++sequence; session.out << "|" << ++sequence;
threadx.output(data); threadx.output(session.out);
data << "|" << std::endl; session.out << "|" << std::endl;
} }
return 1; return 1;
} }

View File

@ -110,7 +110,7 @@ namespace core {
/// @param session the session to write the requested data to. /// @param session the session to write the requested data to.
/// ///
int processCommand(coreutils::ZString command, TCPSession *session, std::stringstream &data) override; ///<Output the threads array to the console. int processCommand(coreutils::ZString &request, TCPSession &session) override; ///<Output the threads array to the console.
void resetSocket(Socket *socket); void resetSocket(Socket *socket);

View File

@ -6,8 +6,9 @@
namespace core { namespace core {
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string text) TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, std::string text)
: TCPSocket(ePoll, text) { : TCPSocket(ePoll, text), commands(delimiter) {
setDescriptor(socket(AF_INET, SOCK_STREAM, 0)); setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
int yes = 1; int yes = 1;
setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
@ -68,27 +69,33 @@ namespace core {
session->send(); session->send();
} }
int TCPServer::processCommand(coreutils::ZString command, TCPSession *session, std::stringstream &data) { int TCPServer::processCommand(coreutils::ZString &request, TCPSession &session) {
int sequence = 0; int sequence = 0;
for(auto *sessionx : sessions) { for(auto *sessionx : sessions) {
data << "|" << ++sequence; session.out << "|" << ++sequence;
sessionx->output(data); sessionx->output(session.out);
data << "|" << std::endl; session.out << "|" << std::endl;
} }
return 1; return 1;
} }
void TCPServer::sendToAll(std::stringstream &data, TCPSession *sender) { void TCPServer::sendToAll(std::stringstream &data) {
for(auto session : sessions) 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()); session->write(data.str());
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) for(auto session : sessions)
if(filter.test(*session)) if(filter.test(*session))
if(session != sender) if(session != &sender)
session->write(data.str()); session->write(data.str());
data.str(""); data.str("");
} }

View File

@ -34,7 +34,7 @@ namespace core {
/// @param commandName the name of the command used to invoke the status display for this object. /// @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. /// The destructor for this object.
@ -87,6 +87,27 @@ namespace core {
void output(TCPSession *session); ///<Output the consoles array to the console. void output(TCPSession *session); ///<Output the consoles array to the console.
///
///
///
void sendToAll(std::stringstream &out);
///
/// Use this sendToAll method to output the contents of the out stream
/// to all the connections on the server excluding the sender session.
///
void sendToAll(std::stringstream &out, TCPSession &sender, SessionFilter filter);
///
/// Use this sendToAll method to output the contents of the out stream
/// to all the connections on the server excluding the sender session
/// and the entries identified by the passed in filter object.
///
void sendToAll(std::stringstream &out, TCPSession &sender);
protected: protected:
/// ///
@ -108,22 +129,7 @@ namespace core {
/// @param the session object to write the output to. /// @param the session object to write the output to.
/// ///
int processCommand(coreutils::ZString command, TCPSession *session, std::stringstream &data) override; int processCommand(coreutils::ZString &request, TCPSession &session) override;
///
/// Use this sendToAll method to output the contents of the out stream
/// to all the connections on the server excluding the sender session.
///
void sendToAll(std::stringstream &out, TCPSession *sender, SessionFilter &filter);
///
/// Use this sendToAll method to output the contents of the out stream
/// to all the connections on the server excluding the sender session
/// and the entries identified by the passed in filter object.
///
void sendToAll(std::stringstream &out, TCPSession *sender);
private: private:

View File

@ -15,7 +15,7 @@ namespace core {
} }
void TCPSession::protocol(coreutils::ZString data) { void TCPSession::protocol(coreutils::ZString data) {
if(!server.commands.processRequest(data, this, out)) if(!server.commands.processRequest(data, *this))
if(data.getLength() != 0) if(data.getLength() != 0)
server.sessionErrorHandler("Invalid data received.", out); server.sessionErrorHandler("Invalid data received.", out);
} }
@ -25,7 +25,7 @@ namespace core {
protocol((char *)""); protocol((char *)"");
send(); send();
if(term) if(term)
shutdown("termination requested"); shutdown("termination requested");
} }
void TCPSession::onConnected() {} void TCPSession::onConnected() {}
@ -39,7 +39,7 @@ namespace core {
if(blockSize == 0) { if(blockSize == 0) {
lineLength = strcspn(lineBuffer, "\r\n"); lineLength = strcspn(lineBuffer, "\r\n");
if(lineLength == lineBufferSize) if(lineLength == lineBufferSize)
break; break;
onLineReceived(coreutils::ZString(lineBuffer, lineLength)); onLineReceived(coreutils::ZString(lineBuffer, lineLength));
if(lineBuffer[lineLength] == '\r') if(lineBuffer[lineLength] == '\r')
++lineLength; ++lineLength;