diff options
Diffstat (limited to 'src/modules/UserBackendMysql')
-rw-r--r-- | src/modules/UserBackendMysql/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/modules/UserBackendMysql/Module.cpp | 28 | ||||
-rw-r--r-- | src/modules/UserBackendMysql/Module.h | 59 | ||||
-rw-r--r-- | src/modules/UserBackendMysql/UserBackendMysql.cpp | 71 | ||||
-rw-r--r-- | src/modules/UserBackendMysql/UserBackendMysql.h | 27 |
5 files changed, 123 insertions, 63 deletions
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 <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 "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 <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_MODULES_USERBACKENDMYSQL_MODULE_H_ +#define MAD_MODULES_USERBACKENDMYSQL_MODULE_H_ + +#include "UserBackendMysql.h" + +#include <Server/Application.h> +#include <Server/UserManager.h> +#include <Common/Module.h> + +namespace Mad { +namespace Modules { +namespace UserBackendMysql { + +class Module : public Common::Module { + private: + Common::Application *application; + + boost::shared_ptr<UserBackendMysql> backend; + + public: + Module(Common::Application *application0) : application(application0), backend(new UserBackendMysql(application)) { + Server::Application *app = dynamic_cast<Server::Application*>(application); + + if(app) + app->getUserManager()->registerBackend(backend); + } + + virtual ~Module() { + Server::Application *app = dynamic_cast<Server::Application*>(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 <Core/ConfigEntry.h> #include <Core/ConfigManager.h> -#include <Core/Logger.h> #include <Core/ThreadManager.h> #include <sstream> @@ -33,8 +32,8 @@ namespace Mad { namespace Modules { +namespace UserBackendMysql { -boost::shared_ptr<UserBackendMysql> 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<std::map<unsigned long, Common::UserInfo> > 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<std::map<unsigned long, Common::UserInfo> > UserBackendMysql:: } boost::shared_ptr<Common::UserInfo> 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<Common::UserInfo> UserBackendMysql::getUserInfo(unsigned long } boost::shared_ptr<Common::UserInfo> 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<char> escapedName(new char[name.length()*2+1]); @@ -230,9 +227,9 @@ boost::shared_ptr<Common::UserInfo> UserBackendMysql::getUserInfoByName(const st } boost::shared_ptr<std::set<unsigned long> > 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<std::set<unsigned long> > UserBackendMysql::getUserGroupList(u boost::shared_ptr<std::map<unsigned long, Common::GroupInfo> > 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<std::map<unsigned long, Common::GroupInfo> > 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<char> escapedName(new char[name.length()*2+1]); @@ -336,9 +333,9 @@ unsigned long UserBackendMysql::getGroupId(const std::string &name) throw(Core:: } boost::shared_ptr<std::set<unsigned long> > 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<std::set<unsigned long> > 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 <http://www.gnu.org/licenses/>. */ -#ifndef MAD_MODULES_USERBACKENDMYSQL_H_ -#define MAD_MODULES_USERBACKENDMYSQL_H_ +#ifndef MAD_MODULES_USERBACKENDMYSQL_USERBACKENDMYSQL_H_ +#define MAD_MODULES_USERBACKENDMYSQL_USERBACKENDMYSQL_H_ #include <Server/UserBackend.h> -#include <Server/UserManager.h> + +#include <Common/Application.h> + #include <Core/Configurable.h> +#include <Core/ConfigManager.h> #include <mysql/mysql.h> - namespace Mad { namespace Modules { +namespace UserBackendMysql { class UserBackendMysql : public Server::UserBackend, private Core::Configurable { private: - static boost::shared_ptr<UserBackendMysql> 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<std::set<unsigned long> > 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_ */ |