Make SubData Reconnections Async

This commit is contained in:
ME1312 2017-04-09 23:39:22 -04:00
parent 34a947e5ea
commit 98187137c4
20 changed files with 174 additions and 116 deletions

2
.gitignore vendored
View File

@ -27,7 +27,9 @@ crashlytics-build.properties
# Hide Unfinished Project Files
/Artifacts/SubServers.Test.jar
/Artifacts/SubServers.Web.jar
/SubServers.Test/
/SubServers.Web/
# Hide Others
/build.ant

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -243,7 +243,7 @@ extends java.lang.Object</pre>
<dl>
<dt><span class="paramLabel">Parameters:</span></dt>
<dd><code>plugin</code> - SubPlugin</dd>
<dd><code>address</code> - Bind Address</dd>
<dd><code>address</code> - Address</dd>
<dd><code>port</code> - Port</dd>
<dt><span class="throwsLabel">Throws:</span></dt>
<dd><code>java.io.IOException</code></dd>

View File

@ -242,8 +242,8 @@ extends java.lang.Object</pre>
<div class="block">SubServers Client Instance</div>
<dl>
<dt><span class="paramLabel">Parameters:</span></dt>
<dd><code>host</code> - SubPlugin</dd>
<dd><code>address</code> - Bind Address</dd>
<dd><code>host</code> - SubServers.Host</dd>
<dd><code>address</code> - Address</dd>
<dd><code>port</code> - Port</dd>
<dt><span class="throwsLabel">Throws:</span></dt>
<dd><code>java.io.IOException</code></dd>

View File

@ -1,5 +1,5 @@
Manifest-Version: 1.0
Class-Path: BungeeCord.jar
Main-Class: net.ME1312.SubServers.Bungee.Launch
Implementation-Version: 2.11.2i
Specification-Version: 0
Implementation-Version: 2.11.2j
Specification-Version: 1

View File

