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.
Classes | Public Member Functions | Private Attributes | List of all members
qtwebapp::HttpSession Class Reference

#include <httpsession.h>

+ Collaboration diagram for qtwebapp::HttpSession:

Classes

struct  HttpSessionData
 

Public Member Functions

 HttpSession (bool canStore=false)
 
 HttpSession (const HttpSession &other)
 
HttpSessionoperator= (const HttpSession &other)
 
virtual ~HttpSession ()
 
QByteArray getId () const
 
bool isNull () const
 
void set (const QByteArray &key, const QVariant &value)
 
void remove (const QByteArray &key)
 
QVariant get (const QByteArray &key) const
 
bool contains (const QByteArray &key) const
 
QMap< QByteArray, QVariant > getAll () const
 
qint64 getLastAccess () const
 
void setLastAccess ()
 

Private Attributes

HttpSessionDatadataPtr
 

Detailed Description

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 also
HttpSessionStore should be used to create and get instances of this class.

Definition at line 25 of file httpsession.h.

Constructor & Destructor Documentation

◆ HttpSession() [1/2]

HttpSession::HttpSession ( bool  canStore = false)

Constructor.

Parameters
canStoreThe session can store data, if this parameter is true. Otherwise all calls to set() and remove() do not have any effect.

Definition at line 12 of file httpsession.cpp.

References dataPtr, qtwebapp::HttpSession::HttpSessionData::id, qtwebapp::HttpSession::HttpSessionData::lastAccess, and qtwebapp::HttpSession::HttpSessionData::refCount.

13 {
14  if (canStore)
15  {
16  dataPtr=new HttpSessionData();
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 }
HttpSessionData * dataPtr
Definition: httpsession.h:118

◆ HttpSession() [2/2]

HttpSession::HttpSession ( const HttpSession other)

Copy constructor. Creates another HttpSession object that shares the data of the other object.

Definition at line 30 of file httpsession.cpp.

References dataPtr, qtwebapp::HttpSession::HttpSessionData::id, qtwebapp::HttpSession::HttpSessionData::lock, and qtwebapp::HttpSession::HttpSessionData::refCount.

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 }
HttpSessionData * dataPtr
Definition: httpsession.h:118

◆ ~HttpSession()

HttpSession::~HttpSession ( )
virtual

Destructor. Detaches from the shared data.

Definition at line 78 of file httpsession.cpp.

References dataPtr, qtwebapp::HttpSession::HttpSessionData::id, qtwebapp::HttpSession::HttpSessionData::lock, and qtwebapp::HttpSession::HttpSessionData::refCount.

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 }
HttpSessionData * dataPtr
Definition: httpsession.h:118

Member Function Documentation

◆ contains()

bool HttpSession::contains ( const QByteArray &  key) const

Check if a key exists. This method is thread safe.

Definition at line 145 of file httpsession.cpp.

References dataPtr, qtwebapp::HttpSession::HttpSessionData::lock, and qtwebapp::HttpSession::HttpSessionData::values.

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 }
QMap< QByteArray, QVariant > values
Definition: httpsession.h:113
HttpSessionData * dataPtr
Definition: httpsession.h:118

◆ get()

QVariant HttpSession::get ( const QByteArray &  key) const

Get a value. This method is thread safe.

Definition at line 133 of file httpsession.cpp.

References dataPtr, qtwebapp::HttpSession::HttpSessionData::lock, and qtwebapp::HttpSession::HttpSessionData::values.

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 }
QMap< QByteArray, QVariant > values
Definition: httpsession.h:113
HttpSessionData * dataPtr
Definition: httpsession.h:118

◆ getAll()

QMap< QByteArray, QVariant > HttpSession::getAll ( ) 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.

Definition at line 157 of file httpsession.cpp.

References dataPtr, qtwebapp::HttpSession::HttpSessionData::lock, and qtwebapp::HttpSession::HttpSessionData::values.

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 }
QMap< QByteArray, QVariant > values
Definition: httpsession.h:113
HttpSessionData * dataPtr
Definition: httpsession.h:118

◆ getId()

QByteArray HttpSession::getId ( ) const

Get the unique ID of this session. This method is thread safe.

Definition at line 97 of file httpsession.cpp.

References dataPtr, and qtwebapp::HttpSession::HttpSessionData::id.

