summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/AdapterCache.h
blob: bb86884d963d75fc19b61af88991e4880d779670 (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) 2003-2006 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#ifndef ICE_GRID_ADAPTERCACHE_H
#define ICE_GRID_ADAPTERCACHE_H

#include <IceUtil/Mutex.h>
#include <IceUtil/Shared.h>
#include <IceGrid/Cache.h>
#include <IceGrid/Query.h>
#include <IceGrid/Internal.h>
#include <IceGrid/Allocatable.h>

namespace IceGrid
{

class AdapterCache;

class ServerEntry;
typedef IceUtil::Handle<ServerEntry> ServerEntryPtr;
typedef std::vector<ServerEntryPtr> ServerEntrySeq;

class AdapterEntry;
typedef IceUtil::Handle<AdapterEntry> AdapterEntryPtr;

class AdapterEntry : public Allocatable, public IceUtil::Mutex
{
public:
    
    AdapterEntry(Cache<std::string, AdapterEntry>&, const std::string&);

    virtual std::vector<std::pair<std::string, AdapterPrx> > getProxies(bool, int&, const SessionIPtr&) 
    {
	assert(false); 
    }
    virtual float getLeastLoadedNodeLoad(LoadSample) const { assert(false); }
    virtual std::string getApplication() const { assert(false); }
    virtual bool canRemove();
    
protected:

    AdapterCache& _cache;
    const std::string _id;
};
typedef IceUtil::Handle<AdapterEntry> AdapterEntryPtr;

class ServerAdapterEntry : public AdapterEntry
{
public:

    ServerAdapterEntry(Cache<std::string, AdapterEntry>&, const std::string&);

    virtual std::vector<std::pair<std::string, AdapterPrx> > getProxies(bool, int&, const SessionIPtr&);
    virtual float getLeastLoadedNodeLoad(LoadSample) const;
    virtual std::string getApplication() const;

    void set(const ServerEntryPtr&, const std::string&, bool);
    void destroy();

    AdapterPrx getProxy(const std::string& = std::string()) const;

    virtual void allocated(const SessionIPtr&);
    virtual void released(const SessionIPtr&);

private:
    
    ServerEntryPtr getServer() const;

    ServerEntryPtr _server;
    std::string _replicaGroupId;
};
typedef IceUtil::Handle<ServerAdapterEntry> ServerAdapterEntryPtr;

class ReplicaGroupEntry : public AdapterEntry
{
public:

    ReplicaGroupEntry(Cache<std::string, AdapterEntry>&, const std::string&);

    virtual std::vector<std::pair<std::string, AdapterPrx> > getProxies(bool, int&, const SessionIPtr&);
    virtual float getLeastLoadedNodeLoad(LoadSample) const;
    virtual std::string getApplication() const;

    void set(const std::string&, const LoadBalancingPolicyPtr&);
    void addReplica(const std::string&, const ServerAdapterEntryPtr&);
    void removeReplica(const std::string&);

    typedef std::vector<std::pair<std::string, ServerAdapterEntryPtr> > ReplicaSeq;

private:

    LoadBalancingPolicyPtr _loadBalancing;
    int _loadBalancingNReplicas;
    LoadSample _loadSample;
    std::string _application;
    ReplicaSeq _replicas;
    int _lastReplica;

    static std::pointer_to_unary_function<int, int> _rand;
};
typedef IceUtil::Handle<ReplicaGroupEntry> ReplicaGroupEntryPtr;

class AdapterCache : public CacheByString<AdapterEntry>
{
public:

    AdapterEntryPtr get(const std::string&) const;
    ServerAdapterEntryPtr getServerAdapter(const std::string&, bool = false) const;
    ReplicaGroupEntryPtr getReplicaGroup(const std::string&, bool = false) const;
    
protected:
    
    virtual AdapterEntryPtr addImpl(const std::string&, const AdapterEntryPtr&);
    virtual AdapterEntryPtr removeImpl(const std::string&);

};

};

#endif