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
199
200
201
202
203
204
205
206
207
208
209
210
|
#pragma once
#include <forstio/id.hpp>
#include <forstio/async/async.hpp>
#include <forstio/codec/data.hpp>
#include <forstio/codec/interface.hpp>
#include <forstio/codec/schema_hash.hpp>
#include <variant>
#include "transfer.hpp"
namespace saw {
/**
* This class acts as a helper for rpc calls and representing data on the remote.
*/
template<typename T, typename Encoding>
class data_or_id {
private:
/**
* Variant representing the either id or data class.
*/
std::variant<id<T>, data<T,Encoding>> doi_;
public:
/**
* Constructor for instantiating.
*/
data_or_id(const id<T>& val):
doi_{val}
{}
/**
* Constructor for instantiating.
*/
data_or_id(data<T,Encoding> val):
doi_{std::move(val)}
{}
/**
* Check if this class holds an id.
*/
bool is_id() const {
return std::holds_alternative<id<T>>(doi_);
}
/**
* Check if this class holds data.
*/
bool is_data() const {
return std::holds_alternative<data<T,Encoding>>(doi_);
}
/**
* Returns the id.
*/
id<T> get_id() const {
return std::get<id<T>>(doi_);
}
/**
* Return a data reference.
*/
data<T,Encoding>& get_data(){
return std::get<data<T,Encoding>>(doi_);
}
/**
* Return a data reference.
*/
const data<T,Encoding>& get_data() const {
return std::get<data<T,Encoding>>(doi_);
}
};
/**
* Representing data on the remote
*/
template<typename T, typename Encoding, typename Remote>
class remote_data;
template<typename Iface, typename Encoding, typename Remote>
class rpc_client;
namespace impl {
template<typename T, typename Encoding, typename Remote>
struct tmpl_group_to_data_server_ptr_std_tuple;
template<typename... T, typename Encoding, typename Remote>
struct tmpl_group_to_data_server_ptr_std_tuple<tmpl_group<T...>, Encoding, Remote> {
using type = std::tuple<ptr<data_server<T,Encoding,Remote>>...>;
};
template<typename T, typename Encoding, typename Remote>
struct tmpl_group_to_data_client_std_tuple;
template<typename... T, typename Encoding, typename Remote>
struct tmpl_group_to_data_client_std_tuple<tmpl_group<T...>, Encoding, Remote> {
using type = std::tuple<data_client<T,Encoding,Remote>...>;
};
}
/**
* Identifier of an interface function on the wire.
*
* Keyed on the member name instead of the member index, so reordering an
* interface doesn't silently rebind calls between versions.
*/
template<string_literal Lit>
struct rpc_function_id {
static constexpr uint32_t value = hash_literal<Lit>::apply(0u);
};
namespace impl {
/**
* Translates a runtime function id back into the compile time member name
* of an interface.
*
* Needed by every backend where the called function arrives as data instead
* of as a template argument.
*/
template<typename Iface>
struct iface_dispatch {
static_assert(always_false<Iface>, "Expecting an Interface Type");
};
template<typename... T, string_literal... Names>
struct iface_dispatch<schema::Interface<schema::Member<T,Names>...>> {
template<typename Ret, uint64_t i, typename Func>
static Ret apply_ele(uint32_t func_id, Func& func){
if constexpr ( i < sizeof...(Names) ){
constexpr string_literal Lit = parameter_key_pack_type<i, Names...>::literal;
if(func_id == rpc_function_id<Lit>::value){
return func.template operator()<Lit>();
}
return apply_ele<Ret, i+1u>(func_id, func);
} else {
(void) func_id;
(void) func;
return make_error<err::not_found>("No interface member with this function id");
}
}
/**
* Calls func.operator()<Name>() for the member matching the id.
* The return type has to be provided, since there is no member to
* deduce it from if the id doesn't match anything.
*/
template<typename Ret, typename Func>
static Ret apply(uint32_t func_id, Func&& func){
return apply_ele<Ret, 0u>(func_id, func);
}
};
}
template<typename Remote>
class i_rpc_server {
protected:
virtual ~i_rpc_server() = default;
public:
virtual std::pair<uint32_t,uint32_t> get_class_id() const = 0;
template<typename To>
error_or<ptr<To>> cast_to(){
{
auto rhs = get_class_id();
if(To::class_id.first == rhs.first && To::class_id.second == rhs.second){
return {ptr<To>{*static_cast<To*>(this)}};
}
}
return make_error<err::invalid_state>("Class IDs are not matching.");
}
};
/**
* Implementation of a remote server on the backend
*/
template<typename Iface, typename Encoding, typename Remote>
class rpc_server {
private:
interface<Iface, Encoding> iface_;
public:
rpc_server(interface<Iface, Encoding> iface):
iface_{std::move(iface)}
{}
};
/**
* Representation of a remote.
* Partially similar to a network address
*/
template<typename Remote>
class remote_address {
static_assert(always_false<Remote>, "Type of remote not supported");
/**
*
*/
};
/**
* Reference Backend structure
*/
template<typename Remote>
class remote {
static_assert(always_false<Remote>, "Type of backend not supported");
};
}
|