Encapsulates port scanning worker thread. More...
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. |
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.
Definition at line 12 of file PortConnect.java.
PortConnect.PortConnect | ( | Context | context, |
int | timeoutMillis | ||
) |
Creates instance of the worker thread.
context | worker thread's presentation and status context |
timeoutMillis | the timeout value to be used in milliseconds |
Definition at line 59 of file PortConnect.java.
References context, and timeoutMillis.
{ this.context = context; this.timeoutMillis = timeoutMillis; }
void PortConnect.reportConnectionStatus | ( | InetSocketAddress | endpoint ) |
Connects to socket address to check if there is service running on server.
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 */ } }
Context PortConnect.context [private] |
Worker thread's socket address pool context.
Definition at line 51 of file PortConnect.java.
Referenced by PortConnect(), reportConnectionStatus(), and run().
int PortConnect.timeoutMillis [private] |
The timeout value to be used when connecting sockets in milliseconds.
Definition at line 46 of file PortConnect.java.
Referenced by PortConnect(), and reportConnectionStatus().