From 7234fe326d16d6bf9f4374a09ddc6ef790e6723f Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 18 Jun 2009 22:03:02 +0200 Subject: Globale Variablen durch Application-Klasse ersetzt --- src/modules/FileLogger/CMakeLists.txt | 3 +- src/modules/FileLogger/FileLogger.cpp | 70 --------------------- src/modules/FileLogger/FileLogger.h | 40 +++--------- src/modules/FileLogger/Module.cpp | 62 +++++++++++++++++++ src/modules/FileLogger/Module.h | 67 ++++++++++++++++++++ src/modules/SystemBackendPosix/CMakeLists.txt | 1 + src/modules/SystemBackendPosix/Module.cpp | 28 +++++++++ src/modules/SystemBackendPosix/Module.h | 52 ++++++++++++++++ .../SystemBackendPosix/SystemBackendPosix.cpp | 21 ++----- .../SystemBackendPosix/SystemBackendPosix.h | 35 +++-------- src/modules/SystemBackendProc/CMakeLists.txt | 1 + src/modules/SystemBackendProc/Module.cpp | 28 +++++++++ src/modules/SystemBackendProc/Module.h | 52 ++++++++++++++++ .../SystemBackendProc/SystemBackendProc.cpp | 21 ++----- src/modules/SystemBackendProc/SystemBackendProc.h | 32 +++------- src/modules/UserBackendMysql/CMakeLists.txt | 1 + src/modules/UserBackendMysql/Module.cpp | 28 +++++++++ src/modules/UserBackendMysql/Module.h | 59 ++++++++++++++++++ src/modules/UserBackendMysql/UserBackendMysql.cpp | 71 ++++++---------------- src/modules/UserBackendMysql/UserBackendMysql.h | 27 ++++---- 20 files changed, 449 insertions(+), 250 deletions(-) delete mode 100644 src/modules/FileLogger/FileLogger.cpp create mode 100644 src/modules/FileLogger/Module.cpp create mode 100644 src/modules/FileLogger/Module.h create mode 100644 src/modules/SystemBackendPosix/Module.cpp create mode 100644 src/modules/SystemBackendPosix/Module.h create mode 100644 src/modules/SystemBackendProc/Module.cpp create mode 100644 src/modules/SystemBackendProc/Module.h create mode 100644 src/modules/UserBackendMysql/Module.cpp create mode 100644 src/modules/UserBackendMysql/Module.h (limited to 'src/modules') diff --git a/src/modules/FileLogger/CMakeLists.txt b/src/modules/FileLogger/CMakeLists.txt index 7d90028..555425d 100644 --- a/src/modules/FileLogger/CMakeLists.txt +++ b/src/modules/FileLogger/CMakeLists.txt @@ -1,5 +1,6 @@ include_directories(${INCLUDES}) add_library(FileLogger MODULE - FileLogger.cpp FileLogger.h + FileLogger.h + Module.cpp Module.h ) diff --git a/src/modules/FileLogger/FileLogger.cpp b/src/modules/FileLogger/FileLogger.cpp deleted file mode 100644 index bdae536..0000000 --- a/src/modules/FileLogger/FileLogger.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * FileLogger.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 "FileLogger.h" - -#include - - -namespace Mad { -namespace Modules { - -FileLogger::ConfigHelper FileLogger::configHelper; -std::set > FileLogger::loggers; - - -bool FileLogger::ConfigHelper::handleConfigEntry(const Core::ConfigEntry &entry, bool handled) { - if(handled) - return false; - - if(entry[0].getKey().matches("Logger")) { - if(entry[0][0].matches("File")) { - if(entry[1].empty()) { - if(!entry[0][1].empty()) { - boost::shared_ptr logger(new FileLogger(entry[0][1])); - - loggers.insert(logger); - Core::LogManager::get()->registerLogger(boost::static_pointer_cast(logger)); - } - else { - Logger::logf(Logger::WARNING, "FileLogger: no filename given."); - } - - return true; - } - } - } - - return false; -} - -} -} - -extern "C" { - -void FileLogger_init() { - Mad::Modules::FileLogger::registerConfigHelper(); -} - -void FileLogger_deinit() { - Mad::Modules::FileLogger::unregisterConfigHelper(); -} - -} diff --git a/src/modules/FileLogger/FileLogger.h b/src/modules/FileLogger/FileLogger.h index 9b0f8e2..a24f747 100644 --- a/src/modules/FileLogger/FileLogger.h +++ b/src/modules/FileLogger/FileLogger.h @@ -17,31 +17,20 @@ * with this program. If not, see . */ -#ifndef MAD_MODULES_FILELOGGER_H_ -#define MAD_MODULES_FILELOGGER_H_ - -#include -#include -#include +#ifndef MAD_MODULES_FILELOGGER_FILELOGGER_H_ +#define MAD_MODULES_FILELOGGER_FILELOGGER_H_ #include +#include +#include + namespace Mad { namespace Modules { +namespace FileLogger { class FileLogger : public Core::Logger, public Core::RemoteLogger { private: - class ConfigHelper : public Core::Configurable { - protected: - virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool handled); - - public: - virtual int getPriority() const {return 1;} - }; - - static ConfigHelper configHelper; - static std::set > loggers; - std::ofstream file; protected: @@ -55,23 +44,10 @@ class FileLogger : public Core::Logger, public Core::RemoteLogger { public: FileLogger(const std::string &filename) : file(filename.c_str(), std::ios::out|std::ios::app) {} - - static void registerConfigHelper() { - Core::ConfigManager::get()->registerConfigurable(&configHelper); - } - - static void unregisterConfigHelper() { - Core::ConfigManager::get()->unregisterConfigurable(&configHelper); - - for(std::set >::iterator logger = loggers.begin(); logger != loggers.end(); ++logger) { - Core::LogManager::get()->unregisterLogger(boost::static_pointer_cast(*logger)); - } - - loggers.clear(); - } }; +} } } -#endif /* MAD_MODULES_FILELOGGER_H_ */ +#endif /* MAD_MODULES_FILELOGGER_FILELOGGER_H_ */ diff --git a/src/modules/FileLogger/Module.cpp b/src/modules/FileLogger/Module.cpp new file mode 100644 index 0000000..829c429 --- /dev/null +++ b/src/modules/FileLogger/Module.cpp @@ -0,0 +1,62 @@ +/* + * Module.cpp + * + * Copyright (C) 2009 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 "Module.h" +#include + +namespace Mad { +namespace Modules { +namespace FileLogger { + +bool Module::handleConfigEntry(const Core::ConfigEntry &entry, bool handled) { + if(handled) + return false; + + if(entry[0].getKey().matches("Logger")) { + if(entry[0][0].matches("File")) { + if(entry[1].empty()) { + if(!entry[0][1].empty()) { + boost::shared_ptr logger(new FileLogger(entry[0][1])); + + loggers.insert(logger); + application->getLogManager()->registerLogger(boost::static_pointer_cast(logger)); + } + else { + application->logf(Core::LoggerBase::WARNING, "FileLogger: no filename given."); + } + + return true; + } + } + } + + return false; +} + +} +} +} + +extern "C" { + +Mad::Common::Module* FileLogger_create(Mad::Common::Application *application) { + return new Mad::Modules::FileLogger::Module(application); +} + +} diff --git a/src/modules/FileLogger/Module.h b/src/modules/FileLogger/Module.h new file mode 100644 index 0000000..e800b69 --- /dev/null +++ b/src/modules/FileLogger/Module.h @@ -0,0 +1,67 @@ +/* + * Module.h + * + * Copyright (C) 2009 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_MODULES_FILELOGGER_MODULE_H_ +#define MAD_MODULES_FILELOGGER_MODULE_H_ + +#include "FileLogger.h" + +#include +#include +#include + +#include +#include + +namespace Mad { +namespace Modules { +namespace FileLogger { + +class Module : public Common::Module, private Core::Configurable { + private: + Common::Application *application; + + std::set > loggers; + + protected: + virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool handled); + + public: + Module(Common::Application *application0) : application(application0) { + application->getConfigManager()->registerConfigurable(this); + } + + virtual ~Module() { + application->getConfigManager()->unregisterConfigurable(this); + + for(std::set >::iterator logger = loggers.begin(); logger != loggers.end(); ++logger) { + application->getLogManager()->unregisterLogger(boost::static_pointer_cast(*logger)); + } + + loggers.clear(); + } + + virtual int getPriority() const {return 1;} +}; + +} +} +} + +#endif /* MAD_MODULES_FILELOGGER_MODULE_H_ */ diff --git a/src/modules/SystemBackendPosix/CMakeLists.txt b/src/modules/SystemBackendPosix/CMakeLists.txt index 223960b..83e8ea1 100644 --- a/src/modules/SystemBackendPosix/CMakeLists.txt +++ b/src/modules/SystemBackendPosix/CMakeLists.txt @@ -1,5 +1,6 @@ include_directories(${INCLUDES}) add_library(SystemBackendPosix MODULE + Module.cpp Module.h SystemBackendPosix.cpp SystemBackendPosix.h ) diff --git a/src/modules/SystemBackendPosix/Module.cpp b/src/modules/SystemBackendPosix/Module.cpp new file mode 100644 index 0000000..230fef4 --- /dev/null +++ b/src/modules/SystemBackendPosix/Module.cpp @@ -0,0 +1,28 @@ +/* + * Module.cpp + * + * Copyright (C) 2009 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 "Module.h" + +extern "C" { + +Mad::Common::Module* SystemBackendPosix_create(Mad::Common::Application *application) { + return new Mad::Modules::SystemBackendPosix::Module(application); +} + +} diff --git a/src/modules/SystemBackendPosix/Module.h b/src/modules/SystemBackendPosix/Module.h new file mode 100644 index 0000000..ee780f6 --- /dev/null +++ b/src/modules/SystemBackendPosix/Module.h @@ -0,0 +1,52 @@ +/* + * Module.h + * + * Copyright (C) 2009 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_MODULES_SYSTEMBACKENDPOSIX_MODULE_H_ +#define MAD_MODULES_SYSTEMBACKENDPOSIX_MODULE_H_ + +#include "SystemBackendPosix.h" + +#include +#include + +namespace Mad { +namespace Modules { +namespace SystemBackendPosix { + +class Module : public Common::Module { + private: + Common::Application *application; + + boost::shared_ptr backend; + + public: + Module(Common::Application *application0) : application(application0), backend(new SystemBackendPosix(application)) { + application->getSystemManager()->registerBackend(backend); + } + + virtual ~Module() { + application->getSystemManager()->unregisterBackend(backend); + } +}; + +} +} +} + +#endif /* MAD_MODULES_SYSTEMBACKENDPOSIX_MODULE_H_ */ diff --git a/src/modules/SystemBackendPosix/SystemBackendPosix.cpp b/src/modules/SystemBackendPosix/SystemBackendPosix.cpp index 4eb5807..801fcd4 100644 --- a/src/modules/SystemBackendPosix/SystemBackendPosix.cpp +++ b/src/modules/SystemBackendPosix/SystemBackendPosix.cpp @@ -26,11 +26,10 @@ namespace Mad { namespace Modules { - -boost::shared_ptr SystemBackendPosix::backend; +namespace SystemBackendPosix { void SystemBackendPosix::getFSInfo(std::vector *fsInfo) throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); FILE *pipe = popen("/bin/df -P -k", "r"); if(!pipe) @@ -79,14 +78,14 @@ void SystemBackendPosix::getFSInfo(std::vector *f } void SystemBackendPosix::shutdown() throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); if(system("/sbin/halt") != 0) throw(Core::Exception(Core::Exception::NOT_AVAILABLE)); } void SystemBackendPosix::reboot() throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); if(system("/sbin/reboot") != 0) throw(Core::Exception(Core::Exception::NOT_AVAILABLE)); @@ -94,16 +93,4 @@ void SystemBackendPosix::reboot() throw(Core::Exception) { } } - - -extern "C" { - -void SystemBackendPosix_init() { - Mad::Modules::SystemBackendPosix::registerBackend(); -} - -void SystemBackendPosix_deinit() { - Mad::Modules::SystemBackendPosix::unregisterBackend(); -} - } diff --git a/src/modules/SystemBackendPosix/SystemBackendPosix.h b/src/modules/SystemBackendPosix/SystemBackendPosix.h index 4aec7dd..6c5ae06 100644 --- a/src/modules/SystemBackendPosix/SystemBackendPosix.h +++ b/src/modules/SystemBackendPosix/SystemBackendPosix.h @@ -17,25 +17,19 @@ * with this program. If not, see . */ -#ifndef MAD_MODULES_SYSTEMBACKENDPOSIX_H_ -#define MAD_MODULES_SYSTEMBACKENDPOSIX_H_ +#ifndef MAD_MODULES_SYSTEMBACKENDPOSIX_SYSTEMBACKENDPOSIX_H_ +#define MAD_MODULES_SYSTEMBACKENDPOSIX_SYSTEMBACKENDPOSIX_H_ #include - -#include -#include -#include - -#include - -#include +#include namespace Mad { namespace Modules { +namespace SystemBackendPosix { class SystemBackendPosix : public Common::SystemBackend { private: - static boost::shared_ptr backend; + Common::Application *application; protected: virtual void getFSInfo(std::vector *fsInfo) throw(Core::Exception); @@ -44,24 +38,11 @@ class SystemBackendPosix : public Common::SystemBackend { virtual void reboot() throw(Core::Exception); public: - static void registerBackend() { - if(backend) - return; - - backend.reset(new SystemBackendPosix()); - Common::SystemManager::get()->registerBackend(backend); - } - - static void unregisterBackend() { - if(!backend) - return; - - Common::SystemManager::get()->unregisterBackend(backend); - backend.reset(); - } + SystemBackendPosix(Common::Application *application0) : application(application0) {} }; +} } } -#endif /* MAD_MODULES_SYSTEMBACKENDPOSIX_H_ */ +#endif /* MAD_MODULES_SYSTEMBACKENDPOSIX_SYSTEMBACKENDPOSIX_H_ */ diff --git a/src/modules/SystemBackendProc/CMakeLists.txt b/src/modules/SystemBackendProc/CMakeLists.txt index ba5642c..42f7431 100644 --- a/src/modules/SystemBackendProc/CMakeLists.txt +++ b/src/modules/SystemBackendProc/CMakeLists.txt @@ -1,5 +1,6 @@ include_directories(${INCLUDES}) add_library(SystemBackendProc MODULE + Module.cpp Module.h SystemBackendProc.cpp SystemBackendProc.h ) diff --git a/src/modules/SystemBackendProc/Module.cpp b/src/modules/SystemBackendProc/Module.cpp new file mode 100644 index 0000000..7dbbbb5 --- /dev/null +++ b/src/modules/SystemBackendProc/Module.cpp @@ -0,0 +1,28 @@ +/* + * Module.cpp + * + * Copyright (C) 2009 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 "Module.h" + +extern "C" { + +Mad::Common::Module* SystemBackendProc_create(Mad::Common::Application *application) { + return new Mad::Modules::SystemBackendProc::Module(application); +} + +} diff --git a/src/modules/SystemBackendProc/Module.h b/src/modules/SystemBackendProc/Module.h new file mode 100644 index 0000000..6cdbd73 --- /dev/null +++ b/src/modules/SystemBackendProc/Module.h @@ -0,0 +1,52 @@ +/* + * Module.h + * + * Copyright (C) 2009 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_MODULES_SYSTEMBACKENDPROC_MODULE_H_ +#define MAD_MODULES_SYSTEMBACKENDPROC_MODULE_H_ + +#include "SystemBackendProc.h" + +#include +#include + +namespace Mad { +namespace Modules { +namespace SystemBackendProc { + +class Module : public Common::Module { + private: + Common::Application *application; + + boost::shared_ptr backend; + + public: + Module(Common::Application *application0) : application(application0), backend(new SystemBackendProc(application)) { + application->getSystemManager()->registerBackend(backend); + } + + virtual ~Module() { + application->getSystemManager()->unregisterBackend(backend); + } +}; + +} +} +} + +#endif /* MAD_MODULES_SYSTEMBACKENDPROC_MODULE_H_ */ diff --git a/src/modules/SystemBackendProc/SystemBackendProc.cpp b/src/modules/SystemBackendProc/SystemBackendProc.cpp index e8b45cd..daae55b 100644 --- a/src/modules/SystemBackendProc/SystemBackendProc.cpp +++ b/src/modules/SystemBackendProc/SystemBackendProc.cpp @@ -26,11 +26,10 @@ namespace Mad { namespace Modules { - -boost::shared_ptr SystemBackendProc::backend; +namespace SystemBackendProc { void SystemBackendProc::getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); uptimeFile.seekg(0, std::ios::beg); @@ -51,7 +50,7 @@ void SystemBackendProc::getUptimeInfo(unsigned long *uptime, unsigned long *idle } void SystemBackendProc::getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); if(totalMem) *totalMem = 0; @@ -93,7 +92,7 @@ void SystemBackendProc::getMemoryInfo(unsigned long *totalMem, unsigned long *fr } void SystemBackendProc::getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); unsigned long currentLoadValue = 0, nProcessesValue = 0; float loadAvg1Value = 0, loadAvg5Value = 0, loadAvg15Value = 0; @@ -129,16 +128,4 @@ void SystemBackendProc::getLoadInfo(unsigned long *currentLoad, unsigned long *n } } - - -extern "C" { - -void SystemBackendProc_init() { - Mad::Modules::SystemBackendProc::registerBackend(); -} - -void SystemBackendProc_deinit() { - Mad::Modules::SystemBackendProc::unregisterBackend(); -} - } diff --git a/src/modules/SystemBackendProc/SystemBackendProc.h b/src/modules/SystemBackendProc/SystemBackendProc.h index 9a0cb71..a54d783 100644 --- a/src/modules/SystemBackendProc/SystemBackendProc.h +++ b/src/modules/SystemBackendProc/SystemBackendProc.h @@ -17,51 +17,37 @@ * with this program. If not, see . */ -#ifndef MAD_MODULES_SYSTEMBACKENDPROC_H_ -#define MAD_MODULES_SYSTEMBACKENDPROC_H_ +#ifndef MAD_MODULES_SYSTEMBACKENDPROC_SYSTEMBACKENDPROC_H_ +#define MAD_MODULES_SYSTEMBACKENDPROC_SYSTEMBACKENDPROC_H_ #include +#include #include -#include namespace Mad { namespace Modules { +namespace SystemBackendProc { -class SystemBackendProc : public Common::SystemBackend, boost::noncopyable { +class SystemBackendProc : public Common::SystemBackend { private: - static boost::shared_ptr backend; + Common::Application *application; std::ifstream uptimeFile; std::ifstream meminfoFile; std::ifstream loadFile; - SystemBackendProc() : uptimeFile("/proc/uptime"), meminfoFile("/proc/meminfo"), loadFile("/proc/loadavg") {} - protected: virtual void getUptimeInfo(unsigned long *uptime, unsigned long *idleTime) throw(Core::Exception); virtual void getMemoryInfo(unsigned long *totalMem, unsigned long *freeMem, unsigned long *totalSwap, unsigned long *freeSwap) throw(Core::Exception); virtual void getLoadInfo(unsigned long *currentLoad, unsigned long *nProcesses, float *loadAvg1, float *loadAvg5, float *loadAvg15) throw(Core::Exception); public: - static void registerBackend() { - if(backend) - return; - - backend.reset(new SystemBackendProc()); - Common::SystemManager::get()->registerBackend(backend); - } - - static void unregisterBackend() { - if(!backend) - return; - - Common::SystemManager::get()->unregisterBackend(backend); - backend.reset(); - } + SystemBackendProc(Common::Application *application0) : application(application0), uptimeFile("/proc/uptime"), meminfoFile("/proc/meminfo"), loadFile("/proc/loadavg") {} }; +} } } -#endif /* MAD_MODULES_SYSTEMBACKENDPROC_H_ */ +#endif /* MAD_MODULES_SYSTEMBACKENDPROC_SYSTEMBACKENDPROC_H_ */ diff --git a/src/modules/UserBackendMysql/CMakeLists.txt b/src/modules/UserBackendMysql/CMakeLists.txt index b185c13..5f32d9c 100644 --- a/src/modules/UserBackendMysql/CMakeLists.txt +++ b/src/modules/UserBackendMysql/CMakeLists.txt @@ -1,6 +1,7 @@ include_directories(${INCLUDES} ${MYSQL_INCLUDE_DIR}) add_library(UserBackendMysql MODULE + Module.cpp Module.h UserBackendMysql.cpp UserBackendMysql.h ) diff --git a/src/modules/UserBackendMysql/Module.cpp b/src/modules/UserBackendMysql/Module.cpp new file mode 100644 index 0000000..c29f65b --- /dev/null +++ b/src/modules/UserBackendMysql/Module.cpp @@ -0,0 +1,28 @@ +/* + * Module.cpp + * + * Copyright (C) 2009 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 "Module.h" + +extern "C" { + +Mad::Common::Module* UserBackendMysql_create(Mad::Common::Application *application) { + return new Mad::Modules::UserBackendMysql::Module(application); +} + +} diff --git a/src/modules/UserBackendMysql/Module.h b/src/modules/UserBackendMysql/Module.h new file mode 100644 index 0000000..31f6692 --- /dev/null +++ b/src/modules/UserBackendMysql/Module.h @@ -0,0 +1,59 @@ +/* + * Module.h + * + * Copyright (C) 2009 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_MODULES_USERBACKENDMYSQL_MODULE_H_ +#define MAD_MODULES_USERBACKENDMYSQL_MODULE_H_ + +#include "UserBackendMysql.h" + +#include +#include +#include + +namespace Mad { +namespace Modules { +namespace UserBackendMysql { + +class Module : public Common::Module { + private: + Common::Application *application; + + boost::shared_ptr backend; + + public: + Module(Common::Application *application0) : application(application0), backend(new UserBackendMysql(application)) { + Server::Application *app = dynamic_cast(application); + + if(app) + app->getUserManager()->registerBackend(backend); + } + + virtual ~Module() { + Server::Application *app = dynamic_cast(application); + + if(app) + app->getUserManager()->unregisterBackend(backend); + } +}; + +} +} +} + +#endif /* MAD_MODULES_USERBACKENDMYSQL_MODULE_H_ */ diff --git a/src/modules/UserBackendMysql/UserBackendMysql.cpp b/src/modules/UserBackendMysql/UserBackendMysql.cpp index f0e9b2e..904c9a6 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.cpp +++ b/src/modules/UserBackendMysql/UserBackendMysql.cpp @@ -22,7 +22,6 @@ #include "UserBackendMysql.h" #include #include -#include #include #include @@ -33,8 +32,8 @@ namespace Mad { namespace Modules { +namespace UserBackendMysql { -boost::shared_ptr UserBackendMysql::backend; bool UserBackendMysql::handleConfigEntry(const Core::ConfigEntry &entry, bool handled) { if(handled) @@ -65,7 +64,7 @@ bool UserBackendMysql::handleConfigEntry(const Core::ConfigEntry &entry, bool ha val = strtol(entry[1][0].c_str(), &endptr, 10); if(endptr != 0 || val < 0 || val > 65535) - Core::Logger::log(Core::Logger::WARNING, "UserBackendMysql: Invalid port"); + application->log(Core::LoggerBase::WARNING, "UserBackendMysql: Invalid port"); else port = val; } @@ -121,21 +120,19 @@ bool UserBackendMysql::handleConfigEntry(const Core::ConfigEntry &entry, bool ha void UserBackendMysql::configFinished() { if(db.empty()) { - Core::Logger::log(Core::Logger::ERROR, "UserBackendMysql: No database name given"); + application->log(Core::LoggerBase::ERROR, "UserBackendMysql: No database name given"); return; } mysql = mysql_init(0); mysql_real_connect(mysql, host.c_str(), username.c_str(), passwd.c_str(), db.c_str(), port, unixSocket.empty() ? 0 : unixSocket.c_str(), 0); - - Server::UserManager::get()->registerBackend(backend); } boost::shared_ptr > UserBackendMysql::getUserList() throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); - if(mysql_ping(mysql)) + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); mysql_real_query(mysql, queryListUsers.c_str(), queryListUsers.length()); @@ -159,9 +156,9 @@ boost::shared_ptr > UserBackendMysql:: } boost::shared_ptr UserBackendMysql::getUserInfo(unsigned long uid) throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); - if(mysql_ping(mysql)) + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); std::string query = queryUserById; @@ -196,9 +193,9 @@ boost::shared_ptr UserBackendMysql::getUserInfo(unsigned long } boost::shared_ptr UserBackendMysql::getUserInfoByName(const std::string &name) throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); - if(mysql_ping(mysql)) + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); boost::scoped_array escapedName(new char[name.length()*2+1]); @@ -230,9 +227,9 @@ boost::shared_ptr UserBackendMysql::getUserInfoByName(const st } boost::shared_ptr > UserBackendMysql::getUserGroupList(unsigned long uid) throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); - if(mysql_ping(mysql)) + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); std::ostringstream tmp; @@ -258,9 +255,9 @@ boost::shared_ptr > UserBackendMysql::getUserGroupList(u boost::shared_ptr > UserBackendMysql::getGroupList() throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); - if(mysql_ping(mysql)) + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); mysql_real_query(mysql, queryListGroups.c_str(), queryListGroups.length()); @@ -281,9 +278,9 @@ boost::shared_ptr > UserBackendMysql: } std::string UserBackendMysql::getGroupName(unsigned long gid) throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); - if(mysql_ping(mysql)) + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); std::string query = queryGroupById; @@ -310,9 +307,9 @@ std::string UserBackendMysql::getGroupName(unsigned long gid) throw(Core::Except } unsigned long UserBackendMysql::getGroupId(const std::string &name) throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); - if(mysql_ping(mysql)) + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); boost::scoped_array escapedName(new char[name.length()*2+1]); @@ -336,9 +333,9 @@ unsigned long UserBackendMysql::getGroupId(const std::string &name) throw(Core:: } boost::shared_ptr > UserBackendMysql::getGroupUserList(unsigned long gid) throw(Core::Exception) { - Core::ThreadManager::get()->detach(); + application->getThreadManager()->detach(); - if(mysql_ping(mysql)) + if(!mysql || mysql_ping(mysql)) throw Core::Exception(Core::Exception::NOT_AVAILABLE); std::ostringstream tmp; @@ -362,36 +359,6 @@ boost::shared_ptr > UserBackendMysql::getGroupUserList(u return users; } - -void UserBackendMysql::registerBackend() { - if(backend) - return; - - backend.reset(new UserBackendMysql()); - Core::ConfigManager::get()->registerConfigurable(backend.get()); } - -void UserBackendMysql::unregisterBackend() { - if(!backend) - return; - - Core::ConfigManager::get()->unregisterConfigurable(backend.get()); - Server::UserManager::get()->unregisterBackend(backend); - - backend.reset(); } - -} -} - -extern "C" { - -void UserBackendMysql_init() { - Mad::Modules::UserBackendMysql::registerBackend(); -} - -void UserBackendMysql_deinit() { - Mad::Modules::UserBackendMysql::unregisterBackend(); -} - } diff --git a/src/modules/UserBackendMysql/UserBackendMysql.h b/src/modules/UserBackendMysql/UserBackendMysql.h index 523806f..aa141da 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.h +++ b/src/modules/UserBackendMysql/UserBackendMysql.h @@ -17,22 +17,25 @@ * with this program. If not, see . */ -#ifndef MAD_MODULES_USERBACKENDMYSQL_H_ -#define MAD_MODULES_USERBACKENDMYSQL_H_ +#ifndef MAD_MODULES_USERBACKENDMYSQL_USERBACKENDMYSQL_H_ +#define MAD_MODULES_USERBACKENDMYSQL_USERBACKENDMYSQL_H_ #include -#include + +#include + #include +#include #include - namespace Mad { namespace Modules { +namespace UserBackendMysql { class UserBackendMysql : public Server::UserBackend, private Core::Configurable { private: - static boost::shared_ptr backend; + Common::Application *application; std::string host, username, passwd, db, unixSocket; uint16_t port; @@ -44,8 +47,6 @@ class UserBackendMysql : public Server::UserBackend, private Core::Configurable MYSQL *mysql; - UserBackendMysql() : port(0), mysql(0) {} - protected: virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool); virtual void configFinished(); @@ -61,18 +62,22 @@ class UserBackendMysql : public Server::UserBackend, private Core::Configurable virtual boost::shared_ptr > getGroupUserList(unsigned long gid) throw(Core::Exception); public: + UserBackendMysql(Common::Application *application0) : application(application0), port(0), mysql(0) { + application->getConfigManager()->registerConfigurable(this); + } + virtual ~UserBackendMysql() { + application->getConfigManager()->unregisterConfigurable(this); + if(mysql) { mysql_close(mysql); mysql = 0; } } - - static void registerBackend(); - static void unregisterBackend(); }; +} } } -#endif /* MAD_MODULES_USERBACKENDMYSQL_H_ */ +#endif /* MAD_MODULES_USERBACKENDMYSQL_USERBACKENDMYSQL_H_ */ -- cgit v1.2.3