diff --git a/cs656/lab2/src/ChatClient.java b/cs656/lab2/src/ChatClient.java index 08c7289..48719fe 100644 --- a/cs656/lab2/src/ChatClient.java +++ b/cs656/lab2/src/ChatClient.java @@ -9,6 +9,7 @@ public class ChatClient private String myUserName; private Thread myListenThread; private ChatClientListener myListener; + private RegistrationInfo myRegistrationInfo; private class ChatClientListener implements Runnable { @@ -49,16 +50,13 @@ public class ChatClient public void run() { myListener = new ChatClientListener(); - RegistrationInfo regInfo = + myRegistrationInfo = new RegistrationInfo(myUserName, myListener.getHost(), myListener.getPort(), true); - try { - myPServ.register(regInfo); - } catch (Exception e) { - System.out.println("Exception caught when registering!"); - e.printStackTrace(); + if (!register()) + { return; } @@ -69,6 +67,30 @@ public class ChatClient mainloop(); } + private boolean register() + { + try { + myPServ.register(myRegistrationInfo); + } catch (Exception e) { + System.out.println("Exception caught when registering!"); + e.printStackTrace(); + return false; + } + return true; + } + + private boolean unregister() + { + try { + myPServ.unregister(myUserName); + } catch (Exception e) { + System.out.println("Exception caught when unregistering!"); + e.printStackTrace(); + return false; + } + return true; + } + private void mainloop() { BufferedReader br = new BufferedReader( @@ -76,7 +98,43 @@ public class ChatClient String line; while ( (line = br.readLine()) != null) { + line = line.trim(); + if (line.equals("")) + continue; + String command, rest; + int spaceIndex = line.indexOf(' '); + if (spaceIndex > 0) + { + command = line.substring(0, spaceIndex); + rest = line.substring(spaceIndex + 1); + rest = rest.trim(); + } + else + { + command = line; + rest = ""; + } + + if (command.equals("help") || command.equals("?")) + { + System.out.println("Commands:\n"); + System.out.println(" quit - disconnect and exit program"); + System.out.println(" friends - view a list of all peers on the system"); + } + else if (command.equals("quit")) + { + // TODO: shut down listen thread + break; + } + else if (command.equals("friends")) + { + } + else + { + System.out.println("Unrecognized Command! Type 'help' or '?' for help!"); + } } + System.exit(0); } public static void main(String args[])