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 --- .../RequestHandlers/UserInfoRequestHandler.cpp | 30 ++++++++--------- .../RequestHandlers/UserInfoRequestHandler.h | 6 ---- .../RequestHandlers/UserListRequestHandler.cpp | 39 ++++++++++------------ .../RequestHandlers/UserListRequestHandler.h | 6 ---- src/Server/UserBackend.h | 15 +++++---- src/Server/UserManager.cpp | 38 +++++++++++---------- src/Server/UserManager.h | 23 ++++++------- 7 files changed, 70 insertions(+), 87 deletions(-) (limited to 'src/Server') diff --git a/src/Server/RequestHandlers/UserInfoRequestHandler.cpp b/src/Server/RequestHandlers/UserInfoRequestHandler.cpp index 740c2a9..287d026 100644 --- a/src/Server/RequestHandlers/UserInfoRequestHandler.cpp +++ b/src/Server/RequestHandlers/UserInfoRequestHandler.cpp @@ -22,8 +22,6 @@ #include #include -#include - namespace Mad { namespace Server { namespace RequestHandlers { @@ -44,24 +42,22 @@ void UserInfoRequestHandler::handlePacket(const Common::XmlPacket &packet) { // TODO Require authentication - if(!UserManager::get()->getUserInfo(packet["uid"], boost::bind(&UserInfoRequestHandler::userInfoHandler, this, _1))) { - Common::XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); - - sendPacket(ret); - signalFinished()(); - } -} + boost::shared_ptr info = UserManager::get()->getUserInfo(packet["uid"]); -void UserInfoRequestHandler::userInfoHandler(const Common::UserInfo &info) { Common::XmlPacket ret; - ret.setType("OK"); - ret.add("uid", info.getUid()); - ret.add("gid", info.getGid()); - ret.add("username", info.getUsername()); - ret.add("fullName", info.getFullName()); + if(info) { + ret.setType("OK"); + + ret.add("uid", info->getUid()); + ret.add("gid", info->getGid()); + ret.add("username", info->getUsername()); + ret.add("fullName", info->getFullName()); + } + else { + ret.setType("Error"); + ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); + } sendPacket(ret); signalFinished()(); diff --git a/src/Server/RequestHandlers/UserInfoRequestHandler.h b/src/Server/RequestHandlers/UserInfoRequestHandler.h index 5b1d466..90032f0 100644 --- a/src/Server/RequestHandlers/UserInfoRequestHandler.h +++ b/src/Server/RequestHandlers/UserInfoRequestHandler.h @@ -21,18 +21,12 @@ #define MAD_SERVER_REQUESTHANDLERS_USERINFOREQUESTHANDLER_H_ #include -#include - -#include namespace Mad { namespace Server { namespace RequestHandlers { class UserInfoRequestHandler : public Common::RequestHandler { - private: - void userInfoHandler(const Common::UserInfo &info); - protected: virtual void handlePacket(const Common::XmlPacket &packet); diff --git a/src/Server/RequestHandlers/UserListRequestHandler.cpp b/src/Server/RequestHandlers/UserListRequestHandler.cpp index cd05c12..713753b 100644 --- a/src/Server/RequestHandlers/UserListRequestHandler.cpp +++ b/src/Server/RequestHandlers/UserListRequestHandler.cpp @@ -22,8 +22,6 @@ #include #include -#include - namespace Mad { namespace Server { namespace RequestHandlers { @@ -44,29 +42,28 @@ void UserListRequestHandler::handlePacket(const Common::XmlPacket &packet) { // TODO Require authentication - if(!UserManager::get()->getUserList(boost::bind(&UserListRequestHandler::userListHandler, this, _1))) { - Common::XmlPacket ret; - ret.setType("Error"); - ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); - - sendPacket(ret); - signalFinished()(); - } -} + boost::shared_ptr > info = UserManager::get()->getUserList(); -void UserListRequestHandler::userListHandler(const std::map &info) { Common::XmlPacket ret; - ret.setType("OK"); - ret.addList("users"); - for(std::map::const_iterator user = info.begin(); user != info.end(); ++user) { - ret["users"].addEntry(); - Common::XmlPacket::Entry &entry = ret["users"].back(); + if(info) { + ret.setType("OK"); + ret.addList("users"); - entry.add("uid", user->second.getUid()); - entry.add("gid", user->second.getGid()); - entry.add("username", user->second.getUsername()); - entry.add("fullName", user->second.getFullName()); + for(std::map::iterator user = info->begin(); user != info->end(); ++user) { + ret["users"].addEntry(); + Common::XmlPacket::Entry &entry = ret["users"].back(); + + entry.add("uid", user->second.getUid()); + entry.add("gid", user->second.getGid()); + entry.add("username", user->second.getUsername()); + entry.add("fullName", user->second.getFullName()); + } + } + + else { + ret.setType("Error"); + ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); } sendPacket(ret); diff --git a/src/Server/RequestHandlers/UserListRequestHandler.h b/src/Server/RequestHandlers/UserListRequestHandler.h index 7c85aa0..4b1a385 100644 --- a/src/Server/RequestHandlers/UserListRequestHandler.h +++ b/src/Server/RequestHandlers/UserListRequestHandler.h @@ -21,18 +21,12 @@ #define MAD_SERVER_REQUESTHANDLERS_USERLISTREQUESTHANDLER_H_ #include -#include - -#include namespace Mad { namespace Server { namespace RequestHandlers { class UserListRequestHandler : public Common::RequestHandler { - private: - void userListHandler(const std::map &info); - protected: virtual void handlePacket(const Common::XmlPacket &packet); diff --git a/src/Server/UserBackend.h b/src/Server/UserBackend.h index 6aac7bc..a673fa9 100644 --- a/src/Server/UserBackend.h +++ b/src/Server/UserBackend.h @@ -27,7 +27,7 @@ #include #include -#include +#include namespace Mad { @@ -41,19 +41,20 @@ class UserBackend { UserBackend() {} - virtual bool getUserList(const boost::function1& > &callback _UNUSED_PARAMETER_) { - return false; + virtual boost::shared_ptr > getUserList() { + return boost::shared_ptr >(); } - virtual bool getUserInfo(unsigned long uid _UNUSED_PARAMETER_, const boost::function1 &callback _UNUSED_PARAMETER_) { - return false; + virtual boost::shared_ptr getUserInfo(unsigned long uid _UNUSED_PARAMETER_) { + return boost::shared_ptr(); } - virtual bool setPassword(unsigned long uid _UNUSED_PARAMETER_, const std::string&, const boost::function1 &callback _UNUSED_PARAMETER_) { + // TODO Better interface... + virtual bool setPassword(unsigned long uid _UNUSED_PARAMETER_, const std::string &password _UNUSED_PARAMETER_) { return false; } - virtual bool addUser(const Common::UserInfo &userInfo _UNUSED_PARAMETER_, const boost::function1 &callback _UNUSED_PARAMETER_) { + virtual bool addUser(const Common::UserInfo &userInfo _UNUSED_PARAMETER_) { return false; } diff --git a/src/Server/UserManager.cpp b/src/Server/UserManager.cpp index d763a8b..3b5887a 100644 --- a/src/Server/UserManager.cpp +++ b/src/Server/UserManager.cpp @@ -26,44 +26,46 @@ namespace Server { UserManager UserManager::userManager; -bool UserManager::Compare::operator() (const UserBackend *b1, const UserBackend *b2) { +bool UserManager::Compare::operator() (boost::shared_ptr b1, boost::shared_ptr b2) { if(b1->getPriority() == b2->getPriority()) - return (b1 > b2); + return (b1.get() > b2.get()); else return (b1->getPriority() > b2->getPriority()); } -bool UserManager::getUserList(const boost::function1& > &callback) { - for(std::set::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->getUserList(callback)) - return true; +boost::shared_ptr > UserManager::getUserList() { + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + boost::shared_ptr > ret = (*backend)->getUserList(); + if(ret) + return ret; } - return false; + return boost::shared_ptr >(); } -bool UserManager::getUserInfo(unsigned long uid, const boost::function1 &callback) { - for(std::set::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->getUserInfo(uid, callback)) - return true; +boost::shared_ptr UserManager::getUserInfo(unsigned long uid) { + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + boost::shared_ptr ret = (*backend)->getUserInfo(uid); + if(ret) + return ret; } - return false; + return boost::shared_ptr(); } -bool UserManager::setPassword(unsigned long uid, const std::string &password, const boost::function1 &callback) { - for(std::set::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->setPassword(uid, password, callback)) +bool UserManager::setPassword(unsigned long uid, const std::string &password) { + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + if((*backend)->setPassword(uid, password)) return true; } return false; } -bool UserManager::addUser(const Common::UserInfo &userInfo, const boost::function1 &callback) { - for(std::set::iterator backend = backends.begin(); backend != backends.end(); ++backend) { - if((*backend)->addUser(userInfo, callback)) +bool UserManager::addUser(const Common::UserInfo &userInfo) { + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + if((*backend)->addUser(userInfo)) return true; } diff --git a/src/Server/UserManager.h b/src/Server/UserManager.h index d0e7074..c3d990d 100644 --- a/src/Server/UserManager.h +++ b/src/Server/UserManager.h @@ -25,42 +25,41 @@ #include #include -#include +#include +#include namespace Mad { namespace Server { class UserBackend; -class UserManager { +class UserManager : boost::noncopyable { private: struct Compare { - bool operator() (const UserBackend *b1, const UserBackend *b2); + bool operator() (boost::shared_ptr b1, boost::shared_ptr b2); }; static UserManager userManager; - std::set backends; + std::set, Compare> backends; UserManager() {} public: - void registerBackend(UserBackend *backend) { + void registerBackend(boost::shared_ptr backend) { backends.insert(backend); } - void unregisterBackend(UserBackend *backend) { + void unregisterBackend(boost::shared_ptr backend) { backends.erase(backend); } + boost::shared_ptr > getUserList(); + boost::shared_ptr getUserInfo(unsigned long uid); - bool getUserList(const boost::function1& > &callback); - bool getUserInfo(unsigned long uid, const boost::function1 &callback); - - bool setPassword(unsigned long uid, const std::string &password, const boost::function1 &callback); - - bool addUser(const Common::UserInfo &userInfo, const boost::function1 &callback); + bool setPassword(unsigned long uid, const std::string &password); + bool addUser(const Common::UserInfo &userInfo); static UserManager *get() { return &userManager; -- cgit v1.2.3