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

TraceroutePane Class Reference

Trace-route/ping application using ICMP from Jpcap. More...

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

List of all members.

Public Member Functions

 TraceroutePane (String args[])
 Creates a new instance of the ChatClientFrame.
void println (String str)
 println() == short cut for logMesage() + logNewLine () without timestamp
void logNewLine ()
 Logs new line.
void logMessage (String str, boolean timestamp)
 Logs message, with optional timestamp.
void onTracerouteCompleted ()
 On trace route completed call-back from the instance of Traceroute
void parseCommand ()
 Parses input message or command from inputMsg.

Static Public Member Functions

static void main (String args[])
 Main entry point.

Private Member Functions

void startButton_Clicked (MouseEvent evt)
 Starts fetching data from URL specified in urlText.

Static Private Member Functions

static String now ()
 Gets current time stamp (millis resolution)

Private Attributes

JButton startButton
JTextField inputCmd
JTextArea logArea
Traceroute tracer
JComboBox ifaceList

Static Private Attributes

static final long serialVersionUID = 3590355845490077407L
 Implements java.io.Serializable interface.

Detailed Description

Trace-route/ping application using ICMP from Jpcap.

Author:
Mikica B Kocic

Definition at line 28 of file TraceroutePane.java.


Constructor & Destructor Documentation

TraceroutePane.TraceroutePane ( String  args[] )

Creates a new instance of the ChatClientFrame.

Parameters:
argsthe command line arguments passed to main

Definition at line 48 of file TraceroutePane.java.

References Traceroute.getInterfaceList(), ifaceList, inputCmd, logArea, logNewLine(), parseCommand(), println(), startButton, startButton_Clicked(), and tracer.

