summaryrefslogtreecommitdiff
path: root/src/CommonAPI/Factory.h
blob: ea358286f99c0c86b12b59a9f87e84ecd079dfcc (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
/* 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_FACTORY_H_
#define COMMONAPI_FACTORY_H_

#include <cassert>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

#include "MiddlewareInfo.h"
#include "Proxy.h"
#include "Runtime.h"
#include "Stub.h"


namespace CommonAPI {


class Factory;
class Runtime;
class MiddlewareInfo;


template<template<typename ...> class _ProxyType, template<typename> class _AttributeExtension>
struct DefaultAttributeProxyFactoryHelper;


template<template<typename ...> class _ProxyClass, template<typename> class _AttributeExtension>
std::shared_ptr<typename DefaultAttributeProxyFactoryHelper<_ProxyClass, _AttributeExtension>::class_t> createProxyWithDefaultAttributeExtension(Factory* specificFactory, const std::string& participantId, const std::string& domain);


class Factory {
 public:
    Factory(const std::shared_ptr<Runtime> runtime,
            const MiddlewareInfo* middlewareInfo):
                runtime_(runtime),
                middlewareInfo_(middlewareInfo) {
    }

    virtual ~Factory() {}

    template<template<typename ...> class _ProxyClass, typename ... _AttributeExtensions>
    std::shared_ptr<_ProxyClass<_AttributeExtensions...> >
    buildProxy(const std::string& participantId,
               const std::string& domain) {

    	std::shared_ptr<Proxy> abstractMiddlewareProxy = createProxy(_ProxyClass<_AttributeExtensions...>::getInterfaceName(), participantId, domain);
    	return std::make_shared<_ProxyClass<_AttributeExtensions...>>(abstractMiddlewareProxy);
    }

    template<template<typename ...> class _ProxyClass, typename ... _AttributeExtensions >
    std::shared_ptr<_ProxyClass<_AttributeExtensions...> >
    buildProxy(const std::string& serviceAddress) {

		std::string domain;
		std::string serviceName;
		std::string participantId;
		if(!splitAddress(serviceAddress, domain, serviceName, participantId)) {
			return false;
		}

		return buildProxy<_ProxyClass, _AttributeExtensions...>(participantId, domain);
    }

    template <template<typename ...> class _ProxyClass, template<typename> class _AttributeExtension>
    std::shared_ptr<typename DefaultAttributeProxyFactoryHelper<_ProxyClass, _AttributeExtension>::class_t>
    buildProxyWithDefaultAttributeExtension(const std::string& participantId,
                                       const std::string& domain) {

    	std::shared_ptr<Proxy> abstractMiddlewareProxy = createProxy(DefaultAttributeProxyFactoryHelper<_ProxyClass, _AttributeExtension>::class_t::getInterfaceName(), participantId, domain);
    	return std::make_shared<typename DefaultAttributeProxyFactoryHelper<_ProxyClass, _AttributeExtension>::class_t>(abstractMiddlewareProxy);
    }

    template <template<typename ...> class _ProxyClass, template<typename> class _AttributeExtension>
    std::shared_ptr<typename DefaultAttributeProxyFactoryHelper<_ProxyClass, _AttributeExtension>::class_t>
    buildProxyWithDefaultAttributeExtension(const std::string& serviceAddress) {

		std::string domain;
		std::string serviceName;
		std::string participantId;
		if(!splitAddress(serviceAddress, domain, serviceName, participantId)) {
			return false;
		}

		return buildProxyWithDefaultAttributeExtension<_ProxyClass, _AttributeExtension>(participantId, domain);
    }

    inline std::shared_ptr<Runtime> getRuntime() {
        return runtime_;
    }

    template<typename _Stub>
    bool registerService(std::shared_ptr<_Stub> stub,
    				     const std::string& participantId,
            			 const std::string& domain) {

    	std::shared_ptr<StubBase> stubBase = std::dynamic_pointer_cast<StubBase>(stub);
		std::shared_ptr<CommonAPI::StubAdapter> stubAdapter = createAdapter(stubBase, _Stub::StubAdapterType::getInterfaceName(), participantId, domain);
		if(!stubAdapter) {
			return false;
		}

		std::string address = domain + ":" + _Stub::StubAdapterType::getInterfaceName() + ":" + participantId;
		registeredServices_.insert( {std::move(address), stubAdapter} );

		return true;
    }

    template<typename _Stub>
    bool registerService(std::shared_ptr<_Stub> stub, const std::string& serviceAddress) {

    	if(registeredServices_.find(serviceAddress) != registeredServices_.end()) {
    		return false;
    	}

		std::string domain;
		std::string serviceName;
		std::string participantId;
		if(!splitAddress(serviceAddress, domain, serviceName, participantId)) {
			return false;
		}

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

    inline bool unregisterService(const std::string& serviceAddress) {
    	return registeredServices_.erase(serviceAddress);
    }

    virtual std::vector<std::string> getAvailableServiceInstances(const std::string& serviceName, const std::string& serviceDomainName = "local") = 0;

    virtual bool isServiceInstanceAlive(const std::string& serviceInstanceID, const std::string& serviceName, const std::string& serviceDomainName = "local") = 0;

 protected:
    virtual std::shared_ptr<Proxy> createProxy(const char* interfaceName, const std::string& participantId, const std::string& domain) = 0;
    virtual std::shared_ptr<StubAdapter> createAdapter(std::shared_ptr<StubBase> stubBase, const char* interfaceName, const std::string& participantId, const std::string& domain) = 0;

 private:
    std::shared_ptr<Runtime> runtime_;
    std::unordered_map<std::string, std::shared_ptr<CommonAPI::StubAdapter>> registeredServices_;

    const MiddlewareInfo* middlewareInfo_;

    inline bool splitAddress(const std::string& serviceAddress, std::string& domain, std::string& serviceName, std::string& participantId) {
    	std::istringstream addressStream(serviceAddress);
		if(!std::getline(addressStream, domain, ':')) {
			return false;
		}
		if(!std::getline(addressStream, serviceName, ':')) {
			return false;
		}
		if(!std::getline(addressStream, participantId, ':')) {
			return false;
		}
		if(std::getline(addressStream, participantId)) {
			return false;
		}
		return true;
    }
};


} // namespace CommonAPI

#endif // COMMONAPI_FACTORY_H_