2009-01-31 07:53:36 +01:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2009-01-31 07:53:36 +01:00
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
2010-02-28 22:33:06 +01:00
|
|
|
* Copyright (c) 2008-2010, Tomasz Sowa
|
2009-01-31 07:53:36 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sessioncontainer.h"
|
2009-04-21 22:50:55 +02:00
|
|
|
#include "log.h"
|
2010-10-24 01:12:47 +02:00
|
|
|
#include "misc.h"
|
2009-01-31 07:53:36 +01:00
|
|
|
|
2010-05-04 23:58:22 +02:00
|
|
|
|
|
|
|
|
2010-11-23 22:52:25 +01:00
|
|
|
SessionContainer::SessionContainer()
|
|
|
|
{
|
|
|
|
request = 0;
|
added: uptime winix function prints how many sessions there are
changed: functions for text/numbers conversions
int Toi(const std::string & str, int base = 10);
int Toi(const std::wstring & str, int base = 10);
int Toi(const char * str, int base = 10);
int Toi(const wchar_t * str, int base = 10);
long Tol(const std::string & str, int base = 10);
long Tol(const std::wstring & str, int base = 10);
long Tol(const char * str, int base = 10);
long Tol(const wchar_t * str, int base = 10);
template<class CharType>
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10);
const wchar_t * Toa(unsigned int value, int base = 10);
const wchar_t * Toa(unsigned long value, int base = 10);
const wchar_t * Toa(int value, int base = 10);
const wchar_t * Toa(long value, int base = 10);
void Toa(int value, std::string & res, int base = 10, bool clear = true);
void Toa(long value, std::string & res, int base = 10, bool clear = true);
void Toa(int value, std::wstring & res, int base = 10, bool clear = true);
void Toa(long value, std::wstring & res, int base = 10, bool clear = true);
added: HtmlTextStream class (files htmltextstream.cpp htmltextstream.h in templates)
this is a special stream for automatically escaping html tags
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@682 e52654a7-88a9-db11-a3e9-0013d4bc506e
2010-11-25 02:34:46 +01:00
|
|
|
table_size = 0;
|
2010-11-23 22:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SessionContainer::SetRequest(Request * prequest)
|
|
|
|
{
|
|
|
|
request = prequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
void SessionContainer::Clear()
|
|
|
|
{
|
2010-11-23 22:52:25 +01:00
|
|
|
Table::iterator inext, i = table.begin();
|
|
|
|
|
|
|
|
// don't use table.clear();
|
|
|
|
// because plugins session data would not be erased
|
|
|
|
while( i != table.end() )
|
|
|
|
{
|
|
|
|
inext = i;
|
|
|
|
++inext;
|
|
|
|
|
|
|
|
request->session = &(*i);
|
|
|
|
table.erase(i);
|
|
|
|
|
|
|
|
i = inext;
|
|
|
|
}
|
|
|
|
|
|
|
|
// erasing all indexes
|
2009-01-31 07:53:36 +01:00
|
|
|
index_id.clear();
|
|
|
|
index_time.clear();
|
added: uptime winix function prints how many sessions there are
changed: functions for text/numbers conversions
int Toi(const std::string & str, int base = 10);
int Toi(const std::wstring & str, int base = 10);
int Toi(const char * str, int base = 10);
int Toi(const wchar_t * str, int base = 10);
long Tol(const std::string & str, int base = 10);
long Tol(const std::wstring & str, int base = 10);
long Tol(const char * str, int base = 10);
long Tol(const wchar_t * str, int base = 10);
template<class CharType>
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10);
const wchar_t * Toa(unsigned int value, int base = 10);
const wchar_t * Toa(unsigned long value, int base = 10);
const wchar_t * Toa(int value, int base = 10);
const wchar_t * Toa(long value, int base = 10);
void Toa(int value, std::string & res, int base = 10, bool clear = true);
void Toa(long value, std::string & res, int base = 10, bool clear = true);
void Toa(int value, std::wstring & res, int base = 10, bool clear = true);
void Toa(long value, std::wstring & res, int base = 10, bool clear = true);
added: HtmlTextStream class (files htmltextstream.cpp htmltextstream.h in templates)
this is a special stream for automatically escaping html tags
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@682 e52654a7-88a9-db11-a3e9-0013d4bc506e
2010-11-25 02:34:46 +01:00
|
|
|
table_size = 0;
|
2010-11-23 22:52:25 +01:00
|
|
|
request->session = 0;
|
2009-01-31 07:53:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
void SessionContainer::SetLastContainer(LastContainer * plast_container)
|
|
|
|
{
|
|
|
|
last_container = plast_container;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-21 00:09:52 +01:00
|
|
|
SessionContainer::TableSize SessionContainer::Size()
|
|
|
|
{
|
added: uptime winix function prints how many sessions there are
changed: functions for text/numbers conversions
int Toi(const std::string & str, int base = 10);
int Toi(const std::wstring & str, int base = 10);
int Toi(const char * str, int base = 10);
int Toi(const wchar_t * str, int base = 10);
long Tol(const std::string & str, int base = 10);
long Tol(const std::wstring & str, int base = 10);
long Tol(const char * str, int base = 10);
long Tol(const wchar_t * str, int base = 10);
template<class CharType>
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10);
const wchar_t * Toa(unsigned int value, int base = 10);
const wchar_t * Toa(unsigned long value, int base = 10);
const wchar_t * Toa(int value, int base = 10);
const wchar_t * Toa(long value, int base = 10);
void Toa(int value, std::string & res, int base = 10, bool clear = true);
void Toa(long value, std::string & res, int base = 10, bool clear = true);
void Toa(int value, std::wstring & res, int base = 10, bool clear = true);
void Toa(long value, std::wstring & res, int base = 10, bool clear = true);
added: HtmlTextStream class (files htmltextstream.cpp htmltextstream.h in templates)
this is a special stream for automatically escaping html tags
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@682 e52654a7-88a9-db11-a3e9-0013d4bc506e
2010-11-25 02:34:46 +01:00
|
|
|
// don't use table.size() as it has O(n) complexity on FreeBSD
|
|
|
|
return table_size;
|
2009-11-21 00:09:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
SessionContainer::Iterator SessionContainer::Begin()
|
|
|
|
{
|
|
|
|
return table.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SessionContainer::Iterator SessionContainer::End()
|
|
|
|
{
|
|
|
|
return table.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Session & SessionContainer::Back()
|
|
|
|
{
|
|
|
|
return table.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool SessionContainer::PushBack(const Session & session)
|
|
|
|
{
|
|
|
|
if( index_id.find(session.id) != index_id.end() )
|
|
|
|
// that element already exists
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Iterator last = table.insert(table.end(), session);
|
|
|
|
index_id.insert( std::make_pair(session.id, last) );
|
|
|
|
index_time.insert( std::make_pair(session.last_time, last) );
|
added: uptime winix function prints how many sessions there are
changed: functions for text/numbers conversions
int Toi(const std::string & str, int base = 10);
int Toi(const std::wstring & str, int base = 10);
int Toi(const char * str, int base = 10);
int Toi(const wchar_t * str, int base = 10);
long Tol(const std::string & str, int base = 10);
long Tol(const std::wstring & str, int base = 10);
long Tol(const char * str, int base = 10);
long Tol(const wchar_t * str, int base = 10);
template<class CharType>
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10);
const wchar_t * Toa(unsigned int value, int base = 10);
const wchar_t * Toa(unsigned long value, int base = 10);
const wchar_t * Toa(int value, int base = 10);
const wchar_t * Toa(long value, int base = 10);
void Toa(int value, std::string & res, int base = 10, bool clear = true);
void Toa(long value, std::string & res, int base = 10, bool clear = true);
void Toa(int value, std::wstring & res, int base = 10, bool clear = true);
void Toa(long value, std::wstring & res, int base = 10, bool clear = true);
added: HtmlTextStream class (files htmltextstream.cpp htmltextstream.h in templates)
this is a special stream for automatically escaping html tags
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@682 e52654a7-88a9-db11-a3e9-0013d4bc506e
2010-11-25 02:34:46 +01:00
|
|
|
table_size += 1;
|
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
log << log3 << "SC: added session, id: " << session.id << logend;
|
added: uptime winix function prints how many sessions there are
changed: functions for text/numbers conversions
int Toi(const std::string & str, int base = 10);
int Toi(const std::wstring & str, int base = 10);
int Toi(const char * str, int base = 10);
int Toi(const wchar_t * str, int base = 10);
long Tol(const std::string & str, int base = 10);
long Tol(const std::wstring & str, int base = 10);
long Tol(const char * str, int base = 10);
long Tol(const wchar_t * str, int base = 10);
template<class CharType>
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10);
const wchar_t * Toa(unsigned int value, int base = 10);
const wchar_t * Toa(unsigned long value, int base = 10);
const wchar_t * Toa(int value, int base = 10);
const wchar_t * Toa(long value, int base = 10);
void Toa(int value, std::string & res, int base = 10, bool clear = true);
void Toa(long value, std::string & res, int base = 10, bool clear = true);
void Toa(int value, std::wstring & res, int base = 10, bool clear = true);
void Toa(long value, std::wstring & res, int base = 10, bool clear = true);
added: HtmlTextStream class (files htmltextstream.cpp htmltextstream.h in templates)
this is a special stream for automatically escaping html tags
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@682 e52654a7-88a9-db11-a3e9-0013d4bc506e
2010-11-25 02:34:46 +01:00
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SessionContainer::Iterator SessionContainer::FindById(long id)
|
|
|
|
{
|
|
|
|
IndexId::iterator i;
|
|
|
|
|
|
|
|
i = index_id.find(id);
|
|
|
|
|
|
|
|
if( i == index_id.end() )
|
|
|
|
return table.end();
|
|
|
|
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-25 23:42:24 +01:00
|
|
|
size_t SessionContainer::DelFirstByTimeInterval(time_t interval, size_t how_many_max, bool skip_remember_flag)
|
2009-01-31 07:53:36 +01:00
|
|
|
{
|
|
|
|
IndexTime::iterator i = index_time.begin();
|
|
|
|
IndexTime::iterator iold;
|
|
|
|
time_t limit = std::time(0) - interval;
|
2010-11-23 22:52:25 +01:00
|
|
|
Session * old_session = request->session;
|
2010-11-25 23:42:24 +01:00
|
|
|
size_t deleted = 0;
|
2009-01-31 07:53:36 +01:00
|
|
|
|
2010-11-25 23:42:24 +01:00
|
|
|
while( i != index_time.end() && i->second->last_time < limit && deleted < how_many_max )
|
2009-01-31 07:53:36 +01:00
|
|
|
{
|
2010-11-25 23:42:24 +01:00
|
|
|
//long id = i->second->id; // !! for logs (not used now)
|
2009-01-31 07:53:36 +01:00
|
|
|
iold = i;
|
2009-04-20 22:22:35 +02:00
|
|
|
++i; // incrementing before deleting old one
|
|
|
|
|
2009-11-21 00:09:52 +01:00
|
|
|
if( skip_remember_flag && iold->second->puser && iold->second->remember_me )
|
|
|
|
// don't delete sessions which have 'remember_me' flag (and a user is logged)
|
|
|
|
continue;
|
|
|
|
|
2009-04-20 22:22:35 +02:00
|
|
|
if( iold->second->puser )
|
2010-08-10 18:12:50 +02:00
|
|
|
last_container->UserLogout(iold->second->puser->id, iold->second->id);
|
2009-01-31 07:53:36 +01:00
|
|
|
|
|
|
|
// we're logging session.id (call this before table.erase())
|
|
|
|
DelFromIdIndex(iold->second);
|
|
|
|
|
2010-11-23 22:52:25 +01:00
|
|
|
request->session = &(*iold->second);
|
2009-01-31 07:53:36 +01:00
|
|
|
table.erase(iold->second);
|
|
|
|
index_time.erase(iold);
|
added: uptime winix function prints how many sessions there are
changed: functions for text/numbers conversions
int Toi(const std::string & str, int base = 10);
int Toi(const std::wstring & str, int base = 10);
int Toi(const char * str, int base = 10);
int Toi(const wchar_t * str, int base = 10);
long Tol(const std::string & str, int base = 10);
long Tol(const std::wstring & str, int base = 10);
long Tol(const char * str, int base = 10);
long Tol(const wchar_t * str, int base = 10);
template<class CharType>
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10);
const wchar_t * Toa(unsigned int value, int base = 10);
const wchar_t * Toa(unsigned long value, int base = 10);
const wchar_t * Toa(int value, int base = 10);
const wchar_t * Toa(long value, int base = 10);
void Toa(int value, std::string & res, int base = 10, bool clear = true);
void Toa(long value, std::string & res, int base = 10, bool clear = true);
void Toa(int value, std::wstring & res, int base = 10, bool clear = true);
void Toa(long value, std::wstring & res, int base = 10, bool clear = true);
added: HtmlTextStream class (files htmltextstream.cpp htmltextstream.h in templates)
this is a special stream for automatically escaping html tags
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@682 e52654a7-88a9-db11-a3e9-0013d4bc506e
2010-11-25 02:34:46 +01:00
|
|
|
table_size -= 1;
|
|
|
|
request->session = 0;
|
2010-11-25 23:42:24 +01:00
|
|
|
deleted += 1;
|
2009-01-31 07:53:36 +01:00
|
|
|
|
2010-11-25 23:42:24 +01:00
|
|
|
//log << log3 << "SC: deleted index_time for session id: " << id << logend;
|
|
|
|
//log << log3 << "SC: deleted session, id: " << id << logend;
|
2009-01-31 07:53:36 +01:00
|
|
|
}
|
2010-11-23 22:52:25 +01:00
|
|
|
|
|
|
|
request->session = old_session;
|
2010-11-25 23:42:24 +01:00
|
|
|
|
|
|
|
return deleted;
|
2009-01-31 07:53:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-25 23:42:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
void SessionContainer::DelFromIdIndex(SessionContainer::Iterator iter)
|
|
|
|
{
|
|
|
|
IndexId::iterator i;
|
|
|
|
|
2010-11-23 22:52:25 +01:00
|
|
|
for(i = index_id.begin() ; i!=index_id.end() ; ++i)
|
2009-01-31 07:53:36 +01:00
|
|
|
{
|
|
|
|
if( i->second == iter )
|
|
|
|
{
|
|
|
|
index_id.erase(i);
|
2010-11-25 23:42:24 +01:00
|
|
|
//log << log3 << "SC: deleted index_id for session id: " << iter->id << logend;
|
2009-01-31 07:53:36 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SessionContainer::UpdateLastTime(SessionContainer::Iterator iter, time_t new_time)
|
|
|
|
{
|
|
|
|
IndexTime::iterator i = index_time.lower_bound(iter->last_time);
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
for( ; i != index_time.end() ; ++i)
|
|
|
|
{
|
|
|
|
if( i->second == iter )
|
|
|
|
{
|
|
|
|
index_time.erase(i);
|
|
|
|
index_time.insert( std::make_pair(new_time, iter) );
|
|
|
|
|
|
|
|
iter->last_time = new_time;
|
2010-10-24 01:12:47 +02:00
|
|
|
iter->tm_last_time = Time(new_time);
|
2009-01-31 07:53:36 +01:00
|
|
|
|
2010-11-25 23:42:24 +01:00
|
|
|
//log << log3 << "SC: last time and the time index for session id: " << iter->id << " updated" << logend;
|
|
|
|
log << log3 << "SC: last time for session id: " << iter->id << " updated" << logend;
|
2009-01-31 07:53:36 +01:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( !found )
|
|
|
|
log << log1 << "SC: cannot update the time, time index not found for session id: " << iter->id << logend;
|
|
|
|
}
|