1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 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;
76 unsigned long int m_id;
79 /// static mutex to lock static m_id
80 static boost::mutex m_mutex;
82 /// static m_id to make sure that there is only one id counter
83 static unsigned long int m_global_id;
87 template <class T> class session_map {
89 void create(T &t, const metaproxy_1::Session &s) {
90 boost::mutex::scoped_lock lock(m_map_mutex);
91 m_map[s] = SessionItem(t);
93 void release(const metaproxy_1::Session &s) {
94 boost::mutex::scoped_lock lock(m_map_mutex);
99 T &get_session_data(const metaproxy_1::Session &s) {
100 boost::mutex::scoped_lock lock(m_map_mutex);
102 typename std::map<metaproxy_1::Session,SessionItem>::const_iterator it;
104 if (it == m_map.end())
106 boost::mutx::scoped_lock *scoped_ptr =
107 new boost::mutex::scoped_lock(it->second->m_item_mutex);
110 bool exist(const metaproxy_1::Session &s) {
111 typename std::map<metaproxy_1::Session,SessionItem>::const_iterator it;
113 return it == m_map.end() ? false : true;
119 SessionItem(T &t) : m_t(t) {};
120 SessionItem &operator =(const SessionItem &s) {
126 SessionItem(const SessionItem &s) {
130 boost::mutex m_item_mutex;
133 boost::mutex m_map_mutex;
134 std::map<metaproxy_1::Session,SessionItem>m_map;
143 * c-file-style: "Stroustrup"
144 * indent-tabs-mode: nil
146 * vim: shiftwidth=4 tabstop=8 expandtab