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
|
|
|
|
|
|
|
|
2009-01-31 07:53:36 +01:00
|
|
|
void SessionContainer::Clear()
|
|
|
|
{
|
|
|
|
table.clear();
|
|
|
|
index_id.clear();
|
|
|
|
index_time.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
return table.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) );
|
|
|
|
|
|
|
|
log << log3 << "SC: added session, id: " << session.id << logend;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-21 00:09:52 +01:00
|
|
|
void SessionContainer::DelFirstByTimeInterval(time_t interval, 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;
|
|
|
|
|
2009-11-21 00:09:52 +01:00
|
|
|
while( i != index_time.end() && i->second->last_time < limit )
|
2009-01-31 07:53:36 +01:00
|
|
|
{
|
|
|
|
long id = i->second->id;
|
|
|
|
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);
|
|
|
|
|
|
|
|
table.erase(iold->second);
|
|
|
|
index_time.erase(iold);
|
|
|
|
|
|
|
|
log << log3 << "SC: deleted index_time for session id: " << id << logend;
|
|
|
|
log << log3 << "SC: deleted session, id: " << id << logend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SessionContainer::DelFromIdIndex(SessionContainer::Iterator iter)
|
|
|
|
{
|
|
|
|
IndexId::iterator i;
|
|
|
|
|
|
|
|
for( i = index_id.begin() ; i!=index_id.end() ; ++i)
|
|
|
|
{
|
|
|
|
if( i->second == iter )
|
|
|
|
{
|
|
|
|
index_id.erase(i);
|
|
|
|
log << log3 << "SC: deleted index_id for session id: " << iter->id << logend;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
log << log3 << "SC: last time and the time index for session id: " << iter->id << " updated" << logend;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( !found )
|
|
|
|
log << log1 << "SC: cannot update the time, time index not found for session id: " << iter->id << logend;
|
|
|
|
}
|