Simple multi-threaded chat server with GUI. More...


Public Member Functions | |
| ChatServerFrame (String args[]) | |
Creates a new instance of the ChatClientFrame. | |
| void | logMessage (String str) |
| Logs message in log area. | |
| void | logStatus (String str) |
| Updates status message by setting window title. | |
Static Public Member Functions | |
| static void | main (String args[]) |
| Main entry point. | |
Static Private Member Functions | |
| static String | now () |
| Gets current time stamp. | |
Private Attributes | |
| int | port = 2000 |
| TCP port that chat server listens for new connections. | |
| JTextArea | logArea |
Static Private Attributes | |
| static final long | serialVersionUID = 7408497530711923022L |
| Implements java.io.Serializable interface. | |
Simple multi-threaded chat server with GUI.
Definition at line 20 of file ChatServerFrame.java.
| ChatServerFrame.ChatServerFrame | ( | String | args[] ) |
Creates a new instance of the ChatClientFrame.
| args | the command line arguments passed to main |
Definition at line 41 of file ChatServerFrame.java.
Referenced by main().
{
super( "IP1-4.1.2: Yet Another Simple Chat Server" );
setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
//////////////////////////////////////////////////////////////////////////////////
/* Top level components
*/
Font textFont = new Font( Font.MONOSPACED, Font.PLAIN, 14 );
logArea = new JTextArea ();
logArea.setFont( textFont );
logArea.setBackground( new Color( 0, 0, 128 ) );
logArea.setForeground( new Color( 220, 220, 192 ) );
logArea.setLineWrap( true );
logArea.setWrapStyleWord( true );
logArea.setEditable( false );
JScrollPane logPane = new JScrollPane ();
logPane.setViewportView( logArea );
add( logPane, BorderLayout.CENTER );
/* Adjust window dimensions not to exceed screen dimensions ...
*/
Dimension win = new Dimension( 1024, 600 );
Dimension scsz = Toolkit.getDefaultToolkit().getScreenSize();
win.width = Math.min( win.width, scsz.width );
win.height = Math.min( win.height, scsz.height - 40 );
setSize( win );
/* ... then center window on the screen.
*/
setLocation( ( scsz.width - win.width )/2, ( scsz.height - 40 - win.height )/2 );
/* Parse arguments: [ <port> ]
*/
if ( args.length >= 1 ) {
try {
port = Integer.parseInt( args[ 0 ] );
} catch ( NumberFormatException e ) {
// TODO might report that we are using default port?
}
}
/* Start receiving connections
*/
ChatServer server = new ChatServer( port, this );
server.start();
}
| void ChatServerFrame.logMessage | ( | String | str ) | [virtual] |
Logs message in log area.
| str | actual message |
Implements ChatServer.Context.
Definition at line 112 of file ChatServerFrame.java.
| void ChatServerFrame.logStatus | ( | String | str ) | [virtual] |
Updates status message by setting window title.
| str | new status message |
Implements ChatServer.Context.
Definition at line 128 of file ChatServerFrame.java.
References logArea.
{
setTitle( "IP1-4.1.2 Chat Server " + str );
/* Change background color to dark red if the socket is dead.
*/
if ( str.contains( ", dead") ) {
logArea.setBackground( new Color( 92, 0, 0 ) );
}
}
| static void ChatServerFrame.main | ( | String | args[] ) | [static] |
Main entry point.
| args | the command line arguments |
Definition at line 144 of file ChatServerFrame.java.
References ChatServerFrame().
{
final String[] copyOfArgs = args;
java.awt.EventQueue.invokeLater(
new Runnable() {
public void run() {
new ChatServerFrame( copyOfArgs ).setVisible( true );
}
}
);
}
| static String ChatServerFrame.now | ( | ) | [static, private] |
Gets current time stamp.
Definition at line 99 of file ChatServerFrame.java.
Referenced by logMessage().
{
Calendar cal = Calendar.getInstance ();
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
return sdf.format( cal.getTime() );
}
JTextArea ChatServerFrame.logArea [private] |
Definition at line 34 of file ChatServerFrame.java.
Referenced by ChatServerFrame(), logMessage(), and logStatus().
int ChatServerFrame.port = 2000 [private] |
TCP port that chat server listens for new connections.
Definition at line 30 of file ChatServerFrame.java.
Referenced by ChatServerFrame().
final long ChatServerFrame.serialVersionUID = 7408497530711923022L [static, private] |
Implements java.io.Serializable interface.
Definition at line 25 of file ChatServerFrame.java.
1.7.2