Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tnappo;
- import java.io.FileInputStream;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.text.DecimalFormat;
- import java.util.Properties;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class Check implements Runnable {
- static DecimalFormat oneDigit = new DecimalFormat("#.#");
- private static final ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
- public static void main(String[] args) {
- try {
- new Thread(new Check()).start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static double roundToOneDigit(double d) {
- return Double.valueOf(oneDigit.format(d)).doubleValue();
- }
- Connection conn = null;
- PreparedStatement getAll;
- PreparedStatement pingReport;
- boolean debug = false;
- public Check() throws Exception {
- Properties defaultProps = new Properties();
- FileInputStream in = new FileInputStream("sql.properties");
- defaultProps.load(in);
- in.close();
- String host = defaultProps.getProperty("host");
- String user = defaultProps.getProperty("user");
- String pass = defaultProps.getProperty("pass");
- String db = defaultProps.getProperty("db");
- debug = Boolean.valueOf(defaultProps.getProperty("debug")).booleanValue();
- String url = "jdbc:mysql://" + host + "/" + db;
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn = DriverManager.getConnection(url, user, pass);
- getAll = conn.prepareStatement("SELECT * FROM servers");
- pingReport = conn.prepareStatement("UPDATE servers SET rank = ?, status = ?,downtime = ?, totaltime = ?, uptime = ?,ping = ?, players = ?, playercount = ?, last_checked = NOW() WHERE id = ?;");
- }
- @Override
- public void run() {
- long rounds = 0L;
- while (true) {
- long start = System.currentTimeMillis();
- try {
- ResultSet rs = getAll.executeQuery();
- pingAndReport(rs);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- long waitTime = 300000L - (System.currentTimeMillis() - start);
- System.out.println(++rounds + " rounds of pings done. Waiting for " + waitTime / 1000L + " seconds");
- try {
- if (waitTime < 1000L) {
- waitTime = 1000L;
- }
- if (waitTime > 300000L) {
- waitTime = 300000L;
- }
- Thread.sleep(waitTime);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public void pingAndReport(final ResultSet rs) throws SQLException {
- while (rs.next()) {
- service.submit(new Runnable() {
- @Override
- public void run() {
- try {
- int index = 0;
- String ip = rs.getString("ip");
- int port = rs.getInt("port");
- boolean online = true;
- long pingstart = System.currentTimeMillis();
- try {
- Socket socket = new Socket();
- socket.setSoTimeout(1000);
- socket.connect(new InetSocketAddress(ip, port));
- socket.close();
- } catch (Exception e) {
- online = false;
- }
- long ping = (System.currentTimeMillis() - pingstart) / 2L;
- int id = rs.getInt("id");
- int newDowntime = rs.getInt("downtime") + (online ? 0 : 5);
- double newTotaltime = rs.getInt("totaltime") + 5;
- double uptime = newTotaltime - newDowntime;
- double uptimePercent = roundToOneDigit(uptime / newTotaltime * 100.0D);
- int rating = rs.getInt("rating");
- int newRank = (int) Math.round(uptimePercent * (0.25D * (uptime / 1440.0D) + 1.5D * rating));
- pingReport.setInt(1, newRank);
- pingReport.setInt(2, online ? 1 : 0);
- pingReport.setInt(3, newDowntime);
- pingReport.setInt(4, (int)newTotaltime);
- pingReport.setInt(5, (int)uptimePercent);
- pingReport.setInt(6, (int)ping);
- pingReport.setString(7, "");
- pingReport.setInt(8, 0);
- pingReport.setInt(9, id);
- pingReport.executeUpdate();
- if (debug) {
- System.out.println(ip + ":" + port + " checked (online=" + online + ")");
- } else if ((index % 100 == 0) && (index > 0)) {
- System.out.println(index + " servers checked");
- }
- index++;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement