From e8ce659d0b0c9f399b55d2abb258b2f52af31cb6 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 6 Jul 2008 20:23:32 +0200 Subject: ConfigurationManager hinzugef?gt; Listener-Adressen aus Konfiguration lesen --- src/Common/ConfigManager.cpp | 54 ++++++++++++++++++++++++++++++++++ src/Common/ConfigManager.h | 45 ++++++++++++++++++++++++++++ src/Common/Makefile.am | 4 +-- src/Common/Makefile.in | 7 +++-- src/Common/Util.h | 62 ++++++++++++++++++++++++++++++++++++++ src/Core/ConfigManager.cpp | 61 ++++++++++++++++++++++++++++++++++++++ src/Core/ConfigManager.h | 56 +++++++++++++++++++++++++++++++++++ src/Core/ConnectionManager.cpp | 16 +++++----- src/Core/ConnectionManager.h | 2 +- src/Core/Makefile.am | 4 +-- src/Core/Makefile.in | 7 +++-- src/Net/IPAddress.h | 67 ++++++++++++++++++++++++++++++++++-------- src/mad-core.conf | 3 ++ src/mad-core.cpp | 5 +++- 14 files changed, 361 insertions(+), 32 deletions(-) create mode 100644 src/Common/ConfigManager.cpp create mode 100644 src/Common/ConfigManager.h create mode 100644 src/Common/Util.h create mode 100644 src/Core/ConfigManager.cpp create mode 100644 src/Core/ConfigManager.h create mode 100644 src/mad-core.conf (limited to 'src') diff --git a/src/Common/ConfigManager.cpp b/src/Common/ConfigManager.cpp new file mode 100644 index 0000000..2159c6e --- /dev/null +++ b/src/Common/ConfigManager.cpp @@ -0,0 +1,54 @@ +/* + * ConfigManager.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 "ConfigManager.h" +#include "Util.h" +#include + +namespace Mad { +namespace Common { + +bool ConfigManager::loadFile(const std::string &filename) { + std::ifstream file(filename.c_str()); + + if(!file.good()) + return false; + + while(!file.eof()) { + std::string line; + + std::getline(file, line); + line = Util::trim(line); + + if(line.empty() || line[0] == '#') + continue; + + size_t pos = line.find_first_of(" \t"); + + if(pos == std::string::npos) + parseLine(line); + else + parseLine(line.substr(0, pos), Util::trim(line.substr(pos))); + } + + return true; +} + +} +} diff --git a/src/Common/ConfigManager.h b/src/Common/ConfigManager.h new file mode 100644 index 0000000..ccbfbaa --- /dev/null +++ b/src/Common/ConfigManager.h @@ -0,0 +1,45 @@ +/* + * ConfigManager.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_COMMON_CONFIGMANAGER_H_ +#define MAD_COMMON_CONFIGMANAGER_H_ + +#include + +namespace Mad { +namespace Common { + +class ConfigManager { + protected: + ConfigManager() {} + + virtual bool parseLine(const std::string &key, const std::string &value = std::string()) = 0; + + bool loadFile(const std::string &filename); + + public: + virtual ~ConfigManager() {} + + //virtual const std::string& getValue(const std::string &value) const = 0; +}; + +} +} + +#endif /* MAD_COMMON_CONFIGMANAGER_H_ */ diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am index aa2e45f..792aadc 100644 --- a/src/Common/Makefile.am +++ b/src/Common/Makefile.am @@ -1,5 +1,5 @@ noinst_LTLIBRARIES = libcommon.la -libcommon_la_SOURCES = RequestManager.cpp +libcommon_la_SOURCES = ConfigManager.cpp RequestManager.cpp -noinst_HEADERS = Request.h RequestManager.h +noinst_HEADERS = ConfigManager.h Request.h RequestManager.h Util.h diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in index 55e52ce..48224e7 100644 --- a/src/Common/Makefile.in +++ b/src/Common/Makefile.in @@ -45,7 +45,7 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libcommon_la_LIBADD = -am_libcommon_la_OBJECTS = RequestManager.lo +am_libcommon_la_OBJECTS = ConfigManager.lo RequestManager.lo libcommon_la_OBJECTS = $(am_libcommon_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -180,8 +180,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = libcommon.la -libcommon_la_SOURCES = RequestManager.cpp -noinst_HEADERS = Request.h RequestManager.h +libcommon_la_SOURCES = ConfigManager.cpp RequestManager.cpp +noinst_HEADERS = ConfigManager.h Request.h RequestManager.h Util.h all: all-am .SUFFIXES: @@ -233,6 +233,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConfigManager.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RequestManager.Plo@am__quote@ .cpp.o: diff --git a/src/Common/Util.h b/src/Common/Util.h new file mode 100644 index 0000000..19e1eeb --- /dev/null +++ b/src/Common/Util.h @@ -0,0 +1,62 @@ +/* + * Util.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 UTIL_H_ +#define UTIL_H_ + +#include +#include + +namespace Mad { +namespace Common { + +class Util { + private: + Util(); + + public: + static std::string tolower(const std::string &str) { + std::string ret; + + for(std::string::const_iterator c = str.begin(); c != str.end(); ++c) + ret += std::tolower(*c); + + return ret; + } + + static std::string trim(const std::string &str) { + size_t beg, end; + + beg = str.find_first_not_of(" \t"); + end = str.find_last_not_of(" \t"); + + if(beg == std::string::npos) + beg = 0; + + if(end != std::string::npos) + end = end-beg+1; + + return str.substr(beg, end); + } +}; + +} +} + +#endif /* UTIL_H_ */ diff --git a/src/Core/ConfigManager.cpp b/src/Core/ConfigManager.cpp new file mode 100644 index 0000000..b358671 --- /dev/null +++ b/src/Core/ConfigManager.cpp @@ -0,0 +1,61 @@ +/* + * ConfigManager.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 "ConfigManager.h" +#include + +#include + +namespace Mad { +namespace Core { + +bool ConfigManager::parseLine(const std::string &key, const std::string &value) { + if(Common::Util::tolower(key) == "configmethod") { + if(Common::Util::tolower(value) == "mysql") + methods |= (unsigned short)MYSQL; + else + return false; + } + else if(Common::Util::tolower(key) == "listen") { + try { + listeners.push_back(Net::IPAddress(value)); + } + catch(Net::InvalidAddressException &e) { + // TODO Logging + } + } + else { + // TODO Logging + + return false; + } + + return true; +} + +ConfigManager::ConfigManager() { + loadFile("mad-core.conf"); +} + +ConfigManager::~ConfigManager() { + // TODO Auto-generated destructor stub +} + +} +} diff --git a/src/Core/ConfigManager.h b/src/Core/ConfigManager.h new file mode 100644 index 0000000..00c5933 --- /dev/null +++ b/src/Core/ConfigManager.h @@ -0,0 +1,56 @@ +/* + * ConfigManager.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_CORE_CONFIGMANAGER_H_ +#define MAD_CORE_CONFIGMANAGER_H_ + +#include +#include +#include + +namespace Mad { +namespace Core { + +class ConfigManager : public Common::ConfigManager { + private: + enum Methods { + MYSQL = (1 << 0) + }; + + unsigned short methods; + + std::vector listeners; + + protected: + virtual bool parseLine(const std::string &key, const std::string &value); + + public: + ConfigManager(); + virtual ~ConfigManager(); + + const std::vector& getListenerAddresses() const {return listeners;} + + //virtual const std::string& getValue(const std::string &value) const; +}; + +} + +} + +#endif /* MAD_CORE_CONFIGMANAGER_H_ */ diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index 020c8a7..48d9c17 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -50,15 +50,15 @@ void ConnectionManager::refreshPollfds() { } } -ConnectionManager::ConnectionManager() { - try { - // TODO: Get listener addresses from config - listeners.push_back(new Net::Listener(Net::IPAddress("0.0.0.0", 6666))); - } - catch(Net::Exception &e) { - // TODO: Log error +ConnectionManager::ConnectionManager(const std::vector &listenerAddresses) { + for(std::vector::const_iterator address = listenerAddresses.begin(); address != listenerAddresses.end(); ++address) { + try { + listeners.push_back(new Net::Listener(*address)); + } + catch(Net::Exception &e) { + // TODO: Log error + } } - refreshPollfds(); } diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h index c7fe669..54d5d5e 100644 --- a/src/Core/ConnectionManager.h +++ b/src/Core/ConnectionManager.h @@ -56,7 +56,7 @@ class ConnectionManager { void refreshPollfds(); public: - ConnectionManager(); + ConnectionManager(const std::vector &listenerAddresses); virtual ~ConnectionManager(); bool wait(int timeout) { diff --git a/src/Core/Makefile.am b/src/Core/Makefile.am index b2a2874..241c030 100644 --- a/src/Core/Makefile.am +++ b/src/Core/Makefile.am @@ -1,5 +1,5 @@ noinst_LTLIBRARIES = libcore.la -libcore_la_SOURCES = ConnectionManager.cpp +libcore_la_SOURCES = ConfigManager.cpp ConnectionManager.cpp -noinst_HEADERS = ConnectionManager.h +noinst_HEADERS = ConfigManager.h ConnectionManager.h diff --git a/src/Core/Makefile.in b/src/Core/Makefile.in index af39f87..1920fbb 100644 --- a/src/Core/Makefile.in +++ b/src/Core/Makefile.in @@ -45,7 +45,7 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libcore_la_LIBADD = -am_libcore_la_OBJECTS = ConnectionManager.lo +am_libcore_la_OBJECTS = ConfigManager.lo ConnectionManager.lo libcore_la_OBJECTS = $(am_libcore_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -180,8 +180,8 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = libcore.la -libcore_la_SOURCES = ConnectionManager.cpp -noinst_HEADERS = ConnectionManager.h +libcore_la_SOURCES = ConfigManager.cpp ConnectionManager.cpp +noinst_HEADERS = ConfigManager.h ConnectionManager.h all: all-am .SUFFIXES: @@ -233,6 +233,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConfigManager.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConnectionManager.Plo@am__quote@ .cpp.o: diff --git a/src/Net/IPAddress.h b/src/Net/IPAddress.h index c1709d5..feeb010 100644 --- a/src/Net/IPAddress.h +++ b/src/Net/IPAddress.h @@ -29,29 +29,72 @@ namespace Net { class IPAddress { private: - std::string addr; - unsigned short p; + unsigned int addr; + unsigned short port; struct sockaddr_in sa; - + public: - IPAddress(const std::string &address, unsigned short port) throw(InvalidAddressException) : addr(address), p(port) { + IPAddress(unsigned int address, unsigned short port0) : addr(address), port(port0) { + sa.sin_family = AF_INET; + sa.sin_port = htons(port); + sa.sin_addr.s_addr = htonl(addr); + } + + IPAddress(const std::string &address) throw(InvalidAddressException) { + std::string ip; + size_t pos = address.find_first_of(':'); + + if(pos == std::string::npos) { + ip = address; + // TODO Default port + port = 6666; + } + else { + ip = address.substr(0, pos); + + char *endptr; + port = std::strtol(address.substr(pos+1).c_str(), &endptr, 10); + if(*endptr != 0 || port == 0) + throw InvalidAddressException(address); + } + sa.sin_family = AF_INET; sa.sin_port = htons(port); + + if(ip == "*") + sa.sin_addr.s_addr = INADDR_ANY; + else if(!inet_pton(AF_INET, ip.c_str(), &sa.sin_addr)) + throw InvalidAddressException(address); + + addr = ntohl(sa.sin_addr.s_addr); + } + + IPAddress(const std::string &address, unsigned short port0) throw(InvalidAddressException) : port(port0) { + sa.sin_family = AF_INET; + sa.sin_port = htons(port); + if(!inet_pton(AF_INET, address.c_str(), &sa.sin_addr)) throw InvalidAddressException(address); + + addr = ntohl(sa.sin_addr.s_addr); } - + IPAddress(const struct sockaddr_in &address) : sa(address) { - p = ntohs(sa.sin_port); - + port = ntohs(sa.sin_port); + addr = ntohl(sa.sin_addr.s_addr); + } + + unsigned int getAddress() const {return addr;} + unsigned short getPort() const {return port;} + + std::string getAddressString() const { char buf[INET_ADDRSTRLEN]; + unsigned int address = htonl(addr); + inet_ntop(AF_INET, &address, buf, sizeof(buf)); - addr = buf; + return std::string(buf); } - - const std::string& getAddress() const {return addr;} - unsigned short getPort() const {return p;} - + struct sockaddr* getSockAddr() {return reinterpret_cast(&sa);} socklen_t getSockAddrLength() const {return sizeof(sa);} }; diff --git a/src/mad-core.conf b/src/mad-core.conf new file mode 100644 index 0000000..13d74f5 --- /dev/null +++ b/src/mad-core.conf @@ -0,0 +1,3 @@ +ConfigMethod Mysql + +Listen * diff --git a/src/mad-core.cpp b/src/mad-core.cpp index 525eb88..d78ec3e 100644 --- a/src/mad-core.cpp +++ b/src/mad-core.cpp @@ -19,6 +19,7 @@ #include "Net/Connection.h" #include "Core/ConnectionManager.h" +#include "Core/ConfigManager.h" #include int main() { @@ -28,9 +29,11 @@ int main() { sigaddset(&signals, SIGPIPE); sigprocmask(SIG_BLOCK, &signals, 0); + Mad::Core::ConfigManager configManager; + Mad::Net::Connection::init(); - Mad::Core::ConnectionManager *connectionManager = new Mad::Core::ConnectionManager; + Mad::Core::ConnectionManager *connectionManager = new Mad::Core::ConnectionManager(configManager.getListenerAddresses()); while(true) { if(connectionManager->wait(10000)) -- cgit v1.2.3