Refactored SocketServer.tick() method

This commit is contained in:
Simon 2018-06-04 18:06:31 -04:00
parent 78f98c8227
commit c389bbc92e

View File

@ -14,6 +14,7 @@ import org.json.simple.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
@WebSocket @WebSocket
public class SocketServer { public class SocketServer {
@ -22,6 +23,8 @@ public class SocketServer {
private MessageDispatcher messageDispatcher = new MessageDispatcher(); private MessageDispatcher messageDispatcher = new MessageDispatcher();
private static final String AUTH_OK_MESSAGE = "{\"t\":\"auth\", \"m\":\"ok\"}";
public SocketServer() { public SocketServer() {
messageDispatcher.addHandler(new UserInfoRequestHandler()); messageDispatcher.addHandler(new UserInfoRequestHandler());
@ -70,7 +73,7 @@ public class SocketServer {
onlineUser.setAuthenticated(true); onlineUser.setAuthenticated(true);
try { try {
session.getRemote().sendString("{\"t\":\"auth\", \"m\":\"ok\"}"); session.getRemote().sendString(AUTH_OK_MESSAGE);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -86,7 +89,7 @@ public class SocketServer {
onlineUser.getUser().getUsername() + session.getRemoteAddress().getAddress()); onlineUser.getUser().getUsername() + session.getRemoteAddress().getAddress());
try { try {
session.getRemote().sendString("{\"t\":\"auth\", \"m\":\"ok\"}"); session.getRemote().sendString(AUTH_OK_MESSAGE);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -105,59 +108,59 @@ public class SocketServer {
*/ */
public void tick() { public void tick() {
//TODO: refactor this function (1. Create json instance for each user 2. Extract functions 3. rename variables) //Avoid ConcurrentModificationException
ArrayList<OnlineUser> onlineUsers = new ArrayList<>(onlineUserManager.getOnlineUsers());
for (OnlineUser user : onlineUsers) {
if (user.getWebSocket().isOpen() && user.getUser() != null) {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("t", "tick"); json.put("t", "tick");
ArrayList<OnlineUser> onlineUsers = new ArrayList<>(onlineUserManager.getOnlineUsers()); //Avoid ConcurrentModificationException if (user.getUser().isGuest()) {
for (OnlineUser user : onlineUsers) {
if (user.getWebSocket().isOpen()) { sendJSONObject(user, json);
if (user.getUser() != null && user.getUser().isGuest()) { } else {
ControllableUnit unit = user.getUser().getControlledUnit();
json.remove("c"); json.put("c", charArraysToJSON(unit.getConsoleMessagesBuffer()));
json.put("keys", intListToJSON(unit.getKeyboardBuffer()));
json.put("cm", unit.getConsoleMode());
sendJSONObject(user, json);
}
}
}
}
private void sendJSONObject(OnlineUser user, JSONObject json) {
try { try {
user.getWebSocket().getRemote().sendString((json.toJSONString())); user.getWebSocket().getRemote().sendString((json.toJSONString()));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}
} else if (user.getUser() != null) {
try {
ControllableUnit unit = user.getUser().getControlledUnit();
//Send keyboard updated buffer
ArrayList<Integer> kbBuffer = unit.getKeyboardBuffer();
JSONArray keys = new JSONArray();
keys.addAll(kbBuffer);
json.put("keys", keys);
//Send console buffer
if (unit.getConsoleMessagesBuffer().size() > 0) {
JSONArray buff = new JSONArray();
for (char[] message : unit.getConsoleMessagesBuffer()) {
buff.add(new String(message));
}
json.put("c", buff);
} else {
json.remove("c");
}
json.put("cm", unit.getConsoleMode());
//Send tick message
user.getWebSocket().getRemote().sendString(json.toJSONString());
} catch (NullPointerException | IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
//Ignore //Ignore
} }
} }
}
} private JSONArray charArraysToJSON(List<char[]> charArrays) {
JSONArray jsonMessages = new JSONArray();
for (char[] message : charArrays) {
jsonMessages.add(new String(message));
}
return jsonMessages;
}
private JSONArray intListToJSON(List<Integer> ints) {
JSONArray jsonInts = new JSONArray();
jsonInts.addAll(ints);
return jsonInts;
} }
} }