winix/core/sessioncontainer.cpp

167 lines
2.7 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;
// don't use table.clear();
// because plugins session data would not be erased
// we must set cur->session for each session and then delete it
while( i != table.end() )
{
cur->session = &(*i);
cur->session->plugin_data.DeleteAll(); // it's better to call it here instead of the destructor
table.erase(i++);
}
// erasing indexes
index_id.clear();
table_size = 0;
cur->session = tmp_session;
}
void SessionContainer::EraseById(IdIterator i)
{
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 desctructor
// 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;
}
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();
}
bool SessionContainer::PushBack(const Session & session)
{
std::pair<IndexId::iterator, bool> index_id_res = index_id.insert( std::make_pair(session.id, table.end()) );
if( !index_id_res.second )
{
// that element already exists (was not inserted now)
return false;
}
Iterator last = table.insert(table.end(), session);
index_id_res.first->second = last;
table_size += 1;
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;
}