| 3 |
|
|
| 4 |
|
//---IMPORTS--- |
| 5 |
|
import uk.ac.ukc.iscream.core.*; |
| 6 |
+ |
import uk.ac.ukc.iscream.componentmanager.*; |
| 7 |
|
import uk.ac.ukc.iscream.filter.*; |
| 8 |
< |
import java.net.Socket; |
| 9 |
< |
import java.net.ServerSocket; |
| 10 |
< |
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.*; |
| 8 |
> |
import uk.ac.ukc.iscream.util.*; |
| 9 |
> |
import java.net.*; |
| 10 |
> |
import java.io.*; |
| 11 |
|
|
| 12 |
|
/** |
| 13 |
|
* A socket listener to listen for new hosts registering with the system. |
| 33 |
|
/** |
| 34 |
|
* Constructs a new listener |
| 35 |
|
* |
| 36 |
< |
* @param logger a reference to the logger we are using |
| 41 |
< |
* @param configManager a reference to the ConfigurationManager we are using |
| 42 |
< |
* @param port The port that the server will listen on. |
| 36 |
> |
* @param listenPort The port that the server will listen on. |
| 37 |
|
*/ |
| 38 |
< |
public HostListener(Logger logger, ConfigurationManager configManager, int port, NamingContextExt ncRef) { |
| 39 |
< |
_logger = logger; |
| 40 |
< |
_ncRef = ncRef; |
| 41 |
< |
_configManager = configManager; |
| 42 |
< |
_port = port; |
| 38 |
> |
public HostListener(int listenPort) { |
| 39 |
> |
// set the Thread name |
| 40 |
> |
setName("filtermanager.HostListener"); |
| 41 |
> |
|
| 42 |
> |
_listenPort = listenPort; |
| 43 |
|
_logger.write(toString(), Logger.SYSINIT, "started"); |
| 44 |
|
} |
| 45 |
|
|
| 55 |
|
* and then passing them off to other processes to deal with. |
| 56 |
|
*/ |
| 57 |
|
public void run(){ |
| 58 |
< |
ServerSocket listenPort=null; |
| 58 |
> |
ServerSocket listenSocket=null; |
| 59 |
|
// We use this boolean so we can break out of the while loop if we want |
| 60 |
|
boolean run = true; |
| 61 |
|
try{ |
| 62 |
|
// Setup the ServerSocket so that clients can connect |
| 63 |
< |
listenPort = new ServerSocket(_port); |
| 63 |
> |
listenSocket = new ServerSocket(_listenPort); |
| 64 |
|
} |
| 65 |
|
catch(IOException e){ |
| 66 |
|
} |
| 69 |
|
_logger.write(toString(), Logger.SYSMSG, "Server listening on " |
| 70 |
|
+InetAddress.getLocalHost().getHostName() |
| 71 |
|
+"/"+InetAddress.getLocalHost().getHostAddress() |
| 72 |
< |
+" port "+listenPort.getLocalPort()); |
| 72 |
> |
+" port "+listenSocket.getLocalPort()); |
| 73 |
|
} |
| 74 |
|
catch(UnknownHostException e){ |
| 75 |
|
_logger.write(toString(), Logger.SYSMSG, "Server listening on UnknownHost " |
| 76 |
< |
+"port "+listenPort.getLocalPort()); |
| 76 |
> |
+"port "+listenSocket.getLocalPort()); |
| 77 |
|
} |
| 78 |
|
// Loop round constantly until we decide to stop |
| 79 |
|
while(run){ |
| 81 |
|
try{ |
| 82 |
|
_logger.write(toString(), Logger.SYSMSG, "Waiting for Connection"); |
| 83 |
|
// This will block until a host connects - at which point we get a Socket |
| 84 |
< |
hostSocket = listenPort.accept(); |
| 84 |
> |
hostSocket = listenSocket.accept(); |
| 85 |
|
_logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString()); |
| 86 |
|
} |
| 87 |
|
catch(IOException e){ |
| 92 |
|
if(run){ |
| 93 |
|
try { |
| 94 |
|
// Setup the HostInit so it can carry on communications with the host |
| 95 |
< |
HostInit init = new HostInit(hostSocket, _configManager, _logger, _ncRef); |
| 95 |
> |
HostInit init = new HostInit(hostSocket); |
| 96 |
|
// and start it |
| 97 |
|
init.start(); |
| 98 |
|
} catch (IOException e) { |
| 108 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 109 |
|
* method to provide clean logging (every class should have this). |
| 110 |
|
* |
| 111 |
+ |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
| 112 |
+ |
* to format the toString() |
| 113 |
+ |
* |
| 114 |
|
* @return the name of this class and its CVS revision |
| 115 |
|
*/ |
| 116 |
|
public String toString() { |
| 117 |
< |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
| 117 |
> |
return FormatName.getName( |
| 118 |
> |
_name, |
| 119 |
> |
this.getClass().getName(), |
| 120 |
> |
REVISION); |
| 121 |
|
} |
| 122 |
|
|
| 123 |
|
//---PRIVATE METHODS--- |
| 127 |
|
//---ATTRIBUTES--- |
| 128 |
|
|
| 129 |
|
/** |
| 130 |
< |
* A reference to the logger the system is using |
| 130 |
> |
* This holds a reference to the |
| 131 |
> |
* system logger that is being used. |
| 132 |
|
*/ |
| 133 |
< |
Logger _logger; |
| 133 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
| 134 |
|
|
| 135 |
|
/** |
| 136 |
< |
* A reference to the configurator the system is using |
| 136 |
> |
* This is the friendly identifier of the |
| 137 |
> |
* component this class is running in. |
| 138 |
> |
* eg, a Filter may be called "filter1", |
| 139 |
> |
* If this class does not have an owning |
| 140 |
> |
* component, a name from the configuration |
| 141 |
> |
* can be placed here. This name could also |
| 142 |
> |
* be changed to null for utility classes. |
| 143 |
|
*/ |
| 144 |
< |
ConfigurationManager _configManager; |
| 144 |
> |
private String _name = FilterManager.NAME; |
| 145 |
|
|
| 146 |
|
/** |
| 147 |
|
* The port on which the server should listen. |
| 148 |
|
*/ |
| 149 |
< |
private int _port; |
| 143 |
< |
|
| 144 |
< |
private NamingContextExt _ncRef; |
| 149 |
> |
private int _listenPort; |
| 150 |
|
|
| 151 |
|
//---STATIC ATTRIBUTES--- |
| 152 |
|
|