summaryrefslogtreecommitdiff
path: root/include/CommonAPI/DBus/DBusFactory.hpp
blob: 644e035c02f680f207a5f831489037d1cdaae63d (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
// 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.hpp> can be included directly, this file may disappear or change contents."
#endif

#ifndef COMMONAPI_DBUS_FACTORY_HPP_
#define COMMONAPI_DBUS_FACTORY_HPP_

#include <map>

#include <CommonAPI/Export.hpp>
#include <CommonAPI/Factory.hpp>
#include <CommonAPI/DBus/DBusTypes.hpp>

namespace CommonAPI {
namespace DBus {

class DBusAddress;
class DBusProxy;
class DBusProxyConnection;
class DBusStubAdapter;

typedef std::shared_ptr<DBusProxy>
(*ProxyCreateFunction)(const DBusAddress &_address,
					   const std::shared_ptr<DBusProxyConnection> &_connection);

typedef std::shared_ptr<DBusStubAdapter>
(*StubAdapterCreateFunction) (const DBusAddress &_address,
							  const std::shared_ptr<DBusProxyConnection> &_connection,
							  const std::shared_ptr<StubBase> &_stub);

class Factory : public CommonAPI::Factory {
public:
	COMMONAPI_EXPORT static std::shared_ptr<Factory> get();

	COMMONAPI_EXPORT Factory();
	COMMONAPI_EXPORT virtual ~Factory();

	COMMONAPI_EXPORT void registerProxyCreateMethod(const std::string &_address,
     							   ProxyCreateFunction _function);

	COMMONAPI_EXPORT void registerStubAdapterCreateMethod(const std::string &_address,
    									 StubAdapterCreateFunction _function);


	COMMONAPI_EXPORT std::shared_ptr<Proxy> createProxy(const std::string &_domain,
            						   const std::string &_interface,
									   const std::string &_instance,
									   const ConnectionId_t &_connectionId);

	COMMONAPI_EXPORT std::shared_ptr<Proxy> createProxy(const std::string &_domain,
    								   const std::string &_interface,
    								   const std::string &_instance,
    								   std::shared_ptr<MainLoopContext> _context);

	COMMONAPI_EXPORT bool registerStub(const std::string &_domain,
    			   	   	  const std::string &_interface,
    			   	   	  const std::string &_instance,
    					  std::shared_ptr<StubBase> _stub,
						  const ConnectionId_t &_connectionId);

	COMMONAPI_EXPORT bool registerStub(const std::string &_domain,
			   	   	  const std::string &_interface,
			   	   	  const std::string &_instance,
					  std::shared_ptr<StubBase> _stub,
					  std::shared_ptr<MainLoopContext> _context);

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

	// Services
	COMMONAPI_EXPORT std::shared_ptr<DBusStubAdapter> getRegisteredService(const std::string &_address);

	// Managed services
	COMMONAPI_EXPORT std::shared_ptr<DBusStubAdapter> createDBusStubAdapter(const std::shared_ptr<StubBase> &_stub,
	        											   const std::string &_interface,
														   const DBusAddress &_address,
														   const std::shared_ptr<DBusProxyConnection> &_connection);
	COMMONAPI_EXPORT bool registerManagedService(const std::shared_ptr<DBusStubAdapter> &_adapter);
	COMMONAPI_EXPORT bool unregisterManagedService(const std::string &_address);

private:
	COMMONAPI_EXPORT std::shared_ptr<DBusConnection> getConnection(const ConnectionId_t &);
	COMMONAPI_EXPORT std::shared_ptr<DBusConnection> getConnection(std::shared_ptr<MainLoopContext>);
	COMMONAPI_EXPORT bool registerStubAdapter(std::shared_ptr<DBusStubAdapter>);
	COMMONAPI_EXPORT bool unregisterStubAdapter(std::shared_ptr<DBusStubAdapter>);

	// Managed services
	typedef std::unordered_map<std::string, std::shared_ptr<DBusStubAdapter>> ServicesMap;
	COMMONAPI_EXPORT bool unregisterManagedService(const ServicesMap::iterator &);

private:
	static std::shared_ptr<Factory> theFactory;

	std::map<ConnectionId_t, std::shared_ptr<DBusConnection>> connections_;
	std::map<MainLoopContext *, std::shared_ptr<DBusConnection>> contextConnections_;

    std::map<std::string, ProxyCreateFunction> proxyCreateFunctions_;
    std::map<std::string, StubAdapterCreateFunction> stubAdapterCreateFunctions_;

    ServicesMap services_;

    DBusType_t dBusBusType_;
};

} // namespace DBus
} // namespace CommonAPI

#endif // COMMONAPI_DBUS_FACTORY_HPP_