From 9c076d2649ff8c6997c2dec1e1ef4f7359d404ec Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 21 May 2009 00:42:57 +0200 Subject: UserBackend-Interface ueberarbeitet --- src/modules/UserBackendMysql/UserBackendMysql.cpp | 46 +++++++++++------------ src/modules/UserBackendMysql/UserBackendMysql.h | 8 ++-- 2 files changed, 26 insertions(+), 28 deletions(-) (limited to 'src/modules') diff --git a/src/modules/UserBackendMysql/UserBackendMysql.cpp b/src/modules/UserBackendMysql/UserBackendMysql.cpp index ebea4dc..a8751ee 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.cpp +++ b/src/modules/UserBackendMysql/UserBackendMysql.cpp @@ -34,7 +34,7 @@ namespace Mad { namespace Modules { -UserBackendMysql *UserBackendMysql::backend; +boost::shared_ptr UserBackendMysql::backend; bool UserBackendMysql::handleConfigEntry(const Common::ConfigEntry &entry, bool handled) { if(handled) @@ -132,16 +132,18 @@ void UserBackendMysql::configFinished() { } -bool UserBackendMysql::getUserList(const boost::function1& > &callback) { +boost::shared_ptr > UserBackendMysql::getUserList() { + Net::ThreadManager::get()->detach(); + mysql_ping(mysql); mysql_real_query(mysql, queryListUsers.c_str(), queryListUsers.length()); MYSQL_RES *result = mysql_use_result(mysql); if(mysql_num_fields(result) < 4) - return true; // TODO Error + return boost::shared_ptr >(); // TODO Error - std::map users; + boost::shared_ptr > users(new std::map()); while(MYSQL_ROW row = mysql_fetch_row(result)) { Common::UserInfo user(strtoul(row[0], 0, 10), row[2]); @@ -149,15 +151,15 @@ bool UserBackendMysql::getUserList(const boost::function1insert(std::make_pair(user.getUid(), user)); } - Net::ThreadManager::get()->pushWork(boost::bind(callback, users)); - - return true; + return users; } -bool UserBackendMysql::getUserInfo(unsigned long uid, const boost::function1 &callback) { +boost::shared_ptr UserBackendMysql::getUserInfo(unsigned long uid) { + Net::ThreadManager::get()->detach(); + mysql_ping(mysql); std::string query = queryUserById; @@ -173,25 +175,22 @@ bool UserBackendMysql::getUserInfo(unsigned long uid, const boost::function1(); // TODO Error MYSQL_ROW row = mysql_fetch_row(result); if(row) { - Common::UserInfo user(strtoul(row[0], 0, 10), row[2]); + boost::shared_ptr user(new Common::UserInfo(strtoul(row[0], 0, 10), row[2])); - user.setGid(strtoul(row[1], 0, 10)); - user.setFullName(row[3]); - - Net::ThreadManager::get()->pushWork(boost::bind(callback, user)); + user->setGid(strtoul(row[1], 0, 10)); + user->setFullName(row[3]); while((row = mysql_fetch_row(result)) != 0) {} - } - else { - Net::ThreadManager::get()->pushWork(boost::bind(callback, Common::UserInfo())); + + return user; } - return true; + return boost::shared_ptr(); } @@ -199,19 +198,18 @@ void UserBackendMysql::registerBackend() { if(backend) return; - backend = new UserBackendMysql(); - Common::ConfigManager::get()->registerConfigurable(backend); + backend.reset(new UserBackendMysql()); + Common::ConfigManager::get()->registerConfigurable(backend.get()); } void UserBackendMysql::unregisterBackend() { if(!backend) return; - Common::ConfigManager::get()->unregisterConfigurable(backend); + Common::ConfigManager::get()->unregisterConfigurable(backend.get()); Server::UserManager::get()->unregisterBackend(backend); - delete backend; - backend = 0; + backend.reset(); } } diff --git a/src/modules/UserBackendMysql/UserBackendMysql.h b/src/modules/UserBackendMysql/UserBackendMysql.h index 2e4f1ff..de28069 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.h +++ b/src/modules/UserBackendMysql/UserBackendMysql.h @@ -30,9 +30,9 @@ namespace Mad { namespace Modules { -class UserBackendMysql : private Server::UserBackend, private Common::Configurable { +class UserBackendMysql : public Server::UserBackend, private Common::Configurable { private: - static UserBackendMysql *backend; + static boost::shared_ptr backend; std::string host, username, passwd, db, unixSocket; uint16_t port; @@ -50,8 +50,8 @@ class UserBackendMysql : private Server::UserBackend, private Common::Configurab virtual bool handleConfigEntry(const Common::ConfigEntry &entry, bool); virtual void configFinished(); - virtual bool getUserList(const boost::function1& > &callback); - virtual bool getUserInfo(unsigned long uid, const boost::function1 &callback); + virtual boost::shared_ptr > getUserList(); + virtual boost::shared_ptr getUserInfo(unsigned long uid); public: virtual ~UserBackendMysql() { -- cgit v1.2.3