Classes | Public Member Functions | Private Attributes

PortConnect Class Reference

Encapsulates port scanning worker thread. More...

Collaboration diagram for PortConnect:
Collaboration graph
[legend]

List of all members.

Classes

interface  Context
 InetSocketAddress pool context where socket addresses (end-points) are retrieved from and where information about end-points are posted to. More...

Public Member Functions

 PortConnect (Context context, int timeoutMillis)
 Creates instance of the worker thread.
void reportConnectionStatus (InetSocketAddress endpoint)
 Connects to socket address to check if there is service running on server.
void run ()
 Worker thread: 1) retrieves next end-point to be checked from the parent's context, 2) detects whether remote end-points is alive or dead, 3) reports what's found to the parent.

Private Attributes

int timeoutMillis
 The timeout value to be used when connecting sockets in milliseconds.
Context context
 Worker thread's socket address pool context.

Detailed Description

Encapsulates port scanning worker thread.

The thread retrieves end-points (to be scanned) from the owner's pool and reports with a call-back what was found on the end-point.

Author:
Mikica B Kocic

Definition at line 12 of file PortConnect.java.


Constructor & Destructor Documentation

PortConnect.PortConnect ( Context  context,
int  timeoutMillis 
)

Creates instance of the worker thread.

Parameters:
contextworker thread's presentation and status context
timeoutMillisthe timeout value to be used in milliseconds

Definition at line 59 of file PortConnect.java.

References context, and timeoutMillis.


Member Function Documentation

void PortConnect.reportConnectionStatus ( InetSocketAddress  endpoint )

Connects to socket address to check if there is service running on server.

Parameters:
endpoint- the socket address (IP address + port)

Definition at line 70 of file PortConnect.java.

References context, PortConnect.Context.onPortConnected(), and timeoutMillis.

Referenced by run().

    {
        boolean portStatus = false; // port dead by default
        String error = "Failed";

        /* Try to connect...
         */
        Socket socket = null;
        long startTime = System.nanoTime ();
        
        try  {
            socket = new Socket ();
            socket.connect( endpoint, timeoutMillis );
            error = "Connected";
            portStatus = true;
        } catch( Exception e ) {
            /* don't care */
            error = e.toString ();
        }

        if ( socket != null && ! socket.isClosed () ) {
            try {
                socket.close ();
            } catch( Exception e ) {
                /* don't care */
            }
        }

        /* ... then call-back the context with port status report.
         */
        this.context.onPortConnected( 
                getId(), endpoint, portStatus, error, 
                (int) ( ( System.nanoTime () - startTime ) / 1000000l ) );
    }
void PortConnect.run (  )

Worker thread: 1) retrieves next end-point to be checked from the parent's context, 2) detects whether remote end-points is alive or dead, 3) reports what's found to the parent.

Definition at line 112 of file PortConnect.java.

References context, PortConnect.Context.getNextSocketAddress(), reportConnectionStatus(), PortConnect.Context.workerThreadSignIn(), and PortConnect.Context.workerThreadSignOut().

    {
        context.workerThreadSignIn( getId () );

        while( true )
        {
            InetSocketAddress endpoint = context.getNextSocketAddress ();
            
            /* If the parent's end-points are depleted, quit thread.
             */
            if ( endpoint == null ) {
                break;
            }
            
            /* Detect whether server on the end-point is dead or alive,
             * then report what's found.
             */
            reportConnectionStatus( endpoint );
        }

        context.workerThreadSignOut( getId () );
        
        /* Block until killed
         */
        try {
            Thread.sleep( 365 * 86400 * 1000 );
        } catch( InterruptedException e ) {
            /* ignored */
        }
    }

Member Data Documentation

Worker thread's socket address pool context.

Definition at line 51 of file PortConnect.java.

Referenced by PortConnect(), reportConnectionStatus(), and run().

The timeout value to be used when connecting sockets in milliseconds.

Definition at line 46 of file PortConnect.java.

Referenced by PortConnect(), and reportConnectionStatus().


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