summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/Reference.h
blob: e9b862c01df5857a4f9245bd442a0e89c5627ba5 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// **********************************************************************
//
// Copyright (c) 2003-2007 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_REFERENCE_H
#define ICE_REFERENCE_H

#include <IceUtil/Shared.h>
#include <IceUtil/RecMutex.h>
#include <Ice/ReferenceF.h>
#include <Ice/ReferenceFactoryF.h>
#include <Ice/EndpointIF.h>
#include <Ice/InstanceF.h>
#include <Ice/RouterF.h>
#include <Ice/LocatorF.h>
#include <Ice/RouterInfoF.h>
#include <Ice/LocatorInfoF.h>
#include <Ice/ConnectionIF.h>
#include <Ice/SharedContext.h>
#include <Ice/Identity.h>

namespace IceInternal
{

class BasicStream;

class Reference : public IceUtil::Shared
{
public:

    class GetConnectionCallback : virtual public IceUtil::Shared
    {
    public:
        
        virtual void setConnection(const Ice::ConnectionIPtr&, bool) = 0;
        virtual void setException(const Ice::LocalException&) = 0;
    };
    typedef IceUtil::Handle<GetConnectionCallback> GetConnectionCallbackPtr;

    enum Type
    {
        TypeDirect,
        TypeIndirect,
        TypeFixed
    };

    enum Mode
    {
        ModeTwoway,
        ModeOneway,
        ModeBatchOneway,
        ModeDatagram,
        ModeBatchDatagram,
        ModeLast = ModeBatchDatagram
    };

    Mode getMode() const { return _mode; }
    const Ice::Identity& getIdentity() const { return _identity; }
    const std::string& getFacet() const { return _facet; }
    const InstancePtr& getInstance() const { return _instance; }
    const SharedContextPtr& getContext() const { return _context; }

    ReferencePtr defaultContext() const;

    Ice::CommunicatorPtr getCommunicator() const;

    virtual RouterInfoPtr getRouterInfo() const { return 0; }
    virtual LocatorInfoPtr getLocatorInfo() const { return 0; }

    virtual Type getType() const = 0;
    virtual bool getSecure() const = 0;
    virtual bool getPreferSecure() const = 0;
    virtual std::string getAdapterId() const = 0;
    virtual std::vector<EndpointIPtr> getEndpoints() const = 0;
    virtual bool getCollocationOptimization() const = 0;
    virtual int getLocatorCacheTimeout() const = 0;
    virtual bool getCacheConnection() const = 0;
    virtual Ice::EndpointSelectionType getEndpointSelection() const = 0;
    virtual bool getThreadPerConnection() const = 0;

    //
    // The change* methods (here and in derived classes) create
    // a new reference based on the existing one, with the
    // corresponding value changed.
    //
    ReferencePtr changeContext(const Ice::Context&) const;
    ReferencePtr changeMode(Mode) const;
    ReferencePtr changeIdentity(const Ice::Identity&) const;
    ReferencePtr changeFacet(const std::string&) const;
    virtual ReferencePtr changeCompress(bool) const;

    virtual ReferencePtr changeSecure(bool) const = 0;
    virtual ReferencePtr changePreferSecure(bool) const = 0;
    virtual ReferencePtr changeRouter(const Ice::RouterPrx&) const = 0;
    virtual ReferencePtr changeLocator(const Ice::LocatorPrx&) const = 0;
    virtual ReferencePtr changeTimeout(int) const = 0;
    virtual ReferencePtr changeConnectionId(const std::string&) const = 0;
    virtual ReferencePtr changeCollocationOptimization(bool) const = 0;
    virtual ReferencePtr changeAdapterId(const std::string&) const = 0;
    virtual ReferencePtr changeEndpoints(const std::vector<EndpointIPtr>&) const = 0;
    virtual ReferencePtr changeLocatorCacheTimeout(int) const = 0;
    virtual ReferencePtr changeCacheConnection(bool) const = 0;
    virtual ReferencePtr changeEndpointSelection(Ice::EndpointSelectionType) const = 0;
    virtual ReferencePtr changeThreadPerConnection(bool) const = 0;
    
    virtual int hash() const; // Conceptually const.

    //
    // Marshal the reference.
    //
    virtual void streamWrite(BasicStream*) const;

    //
    // Convert the reference to its string form.
    //
    virtual std::string toString() const;

    //
    // Get a suitable connection for this reference.
    //
    virtual Ice::ConnectionIPtr getConnection(bool&) const = 0;
    virtual void getConnection(const GetConnectionCallbackPtr&) const = 0;

