It works!!!

This commit is contained in:
Brad Arant 2021-08-11 21:38:22 -07:00
parent 20d5c99517
commit b7398afc05
16 changed files with 160 additions and 197 deletions

View File

@ -12,7 +12,7 @@ namespace core {
out << "Write your own command description here for the help system." << std::endl; out << "Write your own command description here for the help system." << std::endl;
} }
bool Command::check(coreutils::ZString request) { bool Command::check(coreutils::ZString &request) {
return request[0].equals(name); return request[0].equals(name);
} }

View File

@ -37,7 +37,7 @@ namespace core {
/// on this command. /// on this command.
/// ///
virtual bool check(coreutils::ZString request); virtual bool check(coreutils::ZString &request);
/// ///
/// This method is used to implement the functionality of the requested command. /// This method is used to implement the functionality of the requested command.

View File

@ -5,7 +5,7 @@
namespace core { namespace core {
ConsoleServer::ConsoleServer(EPoll &ePoll, IPAddress address) : TCPServer(ePoll, address, "Console") { ConsoleServer::ConsoleServer(EPoll &ePoll, IPAddress address) : TCPServer(ePoll, address, " ", "Console") {
coreutils::Log(this); coreutils::Log(this);
} }

View File

@ -3,78 +3,55 @@
namespace core { namespace core {
ConsoleSession::ConsoleSession(EPoll &ePoll, TCPServer &server) : TerminalSession(ePoll, server) { ConsoleSession::ConsoleSession(EPoll &ePoll, TCPServer &server) : TerminalSession(ePoll, server) {}
coreutils::Log(coreutils::LOG_DEBUG_2) << "Constructing ConsoleSession...";
}
ConsoleSession::~ConsoleSession() {} ConsoleSession::~ConsoleSession() {}
void ConsoleSession::protocol(coreutils::ZString data) { void ConsoleSession::protocol(coreutils::ZString &data) {
coreutils::ZString blank("");
coreutils::Log(coreutils::LOG_DEBUG_1) << "ConsoleSession protocol " << status;
switch (status) { switch (status) {
case WELCOME: case WELCOME:
setBackColor(BG_BLACK);
clear();
setCursorLocation(1, 1);
setBackColor(BG_BLUE);
clearEOL();
out << "ConsoleSession";
setCursorLocation(2, 1);
setBackColor(BG_BLACK);
status = LOGIN; status = LOGIN;
protocol((char*)""); protocol(blank);
break; break;
case LOGIN: case LOGIN:
setCursorLocation(3, 3); out << "User: ";
out << "Enter User Profile: ";
status = WAIT_USER_PROFILE; status = WAIT_USER_PROFILE;
break; break;
case WAIT_USER_PROFILE: case WAIT_USER_PROFILE:
status = PASSWORD; status = PASSWORD;
protocol((char*)""); protocol(blank);
break; break;
case PASSWORD: case PASSWORD:
setCursorLocation(4, 7); out << "Password: ";
out << "Enter Password: ";
status = WAIT_PASSWORD; status = WAIT_PASSWORD;
break; break;
case WAIT_PASSWORD: case WAIT_PASSWORD:
setBackColor(BG_BLACK);
clear();
setCursorLocation(1, 1);
setBackColor(BG_BLUE);
clearEOL();
out << "ConsoleSession";
setCursorLocation(2, 1);
setBackColor(BG_BLACK);
scrollArea(2, 16);
status = PROMPT; status = PROMPT;
protocol((char*)""); protocol(blank);
break; break;
case PROMPT: case PROMPT:
setCursorLocation(17, 1); out << (": ");
clearEOL();
out << ("--> ");
status = INPUT; status = INPUT;
break; break;
case INPUT: case INPUT:
command = data; command = data;
status = PROCESS; status = PROCESS;
protocol((char*)""); protocol(blank);
break; break;
case PROCESS: case PROCESS:
doCommand(command); doCommand(command);
status = command.equals((char *)"exit") ? DONE: PROMPT; status = command.equals((char *)"exit") ? DONE: PROMPT;
protocol((char*)""); protocol(blank);
break; break;
case DONE: case DONE:
@ -93,12 +70,8 @@ namespace core {
restoreCursor(); restoreCursor();
} }
void ConsoleSession::doCommand(coreutils::ZString request) { void ConsoleSession::doCommand(coreutils::ZString &request) {
saveCursor();
setCursorLocation(16, 1);
out << "--> " << request << std::endl;
server.commands.processRequest(request, *this); server.commands.processRequest(request, *this);
restoreCursor();
} }
} }

View File

@ -24,12 +24,12 @@ namespace core {
void writeLog(std::string data); void writeLog(std::string data);
protected: protected:
void protocol(coreutils::ZString data) override; void protocol(coreutils::ZString &data) override;
private: private:
enum Status {WELCOME, LOGIN, WAIT_USER_PROFILE, PASSWORD, WAIT_PASSWORD, PROMPT, INPUT, PROCESS, DONE}; enum Status {WELCOME, LOGIN, WAIT_USER_PROFILE, PASSWORD, WAIT_PASSWORD, PROMPT, INPUT, PROCESS, DONE};
Status status = WELCOME; Status status = WELCOME;
void doCommand(coreutils::ZString request); void doCommand(coreutils::ZString &request);
coreutils::ZString command; coreutils::ZString command;
}; };

View File

@ -7,16 +7,14 @@ namespace core {
EPoll::EPoll() : Command() { EPoll::EPoll() : Command() {
coreutils::Log(coreutils::LOG_DEBUG_2) << "EPoll object being constructed."; coreutils::Log(coreutils::LOG_DEBUG_2) << "EPoll starting.";
maxSockets = 1000; maxSockets = 1000;
epfd = epoll_create1(0); epfd = epoll_create1(0);
terminateThreads = false; terminateThreads = false;
} }
EPoll::~EPoll() { EPoll::~EPoll() {}
coreutils::Log(coreutils::LOG_DEBUG_2) << "BMAEPoll destructed.";
}
bool EPoll::start(int numberOfThreads, int maxSockets) { bool EPoll::start(int numberOfThreads, int maxSockets) {
@ -69,13 +67,11 @@ namespace core {
} }
bool EPoll::registerSocket(Socket *socket) { bool EPoll::registerSocket(Socket *socket) {
coreutils::Log(coreutils::LOG_DEBUG_3) << "Registering socket " << socket->getDescriptor() << ".";
enableSocket(socket); enableSocket(socket);
return true; return true;
} }
bool EPoll::unregisterSocket(Socket *socket) { bool EPoll::unregisterSocket(Socket *socket) {
coreutils::Log(coreutils::LOG_DEBUG_3) << "Unregistering socket " << socket->getDescriptor() << ".";
disableSocket(socket); disableSocket(socket);
return true; return true;
} }
@ -95,7 +91,6 @@ namespace core {
} }
void EPoll::enableSocket(Socket *socket) { void EPoll::enableSocket(Socket *socket) {
coreutils::Log(coreutils::LOG_DEBUG_4) << "Enabling socket " << socket->getDescriptor() << " for events.";
struct epoll_event event; struct epoll_event event;
event.data.ptr = socket; event.data.ptr = socket;
event.events = EPOLLIN | EPOLLONESHOT | EPOLLRDHUP | EPOLLET; event.events = EPOLLIN | EPOLLONESHOT | EPOLLRDHUP | EPOLLET;
@ -103,12 +98,10 @@ namespace core {
} }
void EPoll::disableSocket(Socket *socket) { void EPoll::disableSocket(Socket *socket) {
coreutils::Log(coreutils::LOG_DEBUG_4) << "Disabling socket " << socket->getDescriptor() << " from events.";
epoll_ctl(epfd, EPOLL_CTL_DEL, socket->getDescriptor(), NULL); epoll_ctl(epfd, EPOLL_CTL_DEL, socket->getDescriptor(), NULL);
} }
void EPoll::resetSocket(Socket *socket) { void EPoll::resetSocket(Socket *socket) {
coreutils::Log(coreutils::LOG_DEBUG_4) << "Resetting socket " << socket->getDescriptor() << " for read.";
struct epoll_event event; struct epoll_event event;
event.data.ptr = socket; event.data.ptr = socket;
event.events = EPOLLIN | EPOLLONESHOT | EPOLLRDHUP | EPOLLET; event.events = EPOLLIN | EPOLLONESHOT | EPOLLRDHUP | EPOLLET;

View File

@ -19,7 +19,7 @@ namespace core {
inotify_rm_watch(getDescriptor(), wd); inotify_rm_watch(getDescriptor(), wd);
} }
void INotify::onDataReceived(coreutils::ZString buffer) { void INotify::onDataReceived(coreutils::ZString &buffer) {
const struct inotify_event *event; const struct inotify_event *event;
char *ptr; char *ptr;
for (ptr = buffer.getData(); ptr < buffer.getData() + buffer.getLength(); for (ptr = buffer.getData(); ptr < buffer.getData() + buffer.getLength();

View File

@ -15,7 +15,7 @@ namespace core {
int addWatch(std::string watch); int addWatch(std::string watch);
void removeWatch(int wd); void removeWatch(int wd);
void onDataReceived(coreutils::ZString data) override; void onDataReceived(coreutils::ZString &data) override;
virtual void inAccess(std::string name) {} virtual void inAccess(std::string name) {}
virtual void inAttrib(std::string name) {} virtual void inAttrib(std::string name) {}

View File

@ -14,7 +14,7 @@ namespace core {
Socket::~Socket() { Socket::~Socket() {
free(buffer); free(buffer);
if(descriptor == -1) if(descriptor == -1)
return; return;
onUnregister(); onUnregister();
ePoll.unregisterSocket(this); ePoll.unregisterSocket(this);
coreutils::Log(coreutils::LOG_DEBUG_3) << "Socket destroyed for socket " << descriptor << "."; coreutils::Log(coreutils::LOG_DEBUG_3) << "Socket destroyed for socket " << descriptor << ".";
@ -23,8 +23,8 @@ namespace core {
void Socket::setDescriptor(int descriptor) { void Socket::setDescriptor(int descriptor) {
if((descriptor == -1) && (errno == 24)) { if((descriptor == -1) && (errno == 24)) {
shutdown("Too many files open"); shutdown("Too many files open");
coreutils::Exception("Too many files open. Refusing connection.");; throw coreutils::Exception("Too many files open. Refusing connection.");
} }
lock.lock(); lock.lock();
coreutils::Log(coreutils::LOG_DEBUG_3) << "Descriptor set to " << descriptor << " for Socket."; coreutils::Log(coreutils::LOG_DEBUG_3) << "Descriptor set to " << descriptor << " for Socket.";
@ -44,6 +44,7 @@ namespace core {
void Socket::setBufferSize(int length) { void Socket::setBufferSize(int length) {
this->length = length; this->length = length;
buffer = (char *)realloc(buffer, length); buffer = (char *)realloc(buffer, length);
} }
int Socket::getBufferSize() { int Socket::getBufferSize() {
@ -70,7 +71,8 @@ namespace core {
} }
if(event.events & EPOLLIN) { if(event.events & EPOLLIN) {
receiveData(buffer); coreutils::ZString zbuffer(buffer, length);
receiveData(zbuffer);
} }
if(event.events & EPOLLWRNORM) { if(event.events & EPOLLWRNORM) {
@ -93,11 +95,11 @@ namespace core {
throw coreutils::Exception("Need to override onDataReceived.", __FILE__, __LINE__, -1); throw coreutils::Exception("Need to override onDataReceived.", __FILE__, __LINE__, -1);
} }
void Socket::onDataReceived(coreutils::ZString data) { void Socket::onDataReceived(coreutils::ZString &data) {
onDataReceived(std::string(data.getData(), data.getLength())); onDataReceived(std::string(data.getData(), data.getLength()));
} }
void Socket::receiveData(coreutils::ZString buffer) { void Socket::receiveData(coreutils::ZString &buffer) {
if(buffer.getLength() <= 0) if(buffer.getLength() <= 0)
throw coreutils::Exception("Request to receive data with a zero buffer length.", __FILE__, __LINE__, -1); throw coreutils::Exception("Request to receive data with a zero buffer length.", __FILE__, __LINE__, -1);
@ -106,7 +108,8 @@ namespace core {
int error = -1; int error = -1;
if((len = ::read(getDescriptor(), buffer.getData(), buffer.getLength())) >= 0) { if((len = ::read(getDescriptor(), buffer.getData(), buffer.getLength())) >= 0) {
onDataReceived(buffer); coreutils::ZString zbuffer(buffer.getData(), len);
onDataReceived(zbuffer);
} }
else { else {
@ -120,7 +123,6 @@ namespace core {
case ENOTCONN: case ENOTCONN:
onDataReceived(std::string(buffer.getData(), 0)); onDataReceived(std::string(buffer.getData(), 0));
break; break;
case ECONNRESET: case ECONNRESET:
break; break;
@ -132,21 +134,18 @@ namespace core {
void Socket::writeSocket() { void Socket::writeSocket() {
if(fifo.size() > 0) { if(fifo.size() > 0) {
outlock.lock(); outlock.lock();
coreutils::Log(coreutils::LOG_DEBUG_3) << "Writing data to socket " << getDescriptor() << " [" << fifo.front() << "].";
::write(descriptor, fifo.front().c_str(), fifo.front().length()); ::write(descriptor, fifo.front().c_str(), fifo.front().length());
fifo.pop(); fifo.pop();
if(shutDown && !needsToWrite()) if(shutDown && !needsToWrite())
delete this; delete this;
outlock.unlock(); outlock.unlock();
} }
} }
int Socket::write(std::string data) { int Socket::write(std::string data) {
coreutils::Log(coreutils::LOG_DEBUG_3) << "Writing data to socket " << getDescriptor() << " buffer [" << data << "].";
outlock.lock(); outlock.lock();
fifo.emplace(data); fifo.emplace(data);
coreutils::Log(coreutils::LOG_DEBUG_4) << "Enabling write on socket " << getDescriptor() << " with " << fifo.size() << " entries to write.";
outlock.unlock(); outlock.unlock();
ePoll.resetSocket(this); ePoll.resetSocket(this);
return 1; return 1;
@ -157,7 +156,6 @@ namespace core {
} }
bool Socket::needsToWrite() { bool Socket::needsToWrite() {
coreutils::Log(coreutils::LOG_DEBUG_4) << "Socket " << getDescriptor() << " needs to write is " << (fifo.size() > 0) << ".";
return fifo.size() > 0; return fifo.size() > 0;
} }
@ -165,9 +163,8 @@ namespace core {
coreutils::Log(coreutils::LOG_DEBUG_2) << "Shutdown requested on socket " << descriptor << " with reason " << text << "."; coreutils::Log(coreutils::LOG_DEBUG_2) << "Shutdown requested on socket " << descriptor << " with reason " << text << ".";
shutDown = true; shutDown = true;
reset = false; reset = false;
if(!needsToWrite()) { if(!needsToWrite())
delete this; delete this;
}
} }

View File

@ -151,14 +151,14 @@ namespace core {
/// ///
/// ///
virtual void onDataReceived(coreutils::ZString data); virtual void onDataReceived(coreutils::ZString &data);
/// ///
/// receiveData will read the data from the socket and place it in the socket buffer. /// receiveData will read the data from the socket and place it in the socket buffer.
/// TLS layer overrides this to be able to read from SSL. /// TLS layer overrides this to be able to read from SSL.
/// ///
virtual void receiveData(coreutils::ZString buffer); virtual void receiveData(coreutils::ZString &buffer);
private: private:

View File

@ -14,7 +14,7 @@ namespace core {
data << "|" << ipAddress.getClientAddressAndPort(); data << "|" << ipAddress.getClientAddressAndPort();
} }
void TCPSession::protocol(coreutils::ZString data) { void TCPSession::protocol(coreutils::ZString &data) {
if(!server.commands.processRequest(data, *this)) 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);
@ -22,7 +22,8 @@ namespace core {
void TCPSession::onRegistered() { void TCPSession::onRegistered() {
onConnected(); onConnected();
protocol(coreutils::ZString("")); coreutils::ZString nothing("");
protocol(nothing);
send(); send();
if(term) if(term)
shutdown("termination requested"); shutdown("termination requested");
@ -30,50 +31,50 @@ namespace core {
void TCPSession::onConnected() {} void TCPSession::onConnected() {}
void TCPSession::onDataReceived(coreutils::ZString data) { void TCPSession::onDataReceived(coreutils::ZString &data) {
if(data.getLength() > 0) { if(data.getLength() > 0) {
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength()); lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength());
memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength()); memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength());
lineBufferSize += data.getLength(); lineBufferSize += data.getLength();
while(lineBufferSize > 0) { while(lineBufferSize > 0) {
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)); coreutils::ZString zLine(lineBuffer, lineLength);
if(lineBuffer[lineLength] == '\r') onLineReceived(zLine);
++lineLength; if(lineBuffer[lineLength] == '\r')
if(lineBuffer[lineLength] == '\n') ++lineLength;
++lineLength; if(lineBuffer[lineLength] == '\n')
lineBufferSize -= lineLength; ++lineLength;
if(lineBufferSize > 0) lineBufferSize -= lineLength;
memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize); if(lineBufferSize > 0)
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize);
} else { lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
if(lineBufferSize >= blockLength) { } else if(lineBufferSize >= blockLength) {
onBlockReceived(coreutils::ZString(lineBuffer, blockLength)); coreutils::ZString zBlock(lineBuffer, blockLength);
lineBufferSize -= blockLength; onBlockReceived(zBlock);
if(lineBufferSize > 0) lineBufferSize -= blockLength;
memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize); if(lineBufferSize > 0)
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize);
} lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
} }
} }
} }
} }
void TCPSession::setBlockSize(int blockSize) { void TCPSession::setBlockSize(int blockSize) {
this->blockSize = blockSize; this->blockSize = blockSize;
} }
void TCPSession::onLineReceived(coreutils::ZString line) { void TCPSession::onLineReceived(coreutils::ZString &line) {
protocol(line); protocol(line);
send(); send();
if(term) if(term)
shutdown("termination requested"); shutdown("termination requested");
} }
void TCPSession::onBlockReceived(coreutils::ZString block) { void TCPSession::onBlockReceived(coreutils::ZString &block) {
coreutils::Log(coreutils::LOG_DEBUG_3) << "[" << block.getLength() << "]"; coreutils::Log(coreutils::LOG_DEBUG_3) << "[" << block.getLength() << "]";
if(term) if(term)
shutdown("termination requested"); shutdown("termination requested");

View File

@ -79,7 +79,7 @@ namespace core {
/// received. If you need data split by line termination characters then /// received. If you need data split by line termination characters then
/// override the onLineReceived method instead. /// override the onLineReceived method instead.
/// ///
virtual void onDataReceived(coreutils::ZString data) override; virtual void onDataReceived(coreutils::ZString &data) override;
/// ///
/// Override the onLineReceived method to receive a string of characters that /// Override the onLineReceived method to receive a string of characters that
@ -88,7 +88,7 @@ namespace core {
/// this method explicitly using the class and member name. /// this method explicitly using the class and member name.
/// ///
virtual void onLineReceived(coreutils::ZString line); virtual void onLineReceived(coreutils::ZString &line);
/// ///
/// Override the onBlockReceived method to receive a string of characters that /// Override the onBlockReceived method to receive a string of characters that
@ -97,7 +97,7 @@ namespace core {
/// calls this method explicitly using the class and member name. /// calls this method explicitly using the class and member name.
/// ///
virtual void onBlockReceived(coreutils::ZString block); virtual void onBlockReceived(coreutils::ZString &block);
/// ///
/// This method is called from within the protocol method when protocol is called /// This method is called from within the protocol method when protocol is called
@ -117,7 +117,7 @@ namespace core {
/// data through the protocol method: LINE or BLOCK. /// data through the protocol method: LINE or BLOCK.
/// ///
virtual void protocol(coreutils::ZString data); virtual void protocol(coreutils::ZString &data);
/// ///
/// Use setBlockSize to set the amount of data that should be read at once from the /// Use setBlockSize to set the amount of data that should be read at once from the

View File

@ -15,7 +15,6 @@ namespace core {
setDescriptor(socket(AF_INET, SOCK_STREAM, 0)); setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
if(::connect(getDescriptor(), (struct sockaddr *)&address.addr, address.addressLength) == -1) if(::connect(getDescriptor(), (struct sockaddr *)&address.addr, address.addressLength) == -1)
throw coreutils::Exception("Error on connect to TCP socket."); throw coreutils::Exception("Error on connect to TCP socket.");
} }
void TCPSocket::output(std::stringstream &out) { void TCPSocket::output(std::stringstream &out) {

View File

@ -80,9 +80,9 @@ namespace core {
TLSSession::~TLSSession() {} TLSSession::~TLSSession() {}
void TLSSession::protocol(coreutils::ZString data) {} void TLSSession::protocol(coreutils::ZString &data) {}
void TLSSession::receiveData(coreutils::ZString buffer) { void TLSSession::receiveData(coreutils::ZString &buffer) {
int len; int len;
// int error = -1; // int error = -1;

View File

@ -35,10 +35,10 @@ namespace core {
/// ///
virtual void output(std::stringstream &out); virtual void output(std::stringstream &out);
virtual void protocol(coreutils::ZString data) override; virtual void protocol(coreutils::ZString &data) override;
protected: protected:
void receiveData(coreutils::ZString buffer) override; void receiveData(coreutils::ZString &buffer) override;
void onRegister(); void onRegister();
void onRegistered(); void onRegistered();

View File

@ -5,7 +5,7 @@ do
filename="${file%.*}" filename="${file%.*}"
list="$list $filename.o" list="$list $filename.o"
echo -n "Compiling $filename..." echo -n "Compiling $filename..."
g++ -g -c -I../CoreUtils $file g++ -g -c -I../CoreUtils $file &
if [ $? = '0' ] if [ $? = '0' ]
then then
echo "OK" echo "OK"