SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
httpsession.cpp
Go to the documentation of this file.
1 
6 #include "httpsession.h"
7 #include <QDateTime>
8 #include <QUuid>
9 
10 using namespace qtwebapp;
11 
13 {
14  if (canStore)
15  {
17  dataPtr->refCount=1;
18  dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
19  dataPtr->id=QUuid::createUuid().toString().toLocal8Bit();
20 #ifdef SUPERVERBOSE
21  qDebug("HttpSession: created new session data with id %s",dataPtr->id.data());
22 #endif
23  }
24  else
25  {
26  dataPtr=0;
27  }
28 }
29 
31 {
32  dataPtr=other.dataPtr;
33  if (dataPtr)
34  {
35  dataPtr->lock.lockForWrite();
36  dataPtr->refCount++;
37 #ifdef SUPERVERBOSE
38  qDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
39 #endif
40  dataPtr->lock.unlock();
41  }
42 }
43 
45 {
46  if (this == &other) {
47  return *this;
48  }
49  HttpSessionData* oldPtr=dataPtr;
50  dataPtr=other.dataPtr;
51  if (dataPtr)
52  {
53  dataPtr->lock.lockForWrite();
54  dataPtr->refCount++;
55 #ifdef SUPERVERBOSE
56  qDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
57 #endif
58  dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
59  dataPtr->lock.unlock();
60  }
61  if (oldPtr)
62  {
63  int refCount;
64  oldPtr->lock.lockForRead();
65  refCount=oldPtr->refCount--;
66 #ifdef SUPERVERBOSE
67  qDebug("HttpSession: refCount of %s is %i",oldPtr->id.data(),oldPtr->refCount);
68 #endif
69  oldPtr->lock.unlock();
70  if (refCount==0)
71  {
72  delete oldPtr;
73  }
74  }
75  return *this;
76 }
77 
79 {
80  if (dataPtr) {
81  int refCount;
82  dataPtr->lock.lockForRead();
83  refCount=--dataPtr->refCount;
84 #ifdef SUPERVERBOSE
85  qDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
86 #endif
87  dataPtr->lock.unlock();
88  if (refCount==0)
89  {
90  qDebug("HttpSession: deleting data");
91  delete dataPtr;
92  }
93  }
94 }
95 
96 
97 QByteArray HttpSession::getId() const
98 {
99  if (dataPtr)
100  {
101  return dataPtr->id;
102  }
103  else
104  {
105  return QByteArray();
106  }
107 }
108 
109 bool HttpSession::isNull() const {
110  return dataPtr==0;
111 }
112 
113 void HttpSession::set(const QByteArray& key, const QVariant& value)
114 {
115  if (dataPtr)
116  {
117  dataPtr->lock.lockForWrite();
118  dataPtr->values.insert(key,value);
119  dataPtr->lock.unlock();
120  }
121 }
122 
123 void HttpSession::remove(const QByteArray& key)
124 {
125  if (dataPtr)
126  {
127  dataPtr->lock.lockForWrite();
128  dataPtr->values.remove(key);
129  dataPtr->lock.unlock();
130  }
131 }
132 
133 QVariant HttpSession::get(const QByteArray& key) const
134 {
135  QVariant value;
136  if (dataPtr)
137  {
138  dataPtr->lock.lockForRead();
139  value=dataPtr->values.value(key);
140  dataPtr->lock.unlock();
141  }
142  return value;
143 }
144 
145 bool HttpSession::contains(const QByteArray& key) const
146 {
147  bool found=false;
148  if (dataPtr)
149  {
150  dataPtr->lock.lockForRead();
151  found=dataPtr->values.contains(key);
152  dataPtr->lock.unlock();
153  }
154  return found;
155 }
156 
157 QMap<QByteArray,QVariant> HttpSession::getAll() const
158 {
159  QMap<QByteArray,QVariant> values;
160  if (dataPtr)
161  {
162  dataPtr->lock.lockForRead();
163  values=dataPtr->values;
164  dataPtr->lock.unlock();
165  }
166  return values;
167 }
168 
170 {
171  qint64 value=0;
172  if (dataPtr)
173  {
174  dataPtr->lock.lockForRead();
175  value=dataPtr->lastAccess;
176  dataPtr->lock.unlock();
177  }
178  return value;
179 }
180 
181 
183 {
184  if (dataPtr)
185  {
186  dataPtr->lock.lockForRead();
187  dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
188  dataPtr->lock.unlock();
189  }
190 }
void remove(const QByteArray &key)
qint64 getLastAccess() const
void set(const QByteArray &key, const QVariant &value)
QMap< QByteArray, QVariant > getAll() const
QMap< QByteArray, QVariant > values
Definition: httpsession.h:113
HttpSession(bool canStore=false)
Definition: httpsession.cpp:12
QVariant get(const QByteArray &key) const
HttpSession & operator=(const HttpSession &other)
Definition: httpsession.cpp:44
QByteArray getId() const
Definition: httpsession.cpp:97
HttpSessionData * dataPtr
Definition: httpsession.h:118
bool contains(const QByteArray &key) const