public void StartElection() { m_oks = new Dictionary <uint, bool>(); foreach (var node in m_configuration.Nodes) { m_oks[node.NodeID] = false; } m_oks[m_configuration.NodeID] = true; Messages.ElectionBroadcast msg = new Messages.ElectionBroadcast(m_configuration.NodeID); SendBroadcast(msg); m_electionOkTimeout.Start(); }
private void QueryMessage() { foreach (var item in m_protocol.MessageQueue.GetConsumingEnumerable()) { var json = (JObject)JsonConvert.DeserializeObject(item.Msg); var type = json["type"].ToString(); if (type == "electionOK") { Messages.ElectionOk electionOk = JsonConvert.DeserializeObject <Messages.ElectionOk>(item.Msg); m_election.ElectionOk(electionOk); } else if (type == "electionBroadcast") { Messages.ElectionBroadcast electionBroadcast = JsonConvert.DeserializeObject <Messages.ElectionBroadcast>(item.Msg); m_election.ElectionBroadcast(electionBroadcast); m_elected = false; } else if (type == "electBroadcast") { Messages.ElectBroadcast electBroadcast = JsonConvert.DeserializeObject <Messages.ElectBroadcast>(item.Msg); m_election.ElectBroadcast(electBroadcast); } else if (m_elected) { if (m_elected && type == "request") { Messages.Request request = JsonConvert.DeserializeObject <Messages.Request>(item.Msg); m_suzuki.RequestMessage(request); } else if (m_elected && type == "token") { Messages.TokenMessage token = JsonConvert.DeserializeObject <Messages.TokenMessage>(item.Msg); m_suzuki.TokenMessage(token); } } else { LogMessage(this, "Ignored " + type + " Waiting for election."); } } }
public void ElectionBroadcast(Messages.ElectionBroadcast election) { if (election.senderId < m_configuration.NodeID) { if (m_oks == null) { StartElection(); LogMessage(this, "Election broadcast from [" + election.senderId + "]. Broadcasting number."); } else { LogMessage(this, "Election broadcast from [" + election.senderId + "]. Broadcast already sent."); } } else if (election.senderId > m_configuration.NodeID) { if (!NoElectionResponse) { Messages.ElectionOk ok = new Messages.ElectionOk(m_configuration.NodeID); var senderDesc = m_configuration.FindNode(election.senderId); Send(ok, senderDesc.Port, senderDesc.NodeIP); LogMessage(this, "Election broadcast from [" + election.senderId + "]. Sending OK."); } else { LogMessage(this, "ElectionOk to: [" + election.senderId + "] intentionally not send. Check Menu -> Debug -> No election response"); } // Don't take part in election anymore. m_electionOkTimeout.Stop(); } else { LogMessage(this, "Duplicate node with the same id in network"); } }