1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2009 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 //#include <stdexcept>
24 #include <boost/thread/mutex.hpp>
26 namespace metaproxy_1 {
30 //typedef unsigned long type;
33 /// create new session with new unique id
35 boost::mutex::scoped_lock scoped_lock(m_mutex);
41 /// copy session including old id
42 Session(const Session &s) : m_id(s.m_id), m_close(s.m_close) {};
44 Session& operator=(const Session &s) {
53 bool operator<(const Session &s) const {
54 return m_id < s.m_id ? true : false;
57 unsigned long id() const {
61 bool is_closed() const {
65 /// mark session closed, can not be unset
70 bool operator == (Session &ses) const {
71 return ses.m_id == m_id;
75 unsigned long int m_id;
78 /// static mutex to lock static m_id
79 static boost::mutex m_mutex;
81 /// static m_id to make sure that there is only one id counter
82 static unsigned long int m_global_id;
86 template <class T> class session_map {
88 void create(T &t, const metaproxy_1::Session &s) {
89 boost::mutex::scoped_lock lock(m_map_mutex);
90 m_map[s] = SessionItem(t);
92 void release(const metaproxy_1::Session &s) {
93 boost::mutex::scoped_lock lock(m_map_mutex);
98 T &get_session_data(const metaproxy_1::Session &s) {
99 boost::mutex::scoped_lock lock(m_map_mutex);
101 typename std::map<metaproxy_1::Session,SessionItem>::const_iterator it;
103 if (it == m_map.end())
105 boost::mutx::scoped_lock *scoped_ptr =
106 new boost::mutex::scoped_lock(it->second->m_item_mutex);
109 bool exist(const metaproxy_1::Session &s) {
110 typename std::map<metaproxy_1::Session,SessionItem>::const_iterator it;
112 return it == m_map.end() ? false : true;
118 SessionItem(T &t) : m_t(t) {};
119 SessionItem &operator =(const SessionItem &s) {
125 SessionItem(const SessionItem &s) {
129 boost::mutex m_item_mutex;
132 boost::mutex m_map_mutex;
133 std::map<metaproxy_1::Session,SessionItem>m_map;
142 * c-file-style: "Stroustrup"
143 * indent-tabs-mode: nil
145 * vim: shiftwidth=4 tabstop=8 expandtab