Referenced by main().

    {
        super( "IP1-4.3: Traceroute, Idle" );
        
        setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );

        //////////////////////////////////////////////////////////////////////////////////
        
        /* GUI Components
         */
        Font textFont = new Font( Font.SANS_SERIF, Font.PLAIN, 14 );
        Font logFont  = new Font( Font.MONOSPACED, Font.PLAIN, 16 );
        
        inputCmd = new JTextField( "atlas.dsv.su.se" );
        inputCmd.setFont( textFont );
        inputCmd.setColumns( 30 );
        inputCmd.selectAll ();

        startButton = new JButton ();
        startButton.setText( "Go!" );
        startButton.setForeground( Color.black );

        startButton.addMouseListener(
                new MouseAdapter () {
                    public void mouseClicked( MouseEvent evt ) {
                        startButton_Clicked( evt );
                    }
                }
            );

        ifaceList = new JComboBox ();
        ifaceList.setEditable( false );
        
        /* Scrollable log area
         */
        logArea = new JTextArea ();
        
        logArea.setLineWrap( true );
        logArea.setWrapStyleWord( true );
        logArea.setEditable( false );
        logArea.setFont( logFont );
        logArea.setBackground( new Color(  32,  32,   0 ) );
        logArea.setForeground( new Color( 240, 192,   0 ) );

        JScrollPane logPane = new JScrollPane ();
        logPane.setViewportView( logArea );
            
        //////////////////////////////////////////////////////////////////////////////////
        
        /* Layout (self explained?): 
         *     Upper: startButton, inputCmd and ifaceList
         *     Lower: logPane
         */
        GroupLayout layout = new GroupLayout( getContentPane () );
        getContentPane().setLayout( layout );
        layout.setAutoCreateContainerGaps( true );
        layout.setAutoCreateGaps( true );

        layout.setHorizontalGroup
        (
            layout
            .createParallelGroup( Alignment.LEADING )
            .addGroup
            ( 
                layout
                .createSequentialGroup ()
                .addGroup
                ( 
                    layout
                    .createParallelGroup( Alignment.LEADING )
                    .addGroup
                    (
                        Alignment.TRAILING, 
                        layout
                        .createSequentialGroup ()
                        .addComponent( startButton, 80, 80, 80 )
                        .addComponent( inputCmd )
                        .addComponent( ifaceList, 100, 300, 300 )
                            
                    )
                    .addComponent( logPane )
                )
            )
        );
        
        layout.setVerticalGroup
        (
            layout
            .createParallelGroup( Alignment.LEADING )
            .addGroup
            (
                layout
                .createSequentialGroup ()
                .addGroup
                (
                    layout
                    .createParallelGroup( Alignment.CENTER )
                    .addComponent( startButton )
                    .addComponent( inputCmd, Alignment.CENTER, 0, 27, 27 )
                    .addComponent( ifaceList, Alignment.CENTER, 0, 26, 26 )
                )
                .addComponent( logPane )
            )
        );
        
        pack();
        
        //////////////////////////////////////////////////////////////////////////////////
        
        /* 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 );
        
        /* Ready for user to type in something...
         */
        inputCmd.requestFocus ();

        /* Keyboard event handler for ENTER in input text field
         */
        inputCmd.addKeyListener( new KeyAdapter ()
        {
            @Override public void keyPressed( KeyEvent e )
            {
                int keyCode = e.getKeyCode ();
                
                if( keyCode == KeyEvent.VK_ENTER )
                {
                    parseCommand ();
                }
            }
        } );
        
        /* Creates trace-route instance. We should have heavy exception handling here
         * as we are relying on external components: Jpcap and WinPCAP.
         * Both must be installed properly for <code>Traceroute</code> to be
         * instantiated. 
         */
        try
        {
            tracer = new Traceroute ( this );
        }
        catch( NoClassDefFoundError e ) // Jpcap is probably missing
        {
            tracer = null;

            if ( e.getMessage().toLowerCase().contains( "jpcap" ) )
            {
                println( " " );
                println( " ERROR:" );
                println( "     Jpcap is not installed!" );
                logNewLine ();
                println( " Please, download and install Jpcap from: " );
                println( "     http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/download.html" );
                logNewLine ();
                println( " You will also need to get and install WinPCAP from: " );
                println( "     http://www.winpcap.org/install/default.htm" );
            }
            else
            {
                e.printStackTrace ();
            }
        }
        catch( UnsatisfiedLinkError e ) // Wincap is probably missing
        {
            tracer = null;

            if ( e.getMessage().toLowerCase().contains( "cap.dll" ) )
            {
                println( " " );
                println( " ERROR:" );
                println( "     WinPCAP is not installed!" );
                logNewLine ();
                println( " Please, download and install WinPCAP from: " );
                println( "     http://www.winpcap.org/install/default.htm" );
                logNewLine ();
            }
            else
            {
                e.printStackTrace ();
            }
        }

        if ( tracer != null ) // survived Jpcap & WinPCAP i.e. up and running
        {
            /* Print usage
             */
            println( " " );
            println( " Usage:" );
            println( "    1) Select network interface used for transmission"
                     + " (from the upper right combo box)" );
            println( "    2) Enter hostname then press Enter or click 'Go!'" );
            logNewLine ();
            println( " To ping instead to trace route, add 'ping' after the hostname." );
            logNewLine ();

            /* Add all interfaces to combo box
             */
            for( String s : tracer.getInterfaceList () )
            {
                ifaceList.addItem( s );
            }
        }
        
        /* Transfer command line arguments into inputCmd (if any) 
         * then parse them immediately...
         */
        if ( args.length > 0 )
        {
            String join = "";
            for( String s : args ) {
                join += ( join.length () == 0 ? s : " " + s );
            }
            inputCmd.setText( join );
            parseCommand ();
        }
    }

Member Function Documentation

void TraceroutePane.logMessage ( String  str,
boolean  timestamp 
) [virtual]

Logs message, with optional timestamp.

Parameters:
strmessage that will be logged
timestampwhether to prefix message with timestamp or not

Implements Traceroute.Context.

Definition at line 326 of file TraceroutePane.java.

References logArea, logNewLine(), and now().

Referenced by println().

    {
        synchronized( logArea )
        {
            if ( timestamp )
            {
                // Ensure that there is a new-line before time-stamp
                //
                String log = logArea.getText ();
                if ( log.length () > 0 && log.charAt( log.length () - 1 ) != '\n' ) {
                    logNewLine ();
                }
                
                logArea.append( now () + "  " + str );
            }
            else
            {
                logArea.append( str );
            }

            logArea.setCaretPosition( logArea.getText().length () );
        }
    }
void TraceroutePane.logNewLine (  ) [virtual]

Logs new line.

Implements Traceroute.Context.

Definition at line 306 of file TraceroutePane.java.

References logArea.

Referenced by logMessage(), println(), and TraceroutePane().

    {
        synchronized( logArea )
        {
            if ( logArea.getText().length() != 0 )
            {
                logArea.append( "\n" );
                logArea.setRows( logArea.getRows () + 1 );
                logArea.setCaretPosition( logArea.getText().length () );
            }
        }
    }
