Add receive voted and send success
This commit is contained in:
parent
3cb93361f2
commit
af6f434a78
@ -12,9 +12,16 @@ namespace paxos
|
|||||||
base_path_ += "/";
|
base_path_ += "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Decree> Ledger::get_decrees()
|
Decree Ledger::get_decree()
|
||||||
{
|
{
|
||||||
return std::vector<Decree>();
|
Decree decree;
|
||||||
|
decree.decree = read_file(base_path_ + "decree.txt");
|
||||||
|
return decree;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ledger::set_decree(Decree decree)
|
||||||
|
{
|
||||||
|
write_file(base_path_ + "decree.txt", decree.decree);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Ledger::last_tried()
|
int Ledger::last_tried()
|
||||||
|
@ -12,7 +12,8 @@ namespace paxos
|
|||||||
public:
|
public:
|
||||||
Ledger(std::string path);
|
Ledger(std::string path);
|
||||||
|
|
||||||
std::vector<Decree> get_decrees();
|
Decree get_decree();
|
||||||
|
void set_decree(Decree decree);
|
||||||
|
|
||||||
int last_tried();
|
int last_tried();
|
||||||
void set_last_tried(int b);
|
void set_last_tried(int b);
|
||||||
|
@ -182,18 +182,62 @@ namespace paxos
|
|||||||
vote.decree = decree;
|
vote.decree = decree;
|
||||||
ledger.set_prev_vote(vote);
|
ledger.set_prev_vote(vote);
|
||||||
|
|
||||||
send_voted(ballot, sender);
|
send_voted(ballot, decree, sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Legislator::send_voted(int ballot, std::string receiver)
|
void Legislator::send_voted(int ballot, Decree decree, std::string receiver)
|
||||||
{
|
{
|
||||||
Message message;
|
Message message;
|
||||||
message.set_method("Voted");
|
message.set_method("Voted");
|
||||||
message.add_header("ballot", std::to_string(ballot));
|
message.add_header("ballot", std::to_string(ballot));
|
||||||
message.add_header("sender", self->config_.name);
|
message.add_header("sender", self->config_.name);
|
||||||
|
message.add_header("decree", std::to_string(decree.decree));
|
||||||
SendEW::send_message(message, legislators[receiver]);
|
SendEW::send_message(message, legislators[receiver]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Legislator::receive_voted(Message message)
|
||||||
|
{
|
||||||
|
std::string ballot_str = *message.get_header("ballot");
|
||||||
|
std::string sender = *message.get_header("sender");
|
||||||
|
std::string decree_str = *message.get_header("decree");
|
||||||
|
Decree decree;
|
||||||
|
decree.decree = std::stoi(decree_str);
|
||||||
|
|
||||||
|
log(config_.name + " has received Voted("
|
||||||
|
+ ballot_str
|
||||||
|
+ ") from " + sender, cyan);
|
||||||
|
|
||||||
|
receive_voted(std::stoi(ballot_str), decree, sender);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Legislator::receive_voted(int ballot, Decree decree, std::string voter)
|
||||||
|
{
|
||||||
|
if (ballot != ledger.last_tried())
|
||||||
|
return;
|
||||||
|
quorum_previous_votes.erase(voter);
|
||||||
|
if (quorum_previous_votes.size() == 0)
|
||||||
|
receive_enough_voted(ballot, decree);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Legislator::receive_enough_voted(int ballot, Decree decree)
|
||||||
|
{
|
||||||
|
log(config_.name + " has received enough Voted("
|
||||||
|
+ std::to_string(ballot)
|
||||||
|
+ "), saving decree: " + std::to_string(decree.decree), green);
|
||||||
|
ledger.set_decree(decree);
|
||||||
|
send_success(decree);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Legislator::send_success(Decree decree)
|
||||||
|
{
|
||||||
|
Message message;
|
||||||
|
message.set_method("Success");
|
||||||
|
message.add_header("decree", std::to_string(decree.decree));
|
||||||
|
|
||||||
|
for (auto legislator : legislators)
|
||||||
|
SendEW::send_message(message, legislator.second);
|
||||||
|
}
|
||||||
|
|
||||||
void Legislator::handle_message(Message message)
|
void Legislator::handle_message(Message message)
|
||||||
{
|
{
|
||||||
std::string method = message.get_method();
|
std::string method = message.get_method();
|
||||||
@ -203,5 +247,7 @@ namespace paxos
|
|||||||
receive_last_vote(message);
|
receive_last_vote(message);
|
||||||
else if (method == "BeginBallot")
|
else if (method == "BeginBallot")
|
||||||
receive_begin_ballot(message);
|
receive_begin_ballot(message);
|
||||||
|
else if (method == "Voted")
|
||||||
|
receive_voted(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,12 @@ namespace paxos
|
|||||||
void receive_begin_ballot(Message message);
|
void receive_begin_ballot(Message message);
|
||||||
void receive_begin_ballot(int ballot, int decree, std::string sender);
|
void receive_begin_ballot(int ballot, int decree, std::string sender);
|
||||||
|
|
||||||
void send_voted(int ballot, std::string receiver);
|
void send_voted(int ballot, Decree decree, std::string receiver);
|
||||||
|
void receive_voted(Message message);
|
||||||
|
void receive_voted(int ballot, Decree decree, std::string voter);
|
||||||
|
void receive_enough_voted(int ballot, Decree decree);
|
||||||
|
|
||||||
|
void send_success(Decree decree);
|
||||||
|
|
||||||
void handle_message(Message message);
|
void handle_message(Message message);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user