summaryrefslogtreecommitdiff
path: root/modules/io_codec/c++/rpc.hpp
blob: 2c97d6ba973b362cc594ff463b2328b145ce80af (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
#pragma once

#include <forstio/id.hpp>
#include <forstio/codec/data.hpp>
#include <forstio/async/async.hpp>

#include <forstio/codec/interface.hpp>

#include <variant>

namespace saw {

/**
 * This class acts as a helper for rpc calls and representing data on the remote.
 */
template<typename T, typename Encoding, typename Storage>
class data_or_id {
private:
	/**
	 * Variant representing the either id or data class.
	 */
	std::variant<id<T>, data<T,Encoding, Storage>> doi_;
public:
	/**
	 * Constructor for instantiating.
	 */
	data_or_id(const id<T>& val):
		doi_{val}
	{}

	/**
	 * Constructor for instantiating.
	 */
	data_or_id(data<T,Encoding, Storage> 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,Storage>>(doi_);
	}

	/**
	 * Returns the id.
	 */
	id<T> get_id() const {
		return std::get<id<T>>(doi_);
	}

	/**
	 * Return a data reference.
	 */
	data<T,Encoding, Storage>& get_data(){
		return std::get<data<T,Encoding, Storage>>(doi_);
	}

	/**
	 * Return a data reference.
	 */
	const data<T,Encoding,Storage>& get_data() const {
		return std::get<data<T,Encoding,Storage>>(doi_);
	}
};

/**
 * Representing data on the remote
 */
template<typename T, typename Encoding, typename Storage, typename Remote>
class remote_data {
private:
	id<T> id_;
public:
	remote_data(const id<T>& id):
		id_{id}
	{}

	/**
	 * Wait until data arrives
	 */
	error_or<data<T, Encoding, Storage>> wait(wait_scope& wait);

	/**
	 * Asynchronously wait for a result
	 */
	conveyor<data<T, Encoding, Storage>> on_receive();
};

/**
 * Client RPC reference structure
 */
template<typename Iface, typename Encoding, typename Storage, typename Remote>
class rpc_client {
	/**
	 * request the data from the remote
	 */
	template<typename IdT>
	remote_data<IdT, Encoding, Storage, Remote> request_data(id<IdT> data);
		
	/** @todo
	 * Determine type based on Name
	 */
	/*
	template<string_literal Name>
	error_or<
		id<
			typename schema_member_type<Name, Iface>::type
		>
	> call(data_or_id<Input> inp);
	*/
};

/**
 * Implementation of a remote server on the backend
 */
template<typename Iface, typename Encoding, typename Storage, typename Remote>
class rpc_server {
private:
	interface<Iface, Encoding, Storage> iface_;
public:
	rpc_server(interface<Iface, Encoding, Storage> 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");

	/**
	 * Resolves an address for the remote
	 */
	conveyor<remote_address<Remote>> resolve_address();

	/**
	 * Connect to a remote
	 */
	template<typename Iface, typename Encode, typename Storage>
	conveyor<rpc_client<Iface, Encode, Storage, Remote>> connect(const remote_address<Remote>& addr);

	/**
	 * Start listening
	 */
	template<typename Iface, typename Encode, typename Storage>
	rpc_server<Iface, Encode, Storage, Remote> listen();
};
}