f657613fac86e8dc4b9371b76d3488a6b881e37a
[metaproxy-moved-to-github.git] / src / session.hpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2008 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #ifndef SESSION_HPP
20 #define SESSION_HPP
21
22 //#include <stdexcept>
23 #include <map>
24 #include <boost/thread/mutex.hpp>
25
26 namespace metaproxy_1 {
27     
28     class Session
29     {
30         //typedef unsigned long type;
31     public:
32         
33         /// create new session with new unique id
34         Session() {
35             boost::mutex::scoped_lock scoped_lock(m_mutex);
36             ++m_global_id;
37             m_id =  m_global_id;
38             m_close = false;
39         };
40         
41         /// copy session including old id
42         Session(const Session &s) : m_id(s.m_id), m_close(s.m_close) {};
43         
44         Session& operator=(const Session &s) { 
45             if (this != &s)
46             {
47                 m_id = s.m_id;
48                 m_close = s.m_close;
49             }
50             return *this;
51         }
52
53         bool operator<(const Session &s) const {
54             return m_id < s.m_id ? true : false;
55         }
56         
57         unsigned long id() const {
58             return m_id;
59         };
60         
61         bool is_closed() const {
62             return m_close;
63         };
64         
65         /// mark session closed, can not be unset
66         void close() {
67             m_close = true;
68         };
69
70         bool operator == (Session &ses) const {
71             return ses.m_id == m_id;
72         }
73     private:
74         
75         unsigned long int m_id;
76         bool m_close;
77         
78         /// static mutex to lock static m_id
79         static boost::mutex m_mutex;
80         
81         /// static m_id to make sure that there is only one id counter
82         static unsigned long int m_global_id;
83         
84     };
85
86     template <class T> class session_map {
87     public:
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);
91         };
92         void release(const metaproxy_1::Session &s) {
93             boost::mutex::scoped_lock lock(m_map_mutex);
94
95             m_map.erase(s);
96         };
97 #if 0
98         T &get_session_data(const metaproxy_1::Session &s) {
99             boost::mutex::scoped_lock lock(m_map_mutex);
100
101             typename std::map<metaproxy_1::Session,SessionItem>::const_iterator it;
102             it = m_map.find(s);
103             if (it == m_map.end())
104                 return 0;
105             boost::mutx::scoped_lock *scoped_ptr =
106                 new boost::mutex::scoped_lock(it->second->m_item_mutex);
107         };
108 #endif
109         bool exist(const metaproxy_1::Session &s) {
110             typename std::map<metaproxy_1::Session,SessionItem>::const_iterator it;
111             it = m_map.find(s);
112             return it == m_map.end() ? false : true;
113         }
114     private:
115         class SessionItem {
116         public:
117             SessionItem() {};
118             SessionItem(T &t) : m_t(t) {};
119             SessionItem &operator =(const SessionItem &s) {
120                 if (this != &s) {
121                     m_t = s.m_t;
122                 }
123                 return *this;
124             };
125             SessionItem(const SessionItem &s) {
126                 m_t = s.m_t;
127             };
128             T m_t;
129             boost::mutex m_item_mutex;
130         };
131     private:
132         boost::mutex m_map_mutex;
133         std::map<metaproxy_1::Session,SessionItem>m_map;
134     };
135     
136 }
137
138 #endif
139 /*
140  * Local variables:
141  * c-basic-offset: 4
142  * c-file-style: "Stroustrup"
143  * indent-tabs-mode: nil
144  * End:
145  * vim: shiftwidth=4 tabstop=8 expandtab
146  */
147