Fixing command list delimiter depth.

This commit is contained in:
Brad Arant 2022-07-26 16:07:37 -07:00
parent 45aee7ab6f
commit 9af17daed6
5 changed files with 101 additions and 105 deletions

View File

@ -11,20 +11,20 @@ namespace core {
void CommandList::remove(Command &command) {}
bool CommandList::processRequest(coreutils::ZString &request, TCPSession &session) {
int CommandList::processRequest(coreutils::ZString &request, TCPSession &session) {
if(session.grab != NULL)
return session.grab->processCommand(request, session);
return session.grab->processCommand(request, session);
else {
if(request.equals(""))
return false;
request.split(delimiter, 10);
return 0;
request.split(delimiter, depth);
request.reset();
try {
auto command = commands.at(request[0].str());
return command->processCommand(request, session);
return command->processCommand(request, session);
}
catch(...) {
return false;
return 0;
}
}
return true;
@ -40,12 +40,9 @@ namespace core {
}
int CommandList::processCommand(coreutils::ZString &request, TCPSession &session) {
// for(Command *command : commands)
// session.out << command->getName() << std::endl;
// for(Command *command : commands)
// session.out << command->getName() << std::endl;
return true;
}
}

View File

@ -40,7 +40,7 @@ namespace core {
/// then control is given to the process handler holding the grab on the input.
///
bool processRequest(coreutils::ZString &request, TCPSession &session);
int processRequest(coreutils::ZString &request, TCPSession &session);
///
/// Use grabInput() within a Command object to force the requesting handler to receive

View File

@ -1 +0,0 @@
0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos:

View File

@ -9,24 +9,24 @@ namespace core {
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, int depth, std::string text)
: TCPSocket(ePoll, text), commands(delimiter, depth) {
commands.add(subscriptions, "publish");
commands.add(subscriptions, "unpublish");
commands.add(subscriptions, "subscribe");
commands.add(subscriptions, "unsubscribe");
commands.add(subscriptions, "catalog");
commands.add(subscriptions, "event");
commands.add(subscriptions, "publish");
commands.add(subscriptions, "unpublish");
commands.add(subscriptions, "subscribe");
commands.add(subscriptions, "unsubscribe");
commands.add(subscriptions, "catalog");
commands.add(subscriptions, "event");
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
int yes = 1;
setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
int yes = 1;
setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if(bind(getDescriptor(), address.getPointer(), address.addressLength) < 0)
throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno));
if(bind(getDescriptor(), address.getPointer(), address.addressLength) < 0)
throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno));
if(listen(getDescriptor(), 20) < 0)
throw coreutils::Exception("Error on listen to socket");
if(listen(getDescriptor(), 20) < 0)
throw coreutils::Exception("Error on listen to socket");
}
}
TCPServer::~TCPServer() {
coreutils::Log(coreutils::LOG_DEBUG_2) << "Closing server socket " << getDescriptor() << ".";
@ -70,13 +70,13 @@ namespace core {
void TCPServer::removeFromSessionList(TCPSession *session) {
std::vector<TCPSession *>::iterator cursor;
lock.lock();
lock.lock();
for(cursor = sessions.begin(); cursor < sessions.end(); ++cursor)
if(*cursor == session) {
sessions.erase(cursor);
break;
}
lock.unlock();
sessions.erase(cursor);
break;
}
lock.unlock();
}
void TCPServer::sessionErrorHandler(std::string errorString, std::stringstream &out) {
@ -103,22 +103,22 @@ namespace core {
void TCPServer::sendToAll(std::stringstream &data) {
for(auto session : sessions)
session->write(data.str());
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());
if(session != &sender)
session->write(data.str());
data.str("");
}
void TCPServer::sendToAll(std::stringstream &data, TCPSession &sender, SessionFilter filter) {
for(auto session : sessions)
if(filter.test(*session))
if(session != &sender)
session->write(data.str());
if(filter.test(*session))
if(session != &sender)
session->write(data.str());
data.str("");
}

View File

@ -18,8 +18,8 @@ namespace core {
void TCPSession::protocol(coreutils::ZString &data) {
if(data.getLength() != 0) {
if(!server.commands.processRequest(data, *this)) {
coreutils::Log(coreutils::LOG_DEBUG_1) << "Received data could not be parsed: " << data.str();
if(server.commands.processRequest(data, *this) == 0) {
coreutils::Log(coreutils::LOG_DEBUG_1) << "Received data could not be parsed: " << data.str();
}
}
}
@ -30,40 +30,40 @@ namespace core {
protocol(blank);
send();
if(term)
shutdown("termination requested");
shutdown("termination requested");
}
void TCPSession::onConnected() {}
void TCPSession::onDataReceived(coreutils::ZString &data) {
if(data.getLength() > 0) {
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength());
memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength());
lineBufferSize += data.getLength();
while(lineBufferSize > 0) {
if(blockSize == 0) {
lineLength = strcspn(lineBuffer, "\r\n");
if(lineLength == lineBufferSize)
break;
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength());
memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength());
lineBufferSize += data.getLength();
while(lineBufferSize > 0) {
if(blockSize == 0) {
lineLength = strcspn(lineBuffer, "\r\n");
if(lineLength == lineBufferSize)
break;
coreutils::ZString zLine(lineBuffer, lineLength);
onLineReceived(zLine);
if(lineBuffer[lineLength] == '\r')
++lineLength;
if(lineBuffer[lineLength] == '\n')
++lineLength;
lineBufferSize -= lineLength;
if(lineBufferSize > 0)
memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize);
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
onLineReceived(zLine);
if(lineBuffer[lineLength] == '\r')
++lineLength;
if(lineBuffer[lineLength] == '\n')
++lineLength;
lineBufferSize -= lineLength;
if(lineBufferSize > 0)
memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize);
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
} else if(lineBufferSize >= blockLength) {
coreutils::ZString zBlock(lineBuffer, blockLength);
onBlockReceived(zBlock);
lineBufferSize -= blockLength;
if(lineBufferSize > 0)
memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize);
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
}
}
coreutils::ZString zBlock(lineBuffer, blockLength);
onBlockReceived(zBlock);
lineBufferSize -= blockLength;
if(lineBufferSize > 0)
memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize);
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
}
}
}
}
@ -86,7 +86,7 @@ namespace core {
void TCPSession::send() {
if(out.tellp() > 0)
write(out.str());
write(out.str());
out.str("");
}