summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2009-06-30 21:33:27 +0200
committerMatthias Schiffer <matthias@gamezock.de>2009-06-30 21:33:27 +0200
commit68ec8067feec09a6fecf8bf2fbffb5d757db3d4a (patch)
tree22e36b867f76647cbdc0a14c5ade54d558d2b4c6 /src
parent61ac0032dec6cf6d93ca3f2c36af17f93d1682b9 (diff)
downloadmad-68ec8067feec09a6fecf8bf2fbffb5d757db3d4a.tar
mad-68ec8067feec09a6fecf8bf2fbffb5d757db3d4a.zip
User-Cache in eigenes Backend verlagert
Diffstat (limited to 'src')
-rw-r--r--src/Common/CMakeLists.txt1
-rw-r--r--src/Common/UserBackend.h8
-rw-r--r--src/Common/UserCache.cpp153
-rw-r--r--src/Common/UserCache.h79
-rw-r--r--src/Common/UserManager.cpp246
-rw-r--r--src/Common/UserManager.h80
-rw-r--r--src/mad-server.cpp2
-rw-r--r--src/madc.cpp3
8 files changed, 293 insertions, 279 deletions
diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt
index 7201ed5..74c5029 100644
--- a/src/Common/CMakeLists.txt
+++ b/src/Common/CMakeLists.txt
@@ -22,6 +22,7 @@ add_library(Common
SystemBackend.h
SystemManager.cpp SystemManager.h
UserBackend.h
+ UserCache.cpp UserCache.h
UserInfo.h
UserManager.cpp UserManager.h
XmlPacket.cpp XmlPacket.h
diff --git a/src/Common/UserBackend.h b/src/Common/UserBackend.h
index 8375551..9983ee0 100644
--- a/src/Common/UserBackend.h
+++ b/src/Common/UserBackend.h
@@ -37,10 +37,12 @@
namespace Mad {
namespace Common {
+class UserCache;
class UserManager;
class UserBackend {
protected:
+ friend class UserCache;
friend class UserManager;
UserBackend() {}
@@ -86,12 +88,12 @@ class UserBackend {
throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED));
}
+ public:
+ virtual ~UserBackend() {}
+
virtual int getPriority() const {
return 0;
}
-
- public:
- virtual ~UserBackend() {}
};
}
diff --git a/src/Common/UserCache.cpp b/src/Common/UserCache.cpp
new file mode 100644
index 0000000..63f72dd
--- /dev/null
+++ b/src/Common/UserCache.cpp
@@ -0,0 +1,153 @@
+/*
+ * UserCache.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 "UserCache.h"
+
+#include <iostream>
+
+namespace Mad {
+namespace Common {
+
+boost::shared_ptr<const std::map<unsigned long, UserInfo> > UserCache::getUserList() throw(Core::Exception) {
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
+
+ if(users) {
+ std::cerr << "Cached" << std::endl;
+ return users;
+ }
+ else if(userException)
+ throw userException;
+
+ try {
+ users = backend->getUserList();
+
+ userNames.reset(new std::map<std::string, unsigned long>);
+ for(std::map<unsigned long, UserInfo>::const_iterator user = users->begin(); user != users->end(); ++user)
+ userNames->insert(std::make_pair(user->second.getUsername(), user->first));
+
+ return users;
+ }
+ catch(Core::Exception e) {
+ userException = e;
+ throw userException;
+ }
+}
+
+boost::shared_ptr<const UserInfo> UserCache::getUserInfo(unsigned long uid) throw(Core::Exception) {
+ getUserList();
+
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
+
+ if(!users)
+ throw userException;
+
+ std::map<unsigned long, UserInfo>::const_iterator user = users->find(uid);
+
+ if(user == users->end())
+ throw Core::Exception(Core::Exception::NOT_FOUND);
+
+ return boost::shared_ptr<UserInfo>(new UserInfo(user->second));
+}
+
+boost::shared_ptr<const UserInfo> UserCache::getUserInfoByName(const std::string &name) throw(Core::Exception) {
+ getUserList();
+
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
+
+ if(!users)
+ throw userException;
+
+ std::map<std::string, unsigned long>::const_iterator uid = userNames->find(name);
+ if(uid == userNames->end())
+ throw Core::Exception(Core::Exception::NOT_FOUND);
+
+ std::map<unsigned long, UserInfo>::const_iterator user = users->find(uid->second);
+
+ return boost::shared_ptr<UserInfo>(new UserInfo(user->second));
+}
+
+boost::shared_ptr<const std::set<unsigned long> > UserCache::getUserGroupList(unsigned long uid) throw(Core::Exception) {
+ return backend->getUserGroupList(uid);
+}
+
+
+boost::shared_ptr<const std::map<unsigned long, GroupInfo> > UserCache::getGroupList() throw(Core::Exception) {
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
+
+ if(groups) {
+ std::cerr << "Cached" << std::endl;
+ return groups;
+ }
+ else if(groupException)
+ throw groupException;
+
+ try {
+ groups = backend->getGroupList();
+
+ groupNames.reset(new std::map<std::string, unsigned long>);
+ for(std::map<unsigned long, GroupInfo>::const_iterator group = groups->begin(); group != groups->end(); ++group)
+ groupNames->insert(std::make_pair(group->second.getName(), group->first));
+
+ return groups;
+ }
+ catch(Core::Exception e) {
+ groupException = e;
+ throw groupException;
+ }
+}
+
+boost::shared_ptr<const GroupInfo> UserCache::getGroupInfo(unsigned long gid) throw(Core::Exception) {
+ getGroupList();
+
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
+
+ if(!groups)
+ throw groupException;
+
+ std::map<unsigned long, GroupInfo>::const_iterator group = groups->find(gid);
+
+ if(group == groups->end())
+ throw Core::Exception(Core::Exception::NOT_FOUND);
+
+ return boost::shared_ptr<GroupInfo>(new GroupInfo(group->second));
+}
+
+boost::shared_ptr<const GroupInfo> UserCache::getGroupInfoByName(const std::string &name) throw(Core::Exception) {
+ getGroupList();
+
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
+
+ if(!groups)
+ throw groupException;
+
+ std::map<std::string, unsigned long>::const_iterator gid = groupNames->find(name);
+ if(gid == groupNames->end())
+ throw Core::Exception(Core::Exception::NOT_FOUND);
+
+ std::map<unsigned long, GroupInfo>::const_iterator group = groups->find(gid->second);
+
+ return boost::shared_ptr<GroupInfo>(new GroupInfo(group->second));
+}
+
+boost::shared_ptr<const std::set<unsigned long> > UserCache::getGroupUserList(unsigned long uid) throw(Core::Exception) {
+ return backend->getGroupUserList(uid);
+}
+
+}
+}
diff --git a/src/Common/UserCache.h b/src/Common/UserCache.h
new file mode 100644
index 0000000..1e432fd
--- /dev/null
+++ b/src/Common/UserCache.h
@@ -0,0 +1,79 @@
+/*
+ * UserCache.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_COMMON_USERCACHE_H_
+#define MAD_COMMON_USERCACHE_H_
+
+#include "UserBackend.h"
+
+#include <limits>
+
+#include <boost/thread/shared_mutex.hpp>
+
+namespace Mad {
+namespace Common {
+
+class UserManager;
+
+class UserCache : public UserBackend, private boost::noncopyable {
+ private:
+ friend class UserManager;
+
+ boost::shared_ptr<UserBackend> backend;
+
+ boost::shared_mutex mutex;
+
+ boost::shared_ptr<const std::map<unsigned long, UserInfo> > users;
+ boost::shared_ptr<std::map<std::string, unsigned long> > userNames;
+ Core::Exception userException;
+
+ boost::shared_ptr<const std::map<unsigned long, GroupInfo> > groups;
+ boost::shared_ptr<std::map<std::string, unsigned long> > groupNames;
+ Core::Exception groupException;
+
+ /*boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > userGroups;
+ boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > groupUsers;
+ Core::Exception userGroupException;*/
+
+ protected:
+ virtual boost::shared_ptr<const std::map<unsigned long, UserInfo> > getUserList() throw(Core::Exception);
+ virtual boost::shared_ptr<const UserInfo> getUserInfo(unsigned long uid) throw(Core::Exception);
+ virtual boost::shared_ptr<const UserInfo> getUserInfoByName(const std::string &name) throw(Core::Exception);
+ virtual boost::shared_ptr<const std::set<unsigned long> > getUserGroupList(unsigned long uid) throw(Core::Exception);
+
+ virtual boost::shared_ptr<const std::map<unsigned long, GroupInfo> > getGroupList() throw(Core::Exception);
+ virtual boost::shared_ptr<const GroupInfo> getGroupInfo(unsigned long gid) throw(Core::Exception);
+ virtual boost::shared_ptr<const GroupInfo> getGroupInfoByName(const std::string &name) throw(Core::Exception);
+ virtual boost::shared_ptr<const std::set<unsigned long> > getGroupUserList(unsigned long gid) throw(Core::Exception);
+
+ virtual void setPassword(unsigned long uid, const std::string &password) throw(Core::Exception) {
+ backend->setPassword(uid, password);
+ }
+
+ virtual void addUser(const UserInfo &userInfo) throw(Core::Exception) {
+ backend->addUser(userInfo);
+ }
+
+ UserCache(boost::shared_ptr<UserBackend> backend0) : backend(backend0) {}
+};
+
+}
+}
+
+#endif /* MAD_COMMON_USERCACHE_H_ */
diff --git a/src/Common/UserManager.cpp b/src/Common/UserManager.cpp
index 342d4bf..253466e 100644
--- a/src/Common/UserManager.cpp
+++ b/src/Common/UserManager.cpp
@@ -18,7 +18,7 @@
*/
#include "UserManager.h"
-#include "UserBackend.h"
+#include "UserCache.h"
#include <iostream>
@@ -33,94 +33,40 @@ bool UserManager::Compare::operator() (boost::shared_ptr<UserBackend> b1, boost:
}
-boost::shared_ptr<const std::map<unsigned long, UserInfo> > UserManager::getUserList() throw(Core::Exception) {
+void UserManager::registerBackendCached(boost::shared_ptr<UserBackend> backend) {
{
- boost::shared_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(users)
- return users;
- else if(userException)
- throw *userException;
- }
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
+ backends.insert(std::make_pair(backend, boost::shared_ptr<UserCache>(new UserCache(backend))));
}
+}
- Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::shared_ptr<const std::map<unsigned long, UserInfo> > ret;
+boost::shared_ptr<const std::map<unsigned long, UserInfo> > UserManager::getUserList() throw(Core::Exception) {
+ Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- {
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
- try {
- ret = (*backend)->getUserList();
- break;
- }
- catch(Core::Exception e2) {
- if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
- e = e2;
- }
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ try {
+ return backend->second->getUserList();
}
- }
-
- {
- boost::upgrade_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(ret) {
- boost::upgrade_to_unique_lock<boost::shared_mutex> upgradeLock(lock);
- users = ret;
-
- boost::shared_ptr<std::map<std::string, unsigned long> > names(new std::map<std::string, unsigned long>);
- for(std::map<unsigned long, UserInfo>::const_iterator user = users->begin(); user != users->end(); ++user)
- names->insert(std::make_pair(user->second.getUsername(), user->first));
-
- userNames = names;
- }
- else
- userException.reset(new Core::Exception(e));
+ catch(Core::Exception e2) {
+ if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
+ e = e2;
}
}
- if(ret)
- return ret;
- else
- throw e;
+ throw e;
}
boost::shared_ptr<const UserInfo> UserManager::getUserInfo(unsigned long uid) throw(Core::Exception) {
- {
- boost::shared_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(!users && !userException) {
- lock.unlock();
- getUserList();
- lock.lock();
- }
-
- if(caching) {
- if(users) {
- std::map<unsigned long, UserInfo>::const_iterator user = users->find(uid);
- if(user != users->end())
- return boost::shared_ptr<UserInfo>(new UserInfo(user->second));
- else
- throw Core::Exception(Core::Exception::NOT_FOUND);
- }
- else if(userException)
- throw *userException;
- }
- }
- }
-
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- return (*backend)->getUserInfo(uid);
+ return backend->second->getUserInfo(uid);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -132,37 +78,13 @@ boost::shared_ptr<const UserInfo> UserManager::getUserInfo(unsigned long uid) th
}
boost::shared_ptr<const UserInfo> UserManager::getUserInfoByName(const std::string &name) throw(Core::Exception) {
- {
- boost::shared_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(!users && !userException) {
- lock.unlock();
- getUserList();
- lock.lock();
- }
-
- if(caching) {
- if(userNames) {
- std::map<std::string, unsigned long>::const_iterator user = userNames->find(name);
- if(user != userNames->end())
- return boost::shared_ptr<UserInfo>(new UserInfo(users->find(user->second)->second));
- else
- throw Core::Exception(Core::Exception::NOT_FOUND);
- }
- else if(userException)
- throw *userException;
- }
- }
- }
-
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- return (*backend)->getUserInfoByName(name);
+ return backend->second->getUserInfoByName(name);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -176,11 +98,11 @@ boost::shared_ptr<const UserInfo> UserManager::getUserInfoByName(const std::stri
boost::shared_ptr<const std::set<unsigned long> > UserManager::getUserGroupList(unsigned long uid) throw(Core::Exception) {
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- return (*backend)->getUserGroupList(uid);
+ return backend->second->getUserGroupList(uid);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -192,28 +114,16 @@ boost::shared_ptr<const std::set<unsigned long> > UserManager::getUserGroupList(
}
boost::shared_ptr<const std::map<unsigned long, GroupInfo> > UserManager::getGroupList() throw(Core::Exception) {
- {
- boost::shared_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(groups)
- return groups;
- else if(groupException)
- throw *groupException;
- }
- }
-
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
boost::shared_ptr<const std::map<unsigned long, GroupInfo> > ret;
{
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- ret = (*backend)->getGroupList();
- break;
+ return backend->second->getGroupList();
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -222,63 +132,17 @@ boost::shared_ptr<const std::map<unsigned long, GroupInfo> > UserManager::getGro
}
}
- {
- boost::upgrade_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(ret) {
- boost::upgrade_to_unique_lock<boost::shared_mutex> upgradeLock(lock);
- groups = ret;
-
- boost::shared_ptr<std::map<std::string, unsigned long> > names(new std::map<std::string, unsigned long>);
- for(std::map<unsigned long, GroupInfo>::const_iterator group = groups->begin(); group != groups->end(); ++group)
- names->insert(std::make_pair(group->second.getName(), group->first));
-
- groupNames = names;
- }
- else
- groupException.reset(new Core::Exception(e));
- }
- }
-
- if(ret)
- return ret;
- else
- throw e;
+ throw e;
}
boost::shared_ptr<const GroupInfo> UserManager::getGroupInfo(unsigned long gid) throw(Core::Exception) {
- {
- boost::shared_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(!groups && !groupException) {
- lock.unlock();
- getGroupList();
- lock.lock();
- }
-
- if(caching) {
- if(groups) {
- std::map<unsigned long, GroupInfo>::const_iterator group = groups->find(gid);
- if(group != groups->end())
- return boost::shared_ptr<GroupInfo>(new GroupInfo(group->second));
- else
- throw Core::Exception(Core::Exception::NOT_FOUND);
- }
- else if(groupException)
- throw *groupException;
- }
- }
- }
-
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- return (*backend)->getGroupInfo(gid);
+ return backend->second->getGroupInfo(gid);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -290,37 +154,13 @@ boost::shared_ptr<const GroupInfo> UserManager::getGroupInfo(unsigned long gid)
}
boost::shared_ptr<const GroupInfo> UserManager::getGroupInfoByName(const std::string &name) throw(Core::Exception) {
- {
- boost::shared_lock<boost::shared_mutex> lock(mutex);
-
- if(caching) {
- if(!groups && !groupException) {
- lock.unlock();
- getGroupList();
- lock.lock();
- }
-
- if(caching) {
- if(groupNames) {
- std::map<std::string, unsigned long>::const_iterator group = groupNames->find(name);
- if(group != groupNames->end())
- return boost::shared_ptr<GroupInfo>(new GroupInfo(groups->find(group->second)->second));
- else
- throw Core::Exception(Core::Exception::NOT_FOUND);
- }
- else if(groupException)
- throw *groupException;
- }
- }
- }
-
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- return (*backend)->getGroupInfoByName(name);
+ return backend->second->getGroupInfoByName(name);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -334,11 +174,11 @@ boost::shared_ptr<const GroupInfo> UserManager::getGroupInfoByName(const std::st
boost::shared_ptr<const std::set<unsigned long> > UserManager::getGroupUserList(unsigned long gid) throw(Core::Exception) {
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- return (*backend)->getGroupUserList(gid);
+ return backend->second->getGroupUserList(gid);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -352,11 +192,11 @@ boost::shared_ptr<const std::set<unsigned long> > UserManager::getGroupUserList(
void UserManager::setPassword(unsigned long uid, const std::string &password) throw(Core::Exception) {
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- (*backend)->setPassword(uid, password);
+ backend->second->setPassword(uid, password);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
@@ -370,11 +210,11 @@ void UserManager::setPassword(unsigned long uid, const std::string &password) th
void UserManager::addUser(const UserInfo &userInfo) throw(Core::Exception) {
Core::Exception e(Core::Exception::NOT_IMPLEMENTED);
- boost::lock_guard<boost::shared_mutex> backendLock(backendMutex);
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
- for(std::set<boost::shared_ptr<UserBackend> >::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
+ for(BackendMap::iterator backend = backends.begin(); backend != backends.end(); ++backend) {
try {
- (*backend)->addUser(userInfo);
+ backend->second->addUser(userInfo);
}
catch(Core::Exception e2) {
if(e.getErrorCode() == Core::Exception::NOT_IMPLEMENTED && e2.getErrorCode() != Core::Exception::NOT_IMPLEMENTED)
diff --git a/src/Common/UserManager.h b/src/Common/UserManager.h
index c9b0712..7413970 100644
--- a/src/Common/UserManager.h
+++ b/src/Common/UserManager.h
@@ -36,6 +36,7 @@ namespace Common {
class Application;
class UserBackend;
+class UserCache;
class UserManager : boost::noncopyable {
private:
@@ -45,84 +46,25 @@ class UserManager : boost::noncopyable {
bool operator() (boost::shared_ptr<UserBackend> b1, boost::shared_ptr<UserBackend> b2);
};
- std::set<boost::shared_ptr<UserBackend>, Compare> backends;
+ typedef std::map<boost::shared_ptr<UserBackend>, boost::shared_ptr<UserBackend>, Compare> BackendMap;
- boost::shared_mutex backendMutex;
- boost::shared_mutex mutex;
-
- bool caching;
-
- boost::shared_ptr<const std::map<unsigned long, UserInfo> > users;
- boost::shared_ptr<const std::map<std::string, unsigned long> > userNames;
- boost::shared_ptr<Core::Exception> userException;
+ BackendMap backends;
- boost::shared_ptr<const std::map<unsigned long, GroupInfo> > groups;
- boost::shared_ptr<const std::map<std::string, unsigned long> > groupNames;
- boost::shared_ptr<Core::Exception> groupException;
-
- boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > userGroups;
- boost::shared_ptr<const std::multimap<unsigned long, unsigned long> > groupUsers;
- boost::shared_ptr<Core::Exception> userGroupException;
+ boost::shared_mutex mutex;
- UserManager() : caching(false) {}
+ UserManager() {}
public:
void registerBackend(boost::shared_ptr<UserBackend> backend) {
- {
- boost::lock_guard<boost::shared_mutex> lock(backendMutex);
- backends.insert(backend);
- }
-
- update();
- }
-
- void unregisterBackend(boost::shared_ptr<UserBackend> backend) {
- {
- boost::lock_guard<boost::shared_mutex> lock(backendMutex);
- backends.erase(backend);
- }
-
- update();
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
+ backends.insert(std::make_pair(backend, backend));
}
- bool isCaching() {
- boost::shared_lock<boost::shared_mutex> lock(mutex);
- return caching;
- }
-
- void setCaching(bool cache) {
- {
- boost::lock_guard<boost::shared_mutex> lock(mutex);
- caching = cache;
- }
-
- update();
- }
-
- void update(bool refresh = false) {
- {
- boost::lock_guard<boost::shared_mutex> lock(mutex);
+ void registerBackendCached(boost::shared_ptr<UserBackend> backend);
- if(!caching)
- return;
-
- users.reset();
- userNames.reset();
- userException.reset();
-
- groups.reset();
- groupNames.reset();
- groupException.reset();
-
- userGroups.reset();
- groupUsers.reset();
- userGroupException.reset();
- }
-
- if(refresh) {
- getUserList();
- getGroupList();
- }
+ void unregisterBackend(boost::shared_ptr<UserBackend> backend) {
+ boost::lock_guard<boost::shared_mutex> lock(mutex);
+ backends.erase(backend);
}
boost::shared_ptr<const std::map<unsigned long, UserInfo> > getUserList() throw(Core::Exception);
diff --git a/src/mad-server.cpp b/src/mad-server.cpp
index 7cac86f..59dca1c 100644
--- a/src/mad-server.cpp
+++ b/src/mad-server.cpp
@@ -38,8 +38,6 @@ int main() {
application.getConfigManager()->loadFile("mad-server.conf");
application.getConfigManager()->finish();
- application.getUserManager()->setCaching(true);
-
while(true)
sleep(1000);
diff --git a/src/madc.cpp b/src/madc.cpp
index 7738f29..ba9770e 100644
--- a/src/madc.cpp
+++ b/src/madc.cpp
@@ -80,8 +80,7 @@ int main(int argc, char *argv[]) {
std::cerr << " done." << std::endl << std::endl;
boost::shared_ptr<Common::Backends::NetworkUserBackend> networkUserBackend(new Common::Backends::NetworkUserBackend(&application, connection));
- application.getUserManager()->registerBackend(networkUserBackend);
- application.getUserManager()->setCaching(true);
+ application.getUserManager()->registerBackendCached(networkUserBackend);
Client::CommandParser commandParser(&application, connection);