-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.h
173 lines (139 loc) · 4.61 KB
/
protocol.h
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
170
171
172
173
#ifndef __PROTOCOL_H__
#define __PROTOCOL_H__
#include "LeaderStatus.h"
//------------------------------------------------------------------------------
struct Message {
uint32_t sequence_number;
uint32_t ack_sequence_number;
virtual std::string label() const = 0;
virtual void to_stream(std::ostream&) const = 0;
Message(uint32_t sequence_number, uint32_t ack_sequence_number)
: sequence_number(sequence_number)
, ack_sequence_number(ack_sequence_number)
{}
Message(std::istream& is) { is >> sequence_number >> ack_sequence_number; }
virtual ~Message() {}
};
inline std::ostream& operator<<(std::ostream& os, const Message& msg) {
os << msg.sequence_number << " " << msg.ack_sequence_number << " ";
msg.to_stream(os);
return os;
}
//------------------------------------------------------------------------------
struct PingMsg : Message {
using Message::Message;
std::string label() const override { return "ping"; }
void to_stream(std::ostream&) const override {}
};
//------------------------------------------------------------------------------
struct StartMsg : Message {
using Message::Message;
std::string label() const override { return "start"; }
void to_stream(std::ostream&) const override {}
};
//------------------------------------------------------------------------------
struct NumberMsg : Message {
std::string label() const override { return "number"; }
float random_number;
NumberMsg(uint32_t sequence_number, uint32_t ack_sequence_number
, float random_number)
: Message(sequence_number, ack_sequence_number)
, random_number(random_number)
{}
NumberMsg(std::istream& is) : Message(is) {
is >> random_number;
}
void to_stream(std::ostream& os) const override {
os << random_number;
}
};
//------------------------------------------------------------------------------
struct Update1Msg : Message {
std::string label() const override { return "update1"; }
LeaderStatus status;
Update1Msg(uint32_t sequence_number, uint32_t ack_sequence_number
, LeaderStatus status)
: Message(sequence_number, ack_sequence_number)
, status(status)
{}
Update1Msg(std::istream& is) : Message(is) {
is >> status;
}
void to_stream(std::ostream& os) const override {
os << status;
}
};
//------------------------------------------------------------------------------
struct Update2Msg : Message {
std::string label() const override { return "update2"; }
LeaderStatus status;
Update2Msg(uint32_t sequence_number, uint32_t ack_sequence_number
, LeaderStatus status)
: Message(sequence_number, ack_sequence_number)
, status(status)
{}
Update2Msg(std::istream& is) : Message(is) {
is >> status;
}
void to_stream(std::ostream& os) const override {
os << status;
}
};
//------------------------------------------------------------------------------
struct ResultMsg : Message {
std::string label() const override { return "result"; }
LeaderStatus status;
ResultMsg(uint32_t sequence_number, uint32_t ack_sequence_number
, LeaderStatus status)
: Message(sequence_number, ack_sequence_number)
, status(status)
{}
ResultMsg(std::istream& is) : Message(is) {
is >> status;
}
void to_stream(std::ostream& os) const override {
os << status;
}
};
//------------------------------------------------------------------------------
template< typename PingHandler
, typename StartHandler
, typename NumberHandler
, typename Update1Handler
, typename Update2Handler
, typename ResultHandler
>
void dispatch_message( std::istream& is
, const PingHandler& ping_handler
, const StartHandler& start_handler
, const NumberHandler& random_number_handler
, const Update1Handler& update1_handler
, const Update2Handler& update2_handler
, const ResultHandler& result_handler) {
using namespace std;
string label;
is >> label;
if (label == "ping") {
ping_handler(PingMsg(is));
}
else if (label == "start") {
start_handler(StartMsg(is));
}
else if (label == "number") {
random_number_handler(NumberMsg(is));
}
else if (label == "update1") {
update1_handler(Update1Msg(is));
}
else if (label == "update2") {
update2_handler(Update2Msg(is));
}
else if (label == "result") {
result_handler(ResultMsg(is));
}
else {
throw runtime_error("unrecognized message label");
}
}
//------------------------------------------------------------------------------
#endif // ifndef __PROTOCOL_H__