    virtual bool operator==(const Reference&) const = 0;
    virtual bool operator!=(const Reference&) const = 0;
    virtual bool operator<(const Reference&) const = 0;

    virtual ReferencePtr clone() const = 0;

protected:

    Reference(const InstancePtr&, const Ice::CommunicatorPtr&, const Ice::Identity&, const SharedContextPtr&,
              const std::string&, Mode);
    Reference(const Reference&);

    IceUtil::RecMutex _hashMutex; // For lazy initialization of hash value.
    mutable Ice::Int _hashValue;
    mutable bool _hashInitialized;

    bool _overrideCompress;
    bool _compress; // Only used if _overrideCompress == true

private:

    const InstancePtr _instance;
    const Ice::CommunicatorPtr _communicator;

    Mode _mode;
    Ice::Identity _identity;
    SharedContextPtr _context;
    std::string _facet;
};

class FixedReference : public Reference
{
public:

    FixedReference(const InstancePtr&, const Ice::CommunicatorPtr&, const Ice::Identity&, const SharedContextPtr&, 
                   const std::string&, Mode, const std::vector<Ice::ConnectionIPtr>&);

    virtual Type getType() const;
    virtual bool getSecure() const;
    virtual bool getPreferSecure() const;
    virtual std::string getAdapterId() const;
    virtual std::vector<EndpointIPtr> getEndpoints() const;
    virtual bool getCollocationOptimization() const;
    virtual int getLocatorCacheTimeout() const;
    virtual bool getCacheConnection() const;
    virtual Ice::EndpointSelectionType getEndpointSelection() const;
    virtual bool getThreadPerConnection() const;

    virtual ReferencePtr changeSecure(bool) const;
    virtual ReferencePtr changePreferSecure(bool) const;
    virtual ReferencePtr changeRouter(const Ice::RouterPrx&) const;
    virtual ReferencePtr changeLocator(const Ice::LocatorPrx&) const;
    virtual ReferencePtr changeCollocationOptimization(bool) const;
    virtual ReferencePtr changeTimeout(int) const;
    virtual ReferencePtr changeConnectionId(const std::string&) const;
    virtual ReferencePtr changeLocatorCacheTimeout(int) const;
    virtual ReferencePtr changeAdapterId(const std::string&) const;
    virtual ReferencePtr changeEndpoints(const std::vector<EndpointIPtr>&) const;
    virtual ReferencePtr changeCacheConnection(bool) const;
    virtual ReferencePtr changeEndpointSelection(Ice::EndpointSelectionType) const;
    virtual ReferencePtr changeThreadPerConnection(bool) const;

    virtual void streamWrite(BasicStream*) const;
    virtual std::string toString() const;

    virtual Ice::ConnectionIPtr getConnection(bool&) const;
    virtual void getConnection(const GetConnectionCallbackPtr&) const;

    virtual bool operator==(const Reference&) const;
    virtual bool operator!=(const Reference&) const;
    virtual bool operator<(const Reference&) const;

    virtual ReferencePtr clone() const;

protected:

    FixedReference(const FixedReference&);

    std::vector<Ice::ConnectionIPtr> filterConnections(const std::vector<Ice::ConnectionIPtr>&) const;

private:

    std::vector<Ice::ConnectionIPtr> _fixedConnections;
};

class RoutableReference : public Reference
{
public:

    virtual RouterInfoPtr getRouterInfo() const { return _routerInfo; }

    virtual bool getSecure() const;
    virtual bool getPreferSecure() const;
    virtual bool getCollocationOptimization() const;
    virtual bool getCacheConnection() const;
    virtual Ice::EndpointSelectionType getEndpointSelection() const;
    virtual bool getThreadPerConnection() const;

    virtual ReferencePtr changeSecure(bool) const;
    virtual ReferencePtr changePreferSecure(bool) const;
    virtual ReferencePtr changeRouter(const Ice::RouterPrx&) const;
    virtual ReferencePtr changeCollocationOptimization(bool) const;
    virtual ReferencePtr changeTimeout(int) const;
    virtual ReferencePtr changeConnectionId(const std::string&) const;
    virtual ReferencePtr changeCacheConnection(bool) const;
    virtual ReferencePtr changeEndpointSelection(Ice::EndpointSelectionType) const;
    virtual ReferencePtr changeThreadPerConnection(bool) const;

    virtual int hash() const;

    virtual bool operator==(const Reference&) const = 0;
    virtual bool operator!=(const Reference&) const = 0;
    virtual bool operator<(const Reference&) const = 0;

