summaryrefslogtreecommitdiff
path: root/include/CommonAPI/Runtime.hpp
blob: 8d33d48c8f8d263dc68c461f4c2b3c0a3054c10f (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
// Copyright (C) 2013-2015 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.h> 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();

    COMMONAPI_EXPORT void init();

    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_; };

private:
    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 &);
    COMMONAPI_EXPORT std::shared_ptr<Proxy> createProxyHelper(const std::string &, const std::string &, const std::string &,
                                             std::shared_ptr<MainLoopContext>);


    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 &);
    COMMONAPI_EXPORT bool registerStubHelper(const std::string &, const std::string &, const std::string &,
                            std::shared_ptr<StubBase>, std::shared_ptr<MainLoopContext>);

    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 defaultBinding_;
    std::string defaultFolder_;
    std::string defaultConfig_;

    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_;

    static std::map<std::string, std::string> properties_;
    static std::shared_ptr<Runtime> theRuntime__;

friend class ProxyManager;
};

} // namespace CommonAPI

#endif // COMMONAPI_RUNTIME_HPP_