Skip to content

Commit

Permalink
* Properly parse Server Broadcast Message. (Fixes #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkerm committed May 5, 2015
1 parent ad24491 commit bbfeb1d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions java_client_src/src/ihs/apcs/spacebattle/GraphicalClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public <T> void parseMessage(MwnpMessage msg) throws IOException, IllegalArgumen

RegistrationData data = ship.registerShip(numImages, width, height);

MwnpMessage response = new MwnpMessage(new int[]{netId, 0}, data);
MwnpMessage response = new MwnpMessage(new Integer[]{netId, 0}, data);
messenger.sendMessage(response);
} else if (msg.getCommand().equals("ENV")) {
Environment<?> env = (Environment<?>)msg.getData();
Expand All @@ -140,7 +140,7 @@ public <T> void parseMessage(MwnpMessage msg) throws IOException, IllegalArgumen
} else if (cmd instanceof SelfDestructCommand) {
disconnect();
} else {
MwnpMessage response = new MwnpMessage(new int[]{netId, 0}, cmd);
MwnpMessage response = new MwnpMessage(new Integer[]{netId, 0}, cmd);
messenger.sendMessage(response);
}
} else if (msg.getCommand().equals("ERROR")) {
Expand All @@ -152,7 +152,7 @@ public void disconnect() throws IOException {
System.out.println("Attempting to disconnect...");
if (!disconnected) {
System.out.println("Sending disconnect message...");
MwnpMessage disconnect = new MwnpMessage(new int[]{netId, 0}, "MWNL2_DISCONNECT", null);
MwnpMessage disconnect = new MwnpMessage(new Integer[]{netId, 0}, "MWNL2_DISCONNECT", null);
messenger.sendMessage(disconnect);
System.out.println("Ending listener...");
listener.end();
Expand Down
7 changes: 4 additions & 3 deletions java_client_src/src/ihs/apcs/spacebattle/TextClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static void main(String[] args) {
// Wait for termination command
System.out.println("Type QUIT to disconnect from server and end program");
Scanner kb = new Scanner(System.in);
// TODO: Be able to break-out of this loop and end program when the client receives a disconnect message
while (kb.hasNextLine() && !kb.nextLine().equalsIgnoreCase("QUIT"));
kb.close();
} catch (IOException ex) {
Expand Down Expand Up @@ -147,7 +148,7 @@ public <T> void parseMessage(MwnpMessage msg) throws IOException, IllegalArgumen
RegistrationData data = ship.registerShip(numImages, width, height);
data = new RegistrationData(data.getName() + shipSuffix, data.getColor(), data.getImage());

MwnpMessage response = new MwnpMessage(new int[]{netId, 0}, data);
MwnpMessage response = new MwnpMessage(new Integer[]{netId, 0}, data);
messenger.sendMessage(response);
} else if (msg.getCommand().equals("ENV")) {
Environment<?> env = (Environment<?>)msg.getData();
Expand Down Expand Up @@ -179,7 +180,7 @@ public <T> void parseMessage(MwnpMessage msg) throws IOException, IllegalArgumen
} else if (cmd instanceof SelfDestructCommand) {
disconnect();
} else {
MwnpMessage response = new MwnpMessage(new int[]{netId, 0}, cmd);
MwnpMessage response = new MwnpMessage(new Integer[]{netId, 0}, cmd);
messenger.sendMessage(response);
}
} else if (msg.getCommand().equals("ERROR")) {
Expand All @@ -195,7 +196,7 @@ public void disconnect() throws IOException {
System.out.println("Attempting to disconnect...");
if (!disconnected) {
logMessage("Sending disconnect message...");
MwnpMessage disconnect = new MwnpMessage(new int[]{netId, 0}, "MWNL2_DISCONNECT", null);
MwnpMessage disconnect = new MwnpMessage(new Integer[]{netId, 0}, "MWNL2_DISCONNECT", null);
messenger.sendMessage(disconnect);
logMessage("Ending listener...");
listener.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import ihs.apcs.spacebattle.util.StringStringMap;

public class MwnpMessage {
private int[] ids;
private Integer[] ids;
private String command;
private Object data;

Expand Down Expand Up @@ -47,26 +47,26 @@ public static void RegisterGameType(String gameName)
}
}

public MwnpMessage(int[] ids, String command, Object data) {
public MwnpMessage(Integer[] ids, String command, Object data) {
this.ids = ids;
this.command = command;
this.data = data;
}

public MwnpMessage(int[] ids, ShipCommand cmd) throws IllegalArgumentException, IllegalAccessException {
public MwnpMessage(Integer[] ids, ShipCommand cmd) throws IllegalArgumentException, IllegalAccessException {
this.ids = ids;
this.command = "SCMD";
this.data = cmd.getMessage();
}

public MwnpMessage(int[] ids, RegistrationData regData) {
public MwnpMessage(Integer[] ids, RegistrationData regData) {
this.ids = ids;
this.command = "REGISTER";
this.data = regData;
}

public int getSenderId() { return ids[0]; }
public int getReceiverId() { return ids[1]; }
public Integer getSenderId() { return ids[0]; }
public Integer getReceiverId() { return ids[1]; }
public String getCommand() { return command; }
public Object getData() { return data; }

Expand All @@ -91,12 +91,12 @@ public static MwnpMessage parseMessage(String messageText) {
String data = messageText.substring(id.length() + command.length() + 2);

return new MwnpMessage (
gson.fromJson(id, int[].class),
gson.fromJson(id, Integer[].class),
gson.fromJson(command, String.class),
gson.fromJson(data, dataType));
} else {
return new MwnpMessage (
gson.fromJson(id, int[].class), // TODO: handle [#, null] type message on broadcast of server closing
gson.fromJson(id, Integer[].class),
gson.fromJson(command, String.class),
null);
}
Expand Down

0 comments on commit bbfeb1d

Please sign in to comment.