Skip to content

Commit

Permalink
* Fix Client Disconnect Issues. [For Brett] (Resolves #15, Resolves #28)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkerm committed May 11, 2015
1 parent d35371a commit 518dc4e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
4 changes: 4 additions & 0 deletions java_client_src/src/ihs/apcs/spacebattle/GraphicalClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ public void disconnect() throws IOException {
System.out.println("Disconnect complete.");
}

public boolean isDisconnected(){
return this.disconnected;
}

public void logMessage(String message) {
if (LOGGING) {
Calendar now = Calendar.getInstance();
Expand Down
27 changes: 20 additions & 7 deletions java_client_src/src/ihs/apcs/spacebattle/TextClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,16 @@ 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);
BufferedReader kb = new BufferedReader(new InputStreamReader(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"));
while (!client.disconnected) {
if (kb.ready()) {
String input = kb.readLine();
if (input.equalsIgnoreCase("quit")) {
break;
}
}
}
kb.close();
} catch (IOException ex) {
System.err.println("Server connection failed.");
Expand Down Expand Up @@ -193,19 +200,25 @@ public <T> void parseMessage(MwnpMessage msg) throws IOException, IllegalArgumen
}
}

public void disconnect() throws IOException {
System.out.println("Attempting to disconnect...");
public void disconnect() throws IOException {
if (!disconnected) {
System.out.println("Attempting to disconnect...");

logMessage("Sending disconnect message...");
MwnpMessage disconnect = new MwnpMessage(new Integer[]{netId, 0}, "MWNL2_DISCONNECT", null);
messenger.sendMessage(disconnect);
logMessage("Ending listener...");
listener.end();
logMessage("Ending messenger...");
messenger.end();
}
disconnected = true;
System.out.println("Disconnect complete.");

disconnected = true;
System.out.println("Disconnect complete.");
}
}

public boolean isDisconnected(){
return this.disconnected;
}

public void logMessage(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;

public interface Client {
public boolean isDisconnected();
public void disconnect() throws IOException;
public <T> void parseMessage(MwnpMessage msg) throws IOException, IllegalArgumentException, IllegalAccessException;
public void logMessage(String message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ public ShutdownHook(Client client) {
public void run() {
try {
System.out.println("Shutting down...");
client.disconnect();
if (!client.isDisconnected())
{
client.disconnect();
}
} catch (IOException e) {
System.err.println("Shutdown error...");
System.err.println(e.getMessage());
Expand Down

0 comments on commit 518dc4e

Please sign in to comment.