Got rid of most using directives. Ported tests to python.
[invirt/third/libt4.git] / rpc / rpc.h
1 #ifndef rpc_h
2 #define rpc_h
3
4 #include "types.h"
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7
8 #include "rpc_protocol.h"
9 #include "thr_pool.h"
10 #include "marshall.h"
11 #include "marshall_wrap.h"
12 #include "connection.h"
13
14 using std::chrono::milliseconds;
15
16 namespace rpc {
17     static constexpr milliseconds to_max{12000};
18     static constexpr milliseconds to_min{100};
19 }
20
21 template<class P, class R, class ...Args>
22 struct is_valid_call : false_type {};
23
24 template<class S, class R, class ...Args>
25 struct is_valid_call<S(R &, Args...), R, Args...> : true_type {};
26
27 template<class P, class F>
28 struct is_valid_registration : false_type {};
29
30 template<class S, class R, class ...Args>
31 struct is_valid_registration<
32     S(R &, typename std::decay<Args>::type...),
33     S(R &, Args...)> : true_type {};
34
35 template<class P, class C, class S, class R, class ...Args>
36 struct is_valid_registration<
37     P,
38     S(C::*)(R &, Args...)> : is_valid_registration<P, S(R &, Args...)> {};
39
40 // rpc client endpoint.
41 // manages a xid space per destination socket
42 // threaded: multiple threads can be sending RPCs,
43 class rpcc : private connection_delegate {
44     private:
45         using proc_id_t = rpc_protocol::proc_id_t;
46         template <class S>
47         using proc_t = rpc_protocol::proc_t<S>;
48         using nonce_t = rpc_protocol::nonce_t;
49         using xid_t = rpc_protocol::xid_t;
50
51         // manages per rpc info
52         struct caller {
53             caller(xid_t _xid, string *_rep) : xid(_xid), rep(_rep) {}
54
55             int xid;
56             string *rep;
57             int intret;
58             bool done = false;
59             std::mutex m;
60             cond c;
61         };
62
63         void get_latest_connection(shared_ptr<connection> & ch);
64         void update_xid_rep(xid_t xid, lock & m_lock);
65
66
67         sockaddr_in dst_;
68         nonce_t clt_nonce_;
69         nonce_t srv_nonce_ = 0;
70         bool bind_done_ = false;
71         int lossytest_ = 0;
72         bool reachable_ = true;
73
74         shared_ptr<connection> chan_;
75
76         std::mutex m_; // protect insert/delete to calls[]
77         std::mutex chan_m_;
78
79         bool destroy_wait_ = false;
80         cond destroy_wait_c_;
81
82         std::map<int, caller *> calls_;
83
84         // xid starts with 1 and latest received reply starts with 0
85         xid_t xid_ = 1;
86         std::list<xid_t> xid_rep_window_ = {0};
87
88         struct request {
89             void clear() { buf.clear(); xid = -1; }
90             bool isvalid() { return xid != -1; }
91             string buf;
92             xid_t xid = -1;
93         };
94         request dup_req_;
95         int xid_rep_done_ = -1;
96
97         int call1(proc_id_t proc, milliseconds to, string & rep, marshall & req);
98
99         template<class R>
100         inline int call_m(proc_id_t proc, milliseconds to, R & r, marshall && req) {
101             string rep;
102             int intret = call1(proc, to, rep, req);
103             if (intret < 0) return intret;
104             unmarshall u(rep, true, r);
105             if (u.okdone() != true) {
106                 LOG << "rpcc::call_m: failed to unmarshall the reply.  You are probably "
107                     << "calling RPC 0x" << std::hex << proc << " with the wrong return type.";
108                 VERIFY(0);
109                 return rpc_protocol::unmarshall_reply_failure;
110             }
111             return intret;
112         }
113
114         bool got_pdu(const shared_ptr<connection> & c, const string & b);
115
116     public:
117
118         rpcc(const string & d);
119         ~rpcc();
120
121         nonce_t id() { return clt_nonce_; }
122
123         int bind(milliseconds to = rpc::to_max);
124
125         void set_reachable(bool r) { reachable_ = r; }
126
127         void cancel();
128
129         template<class P, class R, typename ...Args>
130         inline int call(proc_t<P> proc, R & r, const Args & ... args) {
131             return call_timeout(proc, rpc::to_max, r, args...);
132         }
133
134         template<class P, class R, typename ...Args>
135         inline int call_timeout(proc_t<P> proc, milliseconds to, R & r, const Args & ... args) {
136             static_assert(is_valid_call<P, R, Args...>::value, "RPC called with incorrect argument types");
137             return call_m(proc.id, to, r, std::forward<marshall>(marshall(args...)));
138         }
139 };
140
141 // rpc server endpoint.
142 class rpcs : private connection_delegate {
143     private:
144         using proc_id_t = rpc_protocol::proc_id_t;
145         template <class S>
146         using proc_t = rpc_protocol::proc_t<S>;
147         using nonce_t = rpc_protocol::nonce_t;
148         using xid_t = rpc_protocol::xid_t;
149
150         typedef enum {
151             NEW,  // new RPC, not a duplicate
152             INPROGRESS, // duplicate of an RPC we're still processing
153             DONE, // duplicate of an RPC we already replied to (have reply)
154             FORGOTTEN,  // duplicate of an old RPC whose reply we've forgotten
155         } rpcstate_t;
156
157         // state about an in-progress or completed RPC, for at-most-once.
158         // if cb_present is true, then the RPC is complete and a reply
159         // has been sent; in that case buf points to a copy of the reply,
160         // and sz holds the size of the reply.
161         struct reply_t {
162             reply_t (xid_t _xid) : xid(_xid), cb_present(false) {}
163             reply_t (xid_t _xid, const string & _buf) : xid(_xid), cb_present(true), buf(_buf) {}
164             xid_t xid;
165             bool cb_present; // whether the reply buffer is valid
166             string buf;      // the reply buffer
167         };
168
169         in_port_t port_;
170         nonce_t nonce_;
171
172         // provide at most once semantics by maintaining a window of replies
173         // per client that that client hasn't acknowledged receiving yet.
174         // indexed by client nonce.
175         std::map<nonce_t, std::list<reply_t>> reply_window_;
176
177         void add_reply(nonce_t clt_nonce, xid_t xid, const string & b);
178
179         rpcstate_t check_duplicate_and_update(nonce_t clt_nonce, xid_t xid,
180                 xid_t rep_xid, string & b);
181
182         // latest connection to the client
183         std::map<nonce_t, shared_ptr<connection>> conns_;
184
185         bool reachable_ = true;
186
187         // map proc # to function
188         std::map<proc_id_t, handler *> procs_;
189
190         std::mutex procs_m_; // protect insert/delete to procs[]
191         std::mutex reply_window_m_; // protect reply window et al
192         std::mutex conns_m_; // protect conns_
193
194         void dispatch(shared_ptr<connection> c, const string & buf);
195
196         unique_ptr<thread_pool> dispatchpool_{new thread_pool(6, false)};
197         unique_ptr<connection_listener> listener_;
198
199         // RPC handler for clients binding
200         rpc_protocol::status rpcbind(nonce_t & r);
201
202         bool got_pdu(const shared_ptr<connection> & c, const string & b);
203
204     public:
205
206         rpcs(in_port_t port);
207         ~rpcs();
208
209         void set_reachable(bool r) { reachable_ = r; }
210
211         template<class P, class F, class C=void> inline void reg(proc_t<P> proc, F f, C *c=nullptr) {
212             static_assert(is_valid_registration<P, F>::value, "RPC handler registered with incorrect argument types");
213             struct ReturnOnFailure {
214                 static inline int unmarshall_args_failure() {
215                     return rpc_protocol::unmarshall_args_failure;
216                 }
217             };
218             lock pl(procs_m_);
219             VERIFY(procs_.count(proc.id) == 0);
220             procs_[proc.id] = marshalled_func<F, ReturnOnFailure>::wrap(f, c);
221             VERIFY(procs_.count(proc.id) >= 1);
222         }
223
224         void start();
225 };
226
227 #endif