summaryrefslogtreecommitdiff
path: root/src/CommonAPI/ServicePublisher.h
blob: 4d009be7ed3249072f02b6b90b70144f5e98a649 (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
/* Copyright (C) 2013 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef COMMONAPI_SERVICE_PUBLISHER_H_
#define COMMONAPI_SERVICE_PUBLISHER_H_


#include "Factory.h"


namespace CommonAPI {

/**
 * \brief Manages all services that shall be published by the application.
 *
 * Stubs for all services that shall be published will be registered here.
 * This class is defined as singleton per loaded runtime (i.e. per loaded middleware).
 */
class ServicePublisher {
 public:
    virtual ~ServicePublisher() {}

    /**
     * \brief Registers and publishes a service.
     *
     * Registers and publishes a service. Which service is to be published is defined
     * by the stub-pointer that is given as parameter. The given factory will be used
     * to construct all necessary middleware specific objects to do the publishing.
     *
     * \note Note that a call to this method will always result in a registration
     * with the middleware the given factory was instantiated for, not the middleware
     * that matches the runtime this ServicePublisher was retrieved from. Accordingly,
     * unregistering the service will have to be done by using the ServicePublisher
     * that is provided by the runtime matching the middleware that also provided
     * the given factory.
     *
     * @param serviceAddress The CommonAPI address the service shall be reachable at
     * @param stub The stub that provides an implementation for the service
     * @param factory The factory that will be used to construct all necessary middleware specific objects
     *
     * @return 'true' if the service was published successfully, 'false' if not or if another service that uses
     * the exact same address already is registered.
     */
    template<typename _Stub>
    bool registerService(std::shared_ptr<_Stub> stub,
                         const std::string& serviceAddress,
                         std::shared_ptr<Factory> factory) {
        return factory->registerService<_Stub>(stub, serviceAddress);
    }

    /**
     * \brief Registers and publishes a service.
     *
     * Registers and publishes a service. Which service is to be published is defined
     * by the stub-pointer that is given as parameter. The given factory will be used
     * to construct all necessary middleware specific objects to do the publishing.
     *
     * \note Note that a call to this method will always result in a registration
     * with the middleware the given factory was instantiated for, not the middleware
     * that matches the runtime this ServicePublisher was retrieved from. Accordingly,
     * unregistering the service will have to be done by using the ServicePublisher
     * that is provided by the runtime matching the middleware that also provided
     * the given factory.
     *
     * @param participantId The CommonAPI participant ID the service shall be identified with
     * @param serviceName The CommonAPI service name the service shall provide
     * @param domain The CommonAPI domain the service shall be reachable at
     * @param stub The stub that provides an implementation for the service
     * @param factory The factory that will be used to construct all necessary middleware specific objects
     *
     * @return 'true' if the service was published successfully, 'false' if not or if another service that uses
     * the exact same address already is registered.
     */
    template<typename _Stub>
    bool registerService(std::shared_ptr<_Stub> stub,
                         const std::string& participantId,
                         const std::string& serviceName,
                         const std::string& domain,
                         std::shared_ptr<Factory> factory) {

        return factory->registerService<_Stub>(stub, participantId, serviceName, domain);
    }

    /**
     * \brief Unregisters and depublishes the service that was published for the given address.
     *
     * Unregisters and depublishes the service that was published for the given CommonAPI address.
     *
     * @param The CommonAPI address the service was registered for
     *
     * @return 'true' if there was a service for the given address and depublishing
     * was successful, 'false' otherwise
     */
    virtual bool unregisterService(const std::string& serviceAddress) = 0;

    /**
     * \brief Unregisters and depublishes the service that was published for the given address.
     *
     * Unregisters and depublishes the service that was published for the given CommonAPI address.
     *
     * @param The CommonAPI participant ID the service was identified with
     * @param The CommonAPI service name the service provided
     * @param The CommonAPI domain the service was registered for
     *
     * @return 'true' if there was a service for the given address and depublishing
     * was successful, 'false' otherwise
     */
    virtual bool unregisterService(const std::string& participantId, const std::string& serviceName, const std::string& domain) {
        std::string serviceAddress(participantId + ":" + serviceName + ":"+ domain);
        return unregisterService(serviceAddress);
    }
};

} // namespace CommonAPI


#endif /* COMMONAPI_SERVICE_PUBLISHER_H_ */