Implemented FilterFrontendNet which is a network server based on
[metaproxy-moved-to-github.git] / src / session.hpp
1
2 #ifndef SESSION_HPP
3 #define SESSION_HPP
4
5 //#include <stdexcept>
6
7 #include <boost/thread/mutex.hpp>
8
9 namespace yp2 {
10     
11     class Session
12     {
13         //typedef unsigned long type;
14     public:
15         
16         /// create new session with new unique id
17         Session() {
18             boost::mutex::scoped_lock scoped_lock(m_mutex);
19             ++m_global_id;
20             m_id =  m_global_id;
21             m_close = false;
22         };
23         
24         /// copy session including old id
25         Session(const Session &s) : m_id(s.m_id), m_close(s.m_close) {};
26         
27         Session& operator=(const Session &s) {
28             if (this != &s)
29             {
30                 m_id = s.m_id;
31                 m_close = s.m_close;
32             }
33             return *this;
34         }
35         
36         unsigned long id() const {
37             return m_id;
38         };
39         
40         bool is_closed() const {
41             return m_close;
42         };
43         
44         /// mark session closed, can not be unset
45         void close() {
46             m_close = true;
47         };
48         
49     private:
50         
51         unsigned long int m_id;
52         bool m_close;
53         
54         /// static mutex to lock static m_id
55         static boost::mutex m_mutex;
56         
57         /// static m_id to make sure that there is only one id counter
58         static unsigned long int m_global_id;
59         
60     };
61     
62 }
63
64 #endif
65 /*
66  * Local variables:
67  * c-basic-offset: 4
68  * indent-tabs-mode: nil
69  * End:
70  * vim: shiftwidth=4 tabstop=8 expandtab
71  */