GPL v2.
[metaproxy-moved-to-github.git] / src / session.hpp
1 /* $Id: session.hpp,v 1.15 2007-05-09 21:23:09 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #ifndef SESSION_HPP
23 #define SESSION_HPP
24
25 //#include <stdexcept>
26 #include <map>
27 #include <boost/thread/mutex.hpp>
28
29 namespace metaproxy_1 {
30     
31     class Session
32     {
33         //typedef unsigned long type;
34     public:
35         
36         /// create new session with new unique id
37         Session() {
38             boost::mutex::scoped_lock scoped_lock(m_mutex);
39             ++m_global_id;
40             m_id =  m_global_id;
41             m_close = false;
42         };
43         
44         /// copy session including old id
45         Session(const Session &s) : m_id(s.m_id), m_close(s.m_close) {};
46         
47         Session& operator=(const Session &s) { 
48             if (this != &s)
49             {
50                 m_id = s.m_id;
51                 m_close = s.m_close;
52             }
53             return *this;
54         }
55
56         bool operator<(const Session &s) const {
57             return m_id < s.m_id ? true : false;
58         }
59         
60         unsigned long id() const {
61             return m_id;
62         };
63         
64         bool is_closed() const {
65             return m_close;
66         };
67         
68         /// mark session closed, can not be unset
69         void close() {
70             m_close = true;
71         };
72
73         bool operator == (Session &ses) const {
74             return ses.m_id == m_id;
75         }
76     private:
77         
78         unsigned long int m_id;
79         bool m_close;
80         
81         /// static mutex to lock static m_id
82         static boost::mutex m_mutex;
83         
84         /// static m_id to make sure that there is only one id counter
85         static unsigned long int m_global_id;
86         
87     };
88
89     template <class T> class session_map {
90     public:
91         void create(T &t, const metaproxy_1::Session &s) { 
92             boost::mutex::scoped_lock lock(m_map_mutex);
93             m_map[s] = SessionItem(t);
94         };
95         void release(const metaproxy_1::Session &s) {
96             boost::mutex::scoped_lock lock(m_map_mutex);
97
98             m_map.erase(s);
99         };
100 #if 0
101         T &get_session_data(const metaproxy_1::Session &s) {
102             boost::mutex::scoped_lock lock(m_map_mutex);
103
104             typename std::map<metaproxy_1::Session,SessionItem>::const_iterator it;
105             it = m_map.find(s);
106             if (it == m_map.end())
107                 return 0;
108             boost::mutx::scoped_lock *scoped_ptr =
109                 new boost::mutex::scoped_lock(it->second->m_item_mutex);
110         };
111 #endif
112         bool exist(const metaproxy_1::Session &s) {
113             typename std::map<metaproxy_1::Session,SessionItem>::const_iterator it;
114             it = m_map.find(s);
115             return it == m_map.end() ? false : true;
116         }
117     private:
118         class SessionItem {
119         public:
120             SessionItem() {};
121             SessionItem(T &t) : m_t(t) {};
122             SessionItem &operator =(const SessionItem &s) {
123                 if (this != &s) {
124                     m_t = s.m_t;
125                 }
126                 return *this;
127             };
128             SessionItem(const SessionItem &s) {
129                 m_t = s.m_t;
130             };
131             T m_t;
132             boost::mutex m_item_mutex;
133         };
134     private:
135         boost::mutex m_map_mutex;
136         std::map<metaproxy_1::Session,SessionItem>m_map;
137     };
138     
139 }
140
141 #endif
142 /*
143  * Local variables:
144  * c-basic-offset: 4
145  * indent-tabs-mode: nil
146  * c-file-style: "stroustrup"
147  * End:
148  * vim: shiftwidth=4 tabstop=8 expandtab
149  */