Crossfire JXClient, Trunk
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
ChatServer Class Reference
Inheritance diagram for ChatServer:
Inheritance graph
Collaboration diagram for ChatServer:
Collaboration graph

Public Member Functions

 ChatServer (int port) throws IOException
 
SocketAddress getSocketAddress () throws IOException
 
void removeClient (Client client)
 
void run ()
 
void shutdown () throws InterruptedException, IOException
 
void writeMessageToClients (Client client, String message)
 

Static Public Member Functions

static void main (String[] args) throws IOException
 

Private Member Functions

AsynchronousServerSocketChannel createListener (AsynchronousChannelGroup channelGroup) throws IOException
 
void handleNewConnection (AsynchronousSocketChannel channel)
 
AsynchronousServerSocketChannel openChannel (AsynchronousChannelGroup channelGroup) throws IOException
 

Static Private Member Functions

static void usage ()
 

Private Attributes

final AsynchronousChannelGroup channelGroup
 
final List< Clientconnections = Collections.synchronizedList(new ArrayList<Client>())
 
final AsynchronousServerSocketChannel listener
 
int port
 

Detailed Description

Implements a chat server, this class holds the list of

clients

connected to the server. It sets up a server socket using AsynchronousServerSocketChannel listening to a specified port.

Definition at line 54 of file ChatServer.java.

Constructor & Destructor Documentation

◆ ChatServer()

ChatServer.ChatServer ( int  port) throws IOException
Parameters
portto listen to
Exceptions
java.io.IOExceptionwhen failing to start the server

Definition at line 65 of file ChatServer.java.

References channelGroup, createListener(), listener, and port.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ createListener()

AsynchronousServerSocketChannel ChatServer.createListener ( AsynchronousChannelGroup  channelGroup) throws IOException
private

Definition at line 115 of file ChatServer.java.

References channelGroup, listener, openChannel(), and port.

Referenced by ChatServer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getSocketAddress()

SocketAddress ChatServer.getSocketAddress ( ) throws IOException
Returns
The socket address that the server is bound to
Exceptions
java.io.IOExceptionif an I/O error occurs

Definition at line 77 of file ChatServer.java.

References listener.

◆ handleNewConnection()

void ChatServer.handleNewConnection ( AsynchronousSocketChannel  channel)
private

Creates a new client and adds it to the list of connections. Sets the clients handler to the initial state of NameReader

Parameters
channelthe newly accepted channel

Definition at line 132 of file ChatServer.java.

References client, and connections.

Referenced by run().

Here is the caller graph for this function:

◆ main()

static void ChatServer.main ( String[]  args) throws IOException
static

Definition at line 170 of file ChatServer.java.

References ChatServer(), port, and usage().

Here is the call graph for this function:

◆ openChannel()

AsynchronousServerSocketChannel ChatServer.openChannel ( AsynchronousChannelGroup  channelGroup) throws IOException
private

Definition at line 122 of file ChatServer.java.

References channelGroup.

Referenced by createListener().

Here is the caller graph for this function:

◆ removeClient()

void ChatServer.removeClient ( Client  client)

Definition at line 161 of file ChatServer.java.

References client, and connections.

Referenced by ClientReader.run().

Here is the caller graph for this function:

◆ run()

void ChatServer.run ( )

Start accepting connections

Definition at line 84 of file ChatServer.java.

References handleNewConnection(), listener, and result.

Here is the call graph for this function:

◆ shutdown()

void ChatServer.shutdown ( ) throws InterruptedException, IOException

Shuts down the server

Exceptions
InterruptedExceptionif terminated while waiting for shutdown
IOExceptionif failing to shutdown the channel group

Definition at line 107 of file ChatServer.java.

References channelGroup.

◆ usage()

static void ChatServer.usage ( )
staticprivate

Definition at line 165 of file ChatServer.java.

Referenced by main().

Here is the caller graph for this function:

◆ writeMessageToClients()

void ChatServer.writeMessageToClients ( Client  client,
String  message 
)

Sends a message to all clients except the source. The method is synchronized as it is desired that messages are sent to all clients in the same order as received.

Parameters
clientthe message source
messagethe message to be sent

Definition at line 151 of file ChatServer.java.

References client, and connections.

Referenced by MessageReader.beforeRead().

Here is the caller graph for this function:

Member Data Documentation

◆ channelGroup

final AsynchronousChannelGroup ChatServer.channelGroup
private

Definition at line 58 of file ChatServer.java.

Referenced by ChatServer(), createListener(), openChannel(), and shutdown().

◆ connections

final List<Client> ChatServer.connections = Collections.synchronizedList(new ArrayList<Client>())
private

Definition at line 55 of file ChatServer.java.

Referenced by handleNewConnection(), removeClient(), and writeMessageToClients().

◆ listener

final AsynchronousServerSocketChannel ChatServer.listener
private

Definition at line 57 of file ChatServer.java.

Referenced by ChatServer(), createListener(), getSocketAddress(), and run().

◆ port

int ChatServer.port
private

Definition at line 56 of file ChatServer.java.

Referenced by ChatServer(), createListener(), and main().


The documentation for this class was generated from the following file: