summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/NodeSessionManager.h
blob: 791c4c286bed35a0fe69a0668c6f8c16d5039c8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#ifndef ICE_GRID_NODE_SESSION_MANAGER_H
#define ICE_GRID_NODE_SESSION_MANAGER_H

#include <IceGrid/SessionManager.h>
#include <IceGrid/Registry.h>
#include <IceGrid/Internal.h>
#include <set>

namespace IceGrid
{

class NodeI;
class NodeSessionManager;

class NodeSessionKeepAliveThread : public SessionKeepAliveThread<NodeSessionPrx>
{
public:

    NodeSessionKeepAliveThread(const std::shared_ptr<InternalRegistryPrx>&, const std::shared_ptr<NodeI>&,
                               NodeSessionManager&);

    virtual std::shared_ptr<NodeSessionPrx> createSession(std::shared_ptr<InternalRegistryPrx>&,
                                                          std::chrono::seconds&) override;
    virtual void destroySession(const std::shared_ptr<NodeSessionPrx>&) override;
    virtual bool keepAlive(const std::shared_ptr<NodeSessionPrx>&) override;

    std::string getName() const { return "IceGrid session keepalive thread"; }

protected:

    std::shared_ptr<NodeSessionPrx> createSessionImpl(const std::shared_ptr<InternalRegistryPrx>&,
                                                      std::chrono::seconds&);

    const std::shared_ptr<NodeI> _node;
    const std::string _replicaName;
    NodeSessionManager& _manager;
};

class NodeSessionManager : public SessionManager
{
public:

    NodeSessionManager(const std::shared_ptr<Ice::Communicator>&, const std::string&);

    void create(const std::shared_ptr<NodeI>&);
    void create(const std::shared_ptr<InternalRegistryPrx>&);
    void activate();
    bool isWaitingForCreate();
    bool waitForCreate();
    void terminate();
    void destroy();

    void replicaInit(const InternalRegistryPrxSeq&);
    void replicaAdded(const std::shared_ptr<InternalRegistryPrx>&);
    void replicaRemoved(const std::shared_ptr<InternalRegistryPrx>&);

    std::shared_ptr<NodeSessionPrx> getMasterNodeSession() const { return _thread->getSession(); }
    std::vector<std::shared_ptr<IceGrid::QueryPrx>> getQueryObjects() { return findAllQueryObjects(true); }

private:

    std::shared_ptr<NodeSessionKeepAliveThread> addReplicaSession(const std::shared_ptr<InternalRegistryPrx>&);

    void reapReplicas();

    void syncServers(const std::shared_ptr<NodeSessionPrx>&);

    bool isDestroyed()
    {
        std::lock_guard<std::mutex> lock(_mutex);
        return _destroyed;
    }

    class Thread final : public NodeSessionKeepAliveThread
    {
    public:

        Thread(NodeSessionManager& manager) : NodeSessionKeepAliveThread(manager._master, manager._node, manager)
        {
        }

        std::shared_ptr<NodeSessionPrx>
        createSession(std::shared_ptr<InternalRegistryPrx>& 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<NodeSessionPrx>& session) override
        {
            NodeSessionKeepAliveThread::destroySession(session);
            _manager.reapReplicas();
        }

        bool
        keepAlive(const std::shared_ptr<NodeSessionPrx>& session) override
        {
            bool alive = NodeSessionKeepAliveThread::keepAlive(session);
            _manager.reapReplicas();
            return alive;
        }
    };
    friend class Thread;

    void createdSession(const std::shared_ptr<NodeSessionPrx>&);

    const std::shared_ptr<NodeI> _node;
    std::shared_ptr<Thread> _thread;
    bool _destroyed;
    bool _activated;

    using NodeSessionMap = std::map<Ice::Identity, std::shared_ptr<NodeSessionKeepAliveThread>>;
    NodeSessionMap _sessions;
    std::set<Ice::Identity> _replicas;
};

}

#endif