ICE 3.4.2
[php5-ice-freebsdport.git] / cpp / src / IceGrid / NodeSessionManager.h
blob7c1a7cb9080165935d184546cb07d760c8536d18
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 #ifndef ICE_GRID_NODE_SESSION_MANAGER_H
11 #define ICE_GRID_NODE_SESSION_MANAGER_H
13 #include <IceUtil/Handle.h>
14 #include <IceUtil/Mutex.h>
15 #include <IceUtil/Monitor.h>
17 #include <IceGrid/SessionManager.h>
18 #include <IceGrid/Query.h>
19 #include <IceGrid/Internal.h>
20 #include <set>
22 namespace IceGrid
25 class NodeI;
26 typedef IceUtil::Handle<NodeI> NodeIPtr;
28 class NodeSessionKeepAliveThread : public SessionKeepAliveThread<NodeSessionPrx>
30 public:
32 NodeSessionKeepAliveThread(const InternalRegistryPrx&, const NodeIPtr&, const std::vector<QueryPrx>&);
34 virtual NodeSessionPrx createSession(InternalRegistryPrx&, IceUtil::Time&);
35 virtual void destroySession(const NodeSessionPrx&);
36 virtual bool keepAlive(const NodeSessionPrx&);
38 std::string getName() const { return _name; }
40 protected:
42 virtual NodeSessionPrx createSessionImpl(const InternalRegistryPrx&, IceUtil::Time&);
44 const NodeIPtr _node;
45 const std::string _name;
46 const std::vector<QueryPrx> _queryObjects;
48 typedef IceUtil::Handle<NodeSessionKeepAliveThread> NodeSessionKeepAliveThreadPtr;
50 class NodeSessionManager : public IceUtil::Monitor<IceUtil::Mutex>
52 public:
54 NodeSessionManager();
56 void create(const NodeIPtr&);
57 void create(const InternalRegistryPrx&);
58 void activate();
59 bool waitForCreate();
60 void terminate();
61 void destroy();
63 void replicaInit(const InternalRegistryPrxSeq&);
64 void replicaAdded(const InternalRegistryPrx&);
65 void replicaRemoved(const InternalRegistryPrx&);
67 NodeSessionPrx getMasterNodeSession() const { return _thread->getSession(); }
69 private:
71 NodeSessionKeepAliveThreadPtr addReplicaSession(const InternalRegistryPrx&);
73 void reapReplicas();
75 void syncServers(const NodeSessionPrx&);
77 bool isDestroyed()
79 Lock sync(*this);
80 return _destroyed;
83 class Thread : public NodeSessionKeepAliveThread
85 public:
87 Thread(NodeSessionManager& manager) :
88 NodeSessionKeepAliveThread(manager._master, manager._node, manager._queryObjects),
89 _manager(manager)
93 virtual NodeSessionPrx
94 createSession(InternalRegistryPrx& master, IceUtil::Time& timeout)
96 NodeSessionPrx session = NodeSessionKeepAliveThread::createSession(master, timeout);
97 _manager.createdSession(session);
98 _manager.reapReplicas();
99 return session;
102 virtual void
103 destroySession(const NodeSessionPrx& session)
105 NodeSessionKeepAliveThread::destroySession(session);
106 _manager.reapReplicas();
109 virtual bool
110 keepAlive(const NodeSessionPrx& session)
112 bool alive = NodeSessionKeepAliveThread::keepAlive(session);
113 _manager.reapReplicas();
114 return alive;
117 private:
119 NodeSessionManager& _manager;
121 typedef IceUtil::Handle<Thread> ThreadPtr;
122 friend class Thread;
124 void createdSession(const NodeSessionPrx&);
126 const NodeIPtr _node;
127 ThreadPtr _thread;
128 std::vector<QueryPrx> _queryObjects;
129 InternalRegistryPrx _master;
130 unsigned long _serial;
131 bool _destroyed;
132 bool _activated;
134 typedef std::map<Ice::Identity, NodeSessionKeepAliveThreadPtr> NodeSessionMap;
135 NodeSessionMap _sessions;
136 std::set<Ice::Identity> _replicas;
141 #endif