session.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #ifndef SESSION_H
  8. #define SESSION_H
  9. #include <QMap>
  10. #include <atomic>
  11. #include <QMutex>
  12. #include <QObject>
  13. #include <QThread>
  14. #include <QDateTime>
  15. #include <QStringList>
  16. #include <QMutexLocker>
  17. #include "qmapthreadsafety.h"
  18. #include "cppwebframework_global.h"
  19. CWF_BEGIN_NAMESPACE
  20. class Configuration;
  21. class Request;
  22. /**
  23. * @brief The Session class holds information about a client session.
  24. */
  25. class CPPWEBFRAMEWORKSHARED_EXPORT Session
  26. {
  27. friend class HttpReadRequest;
  28. friend class Request;
  29. QString id;
  30. QAtomicInteger<qint64> sessionTimeOut;
  31. QAtomicInteger<qint64> creationTime;
  32. QAtomicInteger<qint64> lastAccessedTime;
  33. QAtomicInteger<qint64> sessionExpirationTime;
  34. QAtomicInteger<qint64> timeOut;
  35. QAtomicInteger<qint8> autoClearAttributes = 0;
  36. QAtomicInteger<qint8> expired = 0;
  37. QMapThreadSafety<QString, QObject*> attributes;
  38. mutable QMutex mutex;
  39. public:
  40. /**
  41. * @brief Construct a session with a unique identifier
  42. */
  43. explicit Session(QString id, qint64 sessionTimeOut);
  44. ~Session();
  45. /**
  46. * @brief Returns a session attribute given a name.
  47. * @warning: If the parameter is not found, nullptr is returned.
  48. */
  49. QObject *getAttribute(const QString &name) const noexcept { return attributes.value(name, nullptr); }
  50. /**
  51. * @brief Returns a session attribute given a name.
  52. */
  53. QStringList getAttributeNames();
  54. /**
  55. * @brief getCreationTime
  56. */
  57. inline qint64 getCreationTime() const noexcept { return creationTime; }
  58. /**
  59. * @brief Returns the unique id
  60. */
  61. QString getId() const;
  62. /**
  63. * @brief Returns the time of the last session access.
  64. */
  65. inline qint64 getLastAccessedTime() const noexcept { return lastAccessedTime; }
  66. /**
  67. * @brief Make a valid session.
  68. */
  69. void validate();
  70. /**
  71. * @brief Make a invalid session.
  72. */
  73. inline void invalidate() noexcept { expired = 1; }
  74. /**
  75. * @brief Removes all the items that have the key key from the map.
  76. * Returns the number of items removed which is usually 1 but will be 0
  77. * if the key isn't in the map, or > 1 if insertMulti() has been used with the key.
  78. */
  79. inline int removeAttribute(const QString &name) noexcept { return attributes.remove(name); }
  80. /**
  81. * @brief This method add an attribute to the session.
  82. */
  83. inline void addAttribute(const QString &name, QObject *value) noexcept { attributes.insert(name, value); }
  84. /**
  85. * @brief getAutoClearAttributes
  86. */
  87. inline bool getAutoClearAttributes() const noexcept { return autoClearAttributes; }
  88. /**
  89. * @brief setAutoClearAttributes
  90. */
  91. inline void setAutoClearAttributes(bool value) noexcept { autoClearAttributes = value; }
  92. /**
  93. * @brief This method returns true if the session is expired otherwise returns false.
  94. */
  95. bool isExpired();
  96. /**
  97. * @brief Returns the session timeout.
  98. */
  99. inline qint64 getSessionTimeOut() const noexcept { return sessionTimeOut; }
  100. /**
  101. * @brief Sets the current session timeout.
  102. * If value is negative then it will be configured according to sessionExpirationTime of the CPPWeb.ini file.
  103. */
  104. void setSessionTimeOut(qint64 value);
  105. };
  106. CWF_END_NAMESPACE
  107. #endif // SESSION_H