212 lines
3.6 KiB
C++
Executable File
212 lines
3.6 KiB
C++
Executable File
/*
|
|
* This file is a part of Winix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2008-2012, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
|
|
#include "sessioncontainer.h"
|
|
#include "log.h"
|
|
#include "misc.h"
|
|
|
|
|
|
|
|
SessionContainer::SessionContainer()
|
|
{
|
|
table_size = 0;
|
|
}
|
|
|
|
|
|
void SessionContainer::SetCur(Cur * pcur)
|
|
{
|
|
cur = pcur;
|
|
}
|
|
|
|
|
|
void SessionContainer::SetConfig(Config * pconfig)
|
|
{
|
|
config = pconfig;
|
|
}
|
|
|
|
|
|
void SessionContainer::SetTmpSession(Session * psession)
|
|
{
|
|
tmp_session = psession;
|
|
}
|
|
|
|
|
|
|
|
void SessionContainer::Clear()
|
|
{
|
|
Table::iterator i = table.begin();
|
|
|
|
log << log3 << "SC: deleting all sessions" << logend;
|
|
|
|
cur->session = tmp_session;
|
|
|
|
// don't use table.clear();
|
|
// because plugins session data would not be erased
|
|
while( i != table.end() )
|
|
{
|
|
i->plugin_data.DeleteAll(); // it's better to call it here instead in the destructor
|
|
table.erase(i++);
|
|
}
|
|
|
|
// erasing indexes
|
|
index_id.clear();
|
|
table_size = 0;
|
|
}
|
|
|
|
|
|
|
|
void SessionContainer::EraseById(long id)
|
|
{
|
|
IndexId::iterator i = index_id.find(id);
|
|
|
|
if( i != index_id.end() )
|
|
{
|
|
Session * old_session = tmp_session;
|
|
|
|
if( cur->session != &(*i->second) )
|
|
old_session = cur->session;
|
|
|
|
cur->session = &(*i->second);
|
|
|
|
log << log4 << "SC: deleting session, id: " << i->second->id << logend;
|
|
|
|
// call first DeleteAll() because if not then it would be called from the destructor
|
|
// and there'll be a problem if it throws an exception there
|
|
i->second->plugin_data.DeleteAll();
|
|
table.erase(i->second);
|
|
index_id.erase(i);
|
|
table_size -= 1;
|
|
|
|
cur->session = old_session;
|
|
}
|
|
else
|
|
{
|
|
log << log1 << "SC: I cannot delete a session with id: " << id
|
|
<< " (there is no such a session)" << logend;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
size_t SessionContainer::Size()
|
|
{
|
|
// don't use table.size() as it has O(n) complexity on FreeBSD
|
|
return table_size;
|
|
}
|
|
|
|
|
|
SessionContainer::Iterator SessionContainer::Begin()
|
|
{
|
|
return table.begin();
|
|
}
|
|
|
|
|
|
SessionContainer::Iterator SessionContainer::End()
|
|
{
|
|
return table.end();
|
|
}
|
|
|
|
|
|
Session & SessionContainer::Back()
|
|
{
|
|
return table.back();
|
|
}
|
|
|
|
|
|
SessionContainer::IdIterator SessionContainer::IdBegin()
|
|
{
|
|
return index_id.begin();
|
|
}
|
|
|
|
|
|
SessionContainer::IdIterator SessionContainer::IdEnd()
|
|
{
|
|
return index_id.end();
|
|
}
|
|
|
|
|
|
|
|
SessionContainer::Iterator SessionContainer::AddSession(long id)
|
|
{
|
|
std::pair<IndexId::iterator, bool> index_id_res = index_id.insert( std::make_pair(id, table.end()) );
|
|
|
|
if( !index_id_res.second )
|
|
{
|
|
// that element already exists (was not inserted now)
|
|
return End();
|
|
}
|
|
|
|
Iterator last = table.insert(table.end(), empty_session);
|
|
last->id = id;
|
|
index_id_res.first->second = last;
|
|
table_size += 1;
|
|
|
|
log << log3 << "SC: added session, id: " << id << logend;
|
|
|
|
return last;
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
bool SessionContainer::ChangeSessionId(SessionContainer::Iterator ses, long new_id)
|
|
{
|
|
std::pair<IndexId::iterator, bool> index_id_res = index_id.insert( std::make_pair(new_id, ses) );
|
|
|
|
if( !index_id_res.second )
|
|
{
|
|
log << log1 << "SC: session with id: " << new_id << " already exists" << logend;
|
|
return false;
|
|
}
|
|
|
|
long old_id = ses->id;
|
|
index_id.erase(old_id); // remove the old index
|
|
ses->id = new_id;
|
|
log << log3 << "SC: changed session id from: " << old_id << " to " << new_id << logend;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool SessionContainer::ChangeSessionId(long old_id, long new_id)
|
|
{
|
|
IndexId::iterator i = index_id.find(old_id);
|
|
|
|
if( i != index_id.end() )
|
|
{
|
|
return ChangeSessionId(i->second, new_id);
|
|
}
|
|
else
|
|
{
|
|
log << log2 << "SC: there is no a session with id: " << old_id << logend;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|