From 707344b4d161cb5a11e7f78d7622cb7cd1a791f6 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 10 Sep 2008 03:26:49 +0200 Subject: Client erweitert; Status-Befehl angefangen --- src/Client/CommandParser.cpp | 116 ++++++++++++++++++++++++++++++++++++++++ src/Client/CommandParser.h | 73 +++++++++++++++++++++++++ src/Client/Makefile.am | 4 +- src/Client/Makefile.in | 8 +-- src/Client/RequestProcessor.cpp | 33 ------------ src/Client/RequestProcessor.h | 49 ----------------- 6 files changed, 195 insertions(+), 88 deletions(-) create mode 100644 src/Client/CommandParser.cpp create mode 100644 src/Client/CommandParser.h delete mode 100644 src/Client/RequestProcessor.cpp delete mode 100644 src/Client/RequestProcessor.h (limited to 'src/Client') 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 + * + * 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 . + */ + +#include "CommandParser.h" +#include +#include + +#include + +namespace Mad { +namespace Client { + +bool CommandParser::split(const std::string &str, std::vector &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 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/CommandParser.h b/src/Client/CommandParser.h new file mode 100644 index 0000000..81c3289 --- /dev/null +++ b/src/Client/CommandParser.h @@ -0,0 +1,73 @@ +/* + * CommandParser.h + * + * Copyright (C) 2008 Matthias Schiffer + * + * 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 . + */ + +#ifndef MAD_CLIENT_COMMANDPARSER_H_ +#define MAD_CLIENT_COMMANDPARSER_H_ + +#include +#include +#include + +namespace Mad { + +namespace Common { +class RequestManager; +} + +namespace Net { +class Connection; +} + +namespace Client { + +class CommandParser { + private: + sigc::signal finished; + + Common::RequestManager *requestManager; + Net::Connection *connection; + + unsigned int activeRequests; + bool disconnect; + + bool split(const std::string &str, std::vector &ret); + + void requestFinished() { + activeRequests--; + + finished(); + } + + public: + 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(); + + sigc::signal signalFinished() const {return finished;} +}; + +} +} + +#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 - * - * 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 . - */ - -#include "RequestProcessor.h" -#include - -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/Client/RequestProcessor.h b/src/Client/RequestProcessor.h deleted file mode 100644 index 9262058..0000000 --- a/src/Client/RequestProcessor.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * RequestProcessor.h - * - * Copyright (C) 2008 Matthias Schiffer - * - * 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 . - */ - -#ifndef MAD_CLIENT_REQUESTPROCESSOR_H_ -#define MAD_CLIENT_REQUESTPROCESSOR_H_ - -namespace Mad { - -namespace Common { -class RequestManager; -} - -namespace Net { -class Connection; -} - -namespace Client { - -class RequestProcessor { - private: - Common::RequestManager *requestManager; - Net::Connection *connection; - - public: - RequestProcessor(Common::RequestManager *requestManager0, Net::Connection *connection0); - - bool requestDisconnect(); -}; - -} -} - -#endif /* MAD_CLIENT_REQUESTPROCESSOR_H_ */ -- cgit v1.2.3