summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordeva <deva>2008-07-30 11:33:15 +0000
committerdeva <deva>2008-07-30 11:33:15 +0000
commit71ac37ecfaf5dff76c8880b62228c30bd207054b (patch)
tree3d0074544ae1141bb9154884de69ece62e8a3519
parentb3b694c8dd17955aec463f50c6f526f570d8b97f (diff)
Added global Conf namepsace with configuration values. These are to be filled with config file values later.
-rw-r--r--server/src/Makefile.am2
-rw-r--r--server/src/configuration.cc45
-rw-r--r--server/src/configuration.h61
-rw-r--r--server/src/configurationparser.cc49
-rw-r--r--server/src/configurationparser.h69
-rw-r--r--server/src/pracrod.cc23
-rw-r--r--server/src/server.cc18
7 files changed, 152 insertions, 115 deletions
diff --git a/server/src/Makefile.am b/server/src/Makefile.am
index 93d830a..704d4d2 100644
--- a/server/src/Makefile.am
+++ b/server/src/Makefile.am
@@ -10,6 +10,7 @@ pracrod_SOURCES = \
daemon.cc \
database.cc \
configuration.cc \
+ configurationparser.cc \
exception.cc \
queryhandler.cc \
queryparser.cc \
@@ -28,6 +29,7 @@ pracrod_SOURCES = \
EXTRA_DIST = \
configuration.h \
+ configurationparser.h \
daemon.h \
database.h \
debug.h \
diff --git a/server/src/configuration.cc b/server/src/configuration.cc
index 3f8e9d6..8883e30 100644
--- a/server/src/configuration.cc
+++ b/server/src/configuration.cc
@@ -26,42 +26,15 @@
*/
#include "configuration.h"
-#include "debug.h"
+port_t Conf::server_port = 12345;
+std::string Conf::server_user = "pracro";
+std::string Conf::server_group = "pracro";
-Configuration::Configuration(std::string filename)
-{
- this->filename = filename;
+std::string Conf::journal_commit_addr = "localhost";
+port_t Conf::journal_commit_port = 18112;
- reload();
-}
+unsigned int Conf::db_max_ttl = 60 * 60 * 24;
+unsigned int Conf::pentominos_max_ttl = 60 * 60 * 24;
-void Configuration::reload()
- throw(ConfigurationException)
-{
- try {
- readFile(this->filename.c_str());
- } catch(libconfig::FileIOException) {
- throw ConfigurationException("Could not read config file: File does not exist.");
- } catch(libconfig::ParseException &e) {
- char lineno[32];
- sprintf(lineno, "%d", e.getLine());
- throw ConfigurationException(std::string("Error when parsing the config file in line ")
- + lineno + ": " + e.getError());
- }
-}
-
-
-static Configuration *_config = NULL;
-
-Configuration *config()
- throw(ConfigurationException)
-{
- if(_config == NULL)
- throw ConfigurationException("Configuration has not yet been set.");
- return _config;
-}
-
-void initConfig(Configuration *config)
-{
- _config = config;
-}
+std::string pentominos_addr = "localhost";
+port_t pentominos_potr = 11108;
diff --git a/server/src/configuration.h b/server/src/configuration.h
index 1b851f3..2272a83 100644
--- a/server/src/configuration.h
+++ b/server/src/configuration.h
@@ -27,60 +27,23 @@
#ifndef __ARTEFACT_CONFIGURATION_H__
#define __ARTEFACT_CONFIGURATION_H__
-#include <libconfig.h++>
#include <string>
+#include <sys/types.h>
+typedef uint16_t port_t;
-#include "exception.h"
+namespace Conf {
+ extern port_t server_port;
+ extern std::string server_user;
+ extern std::string server_group;
-/**
- * This exception is thrown by Configuration when reload fails.
- */
-class ConfigurationException: public Exception {
-public:
- ConfigurationException(std::string reason) :
- Exception(reason) {}
-};
+ extern std::string journal_commit_addr;
+ extern port_t journal_commit_port;
-/**
- * This is the pentominos configuration class.\n
- * It simply wraps the libconfig c++ interface. It can be found at
- * http://www.hyperrealm.com/libconfig/libconfig.html\n
- * To find out how the interface works, see
- * http://www.hyperrealm.com/libconfig/libconfig_manual.html#The-C_002b_002b-API
- */
-class Configuration : public libconfig::Config {
-public:
- /**
- * Constructor.\n
- * @param filename The filename to be loaded.
- */
- Configuration(std::string filename);
+ extern unsigned int db_max_ttl;
+ extern unsigned int pentominos_max_ttl;
- /**
- * reload, simply reloads the configuration file attached to the configuration
- * object.
- */
- void reload() throw(ConfigurationException);
-
-private:
- std::string filename;
+ extern std::string pentominos_addr;
+ extern port_t pentominos_port;
};
-/**
- * Initialize the global configuration.\n
- * This function sets the Configuration global pointer, that can be reaced through
- * the config variable.
- * @param config The value of the Configuration pointer.
- */
-void initConfig(Configuration *config);
-
-/**
- * This function returns the global configuration object pointer.\n
- * Use initConfig to set it.\n
- * @return The pointer to the global configuration.
- */
-Configuration *config()
- throw(ConfigurationException);
-
-
#endif/*__ARTEFACT_CONFIGURATION_H__*/
diff --git a/server/src/configurationparser.cc b/server/src/configurationparser.cc
new file mode 100644
index 0000000..6335fd9
--- /dev/null
+++ b/server/src/configurationparser.cc
@@ -0,0 +1,49 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * configurationparser.cc
+ *
+ * Wed Jul 30 11:48:31 CEST 2008
+ * Copyright 2008 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Pracro.
+ *
+ * Pracro 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Pracro 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 Pracro; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "configurationparser.h"
+
+ConfigurationParser::ConfigurationParser(std::string filename)
+{
+ this->filename = filename;
+
+ reload();
+}
+
+void ConfigurationParser::reload()
+ throw(ConfigurationParserException)
+{
+ try {
+ readFile(this->filename.c_str());
+ } catch(libconfig::FileIOException) {
+ throw ConfigurationParserException("Could not read config file: File does not exist.");
+ } catch(libconfig::ParseException &e) {
+ char lineno[32];
+ sprintf(lineno, "%d", e.getLine());
+ throw ConfigurationParserException(std::string("Error when parsing the config file in line ")
+ + lineno + ": " + e.getError());
+ }
+}
diff --git a/server/src/configurationparser.h b/server/src/configurationparser.h
new file mode 100644
index 0000000..924c041
--- /dev/null
+++ b/server/src/configurationparser.h
@@ -0,0 +1,69 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * configurationparser.h
+ *
+ * Wed Jul 30 11:48:31 CEST 2008
+ * Copyright 2008 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Pracro.
+ *
+ * Pracro 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Pracro 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 Pracro; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#ifndef __PRACRO_CONFIGURATIONPARSER_H__
+#define __PRACRO_CONFIGURATIONPARSER_H__
+
+#include <libconfig.h++>
+#include <string>
+
+#include "exception.h"
+
+/**
+ * This exception is thrown by Configuration when reload fails.
+ */
+class ConfigurationParserException: public Exception {
+public:
+ ConfigurationParserException(std::string reason) :
+ Exception(reason) {}
+};
+
+/**
+ * This is the pentominos configuration class.\n
+ * It simply wraps the libconfig c++ interface. It can be found at
+ * http://www.hyperrealm.com/libconfig/libconfig.html\n
+ * To find out how the interface works, see
+ * http://www.hyperrealm.com/libconfig/libconfig_manual.html#The-C_002b_002b-API
+ */
+class ConfigurationParser : public libconfig::Config {
+public:
+ /**
+ * Constructor.\n
+ * @param filename The filename to be loaded.
+ */
+ ConfigurationParser(std::string filename);
+
+ /**
+ * reload, simply reloads the configuration file attached to the configuration
+ * object.
+ */
+ void reload() throw(ConfigurationParserException);
+
+private:
+ std::string filename;
+};
+
+#endif/*__PRACRO_CONFIGURATIONPARSER_H__*/
diff --git a/server/src/pracrod.cc b/server/src/pracrod.cc
index dff00d9..c668159 100644
--- a/server/src/pracrod.cc
+++ b/server/src/pracrod.cc
@@ -82,8 +82,9 @@ static const char usage_str[] =
" -h, --help Print this message and exit.\n"
;
-void reload(int fisk)
+void reload(int)
{
+ /*
int port;
printf("Reload!\n");
port = config()->lookup("port");
@@ -93,6 +94,7 @@ void reload(int fisk)
TCPSocket socket;
socket.connect("localhost", port);
}
+ */
}
class PracroDaemon : public Daemon {
@@ -167,33 +169,24 @@ int main(int argc, char *argv[])
}
}
+ // Load config
+ /*
char defval[512];
sprintf(defval, ETC"/pracrod.conf");
-
char *cfname = NULL;
if(!configfile) cfname = defval;//ETC"/pracrod.conf";
else cfname = configfile;
Configuration conf(cfname);
initConfig(&conf);
+ */
if(!user) {
- std::string userstr;
- try {
- user = (char*)(const char*)config()->lookup("user");
- } catch( ... ) {
- printf("User not found in config.\n");
- }
+ user = strdup(Conf::server_user.c_str());
}
if(!group) {
- std::string groupstr;
- try {
- group = (char*)(const char*)config()->lookup("group");
- } catch( ... ) {
- printf("Group not found in config.\n");
- }
-
+ group = strdup(Conf::server_group.c_str());
}
PracroDaemon daemon;
diff --git a/server/src/server.cc b/server/src/server.cc
index 20fd856..1bd0ccf 100644
--- a/server/src/server.cc
+++ b/server/src/server.cc
@@ -133,7 +133,7 @@ static void connection(TCPSocket &socket)
#ifndef WITHOUT_PENTOMINOS
// Send the queries to Pentominos (if any)
TCPSocket s;
- s.connect("localhost", 11108);
+ s.connect(Conf::pentominos_addr, Conf::pentominos_port);
QueryHandler qh(&s, transaction.cpr);
std::vector< Macro >::iterator mi = templ->course.macroes.begin();
@@ -212,14 +212,7 @@ static void connection(TCPSocket &socket)
void server()
{
- int port;
- try {
- port = config()->lookup("port");
- } catch( ... ) {
- fprintf(stderr, "Could not read port.");
- return;
- }
-
+ port_t port = Conf::server_port;
TCPSocket *socket = NULL;
try {
@@ -237,12 +230,7 @@ void server()
{ // Reload if new port is assigned.
int old_port = port;
- try {
- port = config()->lookup("port");
- } catch( ... ) {
- fprintf(stderr, "Could not read port.");
- return;
- }
+ port = Conf::server_port;
if(port != old_port) {
// Start listening on the new port