The Artima Developer Community
Sponsored Link

Java Answers Forum
implementation file!!

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
john smith

Posts: 2
Nickname: county02
Registered: Nov, 2002

implementation file!! Posted: Nov 26, 2002 6:08 AM
Reply to this message Reply
Advertisement
Hi, ive also been told that there has to be a small but crucial modification made for this to work as required, i cant see what!!




import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
* 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)) {

// Force sender to have a login ID

String senderLogin = sender.getLogin();
if (senderLogin == null || senderLogin == "")
senderLogin = "Anon";

// Route message from sender to all clients

Iterator i = clientList.iterator();
while (i.hasNext()) {
ChatClient client = (ChatClient) i.next();
client.receiveChat(senderLogin + ": " + message);
}

}
else
throw new ChatException("client not registered!");
}


}

Topic: why does my code print out the same word 12 times Previous Topic   Next Topic Topic: help with reading in a line from a txt file

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use