TEMPLATE MAGIC FOR GREAT JUSTICE
[invirt/third/libt4.git] / rsm.h
1 // replicated state machine interface.
2
3 #ifndef rsm_h
4 #define rsm_h
5
6 #include <string>
7 #include <vector>
8 #include "rsm_protocol.h"
9 #include "rsm_state_transfer.h"
10 #include "rpc/rpc.h"
11 #include <arpa/inet.h>
12 #include "config.h"
13
14 class rsm : public config_view_change {
15     private:
16         void reg1(int proc, handler *);
17     protected:
18         std::map<int, handler *> procs;
19         config *cfg;
20         class rsm_state_transfer *stf;
21         rpcs *rsmrpc;
22         // On slave: expected viewstamp of next invoke request
23         // On primary: viewstamp for the next request from rsm_client
24         viewstamp myvs;
25         viewstamp last_myvs;   // Viewstamp of the last executed request
26         std::string primary;
27         bool insync;
28         bool inviewchange;
29         unsigned vid_commit;  // Latest view id that is known to rsm layer
30         unsigned vid_insync;  // The view id that this node is synchronizing for
31         std::vector<std::string> backups;   // A list of unsynchronized backups
32
33         // For testing purposes
34         rpcs *testsvr;
35         bool partitioned;
36         bool dopartition;
37         bool break1;
38         bool break2;
39
40
41         rsm_client_protocol::status client_members(std::vector<std::string> &r, int i);
42         rsm_protocol::status invoke(int &, int proc, viewstamp vs, std::string mreq);
43         rsm_protocol::status transferreq(rsm_protocol::transferres &r, std::string src,
44                 viewstamp last, unsigned vid);
45         rsm_protocol::status transferdonereq(int &, std::string m, unsigned vid);
46         rsm_protocol::status joinreq(rsm_protocol::joinres &r, std::string src,
47                 viewstamp last);
48         rsm_test_protocol::status test_net_repairreq(int &r, int heal);
49         rsm_test_protocol::status breakpointreq(int &r, int b);
50
51         std::mutex rsm_mutex;
52         std::mutex invoke_mutex;
53         std::condition_variable recovery_cond;
54         std::condition_variable sync_cond;
55
56         void execute(int procno, std::string req, std::string &r);
57         rsm_client_protocol::status client_invoke(std::string &r, int procno,
58                 std::string req);
59         bool statetransfer(std::string m);
60         bool statetransferdone(std::string m);
61         bool join(std::string m);
62         void set_primary(unsigned vid);
63         std::string find_highest(viewstamp &vs, std::string &m, unsigned &vid);
64         bool sync_with_backups();
65         bool sync_with_primary();
66         void net_repair_wo(bool heal);
67         void breakpoint1();
68         void breakpoint2();
69         void partition1();
70         void commit_change_wo(unsigned vid);
71     public:
72         rsm (std::string _first, std::string _me);
73         ~rsm() {};
74
75         bool amiprimary();
76         void set_state_transfer(rsm_state_transfer *_stf) { stf = _stf; };
77         void recovery();
78         void commit_change(unsigned vid);
79
80         template<class F, class C=void> void reg(int proc, F f, C *c=nullptr);
81 };
82
83 template<class F, class C> void rsm::reg(int proc, F f, C *c) {
84     reg1(proc, marshalled_func<F, F>::wrap(f, c));
85 }
86
87 #endif /* rsm_h */