static void TraceroutePane.main ( String  args[] ) [static]

Main entry point.

Creates and makes visible GUI...

Parameters:
argsthe command line arguments

Definition at line 409 of file TraceroutePane.java.

References TraceroutePane().

    {
        final String[] copyOfArgs = args;
        
        java.awt.EventQueue.invokeLater( 
                new Runnable() {
                    public void run() {
                        new TraceroutePane( copyOfArgs ).setVisible( true );
                    }
                }
            );
    }
static String TraceroutePane.now (  ) [static, private]

Gets current time stamp (millis resolution)

Returns:
time in ISO format

Definition at line 286 of file TraceroutePane.java.

Referenced by logMessage().

    {
        Calendar cal = Calendar.getInstance ();
        SimpleDateFormat sdf = new SimpleDateFormat( "HH:mm:ss.SSS" );
        return sdf.format( cal.getTime() );
    }
void TraceroutePane.onTracerouteCompleted (  ) [virtual]

On trace route completed call-back from the instance of Traceroute

Implements Traceroute.Context.

Definition at line 354 of file TraceroutePane.java.

References startButton.

    {
        setTitle( "IP1-4.3: Traceroute, Idle" );
        startButton.setText( "Go!" );
        startButton.setForeground( Color.black );
    }
void TraceroutePane.parseCommand (  )

Parses input message or command from inputMsg.

Also performs various commands :open, :close, :exit ...

Definition at line 365 of file TraceroutePane.java.

References ifaceList, inputCmd, Traceroute.isIdle(), startButton, Traceroute.startPinging(), Traceroute.stopTrace(), and tracer.

Referenced by startButton_Clicked(), and TraceroutePane().

    {
        if ( tracer == null ) {
            return;
        }

        /* Split string into words, removing all leading, trailing 
         * and superfluous (btw words) white-spaces
         */
        String[] args = inputCmd.getText().trim().split( "\\s{1,}" );
        
        if ( args.length < 1 ) {
            return;
        }

        if ( tracer.isIdle () )
        {
            boolean ping = ( args.length >= 2 && args[1].equalsIgnoreCase("ping") );
            
            startButton.setText( "Cancel" );
            startButton.setForeground( Color.red );

            if ( ping ) {
                setTitle( "IP1-4.3: Pinging " + args[0] + "..." );
            } else {
                setTitle( "IP1-4.3: Tracing route to " + args[0] + "..." );
            }
            
            /* Start trace-route/ping. TTL is set to 64 if second word in cmd line
             * is 'ping'.
             */
            tracer.startPinging( ifaceList.getSelectedIndex (), args[0], ping ? 64 : 0 );
        }
        else
        {
            tracer.stopTrace ();
        }
    }
void TraceroutePane.println ( String  str )

println() == short cut for logMesage() + logNewLine () without timestamp

Definition at line 296 of file TraceroutePane.java.

References logMessage(), and logNewLine().

Referenced by TraceroutePane().

    {
        logMessage( str, /*timestamp*/ false );
        logNewLine ();
    }
void TraceroutePane.startButton_Clicked ( MouseEvent  evt ) [private]

Starts fetching data from URL specified in urlText.

Definition at line 276 of file TraceroutePane.java.

References parseCommand().

Referenced by TraceroutePane().

    {
        parseCommand ();
    }

Member Data Documentation

JComboBox TraceroutePane.ifaceList [private]

Definition at line 41 of file TraceroutePane.java.

Referenced by parseCommand(), and TraceroutePane().

JTextField TraceroutePane.inputCmd [private]

Definition at line 38 of file TraceroutePane.java.

Referenced by parseCommand(), and TraceroutePane().

JTextArea TraceroutePane.logArea [private]

Definition at line 39 of file TraceroutePane.java.

Referenced by logMessage(), logNewLine(), and TraceroutePane().

final long TraceroutePane.serialVersionUID = 3590355845490077407L [static, private]

Implements java.io.Serializable interface.

Definition at line 33 of file TraceroutePane.java.

JButton TraceroutePane.startButton [private]

Definition at line 37 of file TraceroutePane.java.

Referenced by onTracerouteCompleted(), parseCommand(), and TraceroutePane().

Definition at line 40 of file TraceroutePane.java.

Referenced by parseCommand(), and TraceroutePane().


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