diff --git a/cs656/lab2/src/ChatClient.java b/cs656/lab2/src/ChatClient.java index 588e459..5b3683c 100644 --- a/cs656/lab2/src/ChatClient.java +++ b/cs656/lab2/src/ChatClient.java @@ -1,10 +1,21 @@ +// Author: Josh Holtrop +// Date: 2008-09-17 +// CS656 +// Lab Excersize 2 + import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.util.*; import java.io.*; import java.net.*; +/** + * This class implements a basic, command-line chat client. + * It connects to a presence service. The location of the presence + * service is determined based on command-line parameters that + * are passed to the ChatClient class when it is started + */ public class ChatClient { private PresenceService myPServ; @@ -13,6 +24,9 @@ public class ChatClient private ChatClientListener myListener; private RegistrationInfo myRegistrationInfo; + /** + * This class provides a thread to listen for incoming connections. + */ private class ChatClientListener implements Runnable { ServerSocket mySocket; @@ -21,6 +35,7 @@ public class ChatClient { try { + /* Just listen on any available TCP port */ mySocket = new ServerSocket(0); } catch (Exception e) @@ -41,9 +56,11 @@ public class ChatClient return mySocket.getInetAddress().getHostName(); } + /* called when the thread is first scheduled */ public void run() { Socket s; + /* wait indefinitely for new connections */ for (;;) { try @@ -58,6 +75,11 @@ public class ChatClient } } + /** + * This class provides a listener for any connections that come in. + * All it does is print any received chat messages from the remote + * user to this user's screen. + */ public class ChatClientListenerThread implements Runnable { Socket mySocket; @@ -91,6 +113,7 @@ public class ChatClient } } + /* construct the main ChatClient object */ public ChatClient(PresenceService pserv, String userName) { myPServ = pserv; @@ -125,6 +148,7 @@ public class ChatClient mainloop(); } + /* call the register function in the presence service */ private boolean register() { try { @@ -137,6 +161,7 @@ public class ChatClient return true; } + /* call the unregister function in the presence service */ private boolean unregister() { try { @@ -149,6 +174,7 @@ public class ChatClient return true; } + /* this is the main wait-for-input-and-respond loop */ private void mainloop() { BufferedReader br = new BufferedReader( @@ -158,13 +184,16 @@ public class ChatClient { for (;;) { + /* print a prompt */ System.out.print("> "); + /* read a line of input */ line = br.readLine(); if (line == null) break; line = line.trim(); if (line.equals("")) continue; + /* break the input into the command word, and the rest */ String command, rest; int spaceIndex = line.indexOf(' '); if (spaceIndex > 0) @@ -225,7 +254,7 @@ public class ChatClient /* don't send a message to ourselves */ if (!ri.getUserName().equals(myUserName)) { - /* send message if user available */ + /* send message only if user available */ if (ri.getStatus()) { sendUserMessage(ri.getUserName(), rest); @@ -282,6 +311,8 @@ public class ChatClient System.exit(0); } + /* a useful subroutine to break out the task of sending a + * message to a particular user */ private void sendUserMessage(String user, String message) { try @@ -304,11 +335,12 @@ public class ChatClient } } + /* the main method is invoked when the ChatClient is first started */ public static void main(String args[]) { String user; - String host = "localhost"; - int port = 1099; + String host = "localhost"; /* default host */ + int port = 1099; /* default port */ if (args.length < 1) { diff --git a/cs656/lab2/src/PresenceServiceImpl.java b/cs656/lab2/src/PresenceServiceImpl.java index 1713c0e..39e672d 100644 --- a/cs656/lab2/src/PresenceServiceImpl.java +++ b/cs656/lab2/src/PresenceServiceImpl.java @@ -1,11 +1,22 @@ +// Author: Josh Holtrop +// Date: 2008-09-17 +// CS656 +// Lab Excersize 2 + import java.util.HashMap; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; +/** + * This class implements the PresenceService interface. + * It provides a presence service for the chat system. + */ public class PresenceServiceImpl implements PresenceService { + /* A structure to store information about the currently + * registered users */ private HashMap myRegisteredUsers = new HashMap(); @@ -14,28 +25,35 @@ public class PresenceServiceImpl implements PresenceService super(); } + /* a user calls this method to register with the presence service */ public void register(RegistrationInfo reg) { myRegisteredUsers.put(reg.getUserName(), reg); // System.out.println("register(): " + reg.getUserName() + ", " + reg.getHost() + ", " + reg.getPort() + ", " + reg.getStatus()); } + /* a user calls this method to unregister with the presence service */ public void unregister(String userName) { myRegisteredUsers.remove(userName); // System.out.println("unregister(): " + userName); } + /* a user calls this method to get the registration information + * for another user by name */ public RegistrationInfo lookup(String name) { return myRegisteredUsers.get(name); } + /* a user calls this method to retrieve a list of all the + * registered users */ public RegistrationInfo[] listRegisteredUsers() { return myRegisteredUsers.values().toArray(new RegistrationInfo[0]); } + /* the main method is invoked when starting the presence service */ public static void main(String[] args) { int port = 1099;