谁在没有领导者的点对点网络中发起领导者选举?这是我在接收消息时实现的一些代码。
if (message.startsWith(ELECTION)) {
int recievedID = Integer.parseInt(ripMessage(ELECTION, message));
if (processID > recievedID) {
sendToAll(ELECTION + processID);
} else if (processID < recievedID) {
sendToAll(ACKNOWLEDGE);
}
} else if (message.startsWith(ACKNOWLEDGE)) {
ackRecv++;
if (connections.size() == ackRecv) {
connection.sendMessage(LEADER + processID);
this.isLeader = true;
this.leaderID = processID;
ackRecv = 0;
}
} else if (message.startsWith(LEADER)) {
int recievedID = Integer.parseInt(ripMessage(LEADER, message));
this.leaderID = recievedID;
this.isLeader = false;
}