Public Member Functions | Static Public Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes

ChatServerFrame Class Reference

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

Inheritance diagram for ChatServerFrame:
Inheritance graph
[legend]
Collaboration diagram for ChatServerFrame:
Collaboration graph
[legend]

List of all members.

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.

Detailed Description

Simple multi-threaded chat server with GUI.

Author:
Mikica B Kocic

Definition at line 20 of file ChatServerFrame.java.


Constructor & Destructor Documentation

ChatServerFrame.ChatServerFrame ( String  args[] )

Creates a new instance of the ChatClientFrame.

Parameters:
argsthe command line arguments passed to main

Definition at line 41 of file ChatServerFrame.java.

References logArea, and port.

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();
    }

Member Function Documentation

void ChatServerFrame.logMessage ( String  str ) [virtual]

Logs message in log area.

Parameters:
stractual message

Implements ChatServer.Context.

Definition at line 112 of file ChatServerFrame.java.

References logArea, and now().

    {
        synchronized( logArea )
        {
            logArea.append( now () + "  " + str + "\n" );
            logArea.setRows( logArea.getRows () + 1 );
            logArea.setCaretPosition( logArea.getText().length () );
        }
    }
void ChatServerFrame.logStatus ( String  str ) [virtual]

Updates status message by setting window title.

Parameters:
strnew 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.

Parameters:
argsthe 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.

Returns:
time in ISO format

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() );
    }

Member Data Documentation

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.


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