| 1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
| 2 |
tdb |
1.2 |
package uk.ac.ukc.iscream.filter; |
| 3 |
tdb |
1.1 |
|
| 4 |
|
|
//---IMPORTS--- |
| 5 |
|
|
import uk.ac.ukc.iscream.core.*; |
| 6 |
|
|
import uk.ac.ukc.iscream.filter.*; |
| 7 |
|
|
import java.net.Socket; |
| 8 |
|
|
import java.net.ServerSocket; |
| 9 |
|
|
import java.io.OutputStream; |
| 10 |
|
|
import java.io.IOException; |
| 11 |
|
|
import java.net.InetAddress; |
| 12 |
|
|
import java.net.UnknownHostException; |
| 13 |
|
|
import org.omg.CORBA.*; |
| 14 |
|
|
import org.omg.CosNaming.*; |
| 15 |
tdb |
1.3 |
import uk.ac.ukc.iscream.refman.*; |
| 16 |
tdb |
1.1 |
|
| 17 |
|
|
/** |
| 18 |
|
|
* A socket listener to listen for new hosts registering with the system. |
| 19 |
|
|
* When a host makes a connection, the connecton is past to an instance |
| 20 |
|
|
* of the HostInit class, which handles further communication. |
| 21 |
|
|
* |
| 22 |
tdb |
1.2 |
* @author $Author: tdb1 $ |
| 23 |
tdb |
1.3 |
* @version $Id: TCPReader.java,v 1.2 2000/11/29 19:26:00 tdb1 Exp $ |
| 24 |
tdb |
1.1 |
*/ |
| 25 |
|
|
class TCPReader extends Thread { |
| 26 |
|
|
|
| 27 |
|
|
//---FINAL ATTRIBUTES--- |
| 28 |
|
|
|
| 29 |
|
|
/** |
| 30 |
|
|
* The current CVS revision of this class |
| 31 |
|
|
*/ |
| 32 |
tdb |
1.3 |
public final String REVISION = "$Revision: 1.2 $"; |
| 33 |
tdb |
1.1 |
|
| 34 |
|
|
//---STATIC METHODS--- |
| 35 |
|
|
|
| 36 |
|
|
//---CONSTRUCTORS--- |
| 37 |
|
|
|
| 38 |
|
|
/** |
| 39 |
|
|
* Constructs a new listener |
| 40 |
|
|
* |
| 41 |
|
|
* @param logger a reference to the logger we are using |
| 42 |
|
|
* @param configManager a reference to the ConfigurationManager we are using |
| 43 |
|
|
* @param port The port that the server will listen on. |
| 44 |
|
|
*/ |
| 45 |
tdb |
1.3 |
public TCPReader(int port, Filter parent) { |
| 46 |
tdb |
1.1 |
_port = port; |
| 47 |
|
|
_parent = parent; |
| 48 |
|
|
_logger.write(toString(), Logger.SYSINIT, "started"); |
| 49 |
|
|
} |
| 50 |
|
|
|
| 51 |
|
|
|
| 52 |
|
|
|
| 53 |
|
|
//---PUBLIC METHODS--- |
| 54 |
|
|
|
| 55 |
|
|
/** |
| 56 |
|
|
* The run() method is the main loop for this thread, and we |
| 57 |
|
|
* will remain in here until such a point as something goes |
| 58 |
|
|
* wrong with the listening. After initially setting up the |
| 59 |
|
|
* ServerSocket we go round a while loop receiving connections |
| 60 |
|
|
* and then passing them off to other processes to deal with. |
| 61 |
|
|
*/ |
| 62 |
|
|
public void run(){ |
| 63 |
|
|
ServerSocket listenPort=null; |
| 64 |
|
|
// We use this boolean so we can break out of the while loop if we want |
| 65 |
|
|
boolean run = true; |
| 66 |
|
|
try{ |
| 67 |
|
|
// Setup the ServerSocket so that clients can connect |
| 68 |
|
|
listenPort = new ServerSocket(_port); |
| 69 |
|
|
} |
| 70 |
|
|
catch(IOException e){ |
| 71 |
|
|
} |
| 72 |
|
|
// Log what machine/port we're listening on |
| 73 |
|
|
try{ |
| 74 |
|
|
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on " |
| 75 |
|
|
+InetAddress.getLocalHost().getHostName() |
| 76 |
|
|
+"/"+InetAddress.getLocalHost().getHostAddress() |
| 77 |
|
|
+" port "+listenPort.getLocalPort()); |
| 78 |
|
|
} |
| 79 |
|
|
catch(UnknownHostException e){ |
| 80 |
|
|
_logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost " |
| 81 |
|
|
+"port "+listenPort.getLocalPort()); |
| 82 |
|
|
} |
| 83 |
|
|
// Loop round constantly until we decide to stop |
| 84 |
|
|
while(run){ |
| 85 |
|
|
Socket hostSocket=null; |
| 86 |
|
|
try{ |
| 87 |
|
|
_logger.write(toString(), Logger.SYSMSG, "Waiting for Connection"); |
| 88 |
|
|
// This will block until a host connects - at which point we get a Socket |
| 89 |
|
|
hostSocket = listenPort.accept(); |
| 90 |
|
|
_logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString()); |
| 91 |
|
|
} |
| 92 |
|
|
catch(IOException e){ |
| 93 |
|
|
// Something went wrong with the ServerSocket, so we'll stop listening |
| 94 |
|
|
run=false; |
| 95 |
|
|
} |
| 96 |
|
|
// If we've stopped on the line above we won't want to try this ! |
| 97 |
|
|
if(run){ |
| 98 |
|
|
try { |
| 99 |
|
|
// Setup the HostInit so it can carry on communications with the host |
| 100 |
tdb |
1.3 |
TCPReaderInit init = new TCPReaderInit(hostSocket, _parent); |
| 101 |
tdb |
1.1 |
// and start it |
| 102 |
|
|
init.start(); |
| 103 |
|
|
} catch (IOException e) { |
| 104 |
|
|
_logger.write(toString(), Logger.ERROR, e.toString()); |
| 105 |
|
|
} |
| 106 |
|
|
} |
| 107 |
|
|
} |
| 108 |
|
|
// Best log the fact that we're stopping |
| 109 |
|
|
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
| 110 |
|
|
} |
| 111 |
|
|
|
| 112 |
|
|
/** |
| 113 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 114 |
|
|
* method to provide clean logging (every class should have this). |
| 115 |
|
|
* |
| 116 |
|
|
* @return the name of this class and its CVS revision |
| 117 |
|
|
*/ |
| 118 |
|
|
public String toString() { |
| 119 |
|
|
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
| 120 |
|
|
} |
| 121 |
|
|
|
| 122 |
|
|
//---PRIVATE METHODS--- |
| 123 |
|
|
|
| 124 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
| 125 |
|
|
|
| 126 |
|
|
//---ATTRIBUTES--- |
| 127 |
|
|
|
| 128 |
|
|
/** |
| 129 |
|
|
* A reference to the logger the system is using |
| 130 |
|
|
*/ |
| 131 |
tdb |
1.3 |
Logger _logger = ReferenceManager.getInstance().getLogger(); |
| 132 |
tdb |
1.1 |
|
| 133 |
|
|
/** |
| 134 |
|
|
* The port on which the server should listen. |
| 135 |
|
|
*/ |
| 136 |
|
|
private int _port; |
| 137 |
|
|
|
| 138 |
|
|
private Filter _parent; |
| 139 |
|
|
|
| 140 |
|
|
//---STATIC ATTRIBUTES--- |
| 141 |
|
|
|
| 142 |
|
|
} |