private void PotentialServerIPAnalysis(IPAddress ip) { if (!bw.CancellationPending) { RestClientResponse answer = RESTConnectToServer.Get(ip.ToString(), ActionsEnum.CheckServer); Log.Instance.W(this, ip.ToString() + " - " + answer.AnswerStatus); if (!string.IsNullOrEmpty(answer.AnswerStatus) && answer.AnswerStatus == "OK".ToUpper()) { var NewServer = new LeaderServer() { ServerDNSName = answer.AnswerContent.Substring(0, 10), ServerIP = ip.ToString(), VisibleName = answer.AnswerContent.Substring(0, 10) }; ServerList.Add( NewServer ); CheckedServersList.Add(ip.ToString()); if (ServerListUpdated != null) { ServerListUpdated(ip.ToString()); } } } else { Log.Instance.W(this, "REST is Canceled"); } }
public static LeaderServer ReturnLeaderInfo(string ip) { //Task.Delay(5); RestClientResponse answer = RESTConnectToServer.Get(ip, ActionsEnum.CheckServer); if (!string.IsNullOrEmpty(answer.AnswerStatus) && answer.AnswerStatus == "OK".ToUpper()) { LeaderServer foundLeader = Newtonsoft.Json.JsonConvert.DeserializeObject <LeaderServer>(answer.AnswerContent); return(null); } else { return(null); } }
public void ScanIPs() { List <IPAddress> ips = new List <IPAddress>(); var interfaces = NetworkInterface.GetAllNetworkInterfaces().ToList(); foreach (var networkInterface in interfaces) { var addresses = networkInterface.GetIPProperties().UnicastAddresses; foreach (var address in addresses) { if (address.Address.AddressFamily == AddressFamily.InterNetwork) { var mask = address.IPv4Mask; if (mask.Equals( new IPAddress(new byte[] { 255, 255, 255, 0 }) )) { byte[] byteIP = address.Address.GetAddressBytes(); for (byte i = 1; i < 255; i++) { var ip = new IPAddress(new byte[] { byteIP[0], byteIP[1], byteIP[2], i } ); ips.Add(ip); } } } } } PotentialServersList.AddRange(ips.ConvertAll(input => input.ToString())); ips.AsParallel() .WithExecutionMode(ParallelExecutionMode.ForceParallelism) .WithMergeOptions(ParallelMergeOptions.NotBuffered) //.WithDegreeOfParallelism(20) .ForAll(x => { if (!bw.CancellationPending) { var j = PingHost(x.ToString()); Log.Instance.W(this, x.ToString() + " + " + j.ToString()); if (!bw.CancellationPending) { if (j) { RestClientResponse answer = RESTConnectToServer.Get(x.ToString(), ActionsEnum.CheckServer); if (!string.IsNullOrEmpty(answer.AnswerStatus) && answer.AnswerStatus == "OK".ToUpper()) { LeaderServer foundLeader = Newtonsoft.Json.JsonConvert.DeserializeObject <LeaderServer>( answer.AnswerContent); if (!ServerList.Any(y => y.VisibleName == foundLeader.VisibleName)) { ServerList.Add( foundLeader ); if (ServerListUpdated != null) { ServerListUpdated(foundLeader.ServerDNSName); } } CheckedServersList.Add(x.ToString()); } } } } ProcessedServerUpdate(1); }); }
private async void PotentialServerAnalysis(string ServerDNS) { var j = PingHost(ServerDNS); Log.Instance.W(this, ServerDNS + " + " + j); if (j) { //Checking for Server get answer if (!CheckedServersList.Contains(ServerDNS) /*&&!PotentialServersList.Contains(ServerDNS*/) { if (!bw.CancellationPending) { await Task.Run(() => { //Task.Delay(5); RestClientResponse answer = RESTConnectToServer.Get(ServerDNS, ActionsEnum.CheckServer); if (!string.IsNullOrEmpty(answer.AnswerStatus) && answer.AnswerStatus == "OK".ToUpper()) { LeaderServer foundLeader = Newtonsoft.Json.JsonConvert.DeserializeObject <LeaderServer>(answer.AnswerContent); string ip = ResolveIP(foundLeader.ServerDNSName)[0]; if (!ServerList.Any(x => x.VisibleName == foundLeader.VisibleName)) { ServerList.Add( foundLeader ); Log.Instance.W(this, $" + server added {ServerDNS} - {ip}"); if (ServerListUpdated != null) { ServerListUpdated(foundLeader.ServerDNSName); } } CheckedServersList.Add(foundLeader.ServerDNSName); } else { try { CheckedServersList.Add(ServerDNS); } catch (Exception e) { Console.WriteLine(e); Log.Instance.W(this, $" Error occured when server suppose to be added: {e.Message}"); } //TODO DELETE Log.Instance.W(this, $" & not answering {ServerDNS}"); } }); } } //TODO DELETE else { Log.Instance.W(this, $" @ already checked {ServerDNS}"); } ProcessedServerUpdate(1); } }