    virtual ReferencePtr clone() const = 0;

    Ice::ConnectionIPtr createConnection(const std::vector<EndpointIPtr>&, bool&) const;
    void createConnection(const std::vector<EndpointIPtr>&, const GetConnectionCallbackPtr&) const;
    void applyOverrides(std::vector<EndpointIPtr>&) const;

protected:

    RoutableReference(const InstancePtr&, const Ice::CommunicatorPtr&, const Ice::Identity&, const SharedContextPtr&,
                      const std::string&, Mode, bool, bool, const RouterInfoPtr&, bool, bool,
                      Ice::EndpointSelectionType, bool);
    RoutableReference(const RoutableReference&);

    std::vector<EndpointIPtr> filterEndpoints(const std::vector<EndpointIPtr>&) const;

private:

    bool _secure;
    bool _preferSecure;
    RouterInfoPtr _routerInfo; // Null if no router is used.
    bool _collocationOptimization;
    bool _cacheConnection;
    Ice::EndpointSelectionType _endpointSelection;

    std::string _connectionId;
    bool _overrideTimeout;
    int _timeout; // Only used if _overrideTimeout == true
    bool _threadPerConnection;
};

class DirectReference : public RoutableReference
{
public:

    DirectReference(const InstancePtr&, const Ice::CommunicatorPtr&, const Ice::Identity&, const SharedContextPtr&,
                    const std::string&, Mode, bool, bool, const std::vector<EndpointIPtr>&, const RouterInfoPtr&, bool,
                    bool, Ice::EndpointSelectionType, bool);

    virtual Type getType() const;
    virtual int getLocatorCacheTimeout() const;
    virtual std::string getAdapterId() const;
    virtual std::vector<EndpointIPtr> getEndpoints() const;

    virtual ReferencePtr changeLocator(const Ice::LocatorPrx&) const;
    virtual ReferencePtr changeCompress(bool) const;
    virtual ReferencePtr changeTimeout(int) const;
    virtual ReferencePtr changeConnectionId(const std::string&) const;
    virtual ReferencePtr changeLocatorCacheTimeout(int) const;
    virtual ReferencePtr changeAdapterId(const std::string&) const;
    virtual ReferencePtr changeEndpoints(const std::vector<EndpointIPtr>&) const;

    virtual void streamWrite(BasicStream*) const;
    virtual std::string toString() const;
    virtual Ice::ConnectionIPtr getConnection(bool&) const;
    virtual void getConnection(const GetConnectionCallbackPtr&) const;

    virtual bool operator==(const Reference&) const;
    virtual bool operator!=(const Reference&) const;
    virtual bool operator<(const Reference&) const;

    virtual ReferencePtr clone() const;

protected:

    DirectReference(const DirectReference&);

private:

    std::vector<EndpointIPtr> _endpoints;
};

class IndirectReference : public RoutableReference
{
public:

    IndirectReference(const InstancePtr&, const Ice::CommunicatorPtr&, const Ice::Identity&, const SharedContextPtr&, 
                      const std::string&, Mode, bool, bool, const std::string&, const RouterInfoPtr&,
                      const LocatorInfoPtr&, bool, bool, Ice::EndpointSelectionType, bool, int);

    virtual LocatorInfoPtr getLocatorInfo() const { return _locatorInfo; }

    virtual Type getType() const;
    virtual int getLocatorCacheTimeout() const;
    virtual std::string getAdapterId() const;
    virtual std::vector<EndpointIPtr> getEndpoints() const;

    virtual ReferencePtr changeLocator(const Ice::LocatorPrx&) const;
    virtual ReferencePtr changeLocatorCacheTimeout(int) const;
    virtual ReferencePtr changeAdapterId(const std::string&) const;
    virtual ReferencePtr changeEndpoints(const std::vector<EndpointIPtr>&) const;

    virtual void streamWrite(BasicStream*) const;
    virtual std::string toString() const;
    virtual Ice::ConnectionIPtr getConnection(bool&) const;
    virtual void getConnection(const GetConnectionCallbackPtr&) const;
    virtual void getConnectionNoRouterInfo(const GetConnectionCallbackPtr&) const;

    virtual int hash() const; // Conceptually const.

    virtual bool operator==(const Reference&) const;
    virtual bool operator!=(const Reference&) const;
    virtual bool operator<(const Reference&) const;

    virtual ReferencePtr clone() const;

protected:

    IndirectReference(const IndirectReference&);

private:

    std::string _adapterId;
    LocatorInfoPtr _locatorInfo;
    int _locatorCacheTimeout;
};

}

#endif