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 {
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) {

View File

@ -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.

View File

@ -3,39 +3,39 @@
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) {
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);
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 *)" ");
request.split(delimiter);
for(auto *command : commands)
if(command->check(function))
return command->processCommand(request, session, data);
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) {
int CommandList::processCommand(coreutils::ZString &request, TCPSession &session) {
for(Command *command : commands)
data << command->getName() << std::endl;
session.out << command->getName() << std::endl;
return true;
}

View File

@ -19,6 +19,8 @@ namespace core {
public:
CommandList(std::string delimiter = "");
///
/// Add a new command to the command list and assign a default search value.
///
@ -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<Command *> commands;
std::string delimiter;
};

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -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; ///<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);

View File

@ -6,8 +6,9 @@
namespace core {
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string text)
: TCPSocket(ePoll, text) {
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, std::string text)
: TCPSocket(ePoll, text), commands(delimiter) {
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
int yes = 1;
setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
@ -68,27 +69,33 @@ namespace core {
session->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("");
}

View File

@ -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); ///<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:
///
@ -108,22 +129,7 @@ namespace core {
/// @param the session object to write the output to.
///
int processCommand(coreutils::ZString command, TCPSession *session, std::stringstream &data) 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);
int processCommand(coreutils::ZString &request, TCPSession &session) override;
private:

View File

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