/** * Implementation for a remote object representing a chat service. * * A small change will need to be made to this file before it will work. * * @author Nick Efford * @version 1.0 (2002-11-06) */
public class ChatServiceImpl {
List clientList; // list of registered clients
/** * Creates a remote ChatService object, initially with no clients. * * @throws RemoteException if remote invocation fails */
public ChatServiceImpl() throws RemoteException { clientList = new ArrayList(); }
/** * Registers a client with this service. * * @param client ChatClient wishing to be registered * @return true if registration succeeded, false if client * is already registered * @throws RemoteException if remote invocation fails */
public boolean addClient(ChatClient client) throws RemoteException { if (! clientList.contains(client)) { clientList.add(client); return true; } else return false; }
/** * Removes a client, so they will no longer receive messages. * * @param client ChatClient to be removed * @return true if removal succeeds, false if client wasn't * already registered * @throws RemoteException if remote invocation fails */
public boolean removeClient(ChatClient client) throws RemoteException { if (clientList.contains(client)) { clientList.remove(client); return true; } else return false; }
/** * Distributes a message from one client to all clients * registered with the service. * * @param sender ChatClient from whom message originates * @param message string of text to be sent to all clients * @throws ChatException if sender is not registered with service * @throws RemoteException if remote invocation fails */
public void sendMessage(ChatClient sender, String message) throws ChatException, RemoteException { if (clientList.contains(sender)) {