Ice 3.8
C++ API Reference
Loading...
Searching...
No Matches
ObjectAdapter.h
1// Copyright (c) ZeroC, Inc.
2
3#ifndef ICE_OBJECT_ADAPTER_H
4#define ICE_OBJECT_ADAPTER_H
5
6#include "CommunicatorF.h"
7#include "Endpoint.h"
8#include "FacetMap.h"
9#include "ObjectAdapterF.h"
10#include "Proxy.h"
11#include "ProxyFunctions.h"
12#include "ServantLocator.h"
13
14#include <memory>
15#include <optional>
16
17namespace Ice
18{
19 class LocatorPrx;
20
21 /// An object adapter is the main server-side Ice API. It has two main purposes:
22 /// - accept incoming connections from clients and dispatch requests received over these connections (see
23 /// #activate); and
24 /// - maintain a dispatch pipeline and servants that handle the requests (see #add, #addDefaultServant, and #use).
25 ///
26 /// An object adapter can dispatch "bidirectional requests"--requests it receives over an outgoing connection
27 /// instead of a more common incoming connection. It can also dispatch collocated requests (with no connection at
28 /// all).
29 /// @see Communicator::createObjectAdapter
30 /// @headerfile Ice/Ice.h
31 class ICE_API ObjectAdapter
32 {
33 public:
34 virtual ~ObjectAdapter();
35
36 /// Gets the name of this object adapter.
37 /// @return This object adapter's name.
38 [[nodiscard]] virtual const std::string& getName() const noexcept = 0;
39
40 /// Gets the communicator that created this object adapter.
41 /// @return This object adapter's communicator.
42 [[nodiscard]] virtual CommunicatorPtr getCommunicator() const noexcept = 0;
43
44 /// Starts receiving and dispatching requests received over incoming connections.
45 /// @remark When this object adapter is an indirect object adapter configured with a locator proxy, this
46 /// function also registers the object adapter's published endpoints with this locator.
47 /// @see #deactivate
48 /// @see #getLocator
49 /// @see #getPublishedEndpoints
50 virtual void activate() = 0;
51
52 /// Stops reading requests from incoming connections. Outstanding dispatches are not affected. The object
53 /// adapter can be reactivated with #activate.
54 /// @remark This function is provided for backward compatibility with older versions of Ice. Don't use it in
55 /// new applications.
56 virtual void hold() = 0;
57
58 /// Waits until the object adapter is in the holding state (see #hold) and the dispatch of requests received
59 /// over incoming connection has completed.
60 /// @remark This function is provided for backward compatibility with older versions of Ice. Don't use it in
61 /// new applications.
62 virtual void waitForHold() = 0;
63
64 /// Deactivates this object adapter: stops accepting new connections from clients and closes gracefully all
65 /// incoming connections created by this object adapter once all outstanding dispatches have completed. If this
66 /// object adapter is indirect, this function also unregisters the object adapter from the locator
67 /// (see #activate).
68 /// This function does not cancel outstanding dispatches: it lets them execute until completion.
69 /// A deactivated object adapter cannot be reactivated again; it can only be destroyed.
70 /// @see #waitForDeactivate
71 /// @see Communicator#shutdown
72 virtual void deactivate() noexcept = 0;
73
74 /// Waits until #deactivate is called on this object adapter and all connections accepted by this object adapter
75 /// are closed. A connection is closed only after all outstanding dispatches on this connection have completed.
76 /// @see Communicator#waitForShutdown
77 virtual void waitForDeactivate() noexcept = 0;
78
79 /// Checks whether or not #deactivate was called on this object adapter.
80 /// @return `true` if #deactivate was called on this object adapter, `false` otherwise.
81 [[nodiscard]] virtual bool isDeactivated() const noexcept = 0;
82
83 /// Destroys this object adapter and cleans up all resources associated with it. Once this function has
84 /// returned, you can recreate another object adapter with the same name.
85 /// @see Communicator#destroy
86 virtual void destroy() noexcept = 0;
87
88 /// Adds a middleware to the dispatch pipeline of this object adapter.
89 /// @param middlewareFactory The middleware factory that creates the new middleware when this object adapter
90 /// creates its dispatch pipeline. A middleware factory is a function that takes an ObjectPtr (the next element
91 /// in the dispatch pipeline) and returns a new ObjectPtr (the middleware you want to install in the pipeline).
92 /// @return This object adapter.
93 /// @remark All middleware must be installed before the first dispatch.
94 /// @remark The middleware are executed in the order they are installed.
95 virtual ObjectAdapterPtr use(std::function<ObjectPtr(ObjectPtr)> middlewareFactory) = 0;
96
97 /// Adds a servant to this object adapter's Active Servant Map (ASM).
98 /// The ASM is a map {identity, facet} -> servant.
99 /// @tparam Prx The type of the proxy to return.
100 /// @param servant The servant to add.
101 /// @param id The identity of the Ice object that is implemented by the servant.
102 /// @return A proxy for @p id, created by this object adapter.
103 /// @throws AlreadyRegisteredException Thrown when a servant with the same identity is already registered.
104 /// @remark This function is equivalent to calling #addFacet with an empty facet.
105 template<typename Prx = ObjectPrx, std::enable_if_t<std::is_base_of_v<ObjectPrx, Prx>, bool> = true>
106 Prx add(const ObjectPtr& servant, const Identity& id)
107 {
108 return uncheckedCast<Prx>(_add(servant, id));
109 }
110
111 /// Adds a servant to this object adapter's Active Servant Map (ASM), while specifying a facet.
112 /// The ASM is a map {identity, facet} -> servant.
113 /// @tparam Prx The type of the proxy to return.
114 /// @param servant The servant to add.
115 /// @param id The identity of the Ice object that is implemented by the servant.
116 /// @param facet The facet of the Ice object that is implemented by the servant.
117 /// @return A proxy for @p id and @p facet, created by this object adapter.
118 /// @throws AlreadyRegisteredException Thrown when a servant with the same identity and facet is already
119 /// registered.
120 template<typename Prx = ObjectPrx, std::enable_if_t<std::is_base_of_v<ObjectPrx, Prx>, bool> = true>
121 Prx addFacet(ObjectPtr servant, Identity id, std::string facet)
122 {
123 return uncheckedCast<Prx>(_addFacet(std::move(servant), std::move(id), std::move(facet)));
124 }
125
126 /// Adds a servant to this object adapter's Active Servant Map (ASM), using an automatically generated UUID as
127 /// its identity.
128 /// @tparam Prx The type of the proxy to return.
129 /// @param servant The servant to add.
130 /// @return A proxy with the generated UUID identity created by this object adapter.
131 template<typename Prx = ObjectPrx, std::enable_if_t<std::is_base_of_v<ObjectPrx, Prx>, bool> = true>
132 [[nodiscard]] Prx addWithUUID(ObjectPtr servant)
133 {
134 return uncheckedCast<Prx>(_addWithUUID(std::move(servant)));
135 }
136
137 /// Adds a servant to this object adapter's Active Servant Map (ASM), using an automatically generated UUID as
138 /// its identity. Also specifies a facet.
139 /// @tparam Prx The type of the proxy to return.
140 /// @param servant The servant to add.
141 /// @param facet The facet of the Ice object that is implemented by the servant.
142 /// @return A proxy with the generated UUID identity and the specified facet.
143 template<typename Prx = ObjectPrx, std::enable_if_t<std::is_base_of_v<ObjectPrx, Prx>, bool> = true>
144 [[nodiscard]] Prx addFacetWithUUID(ObjectPtr servant, std::string facet)
145 {
146 return uncheckedCast<Prx>(_addFacetWithUUID(std::move(servant), std::move(facet)));
147 }
148
149 /// Adds a default servant to handle requests for a specific category. When an object adapter dispatches an
150 /// incoming request, it tries to find a servant for the identity and facet carried by the request in the
151 /// following order:
152 /// - The object adapter tries to find a servant for the identity and facet in the Active Servant Map.
153 /// - If this fails, the object adapter tries to find a default servant for the category component of the
154 /// identity.
155 /// - If this fails, the object adapter tries to find a default servant for the empty category, regardless of
156 /// the category contained in the identity.
157 /// - If this fails, the object adapter tries to find a servant locator for the category component of the
158 /// identity. If there is no such servant locator, the object adapter tries to find a servant locator for the
159 /// empty category.
160 /// - If a servant locator is found, the object adapter tries to find a servant using this servant locator.
161 /// - If all the previous steps fail, the object adapter gives up and the caller receives an
162 /// ObjectNotExistException or a FacetNotExistException.
163 /// @param servant The default servant to add.
164 /// @param category The category for which the default servant is registered. The empty category means it
165 /// handles all categories.
166 /// @throws AlreadyRegisteredException Thrown when a default servant with the same category is already
167 /// registered.
168 virtual void addDefaultServant(ObjectPtr servant, std::string category) = 0;
169
170 /// Removes a servant from the object adapter's Active Servant Map.
171 /// @param id The identity of the Ice object that is implemented by the servant.
172 /// @return The removed servant.
173 /// @throws NotRegisteredException Thrown when no servant with the given identity is registered.
174 virtual ObjectPtr remove(const Identity& id) = 0;
175
176 /// Removes a servant from the object adapter's Active Servant Map, while specifying a facet.
177 /// @param id The identity of the Ice object that is implemented by the servant.
178 /// @param facet The facet. An empty facet means the default facet.
179 /// @return The removed servant.
180 /// @throws NotRegisteredException Thrown when no servant with the given identity and facet is registered.
181 virtual ObjectPtr removeFacet(const Identity& id, std::string_view facet) = 0;
182
183 /// Removes all facets with the given identity from the Active Servant Map. This function completely removes the
184 /// Ice object, including its default facet.
185 /// @param id The identity of the Ice object to be removed.
186 /// @return A collection containing all the facet names and servants of the removed Ice object.
187 /// @throws NotRegisteredException Thrown when no servant with the given identity is registered.
188 virtual FacetMap removeAllFacets(const Identity& id) = 0;
189
190 /// Removes the default servant for a specific category.
191 /// @param category The category of the default servant to remove.
192 /// @return The default servant.
193 /// @throws NotRegisteredException Thrown when no default servant is registered for the given category.
194 virtual ObjectPtr removeDefaultServant(std::string_view category) = 0;
195
196 /// Looks up a servant.
197 /// @param id The identity of an Ice object.
198 /// @return The servant that implements the Ice object with the given identity,
199 /// or nullptr if no such servant has been found.
200 /// @remark This function only tries to find the servant in the ASM and among the default servants. It does not
201 /// attempt to locate a servant using servant locators.
202 [[nodiscard]] virtual ObjectPtr find(const Identity& id) const = 0;
203
204 /// Looks up a servant with an identity and facet.
205 /// @param id The identity of an Ice object.
206 /// @param facet The facet of an Ice object. An empty facet means the default facet.
207 /// @return The servant that implements the Ice object with the given identity and facet,
208 /// or nullptr if no such servant has been found.
209 /// @remark This function only tries to find the servant in the ASM and among the default servants. It does not
210 /// attempt to locate a servant using servant locators.
211 [[nodiscard]] virtual ObjectPtr findFacet(const Identity& id, std::string_view facet) const = 0;
212
213 /// Finds all facets for a given identity in the Active Servant Map.
214 /// @param id The identity.
215 /// @return A collection containing all the facet names and servants that have been found. Can be empty.
216 [[nodiscard]] virtual FacetMap findAllFacets(const Identity& id) const = 0;
217
218 /// Looks up a servant with an identity and a facet. It's equivalent to calling #findFacet.
219 /// @param proxy The proxy that provides the identity and facet to search.
220 /// @return The servant that matches the identity and facet carried by @p proxy, or nullptr if no such servant
221 /// has been found.
222 /// @remark This function only tries to find the servant in the ASM and among the default servants. It does not
223 /// attempt to locate a servant using servant locators.
224 [[nodiscard]] virtual ObjectPtr findByProxy(const ObjectPrx& proxy) const = 0;
225
226 /// Adds a ServantLocator to this object adapter for a specific category.
227 /// @param locator The servant locator to add.
228 /// @param category The category. The empty category means @p locator handles all categories.
229 /// @throws AlreadyRegisteredException Thrown when a servant locator with the same category is already
230 /// registered.
231 /// @see #addDefaultServant
232 virtual void addServantLocator(ServantLocatorPtr locator, std::string category) = 0;
233
234 /// Removes a ServantLocator from this object adapter.
235 /// @param category The category.
236 /// @return The servant locator.
237 /// @throws NotRegisteredException Thrown when no ServantLocator with the given category is registered.
238 virtual ServantLocatorPtr removeServantLocator(std::string_view category) = 0;
239
240 /// Finds a ServantLocator registered with this object adapter.
241 /// @param category The category.
242 /// @return The servant locator, or nullptr if not found.
243 [[nodiscard]] virtual ServantLocatorPtr findServantLocator(std::string_view category) const = 0;
244
245 /// Finds the default servant for a specific category.
246 /// @param category The category.
247 /// @return The default servant, or nullptr if not found.
248 [[nodiscard]] virtual ObjectPtr findDefaultServant(std::string_view category) const = 0;
249
250 /// Gets the dispatch pipeline of this object adapter.
251 /// @return The dispatch pipeline. The returned value is never nullptr.
252 [[nodiscard]] virtual const ObjectPtr& dispatchPipeline() const noexcept = 0;
253
254 /// Creates a proxy from an Ice identity. If this object adapter is configured with an adapter ID, the proxy
255 /// is an indirect proxy that refers to this adapter ID. If a replica group ID is also defined, the proxy is an
256 /// indirect proxy that refers to this replica group ID. Otherwise, the proxy is a direct proxy containing this
257 /// object adapter's published endpoints.
258 /// @tparam Prx The type of the proxy to return.
259 /// @param id An Ice identity.
260 /// @return A proxy with the given identity.
261 template<typename Prx = ObjectPrx, std::enable_if_t<std::is_base_of_v<ObjectPrx, Prx>, bool> = true>
263 {
264 return uncheckedCast<Prx>(_createProxy(std::move(id)));
265 }
266
267 /// Creates a direct proxy from an Ice identity.
268 /// @tparam Prx The type of the proxy to return.
269 /// @param id An Ice identity.
270 /// @return A proxy with the given identity and this published endpoints of this object adapter.
271 template<typename Prx = ObjectPrx, std::enable_if_t<std::is_base_of_v<ObjectPrx, Prx>, bool> = true>
273 {
274 return uncheckedCast<Prx>(_createDirectProxy(std::move(id)));
275 }
276
277 /// Creates an indirect proxy for an Ice identity.
278 /// @tparam Prx The type of the proxy to return.
279 /// @param id An Ice identity.
280 /// @return An indirect proxy with the given identity. If this object adapter is not configured with an adapter
281 /// ID or a replica group ID, the new proxy is a well-known proxy (i.e., an identity-only proxy).
282 template<typename Prx = ObjectPrx, std::enable_if_t<std::is_base_of_v<ObjectPrx, Prx>, bool> = true>
284 {
285 return uncheckedCast<Prx>(_createIndirectProxy(std::move(id)));
286 }
287
288 /// Sets an Ice locator on this object adapter.
289 /// @param loc The locator used by this object adapter.
290 virtual void setLocator(std::optional<LocatorPrx> loc) = 0;
291
292 /// Gets the Ice locator used by this object adapter.
293 /// @return The locator used by this object adapter, or nullptr if no locator is used by this object adapter.
294 [[nodiscard]] virtual std::optional<LocatorPrx> getLocator() const noexcept = 0;
295
296 /// Gets the set of endpoints configured on this object adapter.
297 /// @return The set of endpoints.
298 /// @remark This function remains usable after the object adapter has been deactivated.
299 [[nodiscard]] virtual EndpointSeq getEndpoints() const = 0;
300
301 /// Gets the set of endpoints that proxies created by this object adapter will contain.
302 /// @return The set of published endpoints.
303 /// @remark This function remains usable after the object adapter has been deactivated.
304 [[nodiscard]] virtual EndpointSeq getPublishedEndpoints() const = 0;
305
306 /// Sets the endpoints that proxies created by this object adapter will contain.
307 /// @param newEndpoints The new set of endpoints that the object adapter will embed in proxies.
308 /// @throws invalid_argument Thrown when @p newEndpoints is empty or this adapter is associated with a router.
309 virtual void setPublishedEndpoints(EndpointSeq newEndpoints) = 0;
310
311 protected:
312 /// @cond INTERNAL
313 virtual ObjectPrx _add(ObjectPtr servant, Identity id) = 0;
314 virtual ObjectPrx _addFacet(ObjectPtr servant, Identity id, std::string facet) = 0;
315 virtual ObjectPrx _addWithUUID(ObjectPtr servant) = 0;
316 virtual ObjectPrx _addFacetWithUUID(ObjectPtr servant, std::string facet) = 0;
317 [[nodiscard]] virtual ObjectPrx _createProxy(Identity id) const = 0;
318 [[nodiscard]] virtual ObjectPrx _createDirectProxy(Identity id) const = 0;
319 [[nodiscard]] virtual ObjectPrx _createIndirectProxy(Identity id) const = 0;
320 /// @endcond
321 };
322}
323
324#endif
Client applications use the Locator object to resolve Ice indirect proxies.
Definition Locator.h:45
virtual void addServantLocator(ServantLocatorPtr locator, std::string category)=0
Adds a ServantLocator to this object adapter for a specific category.
virtual EndpointSeq getPublishedEndpoints() const =0
Gets the set of endpoints that proxies created by this object adapter will contain.
virtual ObjectPtr remove(const Identity &id)=0
Removes a servant from the object adapter's Active Servant Map.
virtual bool isDeactivated() const noexcept=0
Checks whether or not deactivate was called on this object adapter.
Prx addWithUUID(ObjectPtr servant)
Adds a servant to this object adapter's Active Servant Map (ASM), using an automatically generated UU...
virtual EndpointSeq getEndpoints() const =0
Gets the set of endpoints configured on this object adapter.
virtual CommunicatorPtr getCommunicator() const noexcept=0
Gets the communicator that created this object adapter.
virtual void activate()=0
Starts receiving and dispatching requests received over incoming connections.
virtual ServantLocatorPtr removeServantLocator(std::string_view category)=0
Removes a ServantLocator from this object adapter.
virtual ObjectPtr removeFacet(const Identity &id, std::string_view facet)=0
Removes a servant from the object adapter's Active Servant Map, while specifying a facet.
virtual const ObjectPtr & dispatchPipeline() const noexcept=0
Gets the dispatch pipeline of this object adapter.
virtual void setPublishedEndpoints(EndpointSeq newEndpoints)=0
Sets the endpoints that proxies created by this object adapter will contain.
virtual ObjectPtr removeDefaultServant(std::string_view category)=0
Removes the default servant for a specific category.
virtual void hold()=0
Stops reading requests from incoming connections.
virtual void deactivate() noexcept=0
Deactivates this object adapter: stops accepting new connections from clients and closes gracefully a...
Prx createProxy(Identity id)
Creates a proxy from an Ice identity.
Prx createIndirectProxy(Identity id)
Creates an indirect proxy for an Ice identity.
virtual void destroy() noexcept=0
Destroys this object adapter and cleans up all resources associated with it.
Prx addFacetWithUUID(ObjectPtr servant, std::string facet)
Adds a servant to this object adapter's Active Servant Map (ASM), using an automatically generated UU...
virtual void waitForHold()=0
Waits until the object adapter is in the holding state (see hold) and the dispatch of requests receiv...
Prx createDirectProxy(Identity id)
Creates a direct proxy from an Ice identity.
virtual const std::string & getName() const noexcept=0
Gets the name of this object adapter.
virtual ObjectPtr findFacet(const Identity &id, std::string_view facet) const =0
Looks up a servant with an identity and facet.
Prx addFacet(ObjectPtr servant, Identity id, std::string facet)
Adds a servant to this object adapter's Active Servant Map (ASM), while specifying a facet.
virtual FacetMap findAllFacets(const Identity &id) const =0
Finds all facets for a given identity in the Active Servant Map.
Prx add(const ObjectPtr &servant, const Identity &id)
Adds a servant to this object adapter's Active Servant Map (ASM).
virtual FacetMap removeAllFacets(const Identity &id)=0
Removes all facets with the given identity from the Active Servant Map.
virtual std::optional< LocatorPrx > getLocator() const noexcept=0
Gets the Ice locator used by this object adapter.
virtual ServantLocatorPtr findServantLocator(std::string_view category) const =0
Finds a ServantLocator registered with this object adapter.
virtual void setLocator(std::optional< LocatorPrx > loc)=0
Sets an Ice locator on this object adapter.
virtual void waitForDeactivate() noexcept=0
Waits until deactivate is called on this object adapter and all connections accepted by this object a...
virtual ObjectPtr findDefaultServant(std::string_view category) const =0
Finds the default servant for a specific category.
virtual ObjectPtr find(const Identity &id) const =0
Looks up a servant.
virtual ObjectPtr findByProxy(const ObjectPrx &proxy) const =0
Looks up a servant with an identity and a facet.
virtual void addDefaultServant(ObjectPtr servant, std::string category)=0
Adds a default servant to handle requests for a specific category.
virtual ObjectAdapterPtr use(std::function< ObjectPtr(ObjectPtr)> middlewareFactory)=0
Adds a middleware to the dispatch pipeline of this object adapter.
An object adapter is the main server-side Ice API.
The base class for all Ice proxies.
Definition Proxy.h:232
std::shared_ptr< Communicator > CommunicatorPtr
A shared pointer to a Communicator.
std::shared_ptr< ObjectAdapter > ObjectAdapterPtr
A shared pointer to an ObjectAdapter.
std::vector< EndpointPtr > EndpointSeq
A sequence of endpoints.
Definition EndpointF.h:41
Prx uncheckedCast(const ObjectPrx &proxy)
Creates a new proxy from an existing proxy.
std::shared_ptr< ServantLocator > ServantLocatorPtr
A shared pointer to a ServantLocator.
std::shared_ptr< Object > ObjectPtr
A shared pointer to an Object.
Definition ObjectF.h:13
std::map< std::string, ObjectPtr, std::less<> > FacetMap
A mapping from facet name to servant.
Definition FacetMap.h:13
The Ice RPC framework.
Definition SampleEvent.h:66
Represents the identity of an Ice object.
Definition Identity.h:47