public void LeaderElection() { Console.WriteLine(ElectionTime); while (true) { if (State == NodeState.Leader) { Console.Write($"\rI AM LEADER - {DateTime.Now.ToString()}"); // new System.Threading.ManualResetEvent(false).WaitOne(50); UDPServer.SendSignal(ServerActions.KeepFollower, FileConnector.GetAllMessagesAsText()); UDPServer.SendSignal(ServerActions.GetLeader, Helper.ServerIP, Helper.ServerPort); // continue; } new System.Threading.ManualResetEvent(false).WaitOne(ElectionTime); switch (State) { case NodeState.Follower: if (!Node.KeepFollower) { Console.Write($"\rI WANT TO BE LEADER {DateTime.Now.ToString()}"); UDPServer.SendSignal(ServerActions.VoteForLeader); VoteCount++; State = NodeState.Leader; continue; } // Console.Write($"\rIM Follower {DateTime.Now.ToString()}"); break; case NodeState.Candidate: break; case NodeState.Leader: break; } KeepFollower = false; } // while(true) // { // if(Node.Nodes.Count != 0) // { // // if(Node.Ping) // { // foreach(var node in Node.Nodes) // { // UDPServer.SendElection(); // Console.WriteLine("IM LEADER"); // } // } // } // Node.Ping = true; // } }
public void ListenUDP() { while (true) { IPEndPoint RemoteIpEndPoint = new IPEndPoint( IPAddress.Any, Helper.UdpPort ); Byte[] receiveBytes = udpServer.Receive(ref RemoteIpEndPoint); string returnData = Encoding.ASCII.GetString(receiveBytes); JObject data = new JObject(); data = JObject.Parse(returnData); // Console.WriteLine(data); Enum.TryParse(data["action"].ToString(), out ServerActions action); switch (action) { case ServerActions.Ping: Node.KeepFollower = true; break; case ServerActions.GetClients: var clients = data["clients"]; lock (Node.Nodes) { foreach (var client in clients) { if (Node.Nodes.FirstOrDefault(x => x.IP == client["IP"].ToString() && x.Port == int.Parse(client["Port"].ToString())) == null) { Node.Nodes.Add( new NodeModel() { IP = client["IP"].ToString(), Port = int.Parse(client["Port"].ToString()) } ); } } Node.Nodes.RemoveAll(x => x.IP == Helper.GetLocalIPAddress() && x.Port == ((IPEndPoint)udpServer.Client.LocalEndPoint).Port); } break; case ServerActions.Election: Console.WriteLine("ping"); Node.KeepFollower = true; break; case ServerActions.VoteForLeader: lock (Node.KeepFollower) { if (Node.State == NodeState.Follower) { Node.KeepFollower = true; SendSignal(ServerActions.Vote, RemoteIpEndPoint.Address.ToString(), RemoteIpEndPoint.Port); } } break; case ServerActions.Vote: Console.WriteLine("I VOTE FOR LEADER"); Node.VoteCount++; break; case ServerActions.KeepFollower: Console.Write($"\rI GOT HEARTBEAT FROM LEADER {DateTime.Now.ToString()}"); Node.KeepFollower = true; Node.State = NodeState.Follower; var dataFromLeader = data["data"].ToString(); if (dataFromLeader != string.Empty) { // JObject deserializedData = new JObject(); // deserializedData = JObject.Parse(dataFromLeader); // var currentDataFromFile = FileConnector.GetAllMessagesAsText(); FileConnector.ClearFile(); fileConnector.WriteDataToSource(dataFromLeader); // if (deserializedData["id"].ToString() != FileConnector.LastID) // { // FileConnector.LastID = deserializedData["id"].ToString(); // fileConnector.WriteDataToSource('\n'+dataFromLeader+'\n'); // } } break; case ServerActions.GetFromLeader: var output = new FileConnector().GetDataFromSource(); SendData(output, Helper.ServerIP, Helper.ServerPort); break; case ServerActions.SendToLeader: var dataToWrite = data["data"].ToString(); JObject deserializedDataFromLeader = new JObject(); deserializedDataFromLeader = JObject.Parse(dataToWrite); var currentData = FileConnector.GetAllMessagesAsText(); FileConnector.ClearFile(); new FileConnector().WriteDataToSource(currentData + deserializedDataFromLeader.ToString()); // FileConnector.LastID = deserializedDataFromLeader["id"].ToString(); // new FileConnector().WriteDataToSource('\n' + dataToWrite.ToString() + '\n'); break; case ServerActions.GetDataFromLeader: SendDataToClient(FileConnector.GetAllMessagesAsText(), data["data"]["address"]["Ip"].ToString() , int.Parse(data["data"]["address"]["Port"].ToString())); break; } } }