Add managed services
[profile/ivi/common-api-runtime.git] / src / CommonAPI / ServicePublisher.h
1 /* Copyright (C) 2013 BMW Group
2  * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
3  * Author: Juergen Gehring (juergen.gehring@bmw.de)
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8 #if !defined (COMMONAPI_INTERNAL_COMPILATION)
9 #error "Only <CommonAPI/CommonAPI.h> can be included directly, this file may disappear or change contents."
10 #endif
11
12 #ifndef COMMONAPI_SERVICE_PUBLISHER_H_
13 #define COMMONAPI_SERVICE_PUBLISHER_H_
14
15 #include <memory>
16 #include <string>
17
18 namespace CommonAPI {
19
20 class StubBase;
21 class Factory;
22
23 /**
24  * \brief Manages all services that shall be published by the application.
25  *
26  * Stubs for all services that shall be published will be registered here.
27  * This class is defined as singleton per loaded runtime (i.e. per loaded middleware).
28  */
29 class ServicePublisher {
30  public:
31     virtual ~ServicePublisher() {}
32
33     /**
34      * \brief Registers and publishes a service.
35      *
36      * Registers and publishes a service. Which service is to be published is defined
37      * by the stub-pointer that is given as parameter. The given factory will be used
38      * to construct all necessary middleware specific objects to do the publishing.
39      *
40      * \note Note that a call to this method will always result in a registration
41      * with the middleware the given factory was instantiated for, not the middleware
42      * that matches the runtime this ServicePublisher was retrieved from. Accordingly,
43      * unregistering the service will have to be done by using the ServicePublisher
44      * that is provided by the runtime matching the middleware that also provided
45      * the given factory.
46      *
47      * @param serviceAddress The CommonAPI address the service shall be reachable at
48      * @param stub The stub that provides an implementation for the service
49      * @param factory The factory that will be used to construct all necessary middleware specific objects
50      *
51      * @return 'true' if the service was published successfully, 'false' if not or if another service that uses
52      * the exact same address already is registered.
53      */
54     template<typename _Stub>
55     bool registerService(std::shared_ptr<_Stub> stub,
56                          const std::string& serviceAddress,
57                          std::shared_ptr<Factory> factory);
58
59     /**
60      * \brief Registers and publishes a service.
61      *
62      * Registers and publishes a service. Which service is to be published is defined
63      * by the stub-pointer that is given as parameter. The given factory will be used
64      * to construct all necessary middleware specific objects to do the publishing.
65      *
66      * \note Note that a call to this method will always result in a registration
67      * with the middleware the given factory was instantiated for, not the middleware
68      * that matches the runtime this ServicePublisher was retrieved from. Accordingly,
69      * unregistering the service will have to be done by using the ServicePublisher
70      * that is provided by the runtime matching the middleware that also provided
71      * the given factory.
72      *
73      * @param participantId The CommonAPI participant ID the service shall be identified with
74      * @param serviceName The CommonAPI service name the service shall provide
75      * @param domain The CommonAPI domain the service shall be reachable at
76      * @param stub The stub that provides an implementation for the service
77      * @param factory The factory that will be used to construct all necessary middleware specific objects
78      *
79      * @return 'true' if the service was published successfully, 'false' if not or if another service that uses
80      * the exact same address already is registered.
81      */
82     template<typename _Stub>
83     bool registerService(std::shared_ptr<_Stub> stub,
84                          const std::string& participantId,
85                          const std::string& serviceName,
86                          const std::string& domain,
87                          std::shared_ptr<Factory> factory);
88
89     /**
90      * \brief Unregisters and depublishes the service that was published for the given address.
91      *
92      * Unregisters and depublishes the service that was published for the given CommonAPI address.
93      *
94      * @param The CommonAPI address the service was registered for
95      *
96      * @return 'true' if there was a service for the given address and depublishing
97      * was successful, 'false' otherwise
98      */
99     virtual bool unregisterService(const std::string& serviceAddress) = 0;
100
101     /**
102      * \brief Unregisters and depublishes the service that was published for the given address.
103      *
104      * Unregisters and depublishes the service that was published for the given CommonAPI address.
105      *
106      * @param The CommonAPI participant ID the service was identified with
107      * @param The CommonAPI service name the service provided
108      * @param The CommonAPI domain the service was registered for
109      *
110      * @return 'true' if there was a service for the given address and depublishing
111      * was successful, 'false' otherwise
112      */
113     bool unregisterService(const std::string& participantId,
114                            const std::string& serviceName,
115                            const std::string& domain) {
116         std::string serviceAddress(participantId + ":" + serviceName + ":" + domain);
117         return unregisterService(serviceAddress);
118     }
119
120  protected:
121     /**
122      * Register stubBase service within a factory.
123      *
124      * This is a new API which deprecates the old Factory::registerAdapter() method.
125      * For compatibility reasons a default implementation is provided. New middleware
126      * implementations should override this method.
127      */
128     virtual bool registerService(const std::shared_ptr<StubBase>& stubBase,
129                                  const char* interfaceId,
130                                  const std::string& participantId,
131                                  const std::string& serviceName,
132                                  const std::string& domain,
133                                  const std::shared_ptr<Factory>& factory);
134 };
135
136 } // namespace CommonAPI
137
138 #include "ServicePublisher.hpp"
139
140 #endif /* COMMONAPI_SERVICE_PUBLISHER_H_ */