Go to the documentation of this file.00001
00006 #include "httpsession.h"
00007 #include <QDateTime>
00008 #include <QUuid>
00009
00010
00011 HttpSession::HttpSession(bool canStore) {
00012 if (canStore) {
00013 dataPtr=new HttpSessionData();
00014 dataPtr->refCount=1;
00015 dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
00016 dataPtr->id=QUuid::createUuid().toString().toAscii();
00017 #ifdef SUPERVERBOSE
00018 qDebug("HttpSession: created new session data with id %s",dataPtr->id.data());
00019 #endif
00020 }
00021 else {
00022 dataPtr=0;
00023 }
00024 }
00025
00026 HttpSession::HttpSession(const HttpSession& other) {
00027 dataPtr=other.dataPtr;
00028 if (dataPtr) {
00029 dataPtr->lock.lockForWrite();
00030 dataPtr->refCount++;
00031 #ifdef SUPERVERBOSE
00032 qDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
00033 #endif
00034 dataPtr->lock.unlock();
00035 }
00036 }
00037
00038 HttpSession& HttpSession::operator= (const HttpSession& other) {
00039 HttpSessionData* oldPtr=dataPtr;
00040 dataPtr=other.dataPtr;
00041 if (dataPtr) {
00042 dataPtr->lock.lockForWrite();
00043 dataPtr->refCount++;
00044 #ifdef SUPERVERBOSE
00045 qDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
00046 #endif
00047 dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
00048 dataPtr->lock.unlock();
00049 }
00050 if (oldPtr) {
00051 int refCount;
00052 oldPtr->lock.lockForRead();
00053 refCount=oldPtr->refCount--;
00054 #ifdef SUPERVERBOSE
00055 qDebug("HttpSession: refCount of %s is %i",oldPtr->id.data(),oldPtr->refCount);
00056 #endif
00057 oldPtr->lock.unlock();
00058 if (refCount==0) {
00059 delete oldPtr;
00060 }
00061 }
00062 return *this;
00063 }
00064
00065 HttpSession::~HttpSession() {
00066 if (dataPtr) {
00067 int refCount;
00068 dataPtr->lock.lockForRead();
00069 refCount=--dataPtr->refCount;
00070 #ifdef SUPERVERBOSE
00071 qDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
00072 #endif
00073 dataPtr->lock.unlock();
00074 if (refCount==0) {
00075 qDebug("HttpSession: deleting data");
00076 delete dataPtr;
00077 }
00078 }
00079 }
00080
00081
00082 QByteArray HttpSession::getId() const {
00083 if (dataPtr) {
00084 return dataPtr->id;
00085 }
00086 else {
00087 return QByteArray();
00088 }
00089 }
00090
00091 bool HttpSession::isNull() const {
00092 return dataPtr==0;
00093 }
00094
00095 void HttpSession::set(const QByteArray& key, const QVariant& value) {
00096 if (dataPtr) {
00097 dataPtr->lock.lockForWrite();
00098 dataPtr->values.insert(key,value);
00099 dataPtr->lock.unlock();
00100 }
00101 }
00102
00103 void HttpSession::remove(const QByteArray& key) {
00104 if (dataPtr) {
00105 dataPtr->lock.lockForWrite();
00106 dataPtr->values.remove(key);
00107 dataPtr->lock.unlock();
00108 }
00109 }
00110
00111 QVariant HttpSession::get(const QByteArray& key) const {
00112 QVariant value;
00113 if (dataPtr) {
00114 dataPtr->lock.lockForRead();
00115 value=dataPtr->values.value(key);
00116 dataPtr->lock.unlock();
00117 }
00118 return value;
00119 }
00120
00121 bool HttpSession::contains(const QByteArray& key) const {
00122 bool found=false;
00123 if (dataPtr) {
00124 dataPtr->lock.lockForRead();
00125 found=dataPtr->values.contains(key);
00126 dataPtr->lock.unlock();
00127 }
00128 return found;
00129 }
00130
00131 QMap<QByteArray,QVariant> HttpSession::getAll() const {
00132 QMap<QByteArray,QVariant> values;
00133 if (dataPtr) {
00134 dataPtr->lock.lockForRead();
00135 values=dataPtr->values;
00136 dataPtr->lock.unlock();
00137 }
00138 return values;
00139 }
00140
00141 qint64 HttpSession::getLastAccess() const {
00142 qint64 value=0;
00143 if (dataPtr) {
00144 dataPtr->lock.lockForRead();
00145 value=dataPtr->lastAccess;
00146 dataPtr->lock.unlock();
00147 }
00148 return value;
00149 }
00150
00151
00152 void HttpSession::setLastAccess() {
00153 if (dataPtr) {
00154 dataPtr->lock.lockForRead();
00155 dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
00156 dataPtr->lock.unlock();
00157 }
00158 }