ch.epfl.lpd.ids.server
Class ChatServer

java.lang.Object
  extended by ch.epfl.lpd.ids.server.ChatServer
All Implemented Interfaces:
IServer

public class ChatServer
extends Object
implements IServer

This class represents the chat server. The server manages a list of connected chat clients, is able to receive messages from those clients as well as dispatch those messages to its connected clients. Moreover, the clients can get a list of the connected clients from this server

Author:
Sebastien Baehni

Constructor Summary
ChatServer()
          Creates a new instance of ChatServer and initialize the ArrayList of InternalClient.
 
Method Summary
 boolean connect(String username)
          Method called by a client to connect to this server.
 boolean disconnect(String username)
          Method called by a client to disconnect from this server.
 String[] getListOfClients()
          Method used to retrieve the list of the connected clients.
 IMessage getMessage(String username)
          Method called by the chat client to get the last message sent from a chat client to this server.
 void sendMessage(String username, IMessage msg)
          Method called by the chat client to send the messages to this server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ChatServer

public ChatServer()
Creates a new instance of ChatServer and initialize the ArrayList of InternalClient.

Method Detail

connect

public boolean connect(String username)
Method called by a client to connect to this server.

Specified by:
connect in interface IServer
Parameters:
username - The username of the client.
Returns:
True if the connection was possible (i.e., no client with the same provided username is already connected), false otherwise.

disconnect

public boolean disconnect(String username)
Method called by a client to disconnect from this server.

Specified by:
disconnect in interface IServer
Parameters:
username - The username of the chat client that wants to be disconnected.
Returns:
True if the chat client was able to disconnect from this server, false otherwise.

sendMessage

public void sendMessage(String username,
                        IMessage msg)
Method called by the chat client to send the messages to this server.

Specified by:
sendMessage in interface IServer
Parameters:
username - The username of the client that is sending the message.
msg - The IMessage sends from the client to the server.

getMessage

public IMessage getMessage(String username)
Method called by the chat client to get the last message sent from a chat client to this server.

Specified by:
getMessage in interface IServer
Parameters:
username - The username of the client that wants to retrieve the last message sent. This username is used to wait on the right IBlockingQueue that corresponds to this user.
Returns:
The last IMessage sent by a chat client to this server.

getListOfClients

public String[] getListOfClients()
Method used to retrieve the list of the connected clients.

Specified by:
getListOfClients in interface IServer
Returns:
An array of string representing the array of connected clients' usernames.