httpsession.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. @file
  3. @author Stefan Frings
  4. */
  5. #ifndef HTTPSESSION_H
  6. #define HTTPSESSION_H
  7. #include <QByteArray>
  8. #include <QVariant>
  9. #include <QReadWriteLock>
  10. #include "httpglobal.h"
  11. namespace stefanfrings {
  12. /**
  13. This class stores data for a single HTTP session.
  14. A session can store any number of key/value pairs. This class uses implicit
  15. sharing for read and write access. This class is thread safe.
  16. @see HttpSessionStore should be used to create and get instances of this class.
  17. */
  18. class DECLSPEC HttpSession {
  19. public:
  20. /**
  21. Constructor.
  22. @param canStore The session can store data, if this parameter is true.
  23. Otherwise all calls to set() and remove() do not have any effect.
  24. */
  25. HttpSession(const bool canStore=false);
  26. /**
  27. Copy constructor. Creates another HttpSession object that shares the
  28. data of the other object.
  29. */
  30. HttpSession(const HttpSession& other);
  31. /**
  32. Copy operator. Detaches from the current shared data and attaches to
  33. the data of the other object.
  34. */
  35. HttpSession& operator= (const HttpSession& other);
  36. /**
  37. Destructor. Detaches from the shared data.
  38. */
  39. virtual ~HttpSession();
  40. /** Get the unique ID of this session. This method is thread safe. */
  41. const QByteArray& getId() const;
  42. /**
  43. Null sessions cannot store data. All calls to set() and remove()
  44. do not have any effect. This method is thread safe.
  45. */
  46. bool isNull() const;
  47. /** Set a value. This method is thread safe. */
  48. void set(const QByteArray& key, const QVariant& value);
  49. /** Remove a value. This method is thread safe. */
  50. void remove(const QByteArray& key);
  51. /** Get a value. This method is thread safe. */
  52. QVariant get(const QByteArray& key) const;
  53. /** Check if a key exists. This method is thread safe. */
  54. bool contains(const QByteArray& key) const;
  55. /**
  56. Get a copy of all data stored in this session.
  57. Changes to the session do not affect the copy and vice versa.
  58. This method is thread safe.
  59. */
  60. QMap<QByteArray,QVariant> getAll() const;
  61. /**
  62. Get the timestamp of last access. That is the time when the last
  63. HttpSessionStore::getSession() has been called.
  64. This method is thread safe.
  65. */
  66. qint64 getLastAccess() const;
  67. /**
  68. Set the timestamp of last access, to renew the timeout period.
  69. Called by HttpSessionStore::getSession().
  70. This method is thread safe.
  71. */
  72. void setLastAccess();
  73. private:
  74. struct HttpSessionData {
  75. /** Unique ID */
  76. QByteArray id;
  77. /** Timestamp of last access, set by the HttpSessionStore */
  78. qint64 lastAccess;
  79. /** Reference counter */
  80. int refCount;
  81. /** Used to synchronize threads */
  82. QReadWriteLock lock;
  83. /** Storage for the key/value pairs; */
  84. QMap<QByteArray,QVariant> values;
  85. };
  86. /** Pointer to the shared data. */
  87. HttpSessionData* dataPtr;
  88. };
  89. } // end of namespace
  90. #endif // HTTPSESSION_H