Chat server principal class that listens on socket for new connections. More...
Classes | |
interface | Context |
Provides message presentation context to instance of ChatServer . More... | |
Public Member Functions | |
ChatServer (int port, Context context) | |
Creates instance of the chat server. | |
int | getClientCount () |
Get number of active client back-ends. | |
void | broadcast (ChatServerClient from, String message) |
Broadcasts message (terminated with new line) to all clients. | |
void | wwhhoo (ChatServerClient from) |
Responds to "wwhhoo" message. | |
void | logEliza (String message, ChatServerClient to) |
Logs messages from Eliza sent to client. | |
void | addClient (Socket clientSocket) |
Adds new client's back-end to the list of back-ends. | |
void | removeClient (ChatServerClient cli) |
Removes existing client's back-end from the list of back-ends. | |
void | run () |
Listens on socket for new connections. | |
Private Attributes | |
String | serverID |
Server identifier; concatenated host name with TCP port. | |
int | port |
TCP port that chat server listens for new connections. | |
ArrayList< ChatServerClient > | clients |
Collection of active threaded chat clients (instances of ChatServerClient ). | |
volatile boolean | running |
Indicates if thread is (or should be) running. | |
Context | context |
Event (call-back) context for the instance. |
Chat server principal class that listens on socket for new connections.
It instantiates a new threaded client back-end for each new connection and keeps all such client connections in the list. Client back-end thread purges itself from the list after remote end is disconnected.
Definition at line 17 of file ChatServer.java.
ChatServer.ChatServer | ( | int | port, |
Context | context | ||
) |
Creates instance of the chat server.
port | TCP port to listen |
context | context where to log messages and send status updates |
Definition at line 73 of file ChatServer.java.
void ChatServer.addClient | ( | Socket | clientSocket ) |
Adds new client's back-end to the list of back-ends.
Definition at line 150 of file ChatServer.java.
References clients, context, ChatServer.Context.logStatus(), and serverID.
Referenced by run().
{ int n; // will contain number of client after removal, used for status message synchronized( clients ) { ChatServerClient client = new ChatServerClient( this, clientSocket ); clients.add( client ); client.start (); n = clients.size (); } /* update context status message appropriately */ context.logStatus( serverID + ( n == 0 ? ", idle" : n == 1 ? ", 1 client active " : ", " + n + " clients active" ) ); }
void ChatServer.broadcast | ( | ChatServerClient | from, |
String | message | ||
) |
Broadcasts message (terminated with new line) to all clients.
from | originator, if null means that system sends a message |
message | textual message that should be broadcasted |
Definition at line 99 of file ChatServer.java.
References clients, context, ChatServerClient.getClientName(), and ChatServer.Context.logMessage().
Referenced by ChatServerClient.respondToMessage(), and ChatServerClient.run().
{ if ( from == null ) { // Message from the system context.logMessage( message ); message = "[System] :: " + message; } else { context.logMessage( "[" + from.getClientName() + "] " + message ); } synchronized( clients ) { for( ChatServerClient c: clients ) { c.send( message ); } } }
int ChatServer.getClientCount | ( | ) |
Get number of active client back-ends.
Definition at line 85 of file ChatServer.java.
References clients.
Referenced by ChatServerClient.respondToMessage(), and ChatServerClient.run().
void ChatServer.logEliza | ( | String | message, |
ChatServerClient | to | ||
) |
Logs messages from Eliza sent to client.
message | textual message that should be broadcasted |
to | client |
Definition at line 142 of file ChatServer.java.
References context, ChatServerClient.getClientName(), and ChatServer.Context.logMessage().
Referenced by ChatServerClient.sendFromEliza().
{ context.logMessage( "Eliza -> [" + to.getClientName() + "] " + message ); }
void ChatServer.removeClient | ( | ChatServerClient | cli ) |
Removes existing client's back-end from the list of back-ends.
Definition at line 175 of file ChatServer.java.
References clients, context, ChatServer.Context.logStatus(), and serverID.
Referenced by ChatServerClient.run().
{ int n; // will contain number of client after removal, used for status message synchronized( clients ) { clients.remove( cli ); n = clients.size (); } /* update context status message appropriately */ context.logStatus( serverID + ( n == 0 ? ", idle" : n == 1 ? ", 1 client active " : ", " + n + " clients active" ) ); }
void ChatServer.run | ( | ) |
Listens on socket for new connections.
After accepting new connection, instantiates a new threaded client back-end for that connection.
Definition at line 199 of file ChatServer.java.
References addClient(), context, ChatServer.Context.logMessage(), ChatServer.Context.logStatus(), port, running, and serverID.
{ ServerSocket socket = null; /* Create socket */ try { socket = new ServerSocket( this.port ); } catch( IOException e ) { context.logMessage( e.toString () ); running = false; } /* Setup server ID, if we have socket... */ if ( running ) { try { serverID = InetAddress.getLocalHost().getHostName () + ":" + socket.getLocalPort (); } catch( UnknownHostException e ) { serverID = "unknown:" + socket.getLocalPort (); } context.logStatus( serverID + ", idle" ); context.logMessage( "Server at " + serverID + " ready to accept new clients..." ); } /* Listen for new connections, accept clients and add them to the * list of back-ends. */ while( running ) { try { addClient( socket.accept () ); } catch ( IOException e ) { context.logMessage( e.toString () ); running = false; } } /* Inform parent that we are dead. Note that <code>", dead"</code> will * trigger also change in color of the log area background. */ context.logStatus( serverID + ":" + port + ", dead" ); context.logMessage( "Done." ); }
void ChatServer.wwhhoo | ( | ChatServerClient | from ) |
Responds to "wwhhoo" message.
Broadcasts list of connected users to all connected users.
Definition at line 121 of file ChatServer.java.
References clients, context, ChatServerClient.getClientName(), and ChatServer.Context.logMessage().
Referenced by ChatServerClient.respondToMessage().
{ context.logMessage( "[" + from.getClientName() + "] wwhhoo" ); synchronized( clients ) { for( ChatServerClient c: clients ) { String message = "WWHHOO: " + c.getClientName(); for( ChatServerClient c2: clients ) { c2.send( message ); } } } }
ArrayList<ChatServerClient> ChatServer.clients [private] |
Collection of active threaded chat clients (instances of ChatServerClient
).
Definition at line 55 of file ChatServer.java.
Referenced by addClient(), broadcast(), ChatServer(), getClientCount(), removeClient(), and wwhhoo().
Context ChatServer.context [private] |
Event (call-back) context for the instance.
Definition at line 65 of file ChatServer.java.
Referenced by addClient(), broadcast(), ChatServer(), logEliza(), removeClient(), run(), and wwhhoo().
int ChatServer.port [private] |
TCP port that chat server listens for new connections.
Definition at line 49 of file ChatServer.java.
Referenced by ChatServer(), and run().
volatile boolean ChatServer.running [private] |
Indicates if thread is (or should be) running.
Definition at line 60 of file ChatServer.java.
Referenced by ChatServer(), and run().
String ChatServer.serverID [private] |
Server identifier; concatenated host name with TCP port.
Definition at line 44 of file ChatServer.java.
Referenced by addClient(), ChatServer(), removeClient(), and run().