b275974198eb720ad76da00d4d5aa043650f802a
[invirt/third/libt4.git] / rsm_client.h
1 #ifndef rsm_client_h
2 #define rsm_client_h
3
4 #include "rpc/rpc.h"
5 #include "rsm_protocol.h"
6 #include <string>
7 #include <vector>
8
9
10 //
11 // rsm client interface.
12 //
13 // The client stubs package up an rpc, and then call the invoke procedure
14 // on the replicated state machine passing the RPC as an argument.  This way
15 // the replicated state machine isn't service specific; any server can use it.
16 //
17
18 class rsm_client {
19     protected:
20         std::string primary;
21         std::vector<std::string> known_mems;
22         std::mutex rsm_client_mutex;
23         void primary_failure();
24         bool init_members();
25     public:
26         rsm_client(std::string dst);
27         rsm_protocol::status invoke(int proc, std::string &rep, const std::string &req);
28
29         template<class R, class ...Args>
30             int call(unsigned int proc, R & r, const Args & ...a1);
31     private:
32         template<class R> int call_m(unsigned int proc, R & r, const marshall & req);
33 };
34
35 template<class R>
36 int rsm_client::call_m(unsigned int proc, R & r, const marshall & req) {
37         std::string rep;
38         std::string res;
39         int intret = invoke(proc, rep, req.cstr());
40         VERIFY( intret == rsm_client_protocol::OK );
41         unmarshall u(rep);
42         u >> intret;
43         if (intret < 0) return intret;
44         u >> res;
45         if (!u.okdone()) {
46                 fprintf(stderr, "rsm_client::call_m: failed to unmarshall the reply.\n"
47                        "You probably forgot to set the reply string in "
48                        "rsm::client_invoke, or you may call RPC 0x%x with wrong return "
49                        "type\n", proc);
50                 VERIFY(0);
51                 return rpc_const::unmarshal_reply_failure;
52         }
53         unmarshall u1(res);
54         u1 >> r;
55         if(!u1.okdone()) {
56                 fprintf(stderr, "rsm_client::call_m: failed to unmarshall the reply.\n"
57                        "You are probably calling RPC 0x%x with wrong return "
58                        "type.\n", proc);
59                 VERIFY(0);
60                 return rpc_const::unmarshal_reply_failure;
61         }
62         return intret;
63 }
64
65 template<class R, class ...Args>
66 int rsm_client::call(unsigned int proc, R & r, const Args & ...a1) {
67     return call_m(proc, r, marshall{a1...});
68 }
69
70 #endif