1 Star 0 Fork 0

GaussianPrince/paxos cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
learner.h 2.08 KB
一键复制 编辑 原始数据 按行查看 历史
#ifndef __LEARNER_H__
#define __LEARNER_H__
#include "message.h"
#include "network.h"
class Learner {
private:
int id;
std::unordered_map<int, Message> acceptedMsgs;
std::shared_ptr<Network> net;
public:
Learner(const int id, const std::shared_ptr<Network> &net,
const std::initializer_list<int> &list)
: id(id), net(net) {
for (auto ele : list) {
acceptedMsgs[ele] = Message{};
}
}
std::string run() {
for (;;) {
std::vector<Message> msgVec;
int ret = net->recvTcpMsg(msgVec);
if (ret <= 0) {
continue;
}
for (auto &msg : msgVec) {
std::cout << "Learner recv Accept " << msg << "\n";
handleRecevAccept(msg);
if (chosen(msg) == nullptr) {
std::cout << "chosen null" << std::endl;
continue;
}
return msg.val;
}
}
}
void handleRecevAccept(Message &acceptMsg) {
if (acceptedMsgs.find(acceptMsg.from) == acceptedMsgs.end()) {
std::cout << "error leaner has no such neighbor " << acceptMsg.from
<< std::endl;
return;
}
Message &hasAcceptedMsg = acceptedMsgs[acceptMsg.from];
if (hasAcceptedMsg.seq < acceptMsg.seq) {
acceptedMsgs[acceptMsg.from] = acceptMsg;
}
}
Message *chosen(Message &msg) {
std::unordered_map<int, int> acceptCount;
std::unordered_map<int, Message> acceptMsgMap;
for (auto &acceptedMsg : acceptedMsgs) {
int proposalNum = acceptedMsg.second.seq;
acceptCount[proposalNum]++;
acceptMsgMap[proposalNum] = acceptedMsg.second;
}
for (auto &acceptedMsg : acceptMsgMap) {
if (acceptCount[acceptedMsg.first] > majority()) {
msg = acceptedMsg.second;
return &msg;
}
}
return nullptr;
}
int majority() { return acceptedMsgs.size() / 2 + 1; }
};
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/GaussianPrince/paxos-cpp.git
git@gitee.com:GaussianPrince/paxos-cpp.git
GaussianPrince
paxos-cpp
paxos cpp
master

搜索帮助