diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-09-10 03:26:49 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-09-10 03:26:49 +0200 |
commit | 707344b4d161cb5a11e7f78d7622cb7cd1a791f6 (patch) | |
tree | 8879f226c610e25fc532d64f40f2fe382e6d8371 | |
parent | 8dd9bc2815347435c8f92bb329a0209b50660618 (diff) | |
download | mad-707344b4d161cb5a11e7f78d7622cb7cd1a791f6.tar mad-707344b4d161cb5a11e7f78d7622cb7cd1a791f6.zip |
Client erweitert; Status-Befehl angefangen
-rw-r--r-- | src/Client/CommandParser.cpp | 116 | ||||
-rw-r--r-- | src/Client/CommandParser.h (renamed from src/Client/RequestProcessor.h) | 38 | ||||
-rw-r--r-- | src/Client/Makefile.am | 4 | ||||
-rw-r--r-- | src/Client/Makefile.in | 8 | ||||
-rw-r--r-- | src/Client/RequestProcessor.cpp | 33 | ||||
-rw-r--r-- | src/Common/Request/CoreStatusRequest.h | 82 | ||||
-rw-r--r-- | src/Common/Request/DisconnectRequest.h | 10 | ||||
-rw-r--r-- | src/Common/Request/Makefile.am | 2 | ||||
-rw-r--r-- | src/Common/Request/Makefile.in | 2 | ||||
-rw-r--r-- | src/Common/Util.h | 58 | ||||
-rw-r--r-- | src/Core/ConnectionManager.cpp | 2 | ||||
-rw-r--r-- | src/Core/RequestHandler/CoreStatusRequestHandler.h | 58 | ||||
-rw-r--r-- | src/Core/RequestHandler/Makefile.am | 2 | ||||
-rw-r--r-- | src/Core/RequestHandler/Makefile.in | 2 | ||||
-rw-r--r-- | src/Net/Packet.h | 3 | ||||
-rw-r--r-- | src/madc.cpp | 51 |
16 files changed, 334 insertions, 137 deletions
diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp new file mode 100644 index 0000000..ebf4b2c --- /dev/null +++ b/src/Client/CommandParser.cpp @@ -0,0 +1,116 @@ +/* + * CommandParser.cpp + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "CommandParser.h" +#include <Common/Request/CoreStatusRequest.h> +#include <Common/Request/DisconnectRequest.h> + +#include <iostream> + +namespace Mad { +namespace Client { + +bool CommandParser::split(const std::string &str, std::vector<std::string> &ret) { + std::string temp; + bool quoteSingle = false, quoteDouble = false, escape = false; + + size_t beg = 0; + + for(size_t cur = 0; cur < str.length(); ++cur) { + if(!escape) { + if(str[cur] == ' ' && !quoteSingle && !quoteDouble) { + if(cur == beg && temp.empty()) { + ++beg; + } + else { + temp += str.substr(beg, cur-beg); + ret.push_back(temp); + temp.clear(); + beg = cur+1; + } + + continue; + } + + if(str[cur] == '"' && !quoteSingle) { + temp += str.substr(beg, cur-beg); + beg = cur+1; + + quoteDouble = !quoteDouble; + continue; + } + if(str[cur] == '\'' && !quoteDouble) { + temp += str.substr(beg, cur-beg); + beg = cur+1; + + quoteSingle = !quoteSingle; + continue; + } + + if(str[cur] == '\\') { + escape = true; + continue; + } + } + + if(escape && ((!quoteSingle && !quoteDouble) || (quoteSingle && str[cur] == '\'') || (quoteDouble && (str[cur] == '"' || str[cur] == '\\')))) { + temp += str.substr(beg, cur-beg-1); + beg = cur; + } + + escape = false; + } + + temp += str.substr(beg, std::string::npos); + ret.push_back(temp); + + return true; +} + +bool CommandParser::parse(const std::string &cmd) { + std::vector<std::string> splitCmd; + + split(cmd, splitCmd); + + if(splitCmd.empty()) + return true; + + if(splitCmd[0] == "quit") { + requestDisconnect(); + } + else if(splitCmd[0] == "status") { + activeRequests++; + Common::Request::CoreStatusRequest::send(connection, *requestManager, sigc::mem_fun(this, &CommandParser::requestFinished)); + } + else { + std::cerr << "Unknown command \"" << splitCmd[0] << "\"." << std::endl; + } + + return true; +} + +void CommandParser::requestDisconnect() { + activeRequests++; + disconnect = true; + + Common::Request::DisconnectRequest::send(connection, *requestManager, sigc::mem_fun(this, &CommandParser::requestFinished)); +} + +} +} diff --git a/src/Client/RequestProcessor.h b/src/Client/CommandParser.h index 9262058..81c3289 100644 --- a/src/Client/RequestProcessor.h +++ b/src/Client/CommandParser.h @@ -1,5 +1,5 @@ /* - * RequestProcessor.h + * CommandParser.h * * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> * @@ -17,8 +17,12 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef MAD_CLIENT_REQUESTPROCESSOR_H_ -#define MAD_CLIENT_REQUESTPROCESSOR_H_ +#ifndef MAD_CLIENT_COMMANDPARSER_H_ +#define MAD_CLIENT_COMMANDPARSER_H_ + +#include <sigc++/signal.h> +#include <string> +#include <vector> namespace Mad { @@ -32,18 +36,38 @@ class Connection; namespace Client { -class RequestProcessor { +class CommandParser { private: + sigc::signal<void> finished; + Common::RequestManager *requestManager; Net::Connection *connection; + unsigned int activeRequests; + bool disconnect; + + bool split(const std::string &str, std::vector<std::string> &ret); + + void requestFinished() { + activeRequests--; + + finished(); + } + public: - RequestProcessor(Common::RequestManager *requestManager0, Net::Connection *connection0); + CommandParser(Common::RequestManager *requestManager0, Net::Connection *connection0) : requestManager(requestManager0), connection(connection0), activeRequests(0), disconnect(false) {} + + bool requestsActive() {return (activeRequests > 0);} + bool willDisconnect() {return disconnect;} + + bool parse(const std::string &cmd); + + void requestDisconnect(); - bool requestDisconnect(); + sigc::signal<void> signalFinished() const {return finished;} }; } } -#endif /* MAD_CLIENT_REQUESTPROCESSOR_H_ */ +#endif /* MAD_CLIENT_COMMANDPARSER_H_ */ diff --git a/src/Client/Makefile.am b/src/Client/Makefile.am index b8a74fb..0a1d8e3 100644 --- a/src/Client/Makefile.am +++ b/src/Client/Makefile.am @@ -1,6 +1,6 @@ SUBDIRS = Request noinst_LTLIBRARIES = libclient.la -libclient_la_SOURCES = RequestProcessor.cpp +libclient_la_SOURCES = CommandParser.cpp -noinst_HEADERS = RequestProcessor.h +noinst_HEADERS = CommandParser.h diff --git a/src/Client/Makefile.in b/src/Client/Makefile.in index 5204f47..2263aff 100644 --- a/src/Client/Makefile.in +++ b/src/Client/Makefile.in @@ -45,7 +45,7 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libclient_la_LIBADD = -am_libclient_la_OBJECTS = RequestProcessor.lo +am_libclient_la_OBJECTS = CommandParser.lo libclient_la_OBJECTS = $(am_libclient_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -191,8 +191,8 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ SUBDIRS = Request noinst_LTLIBRARIES = libclient.la -libclient_la_SOURCES = RequestProcessor.cpp -noinst_HEADERS = RequestProcessor.h +libclient_la_SOURCES = CommandParser.cpp +noinst_HEADERS = CommandParser.h all: all-recursive .SUFFIXES: @@ -244,7 +244,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RequestProcessor.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CommandParser.Plo@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/src/Client/RequestProcessor.cpp b/src/Client/RequestProcessor.cpp deleted file mode 100644 index ae89355..0000000 --- a/src/Client/RequestProcessor.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * RequestProcessor.cpp - * - * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include "RequestProcessor.h" -#include <Common/Request/DisconnectRequest.h> - -namespace Mad { -namespace Client { - -RequestProcessor::RequestProcessor(Common::RequestManager *requestManager0, Net::Connection *connection0) : requestManager(requestManager0), connection(connection0) {} - -bool RequestProcessor::requestDisconnect() { - return Common::Request::DisconnectRequest::send(connection, *requestManager); -} - -} -} diff --git a/src/Common/Request/CoreStatusRequest.h b/src/Common/Request/CoreStatusRequest.h new file mode 100644 index 0000000..97f8318 --- /dev/null +++ b/src/Common/Request/CoreStatusRequest.h @@ -0,0 +1,82 @@ +/* + * CoreStatusRequest.h + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MAD_COMMON_REQUEST_CORESTATUSREQUEST_H_ +#define MAD_COMMON_REQUEST_CORESTATUSREQUEST_H_ + +#include "Request.h" +#include "../RequestManager.h" +#include <Net/Connection.h> +#include <Net/Packet.h> + +#include <sigc++/signal.h> + +namespace Mad { +namespace Common { +namespace Request { + +class CoreStatusRequest: public Request { + private: + sigc::signal<void> finished; + + CoreStatusRequest() {} + + public: + static bool send(Net::Connection *connection, RequestManager &requestManager, const sigc::slot<void> &callback) { + CoreStatusRequest *request = new CoreStatusRequest(); + + request->finished.connect(callback); + + if(requestManager.sendRequest(connection, request)) + return true; + + delete request; + return false; + } + + virtual bool sendRequest(Net::Connection *connection, unsigned short requestId) { + if(isSent()) + return false; + + if(!connection->send(Net::Packet(Net::Packet::TYPE_CORE_STATUS, requestId))) + return false; + + setSent(); + return true; + } + + virtual bool handlePacket(Net::Connection*, const Net::Packet &packet) { + if(isFinished()) + return false; + + if(packet.getType() != Net::Packet::TYPE_OK) + return false; // TODO Logging + + finished(); + + setFinished(); + return true; + } +}; + +} +} +} + +#endif /* MAD_COMMON_REQUEST_CORESTATUSREQUEST_H_ */ diff --git a/src/Common/Request/DisconnectRequest.h b/src/Common/Request/DisconnectRequest.h index 48256a7..c20a3d0 100644 --- a/src/Common/Request/DisconnectRequest.h +++ b/src/Common/Request/DisconnectRequest.h @@ -25,18 +25,24 @@ #include <Net/Connection.h> #include <Net/Packet.h> +#include <sigc++/signal.h> + namespace Mad { namespace Common { namespace Request { class DisconnectRequest: public Request { private: + sigc::signal<void> finished; + DisconnectRequest() {} public: - static bool send(Net::Connection *connection, RequestManager &requestManager) { + static bool send(Net::Connection *connection, RequestManager &requestManager, const sigc::slot<void> &callback) { DisconnectRequest *request = new DisconnectRequest(); + request->finished.connect(callback); + if(requestManager.sendRequest(connection, request)) return true; @@ -64,6 +70,8 @@ class DisconnectRequest: public Request { connection->disconnect(); + finished(); + setFinished(); return true; } diff --git a/src/Common/Request/Makefile.am b/src/Common/Request/Makefile.am index 64c5078..4c6ffd5 100644 --- a/src/Common/Request/Makefile.am +++ b/src/Common/Request/Makefile.am @@ -1 +1 @@ -noinst_HEADERS = DisconnectRequest.h GSSAPIAuthRequest.h IdentifyRequest.h Request.h +noinst_HEADERS = CoreStatusRequest.h DisconnectRequest.h GSSAPIAuthRequest.h IdentifyRequest.h Request.h diff --git a/src/Common/Request/Makefile.in b/src/Common/Request/Makefile.in index bf7652c..8d53cc0 100644 --- a/src/Common/Request/Makefile.in +++ b/src/Common/Request/Makefile.in @@ -162,7 +162,7 @@ sysconfdir = @sysconfdir@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ -noinst_HEADERS = DisconnectRequest.h GSSAPIAuthRequest.h IdentifyRequest.h Request.h +noinst_HEADERS = CoreStatusRequest.h DisconnectRequest.h GSSAPIAuthRequest.h IdentifyRequest.h Request.h all: all-am .SUFFIXES: diff --git a/src/Common/Util.h b/src/Common/Util.h index 71d7b69..19f87b9 100644 --- a/src/Common/Util.h +++ b/src/Common/Util.h @@ -55,64 +55,6 @@ class Util { return str.substr(beg, end); } - - static std::vector<std::string> split(const std::string &str) { - std::vector<std::string> ret; - std::string temp; - bool quoteSingle = false, quoteDouble = false, escape = false; - - size_t beg = 0; - - for(size_t cur = 0; cur < str.length(); ++cur) { - if(!escape) { - if(str[cur] == ' ' && !quoteSingle && !quoteDouble) { - if(cur == beg && temp.empty()) { - ++beg; - } - else { - temp += str.substr(beg, cur-beg); - ret.push_back(temp); - temp.clear(); - beg = cur+1; - } - - continue; - } - - if(str[cur] == '"' && !quoteSingle) { - temp += str.substr(beg, cur-beg); - beg = cur+1; - - quoteDouble = !quoteDouble; - continue; - } - if(str[cur] == '\'' && !quoteDouble) { - temp += str.substr(beg, cur-beg); - beg = cur+1; - - quoteSingle = !quoteSingle; - continue; - } - - if(str[cur] == '\\') { - escape = true; - continue; - } - } - - if(escape && ((!quoteSingle && !quoteDouble) || (quoteSingle && str[cur] == '\'') || (quoteDouble && (str[cur] == '"' || str[cur] == '\\')))) { - temp += str.substr(beg, cur-beg-1); - beg = cur; - } - - escape = false; - } - - temp += str.substr(beg, std::string::npos); - ret.push_back(temp); - - return ret; - } }; } diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index c7a2f42..e953fce 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -19,6 +19,7 @@ #include "ConnectionManager.h" #include "ConfigManager.h" +#include "RequestHandler/CoreStatusRequestHandler.h" #include "RequestHandler/GSSAPIAuthRequestHandler.h" #include <Net/ServerConnection.h> #include <Net/Packet.h> @@ -53,6 +54,7 @@ void ConnectionManager::refreshPollfds() { } ConnectionManager::ConnectionManager(const ConfigManager& configManager) : requestManager(true) { + requestManager.registerPacketType<RequestHandler::CoreStatusRequestHandler>(Net::Packet::TYPE_CORE_STATUS); requestManager.registerPacketType<RequestHandler::GSSAPIAuthRequestHandler>(Net::Packet::TYPE_GSSAPI_AUTH); const std::vector<Net::IPAddress> &listenerAddresses = configManager.getListenerAddresses(); diff --git a/src/Core/RequestHandler/CoreStatusRequestHandler.h b/src/Core/RequestHandler/CoreStatusRequestHandler.h new file mode 100644 index 0000000..5a3c16e --- /dev/null +++ b/src/Core/RequestHandler/CoreStatusRequestHandler.h @@ -0,0 +1,58 @@ +/* + * CoreStatusRequestHandler.h + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MAD_CORE_REQUESTHANDLER_CORESTATUSREQUESTHANDLER_H_ +#define MAD_CORE_REQUESTHANDLER_CORESTATUSREQUESTHANDLER_H_ + +#include <Common/RequestHandler.h> +#include <Net/Packet.h> +#include <cstring> +#include <gssapi/gssapi.h> + +#include <iostream> + +namespace Mad { +namespace Core { +namespace RequestHandler { + +class CoreStatusRequestHandler : public Common::RequestHandler { + public: + CoreStatusRequestHandler() {} + + virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet) { + if(isFinished()) + return false; + + if(packet.getType() != Net::Packet::TYPE_CORE_STATUS) + return false; // TODO Logging + + if(!connection->send(Net::Packet(Net::Packet::TYPE_OK, packet.getRequestId()))) + return false; + + setFinished(); + + return true; + } +}; + +} +} +} + +#endif /* MAD_CORE_REQUESTHANDLER_CORESTATUSREQUESTHANDLER_H_ */ diff --git a/src/Core/RequestHandler/Makefile.am b/src/Core/RequestHandler/Makefile.am index 9de7011..c0abc5c 100644 --- a/src/Core/RequestHandler/Makefile.am +++ b/src/Core/RequestHandler/Makefile.am @@ -1 +1 @@ -noinst_HEADERS = GSSAPIAuthRequestHandler.h +noinst_HEADERS = CoreStatusRequestHandler.h GSSAPIAuthRequestHandler.h diff --git a/src/Core/RequestHandler/Makefile.in b/src/Core/RequestHandler/Makefile.in index d2163f0..bea3870 100644 --- a/src/Core/RequestHandler/Makefile.in +++ b/src/Core/RequestHandler/Makefile.in @@ -162,7 +162,7 @@ sysconfdir = @sysconfdir@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ -noinst_HEADERS = GSSAPIAuthRequestHandler.h +noinst_HEADERS = CoreStatusRequestHandler.h GSSAPIAuthRequestHandler.h all: all-am .SUFFIXES: diff --git a/src/Net/Packet.h b/src/Net/Packet.h index 1952e98..1062d4a 100644 --- a/src/Net/Packet.h +++ b/src/Net/Packet.h @@ -31,7 +31,8 @@ class Packet { public: enum Type { TYPE_OK = 0x0000, TYPE_ERROR = 0x0001, TYPE_DISCONNECT = 0x0002, - TYPE_GSSAPI_AUTH = 0x0010, TYPE_IDENTIFY = 0x0011 + TYPE_GSSAPI_AUTH = 0x0010, TYPE_IDENTIFY = 0x0011, + TYPE_CORE_STATUS = 0x0020 }; struct Data { diff --git a/src/madc.cpp b/src/madc.cpp index af39f2d..f7dc143 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -21,7 +21,7 @@ #include "Net/IPAddress.h" #include "Common/RequestManager.h" #include "Common/Util.h" -#include "Client/RequestProcessor.h" +#include "Client/CommandParser.h" #include <iostream> #include <cstring> @@ -32,38 +32,36 @@ #include <readline/history.h> -static Mad::Client::RequestProcessor *processor; +static Mad::Client::CommandParser *parser; +static struct pollfd fds[2]; + static void usage(const std::string &cmd) { std::cerr << "Usage: " << cmd << " address[:port]" << std::endl; } static void handleCommand(char *cmd) { - if(!cmd) { - processor->requestDisconnect(); - rl_callback_handler_remove(); - + if(!cmd) + parser->requestDisconnect(); + else if(!*cmd) return; + else { + parser->parse(cmd); + add_history(cmd); } - if(!*cmd) - return; - - std::vector<std::string> splitCmd = Mad::Common::Util::split(cmd); - - if(splitCmd.empty()) - return; - - if(splitCmd[0] == "quit") { - processor->requestDisconnect(); - + if(parser->requestsActive()) { rl_callback_handler_remove(); + fds[0].events = 0; } - else { - std::cerr << "Unknown command \"" << splitCmd[0] << "\"." << std::endl; - } +} + +static void activateReadline() { + if(parser->willDisconnect()) + return; - add_history(cmd); + rl_callback_handler_install("mad: ", handleCommand); + fds[0].events = POLLIN; } int main(int argc, char *argv[]) { @@ -93,26 +91,25 @@ int main(int argc, char *argv[]) { requestManager.registerConnection(connection); - processor = new Mad::Client::RequestProcessor(&requestManager, connection); + parser = new Mad::Client::CommandParser(&requestManager, connection); + parser->signalFinished().connect(sigc::ptr_fun(activateReadline)); - struct pollfd fds[2]; fds[0].fd = STDIN_FILENO; - fds[0].events = POLLIN; - rl_callback_handler_install("mad: ", handleCommand); + activateReadline(); while(connection->isConnected()) { fds[1] = connection->getPollfd(); if(poll(fds, 2, 10000) > 0) { - if(fds[0].revents) + if(fds[0].revents & POLLIN) rl_callback_read_char(); connection->sendReceive(fds[1].revents); } } - delete processor; + delete parser; requestManager.unregisterConnection(connection); } |