File: httpsession.h

package info (click to toggle)
goldencheetah 1%3A3.5~DEV1810-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 185,760 kB
  • sloc: cpp: 263,686; ansic: 5,811; xml: 1,708; yacc: 1,515; java: 1,477; python: 625; lex: 452; sh: 404; ruby: 176; makefile: 118; perl: 94
file content (118 lines) | stat: -rw-r--r-- 3,111 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
  @file
  @author Stefan Frings
*/

#ifndef HTTPSESSION_H
#define HTTPSESSION_H

#include <QByteArray>
#include <QVariant>
#include <QReadWriteLock>
#include "httpglobal.h"

/**
  This class stores data for a single HTTP session.
  A session can store any number of key/value pairs. This class uses implicit
  sharing for read and write access. This class is thread safe.
  @see HttpSessionStore should be used to create and get instances of this class.
*/

class DECLSPEC HttpSession {

public:

    /**
      Constructor.
      @param canStore The session can store data, if this parameter is true.
      Otherwise all calls to set() and remove() do not have any effect.
     */
    HttpSession(bool canStore=false);

    /**
      Copy constructor. Creates another HttpSession object that shares the
      data of the other object.
    */
    HttpSession(const HttpSession& other);

    /**
      Copy operator. Detaches from the current shared data and attaches to
      the data of the other object.
    */
    HttpSession& operator= (const HttpSession& other);


    /**
      Destructor. Detaches from the shared data.
    */
    virtual ~HttpSession();

    /** Get the unique ID of this session. This method is thread safe. */
    QByteArray getId() const;

    /**
      Null sessions cannot store data. All calls to set() and remove() 
      do not have any effect.This method is thread safe.
    */
    bool isNull() const;

    /** Set a value. This method is thread safe. */
    void set(const QByteArray& key, const QVariant& value);

    /** Remove a value. This method is thread safe. */
    void remove(const QByteArray& key);

    /** Get a value. This method is thread safe. */
    QVariant get(const QByteArray& key) const;

    /** Check if a key exists. This method is thread safe. */
    bool contains(const QByteArray& key) const;

    /**
      Get a copy of all data stored in this session.
      Changes to the session do not affect the copy and vice versa.
      This method is thread safe.
    */
    QMap<QByteArray,QVariant> getAll() const;

    /**
      Get the timestamp of last access. That is the time when the last
      HttpSessionStore::getSession() has been called.
      This method is thread safe.
    */
    qint64 getLastAccess() const;

    /**
      Set the timestamp of last access, to renew the timeout period.
      Called by  HttpSessionStore::getSession().
      This method is thread safe.
    */
    void setLastAccess();

private:

    struct HttpSessionData {

        /** Unique ID */
        QByteArray id;

        /** Timestamp of last access, set by the HttpSessionStore */
        qint64 lastAccess;

        /** Reference counter */
        int refCount;

        /** Used to synchronize threads */
        QReadWriteLock lock;

        /** Storage for the key/value pairs; */
        QMap<QByteArray,QVariant> values;

    };

    /** Pointer to the shared data. */
    HttpSessionData* dataPtr;

};

#endif // HTTPSESSION_H