• Main Page
  • Related Pages
  • Classes
  • Files
  • File List

PortConnect.java

Go to the documentation of this file.
00001 
00002 import java.net.InetSocketAddress;
00003 import java.net.Socket;
00004 
00005 /**
00006  *  Encapsulates port scanning worker thread. The thread retrieves end-points
00007  *  (to be scanned) from the owner's pool and reports with a call-back what
00008  *  was found on the end-point. 
00009  *  
00010  *  @author Mikica B Kocic
00011  */
00012 public class PortConnect extends Thread 
00013 {
00014     /**
00015      *  InetSocketAddress pool context where socket addresses (end-points) 
00016      *  are retrieved from and where information about end-points are posted to. 
00017      */
00018     public interface Context
00019     {
00020         /**
00021          *  Gets next socket address from the context.
00022          */
00023         public abstract InetSocketAddress getNextSocketAddress ();
00024 
00025         /**
00026          *  Reports information that worker thread has found about
00027          *  socket address (end-point)
00028          */
00029         public abstract void onPortConnected( long threadId, InetSocketAddress addr,
00030                 boolean ok, String error, int timeMillis );
00031         
00032         /**
00033          *  Worker thread reports it is alive
00034          */
00035         public abstract void workerThreadSignIn( long threadId );
00036         
00037         /**
00038          *  Worker thread reports it is dead
00039          */
00040         public abstract void workerThreadSignOut( long threadId );
00041     }
00042 
00043     /**
00044      *  The timeout value to be used when connecting sockets in milliseconds 
00045      */
00046     private int timeoutMillis;
00047     
00048     /**
00049      *  Worker thread's socket address pool context
00050      */
00051     private Context context;
00052 
00053     /**
00054      *  Creates instance of the worker thread.
00055      *  
00056      *  @param context        worker thread's presentation and status context
00057      *  @param timeoutMillis  the timeout value to be used in milliseconds 
00058      */
00059     public PortConnect( Context context, int timeoutMillis )
00060     {
00061         this.context  = context;
00062         this.timeoutMillis  = timeoutMillis;
00063     }
00064 
00065     /**
00066      *  Connects to socket address to check if there is service running on server
00067      *  
00068      *  @param endpoint - the socket address (IP address + port)
00069      */
00070     public void reportConnectionStatus( InetSocketAddress endpoint )
00071     {
00072         boolean portStatus = false; // port dead by default
00073         String error = "Failed";
00074 
00075         /* Try to connect...
00076          */
00077         Socket socket = null;
00078         long startTime = System.nanoTime ();
00079         
00080         try  {
00081             socket = new Socket ();
00082             socket.connect( endpoint, timeoutMillis );
00083             error = "Connected";
00084             portStatus = true;
00085         } catch( Exception e ) {
00086             /* don't care */
00087             error = e.toString ();
00088         }
00089 
00090         if ( socket != null && ! socket.isClosed () ) {
00091             try {
00092                 socket.close ();
00093             } catch( Exception e ) {
00094                 /* don't care */
00095             }
00096         }
00097 
00098         /* ... then call-back the context with port status report.
00099          */
00100         this.context.onPortConnected( 
00101                 getId(), endpoint, portStatus, error, 
00102                 (int) ( ( System.nanoTime () - startTime ) / 1000000l ) );
00103     }
00104     
00105     /**
00106      *  Worker thread:
00107      *  1) retrieves next end-point to be checked from the parent's context,
00108      *  2) detects whether remote end-points is alive or dead,
00109      *  3) reports what's found to the parent.
00110      */
00111     @Override
00112     public void run()
00113     {
00114         context.workerThreadSignIn( getId () );
00115 
00116         while( true )
00117         {
00118             InetSocketAddress endpoint = context.getNextSocketAddress ();
00119             
00120             /* If the parent's end-points are depleted, quit thread.
00121              */
00122             if ( endpoint == null ) {
00123                 break;
00124             }
00125             
00126             /* Detect whether server on the end-point is dead or alive,
00127              * then report what's found.
00128              */
00129             reportConnectionStatus( endpoint );
00130         }
00131 
00132         context.workerThreadSignOut( getId () );
00133         
00134         /* Block until killed
00135          */
00136         try {
00137             Thread.sleep( 365 * 86400 * 1000 );
00138         } catch( InterruptedException e ) {
00139             /* ignored */
00140         }
00141     }
00142 }

Generated on Thu Dec 16 2010 12:29:37 for Port Scanner by  doxygen 1.7.2