diff options
Diffstat (limited to 'src/Client')
-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 |
5 files changed, 153 insertions, 46 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); -} - -} -} |