-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRequestVoteHandler.cpp
More file actions
46 lines (36 loc) · 975 Bytes
/
RequestVoteHandler.cpp
File metadata and controls
46 lines (36 loc) · 975 Bytes
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
#include "RequestVoteHandler.hpp"
#include "Response.hpp"
using namespace locke;
RequestVoteHandler::RequestVoteHandler
(RaftServer& server, const RequestVote& req) : server(server), req(req) {}
void RequestVoteHandler::process()
{
if (req.term() < server.current_term) {
reply(false);
return;
}
if (server.voted_for != NOBODY_YET && server.voted_for != req.candidate()) {
reply(false);
return;
}
Log::Entry last_entry;
Log::last(&last_entry);
if (last_entry.term > req.last_term()) {
reply(false);
return;
}
if (last_entry.term == req.last_term() && last_entry.idx > req.last_index()) {
reply(false);
return;
}
server.voted_for = req.candidate();
server.save();
reply(true);
}
void RequestVoteHandler::reply(bool success)
{
StaticJsonBuffer<JSON_SMALL> buff;
uint32_t current_term = server.current_term;
Response response(buff, success, current_term, RPC::RequestVoteResponse);
response.print();
}