summaryrefslogtreecommitdiff
path: root/include/CommonAPI/Runtime.hpp
blob: 61bd5fea2f847183612a900486f68bc807d827da (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
// Copyright (C) 2013-2020 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// 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/.

#if !defined (COMMONAPI_INTERNAL_COMPILATION)
#error "Only <CommonAPI/CommonAPI.hpp> can be included directly, this file may disappear or change contents."
#endif

#ifndef COMMONAPI_RUNTIME_HPP_
#define COMMONAPI_RUNTIME_HPP_

#include <map>
#include <memory>
#include <mutex>
#include <set>

#include <CommonAPI/AttributeExtension.hpp>
#include <CommonAPI/Export.hpp>
#include <CommonAPI/Factory.hpp>
#include <CommonAPI/Types.hpp>

namespace CommonAPI {

static const ConnectionId_t DEFAULT_CONNECTION_ID = "";

class MainLoopContext;
class Proxy;
class ProxyManager;
class StubBase;

class Runtime {
public:
    COMMONAPI_EXPORT static std::string getProperty(const std::string &_name);
    COMMONAPI_EXPORT static void setProperty(const std::string &_name, const std::string &_value);

    COMMONAPI_EXPORT static std::shared_ptr<Runtime> get();

    COMMONAPI_EXPORT Runtime();
    COMMONAPI_EXPORT virtual ~Runtime();

    template<template<typename ...> class ProxyClass_, typename ... AttributeExtensions_>
    COMMONAPI_EXPORT std::shared_ptr<
        ProxyClass_<AttributeExtensions_...>
    >
    buildProxy(const std::string &_domain,
               const std::string &_instance,
               const ConnectionId_t &_connectionId = DEFAULT_CONNECTION_ID) {
        std::shared_ptr<Proxy> proxy
            = createProxy(_domain,
                          ProxyClass_<AttributeExtensions_...>::getInterface(),
                          _instance,
                          _connectionId);

        if (proxy) {
            return std::make_shared<ProxyClass_<AttributeExtensions_...>>(proxy);
        }
        return nullptr;
    }

    template<template<typename ...> class ProxyClass_, typename ... AttributeExtensions_>
    COMMONAPI_EXPORT std::shared_ptr<
        ProxyClass_<AttributeExtensions_...>
    >
    buildProxy(const std::string &_domain,
               const std::string &_instance,
               std::shared_ptr<MainLoopContext> _context) {
        std::shared_ptr<Proxy> proxy
            = createProxy(_domain,
                          ProxyClass_<AttributeExtensions_...>::getInterface(),
                          _instance,
                          _context);
        if (proxy) {
            return std::make_shared<ProxyClass_<AttributeExtensions_...>>(proxy);
        }
        return nullptr;
    }

    template <template<typename ...> class ProxyClass_, template<typename> class AttributeExtension_>
    COMMONAPI_EXPORT std::shared_ptr<typename DefaultAttributeProxyHelper<ProxyClass_, AttributeExtension_>::class_t>
    buildProxyWithDefaultAttributeExtension(const std::string &_domain,
                                            const std::string &_instance,
                                            const ConnectionId_t &_connectionId = DEFAULT_CONNECTION_ID) {
        std::shared_ptr<Proxy> proxy
            = createProxy(_domain,
                           DefaultAttributeProxyHelper<ProxyClass_, AttributeExtension_>::class_t::getInterface(),
                          _instance,
                          _connectionId);
        if (proxy) {
            return std::make_shared<typename DefaultAttributeProxyHelper<ProxyClass_, AttributeExtension_>::class_t>(proxy);
        }
        return nullptr;
    }

    template <template<typename ...> class ProxyClass_, template<typename> class AttributeExtension_>
    COMMONAPI_EXPORT std::shared_ptr<typename DefaultAttributeProxyHelper<ProxyClass_, AttributeExtension_>::class_t>
    buildProxyWithDefaultAttributeExtension(const std::string &_domain,
                                            const std::string &_instance,
                                            std::shared_ptr<MainLoopContext> _context) {
        std::shared_ptr<Proxy> proxy
            = createProxy(_domain,
                           DefaultAttributeProxyHelper<ProxyClass_, AttributeExtension_>::class_t::getInterface(),
                          _instance,
                          _context);
        if (proxy) {
            return std::make_shared<typename DefaultAttributeProxyHelper<ProxyClass_, AttributeExtension_>::class_t>(proxy);
        }
        return nullptr;
    }

    template<typename Stub_>
    COMMONAPI_EXPORT bool registerService(const std::string &_domain,
                         const std::string &_instance,
                         std::shared_ptr<Stub_> _service,
                         const ConnectionId_t &_connectionId = DEFAULT_CONNECTION_ID) {
        return registerStub(_domain, Stub_::StubInterface::getInterface(), _instance, _service, _connectionId);
    }

    template<typename Stub_>
    COMMONAPI_EXPORT bool registerService(const std::string &_domain,
                         const std::string &_instance,
                         std::shared_ptr<Stub_> _service,
                         std::shared_ptr<MainLoopContext> _context) {
        return registerStub(_domain, Stub_::StubInterface::getInterface(), _instance, _service, _context);
    }

    COMMONAPI_EXPORT bool unregisterService(const std::string &_domain,
                            const std::string &_interface,
                            const std::string &_instance) {
        return unregisterStub(_domain, _interface, _instance);
    }

    COMMONAPI_EXPORT bool registerFactory(const std::string &_ipc, std::shared_ptr<Factory> _factory);
    COMMONAPI_EXPORT bool unregisterFactory(const std::string &_ipc);

    inline const std::string &getDefaultBinding() const { return defaultBinding_; };

    COMMONAPI_EXPORT void initFactories();
    COMMONAPI_EXPORT Timeout_t getDefaultCallTimeout() const;

private:
    COMMONAPI_EXPORT void init();
    COMMONAPI_EXPORT bool readConfiguration();
    COMMONAPI_EXPORT bool splitAddress(const std::string &, std::string &, std::string &, std::string &);

    COMMONAPI_EXPORT std::shared_ptr<Proxy> createProxy(const std::string &, const std::string &, const std::string &,
                                       const ConnectionId_t &);
    COMMONAPI_EXPORT std::shared_ptr<Proxy> createProxy(const std::string &, const std::string &, const std::string &,
                                       std::shared_ptr<MainLoopContext>);

    COMMONAPI_EXPORT std::shared_ptr<Proxy> createProxyHelper(const std::string &, const std::string &, const std::string &,
                                             const ConnectionId_t &, bool);
    COMMONAPI_EXPORT std::shared_ptr<Proxy> createProxyHelper(const std::string &, const std::string &, const std::string &,
                                             std::shared_ptr<MainLoopContext>, bool);


    COMMONAPI_EXPORT bool registerStub(const std::string &, const std::string &, const std::string &,
                      std::shared_ptr<StubBase>, const ConnectionId_t &);
    COMMONAPI_EXPORT bool registerStub(const std::string &, const std::string &, const std::string &,
                      std::shared_ptr<StubBase>, std::shared_ptr<MainLoopContext>);
    COMMONAPI_EXPORT bool registerStubHelper(const std::string &, const std::string &, const std::string &,
                            std::shared_ptr<StubBase>, const ConnectionId_t &, bool);
    COMMONAPI_EXPORT bool registerStubHelper(const std::string &, const std::string &, const std::string &,
                            std::shared_ptr<StubBase>, std::shared_ptr<MainLoopContext>, bool);

    COMMONAPI_EXPORT bool unregisterStub(const std::string &, const std::string &, const std::string &);

    COMMONAPI_EXPORT std::string getLibrary(const std::string &, const std::string &, const std::string &, bool);
    COMMONAPI_EXPORT bool loadLibrary(const std::string &);

private:
    std::string usedConfig_;

    std::string defaultBinding_;
    std::string defaultFolder_;
    std::string defaultConfig_;
    Timeout_t defaultCallTimeout_;

    std::map<std::string, std::shared_ptr<Factory>> factories_;
    std::shared_ptr<Factory> defaultFactory_;
    std::map<std::string, std::map<bool, std::string>> libraries_;
    std::set<std::string> loadedLibraries_; // Library name

    std::mutex mutex_;
    std::mutex factoriesMutex_;
    std::mutex loadMutex_;

    bool isConfigured_;
    bool isInitialized_;

    static std::map<std::string, std::string> properties_;

friend class ProxyManager;
};

} // namespace CommonAPI

#endif // COMMONAPI_RUNTIME_HPP_