Wexus2 0.20
wexus2.src/wexus/SessionManager.h
00001 
00002 /*
00003  * Copyright (c) 2011 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_WEXUS_SESSIONMANAGER_H__
00009 #define __INCLUDED_WEXUS_SESSIONMANAGER_H__
00010 
00011 #include <QMutex>
00012 #include <QUuid>
00013 #include <QMap>
00014 #include <QVariant>
00015 #include <QDateTime>
00016 
00017 #include <wexus/TR1.h>
00018 #include <wexus/Cookies.h>
00019 
00020 namespace wexus
00021 {
00022   class SessionManager;
00023 }
00024 
00025 /**
00026  * A map of UUIDs to sessions.
00027  * rename this, perhaps?
00028  *
00029  * @author Aleksander Demko
00030  */ 
00031 class wexus::SessionManager
00032 {
00033   public:
00034     class Data
00035     {
00036       public:
00037         // called by Session, used to protect fieldValues
00038         QMutex mutex;
00039         QVariantMap fieldValues;
00040         QDateTime lastAccessed;
00041     };
00042 
00043   /**
00044    * Holds a lock on session parameters.
00045    *
00046    * @author Aleksander Demko
00047    */ 
00048   class Locker
00049   {
00050     public:
00051       /**
00052        * Empty ctor.
00053        *
00054        * @author Aleksander Demko
00055        */ 
00056       Locker(void);
00057       /**
00058        * Copy ctor: TRANSFERS ownership of the session.
00059        *
00060        * @author Aleksander Demko
00061        */ 
00062       Locker(const Locker &rhs);
00063       /**
00064        * Creates a session bound to the given Data.
00065        *
00066        * @author Aleksander Demko
00067        */ 
00068       Locker(const QDateTime &now, std::shared_ptr<Data> data);
00069       /**
00070        * Creates an orphan session for the given id.
00071        *
00072        * @author Aleksander Demko
00073        */ 
00074       Locker(const QDateTime &now, SessionManager *sesman, const QUuid &orphanId);
00075 
00076       /// destructor
00077       ~Locker();
00078 
00079       /// gets the linked QVariantMap of session values
00080       QVariantMap & map(void) const;
00081 
00082     private:
00083       /// parse the linked HTTPRequest, if it hasnt already
00084       //void parseRequest(void);
00085 
00086     private:
00087       SessionManager *dm_sesman;
00088       QUuid dm_orphanId;
00089       mutable std::shared_ptr<Data> dm_data;
00090   };
00091 
00092   public:
00093     /**
00094      * Constructs a session manager with the expiry
00095      * time length.
00096      *
00097      *
00098      * @param expiry_seconds the expiry time, in seconds.
00099      * Could be 0, for no expiry, but this is not recommended
00100      * (could leak sessions, then)
00101      * @author Aleksander Demko
00102      */ 
00103     SessionManager(int expiry_seconds = 3600);
00104 
00105     /**
00106      * Gets a session based on the given id.
00107      * If one doesn't exist, a new one will be created and
00108      * returned.
00109      *
00110      * @author Aleksander Demko
00111      */ 
00112     Locker getData(const QUuid &id);
00113 
00114     /**
00115      * Extra the session by the UUID stored in the session cookie,
00116      * if any.
00117      *
00118      * @author Aleksander Demko
00119      */ 
00120     Locker getDataByCookie(Cookies &cookies);
00121 
00122     /**
00123      * Stores the given Data at the given ID.
00124      * this is used by Locker.
00125      *
00126      * @author Aleksander Demko
00127      */ 
00128     void putData(const QUuid &id, std::shared_ptr<Data> &dat);
00129 
00130   protected:
00131     void pruneExpiredSessions(const QDateTime &now);
00132 
00133   private:
00134     const int dm_expiry_seconds;  // RO (but if ever made RW, put it under the following lock:
00135 
00136     QMutex dm_maplock;    // locks the following:
00137     QDateTime dm_last_prunesweep;
00138     typedef QMap<QUuid, std::shared_ptr<Data> > map_t;
00139     map_t dm_map; //shared RW
00140 };
00141 
00142 #endif
00143 
 All Classes Namespaces Functions Variables Enumerations Enumerator