Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 4.1 kB view raw
1package com.legacyminecraft.poseidon; 2 3import net.minecraft.server.MinecraftServer; 4import org.bukkit.craftbukkit.CraftServer; 5import org.json.simple.JSONObject; 6 7import java.io.InputStreamReader; 8import java.io.OutputStream; 9import java.net.HttpURLConnection; 10import java.net.URL; 11import java.util.Random; 12 13public class PoseidonStatisticsAgent { 14 //Default details 15 private int protocolID = 1; 16 public final String postTo = "https://poseidon.johnymuffin.com/statistics.php"; 17 //Unique Details 18 private String uniqueID; 19 private final String sessionID; 20 private final String version; 21 private final String branch; 22 private final Long startTime; 23 private Object syncLock = new Object(); 24 25 public PoseidonStatisticsAgent(MinecraftServer server, CraftServer craftServer) { 26 //This really shouldn't be needed, but it runs once, whats the harm? 27 synchronized (syncLock) { 28 this.startTime = (System.currentTimeMillis() / 1000L); 29 this.uniqueID = PoseidonConfig.getInstance().getString("settings.statistics.key"); 30 //Create temp value 31 Random rnd = new Random(); 32 this.sessionID = String.valueOf(100000 + rnd.nextInt(900000)); 33 this.version = craftServer.getPoseidonVersion(); 34 this.branch = craftServer.getPoseidonReleaseType(); 35 } 36 37 PoseidonStatisticsSender poseidonStatisticsSender = new PoseidonStatisticsSender(); 38 poseidonStatisticsSender.start(); 39 40 } 41 42 public JSONObject getPing() { 43 synchronized (syncLock) { 44 JSONObject ping = new JSONObject(); 45 ping.put("protocol", protocolID); 46 ping.put("uniqueID", uniqueID); 47 ping.put("sessionID", sessionID); 48 ping.put("version", version); 49 ping.put("branch", branch); 50 int uptime = (int) ((System.currentTimeMillis() / 1000L) - startTime); 51 ping.put("uptime", uptime); 52 return ping; 53 } 54 } 55 56 public class PoseidonStatisticsSender extends Thread { 57 public volatile boolean errored = false; 58 59 public void run() { 60 while (true && !this.isInterrupted()) { 61 HttpURLConnection connection = null; 62 try { 63 System.out.println("Submitting Project Poseidon Statistics."); 64 final JSONObject ping = getPing(); 65 URL url = new URL(postTo); 66 //Create Connection 67 connection = (HttpURLConnection) url.openConnection(); 68 connection.setRequestMethod("POST"); 69 connection.setRequestProperty("Content-Type", "application/json"); 70 connection.setUseCaches(false); 71 connection.setDoInput(true); 72 connection.setDoOutput(true); 73 //Write Body 74 OutputStream stream = connection.getOutputStream(); 75 stream.write(ping.toJSONString().getBytes()); 76 stream.flush(); 77 stream.close(); 78 //Get Response 79 String response = String.valueOf(new InputStreamReader(connection.getInputStream())); 80 connection.disconnect(); 81 errored = false; 82 } catch (Exception exception) { 83 if (!errored) { 84 System.out.println("Failed to submit statistics for Project Poseidon. " + exception + " : " + exception.getMessage() + "."); 85 } 86 errored = true; 87 } finally { 88 if (connection != null) { 89 connection.disconnect(); 90 connection = null; 91 } 92 try { 93 Thread.sleep(300000L); 94 } catch (InterruptedException exception) { 95 System.out.println("Project Poseidon statistics thread has been closed."); 96 break; 97 } 98 } 99 } 100 } 101 102 } 103 104}