1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_LOCATOR_INFO_H
6 #define ICE_LOCATOR_INFO_H
7 
8 #include <IceUtil/Shared.h>
9 #include <IceUtil/Mutex.h>
10 #include <IceUtil/Monitor.h>
11 #include <IceUtil/Time.h>
12 #include <Ice/LocatorInfoF.h>
13 #include <Ice/LocatorF.h>
14 #include <Ice/ReferenceF.h>
15 #include <Ice/Identity.h>
16 #include <Ice/EndpointIF.h>
17 #include <Ice/PropertiesF.h>
18 #include <Ice/Version.h>
19 
20 #include <Ice/UniquePtr.h>
21 
22 namespace IceInternal
23 {
24 
25 class LocatorManager : public IceUtil::Shared, public IceUtil::Mutex
26 {
27 public:
28 
29     LocatorManager(const Ice::PropertiesPtr&);
30 
31     void destroy();
32 
33     //
34     // Returns locator info for a given locator. Automatically creates
35     // the locator info if it doesn't exist yet.
36     //
37     LocatorInfoPtr get(const Ice::LocatorPrxPtr&);
38 
39 private:
40 
41     const bool _background;
42 
43 #ifdef ICE_CPP11_MAPPING
44     using LocatorInfoTable = std::map<std::shared_ptr<Ice::LocatorPrx>,
45                                       LocatorInfoPtr,
46                                       Ice::TargetCompare<std::shared_ptr<Ice::LocatorPrx>, std::less>>;
47 #else
48     typedef std::map<Ice::LocatorPrx, LocatorInfoPtr> LocatorInfoTable;
49 #endif
50     LocatorInfoTable _table;
51     LocatorInfoTable::iterator _tableHint;
52 
53     std::map<std::pair<Ice::Identity, Ice::EncodingVersion>, LocatorTablePtr> _locatorTables;
54 };
55 
56 class LocatorTable : public IceUtil::Shared, public IceUtil::Mutex
57 {
58 public:
59 
60     LocatorTable();
61 
62     void clear();
63 
64     bool getAdapterEndpoints(const std::string&, int, ::std::vector<EndpointIPtr>&);
65     void addAdapterEndpoints(const std::string&, const ::std::vector<EndpointIPtr>&);
66     ::std::vector<EndpointIPtr> removeAdapterEndpoints(const std::string&);
67 
68     bool getObjectReference(const Ice::Identity&, int, ReferencePtr&);
69     void addObjectReference(const Ice::Identity&, const ReferencePtr&);
70     ReferencePtr removeObjectReference(const Ice::Identity&);
71 
72 private:
73 
74     bool checkTTL(const IceUtil::Time&, int) const;
75 
76     std::map<std::string, std::pair<IceUtil::Time, std::vector<EndpointIPtr> > > _adapterEndpointsMap;
77     std::map<Ice::Identity, std::pair<IceUtil::Time, ReferencePtr> > _objectMap;
78 };
79 
80 class LocatorInfo : public IceUtil::Shared, public IceUtil::Mutex
81 {
82 public:
83 
84     class GetEndpointsCallback : public virtual IceUtil::Shared
85     {
86     public:
87 
88         virtual void setEndpoints(const std::vector<EndpointIPtr>&, bool) = 0;
89         virtual void setException(const Ice::LocalException&) = 0;
90     };
91     typedef IceUtil::Handle<GetEndpointsCallback> GetEndpointsCallbackPtr;
92 
93     class RequestCallback : public virtual IceUtil::Shared
94     {
95     public:
96 
97         RequestCallback(const ReferencePtr&, int, const GetEndpointsCallbackPtr&);
98 
99         void response(const LocatorInfoPtr&, const Ice::ObjectPrxPtr&);
100         void exception(const LocatorInfoPtr&, const Ice::Exception&);
101 
102     private:
103 
104         const ReferencePtr _reference;
105         const int _ttl;
106         const GetEndpointsCallbackPtr _callback;
107     };
108     typedef IceUtil::Handle<RequestCallback> RequestCallbackPtr;
109 
110     class Request : public virtual IceUtil::Shared
111     {
112     public:
113 
114         void addCallback(const ReferencePtr&, const ReferencePtr&, int, const GetEndpointsCallbackPtr&);
115 
116         void response(const Ice::ObjectPrxPtr&);
117         void exception(const Ice::Exception&);
118 
119     protected:
120 
121         Request(const LocatorInfoPtr&, const ReferencePtr&);
122 
123         virtual void send() = 0;
124 
125         const LocatorInfoPtr _locatorInfo;
126         const ReferencePtr _reference;
127 
128     private:
129 
130         IceUtil::Monitor<IceUtil::Mutex> _monitor;
131         std::vector<RequestCallbackPtr> _callbacks;
132         std::vector<ReferencePtr> _wellKnownRefs;
133         bool _sent;
134         bool _response;
135         Ice::ObjectPrxPtr _proxy;
136         IceInternal::UniquePtr<Ice::Exception> _exception;
137     };
138     typedef IceUtil::Handle<Request> RequestPtr;
139 
140     LocatorInfo(const Ice::LocatorPrxPtr&, const LocatorTablePtr&, bool);
141 
142     void destroy();
143 
144     bool operator==(const LocatorInfo&) const;
145     bool operator<(const LocatorInfo&) const;
146 
getLocator()147     const Ice::LocatorPrxPtr& getLocator() const
148     {
149         //
150         // No mutex lock necessary, _locator is immutable.
151         //
152         return _locator;
153     }
154     Ice::LocatorRegistryPrxPtr getLocatorRegistry();
155 
getEndpoints(const ReferencePtr & ref,int ttl,const GetEndpointsCallbackPtr & cb)156     void getEndpoints(const ReferencePtr& ref, int ttl, const GetEndpointsCallbackPtr& cb)
157     {
158         getEndpoints(ref, 0, ttl, cb);
159     }
160     void getEndpoints(const ReferencePtr&, const ReferencePtr&, int, const GetEndpointsCallbackPtr&);
161 
162     void clearCache(const ReferencePtr&);
163 
164 private:
165 
166     void getEndpointsException(const ReferencePtr&, const Ice::Exception&);
167     void getEndpointsTrace(const ReferencePtr&, const std::vector<EndpointIPtr>&, bool);
168     void trace(const std::string&, const ReferencePtr&, const std::vector<EndpointIPtr>&);
169     void trace(const std::string&, const ReferencePtr&, const ReferencePtr&);
170 
171     RequestPtr getAdapterRequest(const ReferencePtr&);
172     RequestPtr getObjectRequest(const ReferencePtr&);
173 
174     void finishRequest(const ReferencePtr&, const std::vector<ReferencePtr>&, const Ice::ObjectPrxPtr&, bool);
175     friend class Request;
176     friend class RequestCallback;
177 
178     const Ice::LocatorPrxPtr _locator;
179     Ice::LocatorRegistryPrxPtr _locatorRegistry;
180     const LocatorTablePtr _table;
181     const bool _background;
182 
183     std::map<std::string, RequestPtr> _adapterRequests;
184     std::map<Ice::Identity, RequestPtr> _objectRequests;
185 };
186 
187 }
188 
189 #endif
190