From 4d557cb392347c354263631d280384e749b46fd8 Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Sun, 15 Mar 2015 19:28:58 -0700 Subject: [PATCH] Code to improve stat recording, commented out for now. --- .../bungee/playerStats/PlayerStats.java | 30 ++++- .../playerStats/PlayerStatsRepository.java | 40 +++++- .../src/mineplex/bungee/IPGeoData.java | 16 +++ .../src/mineplex/bungee/api/HttpCallBase.java | 122 ++++++++++++++++++ 4 files changed, 205 insertions(+), 3 deletions(-) create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/IPGeoData.java create mode 100644 Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/HttpCallBase.java diff --git a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerStats/PlayerStats.java b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerStats/PlayerStats.java index f91366095..6d09c9d21 100644 --- a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerStats/PlayerStats.java +++ b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerStats/PlayerStats.java @@ -1,5 +1,7 @@ package mineplex.bungee.playerStats; +import java.util.HashMap; + import net.md_5.bungee.api.event.PostLoginEvent; import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Plugin; @@ -7,8 +9,12 @@ import net.md_5.bungee.event.EventHandler; public class PlayerStats implements Listener { + private static Object _cacheLock = new Object(); + private Plugin _plugin; - private PlayerStatsRepository _repository; + private PlayerStatsRepository _repository; + + private HashMap _ipCache = new HashMap(); public PlayerStats(Plugin plugin) { @@ -29,7 +35,27 @@ public class PlayerStats implements Listener { _repository.addPlayer(event.getPlayer().getName()); _repository.addPlayerVersion(event.getPlayer().getName(), event.getPlayer().getPendingConnection().getVersion()); - _repository.addPlayerIP(event.getPlayer().getName(), event.getPlayer().getPendingConnection().getAddress().getAddress().getHostAddress()); + + String address = event.getPlayer().getPendingConnection().getAddress().getAddress().getHostAddress(); + /* + boolean addNewIp = false; + int addressId = 0; + + synchronized (_cacheLock) + { + if (_ipCache.containsKey(address)) + addressId = _ipCache.get(address); + else + addNewIp = true; + } + + if (addNewIp) + addressId = _repository.addNewIP(address); + + _repository.addPlayerIP(event.getPlayer().getName(), addressId); + */ + + _repository.addPlayerIP(event.getPlayer().getName(), address); } }); } diff --git a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerStats/PlayerStatsRepository.java b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerStats/PlayerStatsRepository.java index b1a18dcd2..c4feab3a3 100644 --- a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerStats/PlayerStatsRepository.java +++ b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerStats/PlayerStatsRepository.java @@ -1,6 +1,5 @@ package mineplex.bungee.playerStats; -import java.net.InetAddress; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -23,6 +22,8 @@ public class PlayerStatsRepository private static String CREATE_IP_TABLE = "CREATE TABLE IF NOT EXISTS PlayerIP (id INT NOT NULL AUTO_INCREMENT, playerName VARCHAR(20), ip VARCHAR(20), PRIMARY KEY (id), UNIQUE INDEX unique_player_ip (playerName, ip));"; private static String INSERT_IP_PLAYER = "INSERT IGNORE INTO PlayerIP (playerName, ip) values(?, ?);"; + //private static String INSERT_IP_PLAYER = "INSERT IGNORE INTO playerIp (playerName, ip) values(?, ?);"; + //private static String INSERT_IP_PLAYER = "INSERT IGNORE INTO playerIp (playerName, ip) values(?, ?);"; public void initialize() { @@ -195,4 +196,41 @@ public class PlayerStatsRepository } } } + + public int addNewIP(String address) + { + PreparedStatement preparedStatement = null; + + try + { + if (_connection == null || _connection.isClosed()) + _connection = DriverManager.getConnection(_connectionString, _userName, _password); + + preparedStatement = _connection.prepareStatement(INSERT_IP_PLAYER, Statement.RETURN_GENERATED_KEYS); + + preparedStatement.setString(1, address); + + preparedStatement.executeUpdate(); + } + catch (Exception exception) + { + exception.printStackTrace(); + } + finally + { + if (preparedStatement != null) + { + try + { + preparedStatement.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + } + + return -1; + } } diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/IPGeoData.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/IPGeoData.java new file mode 100644 index 000000000..c8265c0a2 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/IPGeoData.java @@ -0,0 +1,16 @@ +package mineplex.bungee; + +public class IPGeoData +{ + public String ip; + public String country_code; + public String country_name; + public String region_code; + public String region_name; + public String city; + public int zip_code; + public String time_zone; + public double latitude; + public double longitude; + public int metro_code; +} diff --git a/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/HttpCallBase.java b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/HttpCallBase.java new file mode 100644 index 000000000..d4e7b83b0 --- /dev/null +++ b/Plugins/Mineplex.BungeeRotator/src/mineplex/bungee/api/HttpCallBase.java @@ -0,0 +1,122 @@ +package mineplex.bungee.api; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.Type; + +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.conn.scheme.PlainSocketFactory; +import org.apache.http.conn.scheme.Scheme; +import org.apache.http.conn.scheme.SchemeRegistry; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.conn.PoolingClientConnectionManager; + +import com.google.gson.Gson; + +public class HttpCallBase +{ + private String _url; + + public HttpCallBase(String url) + { + _url = url; + } + + public T Execute(Type returnType) + { + HttpGet request = new HttpGet(_url); + + String response = execute(request); + System.out.println(response); + return new Gson().fromJson(response, returnType); + } + + protected String execute(HttpRequestBase request) + { + SchemeRegistry schemeRegistry = new SchemeRegistry(); + schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); + + PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry); + connectionManager.setMaxTotal(200); + connectionManager.setDefaultMaxPerRoute(20); + + HttpClient httpClient = new DefaultHttpClient(connectionManager); + InputStream in = null; + String response = ""; + + try + { + request.addHeader("Content-Type", "application/json"); + HttpResponse httpResponse = httpClient.execute(request); + + if (httpResponse != null) + { + in = httpResponse.getEntity().getContent(); + response = convertStreamToString(in); + } + } + catch (Exception ex) + { + System.out.println("HttpCall Error:\n" + ex.getMessage()); + + for (StackTraceElement trace : ex.getStackTrace()) + { + System.out.println(trace); + } + } + finally + { + httpClient.getConnectionManager().shutdown(); + + if (in != null) + { + try + { + in.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + } + + return response; + } + + protected String convertStreamToString(InputStream is) + { + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + StringBuilder sb = new StringBuilder(); + + String line = null; + try + { + while ((line = reader.readLine()) != null) + { + sb.append(line + "\n"); + } + } + catch (IOException e) + { + e.printStackTrace(); + } + finally + { + try + { + is.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + return sb.toString(); + } +}