Working Version
This commit is contained in:
parent
46f98dff69
commit
20d5c99517
@ -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) {
|
||||
|
42
Command.h
42
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
|
||||
|
@ -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<Command *> commands;
|
||||
std::string delimiter;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
12
EPoll.h
12
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.
|
||||
///
|
||||
|
@ -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) {
|
||||
|
@ -85,7 +85,7 @@ namespace core {
|
||||
|
||||
void removeFromSessionList(TCPSession *session);
|
||||
|
||||
void output(TCPSession *session); ///<Output the consoles array to the console.
|
||||
void output(std::stringstream &out); ///<Output the consoles array to the console.
|
||||
|
||||
///
|
||||
///
|
||||
|
@ -22,7 +22,7 @@ namespace core {
|
||||
|
||||
void TCPSession::onRegistered() {
|
||||
onConnected();
|
||||
protocol((char *)"");
|
||||
protocol(coreutils::ZString(""));
|
||||
send();
|
||||
if(term)
|
||||
shutdown("termination requested");
|
||||
|
57
Thread.cpp
57
Thread.cpp
@ -2,23 +2,23 @@
|
||||
#include "EPoll.h"
|
||||
|
||||
namespace core {
|
||||
|
||||
|
||||
Thread::Thread(EPoll &ePoll) : ePoll(ePoll) {}
|
||||
|
||||
|
||||
Thread::~Thread() {}
|
||||
|
||||
|
||||
void Thread::start() {
|
||||
_thread = new std::thread(&Thread::run, this);
|
||||
_thread = new std::thread(&Thread::run, this);
|
||||
}
|
||||
|
||||
|
||||
void Thread::join() {
|
||||
_thread->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 << ".";
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user