From fcb987ce966f4446c8065b07ea5c3c4b3fbde0b3 Mon Sep 17 00:00:00 2001 From: Matthew Sherborne Date: Thu, 23 Jun 2011 21:41:32 +1000 Subject: [PATCH] Initial SessionManager prototype --- SessionManager.cpp | 94 +++++++++++++++++++ SessionManager.hpp | 52 ++++++++++ messages/{EditButtonBar.xml => ButtonBar.xml} | 0 3 files changed, 146 insertions(+) create mode 100644 SessionManager.cpp create mode 100644 SessionManager.hpp rename messages/{EditButtonBar.xml => ButtonBar.xml} (100%) diff --git a/SessionManager.cpp b/SessionManager.cpp new file mode 100644 index 0000000..46545e7 --- /dev/null +++ b/SessionManager.cpp @@ -0,0 +1,94 @@ +/* + * ===================================================================================== + * + * Filename: SessionManager.cpp + * + * Description: G + * + * Version: 1.0 + * Created: 06/22/2011 09:00:24 PM + * Revision: none + * Compiler: gcc + * + * Author: Matthew Sherborne (), msherborne@gmail.com + * Company: + * + * ===================================================================================== + */ + +#include "SessionManager.hpp" + +using boost::thread::lock_guard; +using boost::thread::shared_lock; + +namespace vidanueva { + +/** +* Info about a single logged in session .. +* Only a single copy of this object exists at one time, and thread locking is handled by +* its container .. SessionManager +*/ +class Session { +private: + string _username; + time_t _loginTime; + string _ip; + string _cookieContents; +public: + Session() {}; + Session(const string& username, const string& ip, const string& cookie) : + _username(username), _loginTime(), _ip(ip), _cookieContents(cookie) {} + const string& username() const { return _username; }; + const time_t& loginTime() const { return _loginTime; }; + const string& ip() const { return _ip; }; + const string& cookieContents() const { return _cookieContents; }; +}; + +void SessionManager::login(const string& username, const string& ip, const string& cookie) { + lock_guard lock(_lock); + sessions.insert(sessions::value_type(cookie, Session(username, ip, cookie))); +} + +/** +* @brief Returns true if the cookie passed is for a logged in user +* +* @param cookie The cookie that the user is giving us +* +* @return true if cookie is of a logged in user +*/ +bool SessionManager::isLoggedIn(const string& cookie) { + shared_lock lock(_lock); + PSession pSession = sessions.find(cookie); + if (pSession != sessions::end()) { + // Check that it hasn't timed out + time_t now = time(NULL); + Session& session = (*pSession).second; + if ((now - session.loginTime()) < _timeout) { + // Cookie is expired - delete it and return result + sessions.erase(cookie) == 1; + + } + } + return false; // Cookie not found +} + +/** +* @brief Logs a user out +* +* @param cookie of the logged in user +* +* @return Returns true if logged the user out +*/ +bool SessionManager::logout(const string& cookie) { + guard_lock lock(_lock); + return sessions.erase(cookie) == 1; +} + +SessionManager::~SessionManager() { + // Delete all the sessions but while locked + guard_lock lock(_lock); + sessions.erase(sessions.begin(), sessions.end()); +} + + +} // namespace vidanueva diff --git a/SessionManager.hpp b/SessionManager.hpp new file mode 100644 index 0000000..5ed709b --- /dev/null +++ b/SessionManager.hpp @@ -0,0 +1,52 @@ +/* + * ===================================================================================== + * + * Filename: SessionManager.hpp + * + * Description: An in memory, thread safe Session manager + * Because witty's 'internalPathChanged' thing is not being too happy + * with html5 api .. I'm having to manage logged in users on my own. + * + * Version: 1.0 + * Created: 06/22/2011 08:32:16 PM + * Revision: none + * Compiler: gcc + * + * Author: Matthew Sherborne (), msherborne@gmail.com + * Company: + * + * ===================================================================================== + */ + +#include +#include +#include +#include + +using std::string; +using std::time_t; +using std::map; +using boost::UpgradeLockable; + +namespace vidanueva { + +class Session; + +/** +* @brief Handles logged in users +*/ +class SessionManager { +private: + typedef map::iterator PSession; + UpgradeLockable _lock; // Many can read session info .. but only one can write it + unsigned long _timeout; /// How long a single session lasts + map _loggedInUsers; /// Holds data for logged in users. Key is the cookie. +public: + SessionManager(unsigned long timeout) : _timeout(timeout); /// timeout for logged in session in msecs + ~SessionManager(); + void login(const string& username, const string& ip, const string& cookie); + bool isLoggedIn(const string& cookie); + bool logout(const string& cookie); +}; + +} // namespace vidanueva diff --git a/messages/EditButtonBar.xml b/messages/ButtonBar.xml similarity index 100% rename from messages/EditButtonBar.xml rename to messages/ButtonBar.xml