summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/AllocatableObjectCache.h
blob: bdae1d6037a95b6352c80d63be7c88b59dbe9034 (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
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#ifndef ICE_GRID_ALLOCATABLEOBJECTCACHE_H
#define ICE_GRID_ALLOCATABLEOBJECTCACHE_H

#include <Ice/CommunicatorF.h>
#include <IceGrid/Cache.h>
#include <IceGrid/Admin.h>
#include <IceGrid/Allocatable.h>

namespace IceGrid
{

class AllocatableObjectCache;
class ServerEntry;

class AllocatableObjectEntry : public Allocatable
{
public:

    AllocatableObjectEntry(AllocatableObjectCache&, const ObjectInfo&, const std::shared_ptr<ServerEntry>&);
    std::shared_ptr<Ice::ObjectPrx> getProxy() const;
    std::string getType() const;

    bool canRemove();

    bool isEnabled() const override;
    void allocated(const std::shared_ptr<SessionI>&) override;
    void released(const std::shared_ptr<SessionI>&) override;
    bool canTryAllocate() override;

    void  destroy();
    void checkAllocatable() override;

private:

    AllocatableObjectCache& _cache;
    const ObjectInfo _info;
    std::shared_ptr<ServerEntry> _server;
    bool _destroyed;
};

class ObjectAllocationRequest : public AllocationRequest
{
public:

    ObjectAllocationRequest(const std::shared_ptr<SessionI>& session) : AllocationRequest(session)
    {
    }

    virtual void response(const std::shared_ptr<Ice::ObjectPrx>&) = 0;
    virtual void exception(std::exception_ptr) = 0;

private:

    void allocated(const std::shared_ptr<Allocatable>& allocatable, const std::shared_ptr<SessionI>&) override
    {
        response(std::dynamic_pointer_cast<AllocatableObjectEntry>(allocatable)->getProxy());
    }

    void canceled(std::exception_ptr ex) override
    {
        exception(ex);
    }
};

class AllocatableObjectCache : public Cache<Ice::Identity, AllocatableObjectEntry>
{
public:

    AllocatableObjectCache(const std::shared_ptr<Ice::Communicator>&);

    void add(const ObjectInfo&, const std::shared_ptr<ServerEntry>&);
    std::shared_ptr<AllocatableObjectEntry> get(const Ice::Identity&) const;
    void remove(const Ice::Identity&);

    void allocateByType(const std::string&, const std::shared_ptr<ObjectAllocationRequest>&);
    bool canTryAllocate(const std::shared_ptr<AllocatableObjectEntry>&);

    const std::shared_ptr<Ice::Communicator>& getCommunicator() const { return _communicator; }

private:

    class TypeEntry
    {
    public:

        void add(const std::shared_ptr<AllocatableObjectEntry>&);
        bool remove(const std::shared_ptr<AllocatableObjectEntry>&);

        void addAllocationRequest(const std::shared_ptr<ObjectAllocationRequest>&);
        bool canTryAllocate(const std::shared_ptr<AllocatableObjectEntry>&, bool);

        const std::vector<std::shared_ptr<AllocatableObjectEntry>>& getObjects() const { return _objects; }

    private:

        std::vector<std::shared_ptr<AllocatableObjectEntry>> _objects;
        std::list<std::shared_ptr<ObjectAllocationRequest>> _requests;
    };

    const std::shared_ptr<Ice::Communicator> _communicator;
    std::map<std::string, TypeEntry> _types;
    std::map<std::string, std::vector<Ice::Identity> > _allocatablesByType;
};

};

#endif