@ -33,6 +33,7 @@ public abstract class Host implements ExtraDataHandler {
*/
public Host(SubPlugin plugin, String name, Boolean enabled, InetAddress address, String directory, String gitBash) {
if (name.contains(" ")) throw new InvalidHostException("Host names cannot have spaces: " + name);
if (name.equals("~")) setDisplayName("Default");
}
/**

View File

@ -27,8 +27,7 @@ public class Client {
private ClientHandler handler;
private PrintWriter writer;
private Timer authorized;
private SubDataServer subdata;
private Client instance;
protected SubDataServer subdata;
/**
* Network Client
@ -42,13 +41,12 @@ public class Client {
socket = client;
writer = new PrintWriter(client.getOutputStream(), true);
address = new InetSocketAddress(client.getInetAddress(), client.getPort());
instance = this;
authorized = new Timer("__subdata_auth_" + client.getRemoteSocketAddress().toString());
authorized.schedule(new TimerTask() {
@Override
public void run() {
if (!socket.isClosed()) try {
subdata.removeClient(instance);
subdata.removeClient(Client.this);
} catch (IOException e) {
e.printStackTrace();
}
@ -66,32 +64,17 @@ public class Client {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String input;
while ((input = in.readLine()) != null) {
try {
JSONObject json = new JSONObject(input);
for (PacketIn packet : SubDataServer.decodePacket(json)) {
if (authorized == null || packet instanceof PacketAuthorization) {
try {
packet.execute(instance, (json.keySet().contains("c")) ? json.getJSONObject("c") : null);
} catch (Throwable e) {
new InvocationTargetException(e, "Exception while executing PacketIn").printStackTrace();
}
} else sendPacket(new PacketAuthorization(-1, "Unauthorized"));
}
} catch (IllegalPacketException e) {
e.printStackTrace();
} catch (JSONException e) {
new IllegalPacketException("Unknown Packet Format: " + input).printStackTrace();
}
recievePacket(input);
}
try {
subdata.removeClient(instance);
subdata.removeClient(Client.this);
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (Exception e) {
if (!(e instanceof SocketException)) e.printStackTrace();
try {
subdata.removeClient(instance);
subdata.removeClient(Client.this);
} catch (IOException e1) {
e1.printStackTrace();
}
@ -110,6 +93,25 @@ public class Client {
authorized = null;
}
protected void recievePacket(String raw) {
try {
JSONObject json = new JSONObject(raw);
for (PacketIn packet : SubDataServer.decodePacket(json)) {
if (authorized == null || packet instanceof PacketAuthorization) {
try {
packet.execute(Client.this, (json.keySet().contains("c")) ? json.getJSONObject("c") : null);
} catch (Throwable e) {
new InvocationTargetException(e, "Exception while executing PacketIn").printStackTrace();
}
} else sendPacket(new PacketAuthorization(-1, "Unauthorized"));
}
} catch (IllegalPacketException e) {
e.printStackTrace();
} catch (JSONException e) {
new IllegalPacketException("Unknown Packet Format: " + raw).printStackTrace();
}
}
/**
* Send Packet to Client
*

View File

@ -0,0 +1,76 @@
package net.ME1312.SubServers.Bungee.Network.Packet;
import net.ME1312.SubServers.Bungee.Library.Version.Version;
import net.ME1312.SubServers.Bungee.Network.Client;
import net.ME1312.SubServers.Bungee.Network.PacketIn;
import net.ME1312.SubServers.Bungee.Network.PacketOut;
import net.ME1312.SubServers.Bungee.SubPlugin;
import org.json.JSONObject;
/**
* Download Proxy Info Packet
*/
public class PacketDownloadProxyInfo implements PacketIn, PacketOut {
private SubPlugin plugin;
private String id;
/**
* New PacketDownloadProxyInfo (In)
*
* @param plugin SubPlugin
*/
public PacketDownloadProxyInfo(SubPlugin plugin) {
this.plugin = plugin;
}
/**
* New PacketDownloadProxyInfo (Out)
*
* @param plugin SubPlugin
* @param id Receiver ID
*/
public PacketDownloadProxyInfo(SubPlugin plugin, String id) {
this.plugin = plugin;
this.id = id;
}
@Override
public JSONObject generate() {
JSONObject json = new JSONObject();
if (id != null) json.put("id", id);
JSONObject subservers = new JSONObject();
subservers.put("version", plugin.version.toString());
if (plugin.bversion != null) subservers.put("beta", plugin.bversion.toString());
subservers.put("hosts", plugin.api.getHosts().size());
subservers.put("subservers", plugin.api.getSubServers().size());
json.put("subservers", subservers);
JSONObject bungee = new JSONObject();
bungee.put("version", plugin.api.getProxyVersion());
bungee.put("servers", plugin.api.getServers().size());
json.put("bungee", bungee);
JSONObject minecraft = new JSONObject();
minecraft.put("version", plugin.api.getGameVersion());
minecraft.put("players", plugin.getPlayers().size());
json.put("minecraft", minecraft);
JSONObject system = new JSONObject();
JSONObject os = new JSONObject();
os.put("name", System.getProperty("os.name"));
os.put("version", System.getProperty("os.version"));
system.put("os", os);
JSONObject java = new JSONObject();
java.put("version", System.getProperty("java.version"));
system.put("java", java);
json.put("system", system);
return json;
}
@Override
public void execute(Client client, JSONObject data) {
client.sendPacket(new PacketDownloadProxyInfo(plugin, (data != null && data.keySet().contains("id"))?data.getString("id"):null));
}
@Override
public Version getVersion() {
return new Version("2.11.0a");
}
}

View File

@ -37,7 +37,7 @@ public class PacketDownloadServerInfo implements PacketIn, PacketOut {
* @param id Receiver ID
*/
public PacketDownloadServerInfo(SubPlugin plugin, Server server, String id) {
if (Util.isNull(plugin, server)) throw new NullPointerException();
if (Util.isNull(plugin)) throw new NullPointerException();
this.plugin = plugin;
this.server = server;
this.id = id;

View File

@ -65,25 +65,16 @@ public class PacketOutRunEvent implements Listener, PacketOut {
@EventHandler(priority = EventPriority.HIGHEST)
public void event(SubAddServerEvent event) {
if (!event.isCancelled()) {
List<ClientHandler> list = new ArrayList<ClientHandler>();
list.addAll(plugin.api.getServers().values());
for (Host host : plugin.api.getHosts().values()) if (host instanceof ClientHandler) list.add((ClientHandler) host);
for (ClientHandler client : list) {
JSONObject args = new JSONObject();
args.put("player", ((event.getPlayer() == null)?null:event.getPlayer().toString()));
args.put("host", ((event.getHost() == null)?null:event.getHost()));
args.put("server", event.getServer().getName());
if (client.getSubDataClient() != null) client.getSubDataClient().sendPacket(new PacketOutRunEvent(event.getClass(), args));
}
plugin.subdata.broadcastPacket(new PacketOutRunEvent(event.getClass(), args));
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void event(SubCreateEvent event) {
if (!event.isCancelled()) {
List<ClientHandler> list = new ArrayList<ClientHandler>();
list.addAll(plugin.api.getServers().values());
for (Host host : plugin.api.getHosts().values()) if (host instanceof ClientHandler) list.add((ClientHandler) host);
for (ClientHandler client : list) {
JSONObject args = new JSONObject();
args.put("player", ((event.getPlayer() == null)?null:event.getPlayer().toString()));
args.put("host", event.getHost().getName());
@ -92,78 +83,54 @@ public class PacketOutRunEvent implements Listener, PacketOut {
args.put("version", event.getVersion().toString());
args.put("port", event.getPort());
args.put("memory", event.getMemory());
if (client.getSubDataClient() != null) client.getSubDataClient().sendPacket(new PacketOutRunEvent(event.getClass(), args));
}
plugin.subdata.broadcastPacket(new PacketOutRunEvent(event.getClass(), args));
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void event(SubSendCommandEvent event) {
if (!event.isCancelled()) {
List<ClientHandler> list = new ArrayList<ClientHandler>();
list.addAll(plugin.api.getServers().values());
for (Host host : plugin.api.getHosts().values()) if (host instanceof ClientHandler) list.add((ClientHandler) host);
for (ClientHandler client : list) {
JSONObject args = new JSONObject();
args.put("player", ((event.getPlayer() == null)?null:event.getPlayer().toString()));
args.put("server", event.getServer().getName());
args.put("command", event.getCommand());
if (client.getSubDataClient() != null) client.getSubDataClient().sendPacket(new PacketOutRunEvent(event.getClass(), args));
}
plugin.subdata.broadcastPacket(new PacketOutRunEvent(event.getClass(), args));
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void event(SubStartEvent event) {
if (!event.isCancelled()) {
List<ClientHandler> list = new ArrayList<ClientHandler>();
list.addAll(plugin.api.getServers().values());
for (Host host : plugin.api.getHosts().values()) if (host instanceof ClientHandler) list.add((ClientHandler) host);
for (ClientHandler client : list) {
JSONObject args = new JSONObject();
args.put("player", ((event.getPlayer() == null)?null:event.getPlayer().toString()));
args.put("server", event.getServer().getName());
if (client.getSubDataClient() != null) client.getSubDataClient().sendPacket(new PacketOutRunEvent(event.getClass(), args));
}
plugin.subdata.broadcastPacket(new PacketOutRunEvent(event.getClass(), args));
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void event(SubStopEvent event) {
if (!event.isCancelled()) {
List<ClientHandler> list = new ArrayList<ClientHandler>();
list.addAll(plugin.api.getServers().values());
for (Host host : plugin.api.getHosts().values()) if (host instanceof ClientHandler) list.add((ClientHandler) host);
for (ClientHandler client : list) {
JSONObject args = new JSONObject();
args.put("player", ((event.getPlayer() == null)?null:event.getPlayer().toString()));
args.put("server", event.getServer().getName());
args.put("force", event.isForced());
if (client.getSubDataClient() != null) client.getSubDataClient().sendPacket(new PacketOutRunEvent(event.getClass(), args));
}
plugin.subdata.broadcastPacket(new PacketOutRunEvent(event.getClass(), args));
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void event(SubStoppedEvent event) {
List<ClientHandler> list = new ArrayList<ClientHandler>();
list.addAll(plugin.api.getServers().values());
for (Host host : plugin.api.getHosts().values()) if (host instanceof ClientHandler) list.add((ClientHandler) host);
for (ClientHandler client : list) {
JSONObject args = new JSONObject();
args.put("server", event.getServer().getName());
if (client.getSubDataClient() != null) client.getSubDataClient().sendPacket(new PacketOutRunEvent(event.getClass(), args));
}
plugin.subdata.broadcastPacket(new PacketOutRunEvent(event.getClass(), args));
}
@EventHandler(priority = EventPriority.HIGHEST)
public void event(SubRemoveServerEvent event) {
if (!event.isCancelled()) {
List<ClientHandler> list = new ArrayList<ClientHandler>();
list.addAll(plugin.api.getServers().values());
for (Host host : plugin.api.getHosts().values()) if (host instanceof ClientHandler) list.add((ClientHandler) host);
for (ClientHandler client : list) {
JSONObject args = new JSONObject();
args.put("player", ((event.getPlayer() == null)?null:event.getPlayer().toString()));
args.put("host", ((event.getHost() == null)?null:event.getHost()));
args.put("server", event.getServer().getName());
if (client.getSubDataClient() != null) client.getSubDataClient().sendPacket(new PacketOutRunEvent(event.getClass(), args));
}
plugin.subdata.broadcastPacket(new PacketOutRunEvent(event.getClass(), args));
}
}
}

View File

@ -7,12 +7,15 @@ import net.ME1312.SubServers.Bungee.Network.Packet.*;
import net.ME1312.SubServers.Bungee.SubPlugin;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;
/**
* SubDataServer Class
@ -68,6 +71,7 @@ public final class SubDataServer {
registerPacket(new PacketDownloadHostInfo(plugin), "SubDownloadHostInfo");
registerPacket(new PacketDownloadLang(plugin), "SubDownloadLang");
registerPacket(new PacketDownloadPlayerList(plugin), "SubDownloadPlayerList");
registerPacket(new PacketDownloadProxyInfo(plugin), "SubDownloadProxyInfo");
registerPacket(new PacketDownloadServerInfo(plugin), "SubDownloadServerInfo");
registerPacket(new PacketDownloadServerList(plugin), "SubDownloadServerList");
registerPacket(new PacketExAddServer(), "SubExAddServer");
@ -91,6 +95,7 @@ public final class SubDataServer {
registerPacket(PacketDownloadHostInfo.class, "SubDownloadHostInfo");
registerPacket(PacketDownloadLang.class, "SubDownloadLang");
registerPacket(PacketDownloadPlayerList.class, "SubDownloadPlayerList");
registerPacket(PacketDownloadProxyInfo.class, "SubDownloadProxyInfo");
registerPacket(PacketDownloadServerInfo.class, "SubDownloadServerInfo");
registerPacket(PacketDownloadServerList.class, "SubDownloadServerList");
registerPacket(PacketExAddServer.class, "SubExAddServer");
@ -131,7 +136,7 @@ public final class SubDataServer {
clients.put(client.getAddress(), client);
return client;
} else {
System.out.println("SubData > " + socket.getInetAddress().toString() + ":" + socket.getPort() + " attempted to connect, but isn't whitelisted");
System.out.println("SubData > " + socket.getInetAddress().toString() + " attempted to connect, but isn't white-listed");
socket.close();
return null;
}

View File

@ -173,7 +173,7 @@ public final class SubPlugin extends BungeeCord {
lang = new YAMLConfig(new UniversalFile(dir, "SubServers:lang.yml"));
subdata = new SubDataServer(this, Integer.parseInt(config.get().getSection("Settings").getSection("SubData").getRawString("Address", "127.0.0.1:4391").split(":")[1]), 10,
(config.get().getSection("Settings").getSection("SubData").getRawString("Address", "127.0.0.1:4391").split(":")[0].equals("0.0.0.0"))?null:InetAddress.getByName(config.get().getSection("Settings").getSection("SubData").getRawString("Address", "127.0.0.1:4391").split(":")[0]));
System.out.println("SubServers > SubData Listening on /" + config.get().getSection("Settings").getSection("SubData").getRawString("Address", "127.0.0.1:4391"));
System.out.println("SubServers > SubData Direct Listening on /" + config.get().getSection("Settings").getSection("SubData").getRawString("Address", "127.0.0.1:4391"));
loop();
int hosts = 0;

View File

@ -39,7 +39,7 @@ public final class SubDataClient {
* SubServers Client Instance
*
* @param plugin SubPlugin
* @param address Bind Address
* @param address Address
* @param port Port
* @throws IOException
*/
@ -275,7 +275,7 @@ public final class SubDataClient {
Bukkit.getLogger().info("SubServers > The SubData Connection was closed");
if (reconnect) {
Bukkit.getLogger().info("SubServers > Attempting to reconnect in 10 seconds");
Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() {
@Override
public void run() {
try {

View File

@ -1,4 +1,4 @@
name: 'SubServers'
name: 'SubServers-Client-Bukkit'
main: 'net.ME1312.SubServers.Client.Bukkit.SubPlugin'
version: '2.11.2e'
authors: [ME1312]

View File

@ -7,6 +7,7 @@ import net.ME1312.SubServers.Bungee.Host.Host;
import net.ME1312.SubServers.Bungee.Host.SubCreator;
import net.ME1312.SubServers.Bungee.Host.SubServer;
import net.ME1312.SubServers.Bungee.Library.Config.YAMLConfig;
import net.ME1312.SubServers.Bungee.SubAPI;
import net.ME1312.SubServers.Bungee.SubPlugin;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
@ -26,6 +27,10 @@ public final class ConsolePlugin extends Plugin implements Listener {
@Override
public void onEnable() {
SubAPI.getInstance().addListener(this::enable, this::disable);
}
public void enable() {
try {this.
getDataFolder().mkdirs();
config = new YAMLConfig(new File(getDataFolder(), "config.yml"));
@ -102,8 +107,7 @@ public final class ConsolePlugin extends Plugin implements Listener {
}
}
@Override
public void onDisable() {
public void disable() {
for (ConsoleWindow window : sCurrent.values()) {
window.destroy();
}

View File

@ -1,23 +1,24 @@
package net.ME1312.SubServers.Host.Library.Log;
import net.ME1312.SubServers.Host.Library.NamedContainer;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
/**
* System.out and System.err Override Class
*/
public final class SystemLogger extends OutputStream {
private HashMap<String, Logger> stream = new HashMap<String, Logger>();
private boolean level;
private NamedContainer<String, Logger> last = new NamedContainer<String, Logger>("", null);
private boolean error;
private File dir;
protected SystemLogger(boolean level, File dir) throws IOException {
if (!new File(dir, SystemLogger.class.getCanonicalName().replace(".", File.separator) + ".class").exists()) {
throw new IOException("Invalid directory for logging:" + dir.getPath());
}
this.level = level;
this.error = level;
this.dir = dir;
}
@ -32,11 +33,11 @@ public final class SystemLogger extends OutputStream {
}
i++;
}
if (!stream.keySet().contains(origin)) stream.put(origin, new Logger(origin));
if (level) {
stream.get(origin).error.print((char) c);
if (!last.name().equals(origin)) last = new NamedContainer<String, Logger>(origin, new Logger(origin));
if (error) {
last.get().error.print((char) c);
} else {
stream.get(origin).info.print((char) c);
last.get().info.print((char) c);
}
}
}

View File

@ -38,8 +38,8 @@ public final class SubDataClient {
/**
* SubServers Client Instance
*
* @param host SubPlugin
* @param address Bind Address
* @param host SubServers.Host
* @param address Address
* @param port Port
* @throws IOException
*/