C++ Web Framework  3.0
session.h
1 /*
2  Copyright 2017 Herik Lima de Castro and Marcelo Medeiros Eler
3  Distributed under MIT license, or public domain if desired and
4  recognized in your jurisdiction.
5  See file LICENSE for detail.
6 */
7 
8 #ifndef SESSION_H
9 #define SESSION_H
10 
11 #include <QMap>
12 #include <atomic>
13 #include <QMutex>
14 #include <QObject>
15 #include <QThread>
16 #include <QDateTime>
17 #include <QStringList>
18 #include <QMutexLocker>
19 #include "qmapthreadsafety.h"
20 #include "cppwebframework_global.h"
21 
22 CWF_BEGIN_NAMESPACE
23 
24 class Configuration;
25 class Request;
26 
30 class CPPWEBFRAMEWORKSHARED_EXPORT Session
31 {
32  friend class HttpReadRequest;
33  friend class Request;
34  QString id;
35  QAtomicInteger<qint64> sessionTimeOut;
36  QAtomicInteger<qint64> creationTime;
37  QAtomicInteger<qint64> lastAccessedTime;
38  QAtomicInteger<qint64> sessionExpirationTime;
39  QAtomicInteger<qint64> timeOut;
40  QAtomicInteger<qint8> autoClearAttributes = 0;
41  QAtomicInteger<qint8> expired = 0;
43  mutable QMutex mutex;
44 public:
48  explicit Session(const QString &id, qint64 sessionTimeOut);
49 
50 
51  ~Session();
56  QObject *getAttribute(const QString &name) const noexcept { return attributes.value(name, nullptr); }
60  QStringList getAttributeNames();
64  inline qint64 getCreationTime() const noexcept { return creationTime; }
68  QString getId() const;
72  inline qint64 getLastAccessedTime() const noexcept { return lastAccessedTime; }
76  void validate();
80  inline void invalidate() noexcept { expired = 1; }
86  inline int removeAttribute(const QString &name) noexcept { return attributes.remove(name); }
90  inline void addAttribute(const QString &name, QObject *value) noexcept { attributes.insert(name, value); }
94  inline bool getAutoClearAttributes() const noexcept { return autoClearAttributes; }
98  inline void setAutoClearAttributes(bool value) noexcept { autoClearAttributes = value; }
102  bool isExpired();
106  inline qint64 getSessionTimeOut() const noexcept { return sessionTimeOut; }
111  void setSessionTimeOut(qint64 value);
112 };
113 
114 CWF_END_NAMESPACE
115 
116 #endif // SESSION_H
void addAttribute(const QString &name, QObject *value) noexcept
This method add an attribute to the session.
Definition: session.h:90
qint64 getLastAccessedTime() const noexcept
Returns the time of the last session access.
Definition: session.h:72
bool getAutoClearAttributes() const noexcept
getAutoClearAttributes
Definition: session.h:94
int remove(const Key &key)
This method removes a specific element given a specific key.
Definition: qmapthreadsafety.h:231
QObject * getAttribute(const QString &name) const noexcept
Returns a session attribute given a name.
Definition: session.h:56
iterator insert(const Key &key, const T &value)
This method inserts a new key and value in the map.
Definition: qmapthreadsafety.h:166
The Request class holds all information about a http request.
Definition: request.h:27
void setAutoClearAttributes(bool value) noexcept
setAutoClearAttributes
Definition: session.h:98
The HttpReadRequest class is created automatically by the CppWebServer and inserted in a QThreadPoo...
Definition: httpreadrequest.h:36
The QMapThreadSafety class is a thread safe QMap.
Definition: qmapthreadsafety.h:23
int removeAttribute(const QString &name) noexcept
Removes all the items that have the key key from the map. Returns the number of items removed which i...
Definition: session.h:86
void invalidate() noexcept
Make a invalid session.
Definition: session.h:80
The Session class holds information about a client session.
Definition: session.h:30
All classes of C++ Web Framework are contained within the namespace CWF.
Definition: configuration.h:24
qint64 getCreationTime() const noexcept
getCreationTime
Definition: session.h:64
qint64 getSessionTimeOut() const noexcept
Returns the session timeout.
Definition: session.h:106