代码拉取完成,页面将自动刷新
#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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。