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