Referenced by qtwebapp::HttpSessionStore::getSession(), qtwebapp::HttpSessionStore::removeSession(), and qtwebapp::HttpSessionStore::sessionTimerEvent().

98 {
99  if (dataPtr)
100  {
101  return dataPtr->id;
102  }
103  else
104  {
105  return QByteArray();
106  }
107 }
HttpSessionData * dataPtr
Definition: httpsession.h:118
+ Here is the caller graph for this function:

◆ getLastAccess()

qint64 HttpSession::getLastAccess ( ) const

Get the timestamp of last access. That is the time when the last HttpSessionStore::getSession() has been called. This method is thread safe.

Definition at line 169 of file httpsession.cpp.

References dataPtr, qtwebapp::HttpSession::HttpSessionData::lastAccess, and qtwebapp::HttpSession::HttpSessionData::lock.

Referenced by qtwebapp::HttpSessionStore::sessionTimerEvent().

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 }
HttpSessionData * dataPtr
Definition: httpsession.h:118
+ Here is the caller graph for this function:

◆ isNull()

bool HttpSession::isNull ( ) const

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

Definition at line 109 of file httpsession.cpp.

References dataPtr.

Referenced by qtwebapp::HttpSessionStore::getSession().

109  {
110  return dataPtr==0;
111 }
HttpSessionData * dataPtr
Definition: httpsession.h:118
+ Here is the caller graph for this function:

◆ operator=()

HttpSession & HttpSession::operator= ( const HttpSession other)

Copy operator. Detaches from the current shared data and attaches to the data of the other object.

Definition at line 44 of file httpsession.cpp.

References dataPtr, qtwebapp::HttpSession::HttpSessionData::id, qtwebapp::HttpSession::HttpSessionData::lastAccess, qtwebapp::HttpSession::HttpSessionData::lock, and qtwebapp::HttpSession::HttpSessionData::refCount.

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 }
HttpSessionData * dataPtr
Definition: httpsession.h:118

◆ remove()

void HttpSession::remove ( const QByteArray &  key)

Remove a value. This method is thread safe.

Definition at line 123 of file httpsession.cpp.

References dataPtr, qtwebapp::HttpSession::HttpSessionData::lock, and qtwebapp::HttpSession::HttpSessionData::values.

124 {
125  if (dataPtr)
126  {
127  dataPtr->lock.lockForWrite();
128  dataPtr->values.remove(key);
129  dataPtr->lock.unlock();
130  }
131 }
QMap< QByteArray, QVariant > values
Definition: httpsession.h:113
HttpSessionData * dataPtr
Definition: httpsession.h:118

◆ set()

void HttpSession::set ( const QByteArray &  key,
const QVariant &  value 
)

Set a value. This method is thread safe.

Definition at line 113 of file httpsession.cpp.

References dataPtr, qtwebapp::HttpSession::HttpSessionData::lock, and qtwebapp::HttpSession::HttpSessionData::values.

114 {
115  if (dataPtr)
116  {
117  dataPtr->lock.lockForWrite();
118  dataPtr->values.insert(key,value);
119  dataPtr->lock.unlock();
120  }
121 }
QMap< QByteArray, QVariant > values
Definition: httpsession.h:113
HttpSessionData * dataPtr
Definition: httpsession.h:118

◆ setLastAccess()

void HttpSession::setLastAccess ( )

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

Definition at line 182 of file httpsession.cpp.

References dataPtr, qtwebapp::HttpSession::HttpSessionData::lastAccess, and qtwebapp::HttpSession::HttpSessionData::lock.

Referenced by qtwebapp::HttpSessionStore::getSession().

183 {
184  if (dataPtr)
185  {
186  dataPtr->lock.lockForRead();
187  dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
188  dataPtr->lock.unlock();
189  }
190 }
HttpSessionData * dataPtr
Definition: httpsession.h:118
+ Here is the caller graph for this function:

Member Data Documentation

◆ dataPtr

HttpSessionData* qtwebapp::HttpSession::dataPtr
private

Pointer to the shared data.

Definition at line 118 of file httpsession.h.

Referenced by contains(), get(), getAll(), getId(), getLastAccess(), HttpSession(), isNull(), operator=(), remove(), set(), setLastAccess(), and ~HttpSession().


The documentation for this class was generated from the following files: