File: httpsession.cpp

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 (158 lines) | stat: -rw-r--r-- 3,939 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
  @file
  @author Stefan Frings
*/

#include "httpsession.h"
#include <QDateTime>
#include <QUuid>


HttpSession::HttpSession(bool canStore) {
    if (canStore) {
        dataPtr=new HttpSessionData();
        dataPtr->refCount=1;
        dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
        dataPtr->id=QUuid::createUuid().toString().toLocal8Bit();
#ifdef SUPERVERBOSE
        wDebug("HttpSession: created new session data with id %s",dataPtr->id.data());
#endif
    }
    else {
        dataPtr=0;
    }
}

HttpSession::HttpSession(const HttpSession& other) {
    dataPtr=other.dataPtr;
    if (dataPtr) {
        dataPtr->lock.lockForWrite();
        dataPtr->refCount++;
#ifdef SUPERVERBOSE
        wDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
#endif
        dataPtr->lock.unlock();
    }
}

HttpSession& HttpSession::operator= (const HttpSession& other) {
    HttpSessionData* oldPtr=dataPtr;
    dataPtr=other.dataPtr;
    if (dataPtr) {
        dataPtr->lock.lockForWrite();
        dataPtr->refCount++;
#ifdef SUPERVERBOSE
        wDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
#endif
        dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
        dataPtr->lock.unlock();
    }
    if (oldPtr) {
        int refCount;
        oldPtr->lock.lockForRead();
        refCount=oldPtr->refCount--;
#ifdef SUPERVERBOSE
        wDebug("HttpSession: refCount of %s is %i",oldPtr->id.data(),oldPtr->refCount);
#endif
        oldPtr->lock.unlock();
        if (refCount==0) {
            delete oldPtr;
        }
    }
    return *this;
}

HttpSession::~HttpSession() {
    if (dataPtr) {
        int refCount;
        dataPtr->lock.lockForRead();
        refCount=--dataPtr->refCount;
#ifdef SUPERVERBOSE
        wDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
#endif
        dataPtr->lock.unlock();
        if (refCount==0) {
            wDebug("HttpSession: deleting data");
            delete dataPtr;
        }
    }
}


QByteArray HttpSession::getId() const {
    if (dataPtr) {
        return dataPtr->id;
    }
    else {
        return QByteArray();
    }
}

bool HttpSession::isNull() const {
    return dataPtr==0;
}

void HttpSession::set(const QByteArray& key, const QVariant& value) {
    if (dataPtr) {
        dataPtr->lock.lockForWrite();
        dataPtr->values.insert(key,value);
        dataPtr->lock.unlock();
    }
}

void HttpSession::remove(const QByteArray& key) {
    if (dataPtr) {
        dataPtr->lock.lockForWrite();
        dataPtr->values.remove(key);
        dataPtr->lock.unlock();
    }
}

QVariant HttpSession::get(const QByteArray& key) const {
    QVariant value;
    if (dataPtr) {
        dataPtr->lock.lockForRead();
        value=dataPtr->values.value(key);
        dataPtr->lock.unlock();
    }
    return value;
}

bool HttpSession::contains(const QByteArray& key) const {
    bool found=false;
    if (dataPtr) {
        dataPtr->lock.lockForRead();
        found=dataPtr->values.contains(key);
        dataPtr->lock.unlock();
    }
    return found;
}

QMap<QByteArray,QVariant> HttpSession::getAll() const {
    QMap<QByteArray,QVariant> values;
    if (dataPtr) {
        dataPtr->lock.lockForRead();
        values=dataPtr->values;
        dataPtr->lock.unlock();
    }
    return values;
}

qint64 HttpSession::getLastAccess() const {
    qint64 value=0;
    if (dataPtr) {
        dataPtr->lock.lockForRead();
        value=dataPtr->lastAccess;
        dataPtr->lock.unlock();
    }
    return value;
}


void HttpSession::setLastAccess() {
    if (dataPtr) {
        dataPtr->lock.lockForRead();
        dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
        dataPtr->lock.unlock();
    }
}