Made updates in JSON format. Combined chat and tile queues into one UpdateQueue. Fixed UpdateQueue.
This commit is contained in:
parent
7c257af454
commit
3e398e9124
18 changed files with 208 additions and 239 deletions
|
|
@ -61,15 +61,16 @@ public class HttpServerConnection extends Thread {
|
|||
sb.append(response.statusCode);
|
||||
sb.append(" ");
|
||||
sb.append(response.statusMessage);
|
||||
sb.append("\n");
|
||||
sb.append("\r\n");
|
||||
for (Entry<String, String> field : response.fields.entrySet()) {
|
||||
sb.append(field.getKey());
|
||||
sb.append(": ");
|
||||
sb.append(field.getValue());
|
||||
sb.append("\n");
|
||||
sb.append("\r\n");
|
||||
}
|
||||
sb.append("\n");
|
||||
sb.append("\r\n");
|
||||
o.write(sb.toString().getBytes());
|
||||
o.flush();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
|
@ -84,34 +85,41 @@ public class HttpServerConnection extends Thread {
|
|||
|
||||
// TODO: Optimize HttpHandler-finding by using a real path-aware
|
||||
// tree.
|
||||
HttpResponse response = null;
|
||||
HttpHandler handler = null;
|
||||
String relativePath = null;
|
||||
for (Entry<String, HttpHandler> entry : server.handlers.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
boolean directoryHandler = key.endsWith("/");
|
||||
if (directoryHandler && request.path.startsWith(entry.getKey()) || !directoryHandler && request.path.equals(entry.getKey())) {
|
||||
String path = request.path.substring(entry.getKey().length());
|
||||
|
||||
response = new HttpResponse(socket.getOutputStream());
|
||||
entry.getValue().handle(path, request, response);
|
||||
relativePath = request.path.substring(entry.getKey().length());
|
||||
handler = entry.getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (response != null) {
|
||||
if (response.fields.get("Content-Length") == null) {
|
||||
response.fields.put("Content-Length", "0");
|
||||
OutputStream out = response.getBody();
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
if (handler == null) {
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
String connection = response.fields.get("Connection");
|
||||
if (connection == null || connection.equals("close")) {
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
HttpResponse response = new HttpResponse(socket.getOutputStream());
|
||||
|
||||
try {
|
||||
handler.handle(relativePath, request, response);
|
||||
} catch (Exception e) {
|
||||
log.log(Level.SEVERE, "HttpHandler '" + handler + "' has thown an exception", e);
|
||||
e.printStackTrace();
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.fields.get("Content-Length") == null) {
|
||||
response.fields.put("Content-Length", "0");
|
||||
/* OutputStream out = */response.getBody();
|
||||
}
|
||||
|
||||
String connection = response.fields.get("Connection");
|
||||
if (connection != null && connection.equals("close")) {
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,57 +6,52 @@ import java.util.Date;
|
|||
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.dynmap.ChatQueue;
|
||||
import org.dynmap.Client;
|
||||
import org.dynmap.MapManager;
|
||||
import org.dynmap.PlayerList;
|
||||
import org.dynmap.TileUpdate;
|
||||
import org.dynmap.web.HttpHandler;
|
||||
import org.dynmap.web.HttpRequest;
|
||||
import org.dynmap.web.HttpResponse;
|
||||
import org.dynmap.web.Json;
|
||||
|
||||
public class ClientUpdateHandler implements HttpHandler {
|
||||
private MapManager mapManager;
|
||||
private PlayerList playerList;
|
||||
private World world;
|
||||
|
||||
public ClientUpdateHandler(MapManager mapManager, PlayerList playerList, World world) {
|
||||
this.mapManager = mapManager;
|
||||
this.playerList = playerList;
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(String path, HttpRequest request, HttpResponse response) throws IOException {
|
||||
int current = (int) (System.currentTimeMillis() / 1000);
|
||||
long current = System.currentTimeMillis();
|
||||
long cutoff = 0;
|
||||
|
||||
if (path.length() > 0) {
|
||||
try {
|
||||
cutoff = ((long) Integer.parseInt(path)) * 1000;
|
||||
cutoff = Long.parseLong(path);
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
long relativeTime = world.getTime() % 24000;
|
||||
sb.append(current + " " + relativeTime + "\n");
|
||||
|
||||
|
||||
Client.Update update = new Client.Update();
|
||||
update.timestamp = current;
|
||||
update.servertime = world.getTime() % 24000;
|
||||
|
||||
|
||||
Player[] players = playerList.getVisiblePlayers();
|
||||
for (Player player : players) {
|
||||
sb.append("player " + player.getName() + " " + player.getLocation().getX() + " " + player.getLocation().getY() + " " + player.getLocation().getZ() + "\n");
|
||||
update.players = new Client.Player[players.length];
|
||||
for(int i=0;i<players.length;i++) {
|
||||
Player p = players[i];
|
||||
update.players[i] = new Client.Player(p.getName(), p.getLocation().getX(), p.getLocation().getY(), p.getLocation().getZ());
|
||||
}
|
||||
|
||||
TileUpdate[] tileUpdates = mapManager.staleQueue.getTileUpdates(cutoff);
|
||||
for (TileUpdate tu : tileUpdates) {
|
||||
sb.append("tile " + tu.tile.getName() + "\n");
|
||||
}
|
||||
|
||||
ChatQueue.ChatMessage[] messages = mapManager.chatQueue.getChatMessages(cutoff);
|
||||
for (ChatQueue.ChatMessage cu : messages) {
|
||||
sb.append("chat " + cu.playerName + " " + cu.message + "\n");
|
||||
}
|
||||
|
||||
//debugger.debug("Sending " + players.length + " players, " + tileUpdates.length + " tile-updates, and " + messages.length + " chats. " + path + ";" + cutoff);
|
||||
|
||||
byte[] bytes = sb.toString().getBytes();
|
||||
|
||||
update.updates = mapManager.updateQueue.getUpdatedObjects(cutoff);
|
||||
|
||||
byte[] bytes = Json.stringifyJson(update).getBytes();
|
||||
|
||||
String dateStr = new Date().toString();
|
||||
response.fields.put("Date", dateStr);
|
||||
|
|
|
|||
|
|
@ -66,8 +66,9 @@ public abstract class FileHandler implements HttpHandler {
|
|||
}
|
||||
|
||||
String extension = getExtension(path);
|
||||
String mimeType = getMimeTypeFromExtension(extension);
|
||||
|
||||
response.fields.put("Content-Type", getMimeTypeFromExtension(extension));
|
||||
response.fields.put("Content-Type", mimeType);
|
||||
response.fields.put("Connection", "close");
|
||||
OutputStream out = response.getBody();
|
||||
try {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue