Advertisement
Guest User

Untitled

a guest
Sep 30th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.36 KB | None | 0 0
  1. package com.tnappo;
  2.  
  3. import java.io.FileInputStream;
  4. import java.net.InetSocketAddress;
  5. import java.net.Socket;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.text.DecimalFormat;
  12. import java.util.Properties;
  13. import java.util.concurrent.ExecutorService;
  14. import java.util.concurrent.Executors;
  15.  
  16. public class Check implements Runnable {
  17.  
  18.     static DecimalFormat oneDigit = new DecimalFormat("#.#");
  19.  
  20.     private static final ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
  21.  
  22.     public static void main(String[] args) {
  23.         try {
  24.             new Thread(new Check()).start();
  25.         } catch (Exception e) {
  26.             e.printStackTrace();
  27.         }
  28.     }
  29.  
  30.     public static double roundToOneDigit(double d) {
  31.         return Double.valueOf(oneDigit.format(d)).doubleValue();
  32.     }
  33.  
  34.     Connection conn = null;
  35.     PreparedStatement getAll;
  36.     PreparedStatement pingReport;
  37.  
  38.     boolean debug = false;
  39.  
  40.     public Check() throws Exception {
  41.         Properties defaultProps = new Properties();
  42.         FileInputStream in = new FileInputStream("sql.properties");
  43.         defaultProps.load(in);
  44.         in.close();
  45.  
  46.         String host = defaultProps.getProperty("host");
  47.         String user = defaultProps.getProperty("user");
  48.         String pass = defaultProps.getProperty("pass");
  49.         String db = defaultProps.getProperty("db");
  50.         debug = Boolean.valueOf(defaultProps.getProperty("debug")).booleanValue();
  51.  
  52.         String url = "jdbc:mysql://" + host + "/" + db;
  53.         Class.forName("com.mysql.jdbc.Driver").newInstance();
  54.         conn = DriverManager.getConnection(url, user, pass);
  55.  
  56.         getAll = conn.prepareStatement("SELECT * FROM servers");
  57.         pingReport = conn.prepareStatement("UPDATE servers SET rank = ?, status = ?,downtime = ?, totaltime = ?, uptime = ?,ping = ?, players = ?, playercount = ?, last_checked = NOW() WHERE id = ?;");
  58.     }
  59.  
  60.     @Override
  61.     public void run() {
  62.         long rounds = 0L;
  63.         while (true) {
  64.             long start = System.currentTimeMillis();
  65.             try {
  66.                 ResultSet rs = getAll.executeQuery();
  67.                 pingAndReport(rs);
  68.             } catch (SQLException e1) {
  69.                 e1.printStackTrace();
  70.             }
  71.  
  72.             long waitTime = 300000L - (System.currentTimeMillis() - start);
  73.  
  74.             System.out.println(++rounds + " rounds of pings done. Waiting for " + waitTime / 1000L + " seconds");
  75.             try {
  76.                 if (waitTime < 1000L) {
  77.                     waitTime = 1000L;
  78.                 }
  79.                 if (waitTime > 300000L) {
  80.                     waitTime = 300000L;
  81.                 }
  82.                 Thread.sleep(waitTime);
  83.             } catch (InterruptedException e) {
  84.                 e.printStackTrace();
  85.             }
  86.         }
  87.     }
  88.  
  89.     public void pingAndReport(final ResultSet rs) throws SQLException {
  90.         while (rs.next()) {
  91.             service.submit(new Runnable() {
  92.                 @Override
  93.                 public void run() {
  94.                     try {
  95.                         int index = 0;
  96.                         String ip = rs.getString("ip");
  97.                         int port = rs.getInt("port");
  98.  
  99.                         boolean online = true;
  100.  
  101.                         long pingstart = System.currentTimeMillis();
  102.                         try {
  103.                             Socket socket = new Socket();
  104.                             socket.setSoTimeout(1000);
  105.                             socket.connect(new InetSocketAddress(ip, port));
  106.                             socket.close();
  107.                         } catch (Exception e) {
  108.                             online = false;
  109.                         }
  110.  
  111.                         long ping = (System.currentTimeMillis() - pingstart) / 2L;
  112.  
  113.                         int id = rs.getInt("id");
  114.  
  115.                         int newDowntime = rs.getInt("downtime") + (online ? 0 : 5);
  116.                         double newTotaltime = rs.getInt("totaltime") + 5;
  117.                         double uptime = newTotaltime - newDowntime;
  118.  
  119.                         double uptimePercent = roundToOneDigit(uptime / newTotaltime * 100.0D);
  120.  
  121.                         int rating = rs.getInt("rating");
  122.  
  123.                         int newRank = (int) Math.round(uptimePercent * (0.25D * (uptime / 1440.0D) + 1.5D * rating));
  124.  
  125.                         pingReport.setInt(1, newRank);
  126.                         pingReport.setInt(2, online ? 1 : 0);
  127.                         pingReport.setInt(3, newDowntime);
  128.                         pingReport.setInt(4, (int)newTotaltime);
  129.                         pingReport.setInt(5, (int)uptimePercent);
  130.                         pingReport.setInt(6, (int)ping);
  131.                         pingReport.setString(7, "");
  132.                         pingReport.setInt(8, 0);
  133.                         pingReport.setInt(9, id);
  134.  
  135.                         pingReport.executeUpdate();
  136.  
  137.                         if (debug) {
  138.                             System.out.println(ip + ":" + port + " checked (online=" + online + ")");
  139.                         } else if ((index % 100 == 0) && (index > 0)) {
  140.                             System.out.println(index + " servers checked");
  141.                         }
  142.  
  143.                         index++;
  144.                     } catch (Exception e) {
  145.                         e.printStackTrace();
  146.                     }
  147.                 }
  148.             });
  149.         }
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement