commented source a bit more

git-svn-id: svn://anubis/gvsu@159 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-09-18 01:46:13 +00:00
parent c057fee145
commit c7e988e92b
2 changed files with 53 additions and 3 deletions

View File

@ -1,10 +1,21 @@
// Author: Josh Holtrop
// Date: 2008-09-17
// CS656
// Lab Excersize 2
import java.rmi.registry.LocateRegistry; import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry; import java.rmi.registry.Registry;
import java.util.*; import java.util.*;
import java.io.*; import java.io.*;
import java.net.*; 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 public class ChatClient
{ {
private PresenceService myPServ; private PresenceService myPServ;
@ -13,6 +24,9 @@ public class ChatClient
private ChatClientListener myListener; private ChatClientListener myListener;
private RegistrationInfo myRegistrationInfo; private RegistrationInfo myRegistrationInfo;
/**
* This class provides a thread to listen for incoming connections.
*/
private class ChatClientListener implements Runnable private class ChatClientListener implements Runnable
{ {
ServerSocket mySocket; ServerSocket mySocket;
@ -21,6 +35,7 @@ public class ChatClient
{ {
try try
{ {
/* Just listen on any available TCP port */
mySocket = new ServerSocket(0); mySocket = new ServerSocket(0);
} }
catch (Exception e) catch (Exception e)
@ -41,9 +56,11 @@ public class ChatClient
return mySocket.getInetAddress().getHostName(); return mySocket.getInetAddress().getHostName();
} }
/* called when the thread is first scheduled */
public void run() public void run()
{ {
Socket s; Socket s;
/* wait indefinitely for new connections */
for (;;) for (;;)
{ {
try 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 public class ChatClientListenerThread implements Runnable
{ {
Socket mySocket; Socket mySocket;
@ -91,6 +113,7 @@ public class ChatClient
} }
} }
/* construct the main ChatClient object */
public ChatClient(PresenceService pserv, String userName) public ChatClient(PresenceService pserv, String userName)
{ {
myPServ = pserv; myPServ = pserv;
@ -125,6 +148,7 @@ public class ChatClient
mainloop(); mainloop();
} }
/* call the register function in the presence service */
private boolean register() private boolean register()
{ {
try { try {
@ -137,6 +161,7 @@ public class ChatClient
return true; return true;
} }
/* call the unregister function in the presence service */
private boolean unregister() private boolean unregister()
{ {
try { try {
@ -149,6 +174,7 @@ public class ChatClient
return true; return true;
} }
/* this is the main wait-for-input-and-respond loop */
private void mainloop() private void mainloop()
{ {
BufferedReader br = new BufferedReader( BufferedReader br = new BufferedReader(
@ -158,13 +184,16 @@ public class ChatClient
{ {
for (;;) for (;;)
{ {
/* print a prompt */
System.out.print("> "); System.out.print("> ");
/* read a line of input */
line = br.readLine(); line = br.readLine();
if (line == null) if (line == null)
break; break;
line = line.trim(); line = line.trim();
if (line.equals("")) if (line.equals(""))
continue; continue;
/* break the input into the command word, and the rest */
String command, rest; String command, rest;
int spaceIndex = line.indexOf(' '); int spaceIndex = line.indexOf(' ');
if (spaceIndex > 0) if (spaceIndex > 0)
@ -225,7 +254,7 @@ public class ChatClient
/* don't send a message to ourselves */ /* don't send a message to ourselves */
if (!ri.getUserName().equals(myUserName)) if (!ri.getUserName().equals(myUserName))
{ {
/* send message if user available */ /* send message only if user available */
if (ri.getStatus()) if (ri.getStatus())
{ {
sendUserMessage(ri.getUserName(), rest); sendUserMessage(ri.getUserName(), rest);
@ -282,6 +311,8 @@ public class ChatClient
System.exit(0); 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) private void sendUserMessage(String user, String message)
{ {
try 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[]) public static void main(String args[])
{ {
String user; String user;
String host = "localhost"; String host = "localhost"; /* default host */
int port = 1099; int port = 1099; /* default port */
if (args.length < 1) if (args.length < 1)
{ {

View File

@ -1,11 +1,22 @@
// Author: Josh Holtrop
// Date: 2008-09-17
// CS656
// Lab Excersize 2
import java.util.HashMap; import java.util.HashMap;
import java.rmi.registry.LocateRegistry; import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry; import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject; 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 public class PresenceServiceImpl implements PresenceService
{ {
/* A structure to store information about the currently
* registered users */
private HashMap<String, RegistrationInfo> myRegisteredUsers = private HashMap<String, RegistrationInfo> myRegisteredUsers =
new HashMap<String, RegistrationInfo>(); new HashMap<String, RegistrationInfo>();
@ -14,28 +25,35 @@ public class PresenceServiceImpl implements PresenceService
super(); super();
} }
/* a user calls this method to register with the presence service */
public void register(RegistrationInfo reg) public void register(RegistrationInfo reg)
{ {
myRegisteredUsers.put(reg.getUserName(), reg); myRegisteredUsers.put(reg.getUserName(), reg);
// System.out.println("register(): " + reg.getUserName() + ", " + reg.getHost() + ", " + reg.getPort() + ", " + reg.getStatus()); // 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) public void unregister(String userName)
{ {
myRegisteredUsers.remove(userName); myRegisteredUsers.remove(userName);
// System.out.println("unregister(): " + 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) public RegistrationInfo lookup(String name)
{ {
return myRegisteredUsers.get(name); return myRegisteredUsers.get(name);
} }
/* a user calls this method to retrieve a list of all the
* registered users */
public RegistrationInfo[] listRegisteredUsers() public RegistrationInfo[] listRegisteredUsers()
{ {
return myRegisteredUsers.values().toArray(new RegistrationInfo[0]); return myRegisteredUsers.values().toArray(new RegistrationInfo[0]);
} }
/* the main method is invoked when starting the presence service */
public static void main(String[] args) public static void main(String[] args)
{ {
int port = 1099; int port = 1099;