1 |
package dk.thoerup.bukkit.hoeruputils.web; |
package dk.thoerup.bukkit.hoeruputils.web; |
2 |
|
|
3 |
import java.io.File; |
import java.io.IOException; |
4 |
|
import java.io.OutputStream; |
5 |
|
import java.net.HttpURLConnection; |
6 |
import java.text.SimpleDateFormat; |
import java.text.SimpleDateFormat; |
7 |
import java.util.Date; |
import java.util.Date; |
8 |
import java.util.Set; |
|
|
import java.util.Map; |
|
9 |
import org.bukkit.Location; |
import org.bukkit.Location; |
|
import org.bukkit.Server; |
|
10 |
import org.bukkit.OfflinePlayer; |
import org.bukkit.OfflinePlayer; |
11 |
|
import org.bukkit.Server; |
12 |
|
import org.bukkit.configuration.Configuration; |
13 |
import org.bukkit.entity.Player; |
import org.bukkit.entity.Player; |
14 |
import org.bukkit.plugin.Plugin; |
import org.bukkit.plugin.Plugin; |
15 |
|
|
16 |
import org.bukkit.configuration.Configuration; |
import com.sun.net.httpserver.HttpExchange; |
17 |
|
import com.sun.net.httpserver.HttpHandler; |
18 |
|
|
|
import org.kokakiwi.apicraft.events.ApiEvent; |
|
|
import org.kokakiwi.apicraft.events.ApiListener; |
|
19 |
|
|
20 |
public class StatusListener extends ApiListener { |
public class StatusListener implements HttpHandler{ |
21 |
|
|
22 |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
23 |
|
|
28 |
long cachedTime = 0; |
long cachedTime = 0; |
29 |
String cachedXml = ""; |
String cachedXml = ""; |
30 |
|
|
31 |
public StatusListener(Server server, Configuration loginConfig, Configuration homeConfig) { |
public StatusListener(Plugin plugin, Server server, Configuration loginConfig, Configuration homeConfig) { |
32 |
|
|
33 |
this.server = server; |
this.server = server; |
34 |
this.loginConfig = loginConfig; |
this.loginConfig = loginConfig; |
35 |
this.homeConfig = homeConfig; |
this.homeConfig = homeConfig; |
44 |
} |
} |
45 |
|
|
46 |
@Override |
@Override |
47 |
public void onApiEvent(ApiEvent event) { |
public void handle(HttpExchange http) throws IOException { |
|
if(!event.path[0].equalsIgnoreCase("webstatus")) { |
|
|
return; |
|
|
} |
|
48 |
|
|
49 |
String xml; |
String xml; |
50 |
long now = System.currentTimeMillis(); |
|
51 |
if ( (now-cachedTime) > 5000) { |
synchronized(this) { //is this necessary ? |
52 |
xml = buildXML(); |
long now = System.currentTimeMillis(); |
53 |
cachedXml = xml; |
if ( (now-cachedTime) > 5000) { |
54 |
cachedTime = now; |
xml = buildXML(); |
55 |
} else { |
cachedXml = xml; |
56 |
xml = cachedXml; |
cachedTime = now; |
57 |
} |
} else { |
58 |
|
xml = cachedXml; |
59 |
|
} |
60 |
|
} |
61 |
|
|
62 |
|
|
63 |
event.setResponse(xml); |
byte bytes[] = xml.getBytes(); |
64 |
event.setActionTaken(true); |
http.getResponseHeaders().add("Content-Type", "text/plain"); |
65 |
|
http.sendResponseHeaders(HttpURLConnection.HTTP_OK, bytes.length ); |
66 |
|
final OutputStream os = http.getResponseBody(); |
67 |
|
os.write(bytes); |
68 |
|
os.close(); |
69 |
|
http.close(); |
70 |
} |
} |
71 |
|
|
72 |
private String buildXML() { |
private String buildXML() { |
98 |
|
|
99 |
sb.append("<player>\n"); |
sb.append("<player>\n"); |
100 |
sb.append("<name>" + player.getName() + "</name>\n" ); |
sb.append("<name>" + player.getName() + "</name>\n" ); |
101 |
|
sb.append("<uuid>" + player.getUniqueId() + "</uuid>\n" ); |
102 |
|
|
103 |
sb.append("<firstseen>" + formatTime(first) + "</firstseen>\n"); |
sb.append("<firstseen>" + formatTime(first) + "</firstseen>\n"); |
104 |
sb.append("<lastseen>" + formatTime(last) + "</lastseen>\n"); |
sb.append("<lastseen>" + formatTime(last) + "</lastseen>\n"); |
109 |
long login = loginConfig.getLong( player.getName() + ".logintime", -1); |
long login = loginConfig.getLong( player.getName() + ".logintime", -1); |
110 |
long elapsed = (login>-1 ? (now-login) / 1000 : -1); |
long elapsed = (login>-1 ? (now-login) / 1000 : -1); |
111 |
|
|
112 |
Player p2 = server.getPlayer( player.getName() ); |
Player p2 = server.getPlayer( player.getUniqueId() ); |
113 |
Location loc = p2.getLocation(); |
Location loc = p2.getLocation(); |
114 |
|
|
115 |
sb.append("<sessionlength>" + elapsed + "</sessionlength>\n" ); |
sb.append("<sessionlength>" + elapsed + "</sessionlength>\n" ); |