summaryrefslogtreecommitdiff
path: root/cpp/src/IceDiscovery/LookupI.h
blob: b97382d2c22d4a36e2d7969e1bd5d1e108d2f380 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// **********************************************************************
//
// Copyright (c) 2003-2014 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 LOOKUPI_H
#define LOOKUPI_H

#include <IceDiscovery/IceDiscovery.h>
#include <IceDiscovery/LocatorI.h>

#include <IceUtil/Timer.h>
#include <Ice/Properties.h>

namespace IceDiscovery
{

class LookupI;

class Request : public IceUtil::TimerTask
{
public:

    Request(const LookupIPtr&, int);

    virtual bool retry();

protected:

    LookupIPtr _lookup;
    int _nRetry;
};

template<class T, class CB> class RequestT : public Request
{
public:
    
    RequestT(LookupI* lookup, T id, int retryCount) : Request(lookup, retryCount), _id(id)
    {
    }

    T getId() const
    {
        return _id;
    }

    bool addCallback(CB cb)
    {
        _callbacks.push_back(cb);
        return _callbacks.size() == 1;
    }

    virtual void finished(const Ice::ObjectPrx& proxy)
    {
        for(typename std::vector<CB>::const_iterator p = _callbacks.begin(); p != _callbacks.end(); ++p)
        {
            (*p)->ice_response(proxy);
        }
        _callbacks.clear();
    }

protected:

    const T _id;
    std::vector<CB> _callbacks;
};

class ObjectRequest : public RequestT<Ice::Identity, Ice::AMD_Locator_findObjectByIdPtr>
{
public:

    ObjectRequest(LookupI* lookup, const Ice::Identity& id, int retryCount) : 
        RequestT<Ice::Identity, Ice::AMD_Locator_findObjectByIdPtr>(lookup, id, retryCount)
    {
    }

    void response(const Ice::ObjectPrx&);

private:

    virtual void runTimerTask();
};
typedef IceUtil::Handle<ObjectRequest> ObjectRequestPtr;

class AdapterRequest : public RequestT<std::string, Ice::AMD_Locator_findAdapterByIdPtr>
{
public:

    AdapterRequest(LookupI* lookup, const std::string& adapterId, int retryCount) : 
        RequestT<std::string, Ice::AMD_Locator_findAdapterByIdPtr>(lookup, adapterId, retryCount),
        _start(IceUtil::Time::now())
    {
    }

    bool response(const Ice::ObjectPrx&, bool);

    virtual bool retry();
    virtual void finished(const Ice::ObjectPrx&);

private:

    virtual void runTimerTask();
    std::vector<Ice::ObjectPrx> _proxies;
    IceUtil::Time _start;
    IceUtil::Time _latency;
};
typedef IceUtil::Handle<AdapterRequest> AdapterRequestPtr;

class LookupI : public Lookup, private IceUtil::Mutex
{
public:

    LookupI(const LocatorRegistryIPtr&, const LookupPrx&, const Ice::PropertiesPtr&);
    virtual ~LookupI();

    void destroy();

    void setLookupReply(const LookupReplyPrx&);

    virtual void findObjectById(const std::string&, const Ice::Identity&, const IceDiscovery::LookupReplyPrx&, 
                                const Ice::Current&);
    virtual void findAdapterById(const std::string&, const std::string&, const IceDiscovery::LookupReplyPrx&, 
                                 const Ice::Current&);

    void findObject(const Ice::AMD_Locator_findObjectByIdPtr&, const Ice::Identity&);
    void findAdapter(const Ice::AMD_Locator_findAdapterByIdPtr&, const std::string&);

    void foundObject(const Ice::Identity&, const Ice::ObjectPrx&);
    void foundAdapter(const std::string&, const Ice::ObjectPrx&, bool);

    void adapterRequestTimedOut(const AdapterRequestPtr&);
    void objectRequestTimedOut(const ObjectRequestPtr&);

    const IceUtil::TimerPtr&
    timer()
    {
        return _timer;
    }

    int
    latencyMultiplier()
    {
        return _latencyMultiplier;
    }

private:

    LocatorRegistryIPtr _registry;
    const LookupPrx _lookup;
    LookupReplyPrx _lookupReply;
    const IceUtil::Time _timeout;
    const int _retryCount;
    const int _latencyMultiplier;
    const std::string _domainId;

    IceUtil::TimerPtr _timer;
    Ice::ObjectPrx _wellKnownProxy;

    std::map<Ice::Identity, ObjectRequestPtr> _objectRequests;
    std::map<std::string, AdapterRequestPtr> _adapterRequests;
};

class LookupReplyI : public LookupReply
{
public:

    LookupReplyI(const LookupIPtr&);

    virtual void foundObjectById(const Ice::Identity&, const Ice::ObjectPrx&, const Ice::Current&);
    virtual void foundAdapterById(const std::string&, const Ice::ObjectPrx&, bool, const Ice::Current&);

private:

    const LookupIPtr _lookup;
};

};

#endif