From 7e6b4e98520bee91b8c50153c4903ce4ae516833 Mon Sep 17 00:00:00 2001 From: Trever Fischer Date: Sun, 21 Oct 2012 20:04:34 -0400 Subject: [PATCH] Implement server polling --- src/main/java/us/camin/Plugin.java | 25 +++++++++ src/main/java/us/camin/ServerEventPoller.java | 51 +++++++++++++++++ .../java/us/camin/api/BroadcastEvent.java | 32 +++++++++++ .../java/us/camin/api/PlayerMessageEvent.java | 35 ++++++++++++ src/main/java/us/camin/api/Server.java | 37 ++++++++++++- src/main/java/us/camin/api/ServerEvent.java | 55 +++++++++++++++++++ 6 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 src/main/java/us/camin/ServerEventPoller.java create mode 100644 src/main/java/us/camin/api/BroadcastEvent.java create mode 100644 src/main/java/us/camin/api/PlayerMessageEvent.java create mode 100644 src/main/java/us/camin/api/ServerEvent.java diff --git a/src/main/java/us/camin/Plugin.java b/src/main/java/us/camin/Plugin.java index 6b26378..8fa4b15 100644 --- a/src/main/java/us/camin/Plugin.java +++ b/src/main/java/us/camin/Plugin.java @@ -17,6 +17,7 @@ package us.camin; along with Caminus. If not, see . */ +import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -36,8 +37,11 @@ import java.util.HashMap; import java.util.logging.Logger; import java.util.logging.Level; import java.io.IOException; +import java.util.concurrent.Callable; import us.camin.api.Server; +import us.camin.api.ServerEvent; +import us.camin.api.BroadcastEvent; public class Plugin extends JavaPlugin { @@ -46,6 +50,7 @@ public class Plugin extends JavaPlugin { private JoinListener m_listener; private MOTDCommand m_motdCommand; private VomitCommand m_vomitCommand; + private ServerEventPoller m_eventPoll; public Server api() { return m_api; @@ -56,6 +61,23 @@ public class Plugin extends JavaPlugin { m_api = null; } + public void handleEvent(ServerEvent e) { + if (e instanceof BroadcastEvent) { + final BroadcastEvent evt = (BroadcastEvent)(e); + getServer().getScheduler().callSyncMethod(this, new Callable() { + public Void call() { + getServer().broadcastMessage(evt.message); + return null; + } + }); + } + try { + m_api.notifyEventHandled(e); + } catch (IOException ex) { + log.severe("Could not close out event. Duplicates will happen!!!"); + } + } + public void onEnable() { PluginManager pm = this.getServer().getPluginManager(); m_listener = new JoinListener(this); @@ -74,6 +96,8 @@ public class Plugin extends JavaPlugin { pm.registerEvents(m_listener, this); + m_eventPoll = new ServerEventPoller(this); + m_motdCommand = new MOTDCommand(this); getCommand("motd").setExecutor(m_motdCommand); m_vomitCommand = new VomitCommand(this); @@ -88,6 +112,7 @@ public class Plugin extends JavaPlugin { sm.register(Economy.class, econAPI, this, ServicePriority.High); log.info("[Caminus] Plugin enabled"); + getServer().getScheduler().scheduleAsyncDelayedTask(this, m_eventPoll); } public void checkFreeHalfDoorDay(Player sender) { diff --git a/src/main/java/us/camin/ServerEventPoller.java b/src/main/java/us/camin/ServerEventPoller.java new file mode 100644 index 0000000..dadb99a --- /dev/null +++ b/src/main/java/us/camin/ServerEventPoller.java @@ -0,0 +1,51 @@ +package us.camin; + +/* + This file is part of Caminus + + Caminus is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Caminus is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Caminus. If not, see . + */ + +import java.lang.Runnable; +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONException; +import us.camin.api.ServerEvent; +import java.io.IOException; +import java.util.logging.Logger; +import org.bukkit.scheduler.BukkitScheduler; + +public class ServerEventPoller implements Runnable { + private Plugin m_plugin; + Logger log = Logger.getLogger("CaminusEventPoll"); + + public ServerEventPoller(Plugin plugin) { + m_plugin = plugin; + } + + public void run() { + log.info("Poll events"); + ServerEvent[] events = new ServerEvent[0]; + try { + events = m_plugin.api().pollEventQueue(); + } catch (IOException e) { + } + for(ServerEvent e : events) { + m_plugin.handleEvent(e); + } + final BukkitScheduler scheduler = m_plugin.getServer().getScheduler(); + scheduler.scheduleAsyncDelayedTask(m_plugin, this); + log.info("Events handled."); + } +} diff --git a/src/main/java/us/camin/api/BroadcastEvent.java b/src/main/java/us/camin/api/BroadcastEvent.java new file mode 100644 index 0000000..8bf20d4 --- /dev/null +++ b/src/main/java/us/camin/api/BroadcastEvent.java @@ -0,0 +1,32 @@ +package us.camin.api; + +/* + This file is part of Caminus + + Caminus is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Caminus is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Caminus. If not, see . + */ + +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONException; + +public class BroadcastEvent extends ServerEvent { + public String message; + public static BroadcastEvent fromJSON(JSONObject obj, int id) { + BroadcastEvent ret = new BroadcastEvent(); + ret.message = obj.optString("message"); + ret.id = id; + return ret; + } +} diff --git a/src/main/java/us/camin/api/PlayerMessageEvent.java b/src/main/java/us/camin/api/PlayerMessageEvent.java new file mode 100644 index 0000000..fd6588a --- /dev/null +++ b/src/main/java/us/camin/api/PlayerMessageEvent.java @@ -0,0 +1,35 @@ +package us.camin.api; + +/* + This file is part of Caminus + + Caminus is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Caminus is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Caminus. If not, see . + */ + +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONException; + +public class PlayerMessageEvent extends ServerEvent { + public String message; + public String player; + public static PlayerMessageEvent fromJSON(JSONObject obj, int id) { + PlayerMessageEvent ret = new PlayerMessageEvent(); + ret.message = obj.optString("message"); + ret.player = obj.optString("player"); + ret.id = id; + return ret; + } +} + diff --git a/src/main/java/us/camin/api/Server.java b/src/main/java/us/camin/api/Server.java index 44b94a8..d93e96a 100644 --- a/src/main/java/us/camin/api/Server.java +++ b/src/main/java/us/camin/api/Server.java @@ -47,7 +47,7 @@ import java.util.Random; import org.apache.commons.codec.binary.Hex; public class Server { - Logger log = Logger.getLogger("Caminus.API"); + Logger log = Logger.getLogger("Caminus.API"); private String m_url; private String m_name; private String m_secret; @@ -216,13 +216,15 @@ public class Server { public boolean pingAPI() { log.info("Pinging API server to verify credentials"); + JSONObject response; try { - get("server/whoami"); + response = get("server/whoami"); } catch (IOException e) { log.log(Level.SEVERE, "Could not ping API server.", e); return false; } - return true; + log.info("Connected to server running "+response.optString("server-version")+", api "+response.optInt("api-version")); + return response.optInt("api-version") == 2; } public void closeSession(String player) throws IOException { @@ -230,6 +232,35 @@ public class Server { get("server/session/"+player+"/close"); } + public void notifyEventHandled(ServerEvent event) throws IOException { + log.info("Closing event "+event.id); + HashMap params = new HashMap(); + params.put("job", Integer.toString(event.id)); + post("server/events", params); + } + + public ServerEvent[] pollEventQueue() throws IOException { + log.info("Polling server for events"); + JSONObject jsonObj = get("server/events"); + JSONArray eventList; + try { + eventList = jsonObj.getJSONArray("events"); + } catch (JSONException e) { + return new ServerEvent[0]; + } + + ServerEvent[] events = new ServerEvent[eventList.length()]; + for (int i = 0;i. + */ + +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONException; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class ServerEvent { + public int id; + static Logger log = Logger.getLogger("Caminus.api"); + public static ServerEvent fromJSON(JSONObject obj) { + int id = obj.optInt("id"); + JSONObject event; + try { + event = obj.getJSONObject("event"); + } catch (JSONException e) { + log.log(Level.SEVERE, "Bad JSON", e); + return null; + } + String type = event.optString("type"); + JSONObject payload; + try { + payload = event.getJSONObject("payload"); + } catch (JSONException e) { + log.log(Level.SEVERE, "Bad JSON ", e); + return null; + } + if (type.equals("broadcast")) { + return BroadcastEvent.fromJSON(payload, id); + } else if (type.equals("player-message")) { + return PlayerMessageEvent.fromJSON(payload, id); + } else { + log.log(Level.SEVERE, "Unhandled event type: "+type); + return null; + } + } +}