074cbe9361e9a57e9ad8af7a729cf2ac5e961c51
[invirt/third/libt4.git] / config.h
1 #ifndef config_h
2 #define config_h
3
4 #include <string>
5 #include <vector>
6 #include "paxos.h"
7 #include "lock.h"
8
9 using std::chrono::steady_clock;
10 using std::chrono::seconds;
11 using std::string;
12 using std::vector;
13 using std::thread;
14 using std::ostringstream;
15 using std::istringstream;
16 using std::ostream_iterator;
17 using std::istream_iterator;
18 using std::copy;
19 using std::min;
20 using std::min_element;
21
22 class config_view_change {
23     public:
24         virtual void commit_change(unsigned view_id) = 0;
25         virtual ~config_view_change() {}
26 };
27
28 class config : public paxos_change {
29     private:
30         unsigned my_view_id;
31         string first;
32         string me;
33         config_view_change *vc;
34         acceptor paxos_acceptor;
35         proposer paxos_proposer;
36         vector<string> mems;
37         mutex cfg_mutex;
38         cond config_cond;
39         paxos_protocol::status heartbeat(int &r, string m, unsigned instance);
40         string value(const vector<string> &mems) const;
41         vector<string> members(const string &v) const;
42         void get_view(unsigned instance, vector<string> &m, lock &cfg_mutex_lock);
43         bool remove(const string &, lock &cfg_mutex_lock);
44         void reconstruct(lock &cfg_mutex_lock);
45         typedef enum {
46             OK, // response and same view #
47             VIEWERR,    // response but different view #
48             FAILURE,    // no response
49         } heartbeat_t;
50         heartbeat_t doheartbeat(const string &m, lock &cfg_mutex_lock);
51     public:
52         config(const string &_first, const string &_me, config_view_change *_vc);
53         unsigned view_id() { return my_view_id; }
54         const string &myaddr() const { return me; }
55         string dump() { return paxos_acceptor.dump(); }
56         void get_view(unsigned instance, vector<string> &m);
57         void restore(const string &s);
58         bool add(const string &, unsigned view_id);
59         bool ismember(const string &m, unsigned view_id);
60         void heartbeater(void);
61         void paxos_commit(unsigned instance, const string &v);
62         // XXX hack; maybe should have its own port number
63         rpcs *get_rpcs() { return paxos_acceptor.get_rpcs(); }
64         void breakpoint(int b) { paxos_proposer.breakpoint(b); }
65 };
66
67 #endif