ch.epfl.lpd.ids.client
Class ChatClient

java.lang.Object
  extended by java.util.Observable
      extended by ch.epfl.lpd.ids.client.ChatClient
All Implemented Interfaces:
IChatClient, Runnable

public class ChatClient
extends Observable
implements Runnable, IChatClient

This class represents a chat client. A chat client is responsible for creating its GUI as well as the two threads that will fetch new messages and the list of connected clients.

Author:
Sebastien Baehni

Field Summary
protected  ClientListFetcher clf
          The reference to the ClientListFetcher that is responsible for retrieving the list of the connected clients.
protected  boolean connected
          Boolean used to check is this client is connected to the server or not.
protected  IWindowManager iw
          The reference to the IWindowManager in order to correctly dispose the ressources when the client is closed.
protected  IServer server
          The reference to the IServer.
protected  String username
          The username of this chat client.
 
Constructor Summary
ChatClient(IServer server, IWindowManager iw)
          Creates a new instance of ChatClient.
 
Method Summary
 void close()
          Method called by ClientGUI when the user close the chat GUI.
 boolean connect(String username)
          Method called by the ClientGUI when the user wants to connect to the server.
 boolean disconnect(String username)
          Method called by the ClientGUI when the user wants to disconnect from the IServer.
 void run()
          Method called when a new thread representing this client is started.
 void sendMessage(IMessage msg)
          Method called by the ClientGUI when the user wants to send a message to the other chat clients.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

server

protected IServer server
The reference to the IServer. This reference will be used to send and receive messages.


iw

protected IWindowManager iw
The reference to the IWindowManager in order to correctly dispose the ressources when the client is closed.


username

protected String username
The username of this chat client.


connected

protected boolean connected
Boolean used to check is this client is connected to the server or not.


clf

protected ClientListFetcher clf
The reference to the ClientListFetcher that is responsible for retrieving the list of the connected clients.

Constructor Detail

ChatClient

public ChatClient(IServer server,
                  IWindowManager iw)
Creates a new instance of ChatClient. This constructor basically creates the ClientGUI, the ClientListFetcher and initialize the MVC.

Parameters:
server - The IServer reference.
iw - The reference to the IWindowManager.
Method Detail

connect

public boolean connect(String username)
Method called by the ClientGUI when the user wants to connect to the server. This method calls the connect() method on the IServer

Specified by:
connect in interface IChatClient
Parameters:
username - The username of the client.
Returns:
True if the client was able to connect, false otherwise.

disconnect

public boolean disconnect(String username)
Method called by the ClientGUI when the user wants to disconnect from the IServer.

Specified by:
disconnect in interface IChatClient
Parameters:
username - The username of the client.
Returns:
True if the client was successfully disconnected, false otherwise.

sendMessage

public void sendMessage(IMessage msg)
Method called by the ClientGUI when the user wants to send a message to the other chat clients. This method creates a new IMessage and sends it to the server.

Specified by:
sendMessage in interface IChatClient
Parameters:
msg - The message as a IMessage.

close

public void close()
Method called by ClientGUI when the user close the chat GUI. This stops the different threads, disconnects from the server and calls the IWindowManager to inform it that it can release the ressources for this chat client.

Specified by:
close in interface IChatClient

run

public void run()
Method called when a new thread representing this client is started. This thread wait on its IBlockingQueue on the server until a new message is received. Once this message is received, it is put in the GUI.

Specified by:
run in interface Runnable