summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2008-07-06 20:23:32 +0200
committerMatthias Schiffer <matthias@gamezock.de>2008-07-06 20:23:32 +0200
commite8ce659d0b0c9f399b55d2abb258b2f52af31cb6 (patch)
tree8bc71e127ade0cb4082ea960eb283581beaf1d93 /src
parent77990eb4c0be93f510e8d5095c9133485999fef6 (diff)
downloadmad-e8ce659d0b0c9f399b55d2abb258b2f52af31cb6.tar
mad-e8ce659d0b0c9f399b55d2abb258b2f52af31cb6.zip
ConfigurationManager hinzugef?gt; Listener-Adressen aus Konfiguration lesen
Diffstat (limited to 'src')
-rw-r--r--src/Common/ConfigManager.cpp54
-rw-r--r--src/Common/ConfigManager.h45
-rw-r--r--src/Common/Makefile.am4
-rw-r--r--src/Common/Makefile.in7
-rw-r--r--src/Common/Util.h62
-rw-r--r--src/Core/ConfigManager.cpp61
-rw-r--r--src/Core/ConfigManager.h56
-rw-r--r--src/Core/ConnectionManager.cpp16
-rw-r--r--src/Core/ConnectionManager.h2
-rw-r--r--src/Core/Makefile.am4
-rw-r--r--src/Core/Makefile.in7
-rw-r--r--src/Net/IPAddress.h67
-rw-r--r--src/mad-core.conf3
-rw-r--r--src/mad-core.cpp5
14 files changed, 361 insertions, 32 deletions
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 <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 "ConfigManager.h"
+#include "Util.h"
+#include <fstream>
+
+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 <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_CONFIGMANAGER_H_
+#define MAD_COMMON_CONFIGMANAGER_H_
+
+#include <string>
+
+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 <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 UTIL_H_
+#define UTIL_H_
+
+#include <string>
+#include <locale>
+
+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 <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 "ConfigManager.h"
+#include <Common/Util.h>
+
+#include <iostream>
+
+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 <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_CONFIGMANAGER_H_
+#define MAD_CORE_CONFIGMANAGER_H_
+
+#include <Common/ConfigManager.h>
+#include <Net/IPAddress.h>
+#include <vector>
+
+namespace Mad {
+namespace Core {
+
+class ConfigManager : public Common::ConfigManager {
+ private:
+ enum Methods {
+ MYSQL = (1 << 0)
+ };
+
+ unsigned short methods;
+
+ std::vector<Net::IPAddress> listeners;
+
+ protected:
+ virtual bool parseLine(const std::string &key, const std::string &value);
+
+ public:
+ ConfigManager();
+ virtual ~ConfigManager();
+
+ const std::vector<Net::IPAddress>& 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<Net::IPAddress> &listenerAddresses) {
+ for(std::vector<Net::IPAddress>::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<Net::IPAddress> &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<struct sockaddr*>(&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 <signal.h>
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))