// // Copyright (c) ZeroC, Inc. All rights reserved. // #ifndef ICE_GRID_NODE_SESSION_MANAGER_H #define ICE_GRID_NODE_SESSION_MANAGER_H #include #include #include #include namespace IceGrid { class NodeI; class NodeSessionManager; class NodeSessionKeepAliveThread : public SessionKeepAliveThread { public: NodeSessionKeepAliveThread(const std::shared_ptr&, const std::shared_ptr&, NodeSessionManager&); virtual std::shared_ptr createSession(std::shared_ptr&, std::chrono::seconds&) override; virtual void destroySession(const std::shared_ptr&) override; virtual bool keepAlive(const std::shared_ptr&) override; std::string getName() const { return "IceGrid session keepalive thread"; } protected: std::shared_ptr createSessionImpl(const std::shared_ptr&, std::chrono::seconds&); const std::shared_ptr _node; const std::string _replicaName; NodeSessionManager& _manager; }; class NodeSessionManager : public SessionManager { public: NodeSessionManager(const std::shared_ptr&, const std::string&); void create(const std::shared_ptr&); void create(const std::shared_ptr&); void activate(); bool isWaitingForCreate(); bool waitForCreate(); void terminate(); void destroy(); void replicaInit(const InternalRegistryPrxSeq&); void replicaAdded(const std::shared_ptr&); void replicaRemoved(const std::shared_ptr&); std::shared_ptr getMasterNodeSession() const { return _thread->getSession(); } std::vector> getQueryObjects() { return findAllQueryObjects(true); } private: std::shared_ptr addReplicaSession(const std::shared_ptr&); void reapReplicas(); void syncServers(const std::shared_ptr&); bool isDestroyed() { std::lock_guard lock(_mutex); return _destroyed; } class Thread final : public NodeSessionKeepAliveThread { public: Thread(NodeSessionManager& manager) : NodeSessionKeepAliveThread(manager._master, manager._node, manager) { } std::shared_ptr createSession(std::shared_ptr& master, std::chrono::seconds& timeout) override { auto session = NodeSessionKeepAliveThread::createSession(master, timeout); _manager.createdSession(session); _manager.reapReplicas(); return session; } void destroySession(const std::shared_ptr& session) override { NodeSessionKeepAliveThread::destroySession(session); _manager.reapReplicas(); } bool keepAlive(const std::shared_ptr& session) override { bool alive = NodeSessionKeepAliveThread::keepAlive(session); _manager.reapReplicas(); return alive; } }; friend class Thread; void createdSession(const std::shared_ptr&); const std::shared_ptr _node; std::shared_ptr _thread; bool _destroyed; bool _activated; using NodeSessionMap = std::map>; NodeSessionMap _sessions; std::set _replicas; }; } #endif