ruusey (03-08-2019)
Haven't been on MPGH for a while, it's nice to see people contributing.
As a challenge, I wanted to rewrite this program to be even faster, but it only resulted in messier code :
I ended up reading your code and finding that it is pretty elegant. The only thing that bothered me was the weird spelling and that it wasn't sorting the results.
Changes :
- Fixed minor Java issues
- Added sorting
Here are some changes I invite you to see
Feel free to useCode:package com.main; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; import java.net.InetAddress; import java.util.*; public class Ping { private static ArrayList<String> regions = new ArrayList<>(); private static ArrayList<String> sNames = new ArrayList<>(); public static void main(String[] args) { System.out.println("RotMG Ping Finder by Ruusey"); long totalTime = 0L; try { Document doc = null; totalTime = System.currentTimeMillis(); doc = Jsoup.connect("https://realmofthemadgodhrd.appspo*****m/char/list").get(); if (doc.getAllElements().text().contains("Error")) { System.err.println("Error : '" + doc.getElementsByTag("Error").text() + "'."); } Elements ips = doc.getElementsByTag("DNS"); Elements names = doc.getElementsByTag("Name"); names.remove(0); for (int i = 0; i < ips.size(); i++) { String ip = ips.get(i).text(); String name = names.get(i).text(); sNames.add(name); InetAddress host = InetAddress.getByName(ip); String hostDns = host.getHostName(); int startIdx = hostDns.indexOf(".") + 1; String region = hostDns.substring(startIdx, hostDns.indexOf(".", startIdx + 1)); if (hostDns.contains("compute-1")) { regions.add("us-west-1"); } else { regions.add(region); } } HashMap<String, Long> serverAndTime = new HashMap<>(); for (int i = 0; i < regions.size(); i++) { try { Jsoup.connect("https://ec2." + regions.get(i) + ".amazonaws.com/ping").get(); long end = getPingTime(regions.get(i)); System.out.println(getPercentage(i + 1, regions.size()) + "% completed."); serverAndTime.put(sNames.get(i), end); } catch (Exception localException) { localException.printStackTrace(); } } serverAndTime = sortByValue(serverAndTime); for (String s : serverAndTime.keySet()) { System.out.println("Your ping to server " + s + " is " + serverAndTime.get(s) + "ms."); } System.out.println("Total runtime is " + (System.currentTimeMillis() - totalTime) + "ms."); } catch (IOException e) { e.printStackTrace(); } } public static long getPingTime(String addr) { long start; start = System.currentTimeMillis(); try { Jsoup.connect("https://ec2." + addr + ".amazonaws.com/ping").get(); } catch (IOException e) { e.printStackTrace(); } long end = System.currentTimeMillis() - start; return end; } public static int getPercentage(double current, double max) { if (max == 0) { return 100; } return (int) (current / max * 100); } // function to sort hashmap by values public static HashMap<String, Long> sortByValue(HashMap<String, Long> hm) { // Create a list from elements of HashMap List<Map.Entry<String, Long>> list = new LinkedList<>(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<>() { public int compare(Map.Entry<String, Long> o1, Map.Entry<String, Long> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<String, Long> temp = new LinkedHashMap<>(); for (Map.Entry<String, Long> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } }
Last edited by Alde.; 03-07-2019 at 01:32 AM.
Alde is Alde is
ruusey (03-08-2019)
ruusey (03-25-2019)
This really helped me out a lot. Thanks!!
Pretty good release, but is this even like, valuable? Shouldnt you be able to get a general idea of your connection just based off of location?
Its a better ping checker, a general idea = normal
Numbers = better
there's where the value is
One day... machines will rule the world.
Thanks for the ping checker, works as intended!
Also inspired me to do one myself!
I have re-done a ping checker myself too since i wanted to be even faster (and a GUI). I achieved a total time needed to ping all 23 servers of about 400ms, minimum i have seen was 247ms and maximum about 4 seconds. This is possible due to Multi-Threading.
C#Code:public void PingAllServer() { pings = 0; timerClearError.Start(); //will stop the current pings and start over again after 10 secs. Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); //Starting to count the total ping time needed foreach (ServerInfo server in servers) { Thread pingThread = new Thread(delegat => PingThreadInfo(server)); //Creating a new Thread with a ServerInfo obj (ServerInfo = an object that holds some data, IP, Name, ping) pingThread.IsBackground = true; pingThread.Start(); //Start that Thread } while (pings < servers.Count) //Wait for all Threads to finish { Application.DoEvents(); //Ensures to not block the GUI Thread } stopwatch.Stop(); //Stops the total time needed to ping AddInfoToUI(); //Just sorts and adds the values to the GUI lTotalTime.Text = stopwatch.ElapsedMilliseconds + " m/s"; timerClearError.Stop(); } private void PingThreadInfo(ServerInfo server) { PingServerInfo(server); //Pings the server pings++; //Count up to let the waiting Thread know one more finished Thread.CurrentThread.Abort(); //Ensures to abort this Thread, as Threads may not close properly automaticly, even like this it can fail tho) } private void PingServerInfo(ServerInfo server) { try //Try and catch just to be sure { System.Net.Sockets.TcpClient c = new System.Net.Sockets.TcpClient(); //Creating a new TCPClient socket Stopwatch watch = new Stopwatch(); watch.Start(); //Starting the stopwatch c.Connect(server.ip, pingPort); //Try to connect to the server, wich will accept the request and the client will get an answer watch.Stop(); //Stop the stopwatch server.lastPing = watch.ElapsedMilliseconds; //Give the server Object the ping time } catch (Exception) //Don't need to handle the catch, the Server will just be displayed as -1ms { } }
Last edited by Maik8; 04-17-2019 at 03:49 PM.
Alde. (10-14-2019)