903d5d025bd492c8250c69d7d26d29484bae0bfe
[metaproxy-moved-to-github.git] / include / metaproxy / session.hpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2012 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
74     private:
75
76         unsigned long int m_id;
77         bool m_close;
78
79         /// static mutex to lock static m_id
80         static boost::mutex m_mutex;
81
82         /// static m_id to make sure that there is only one id counter
83         static unsigned long int m_global_id;
84
85     };
86
87     template <class T> class session_map {
88     public:
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);
92         };
93         void release(const metaproxy_1::Session &s) {
94             boost::mutex::scoped_lock lock(m_map_mutex);
95
96             m_map.erase(s);
97         };
98 #if 0
99         T &get_session_data(const metaproxy_1::Session &s) {
100             boost::mutex::scoped_lock lock(m_map_mutex);
101
102             typename std::map<metaproxy_1::Session,SessionItem>::const_iterator it;
103             it = m_map.find(s);
104             if (it == m_map.end())
105                 return 0;
106             boost::mutx::scoped_lock *scoped_ptr =
107                 new boost::mutex::scoped_lock(it->second->m_item_mutex);
108         };
109 #endif
110         bool exist(const metaproxy_1::Session &s) {
111             typename std::map<metaproxy_1::Session,SessionItem>::const_iterator it;
112             it = m_map.find(s);
113             return it == m_map.end() ? false : true;
114         }
115     private:
116         class SessionItem {
117         public:
118             SessionItem() {};
119             SessionItem(T &t) : m_t(t) {};
120             SessionItem &operator =(const SessionItem &s) {
121                 if (this != &s) {
122                     m_t = s.m_t;
123                 }
124                 return *this;
125             };
126             SessionItem(const SessionItem &s) {
127                 m_t = s.m_t;
128             };
129             T m_t;
130             boost::mutex m_item_mutex;
131         };
132     private:
133         boost::mutex m_map_mutex;
134         std::map<metaproxy_1::Session,SessionItem>m_map;
135     };
136
137 }
138
139 #endif
140 /*
141  * Local variables:
142  * c-basic-offset: 4
143  * c-file-style: "Stroustrup"
144  * indent-tabs-mode: nil
145  * End:
146  * vim: shiftwidth=4 tabstop=8 expandtab
147  */
148