1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.filtermanager; |
2 |
> |
package uk.org.iscream.filtermanager; |
3 |
|
|
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.*; |
5 |
> |
import uk.org.iscream.core.*; |
6 |
> |
import uk.org.iscream.componentmanager.*; |
7 |
> |
import uk.org.iscream.filter.*; |
8 |
> |
import uk.org.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; |
48 |
< |
_port = port; |
38 |
> |
public HostListener() { |
39 |
> |
// set the Thread name |
40 |
> |
setName("filtermanager.HostListener"); |
41 |
> |
|
42 |
|
_logger.write(toString(), Logger.SYSINIT, "started"); |
43 |
|
} |
44 |
|
|
52 |
– |
|
53 |
– |
|
45 |
|
//---PUBLIC METHODS--- |
46 |
|
|
47 |
|
/** |
52 |
|
* and then passing them off to other processes to deal with. |
53 |
|
*/ |
54 |
|
public void run(){ |
55 |
< |
ServerSocket listenPort=null; |
55 |
> |
ServerSocket listenSocket=null; |
56 |
|
// We use this boolean so we can break out of the while loop if we want |
57 |
|
boolean run = true; |
58 |
|
try{ |
59 |
+ |
// Work out the port we want |
60 |
+ |
int listenPort = Integer.parseInt(ConfigurationProxy.getInstance().getProperty("FilterManager", "FilterManager.listenPort")); |
61 |
|
// Setup the ServerSocket so that clients can connect |
62 |
< |
listenPort = new ServerSocket(_port); |
63 |
< |
} |
71 |
< |
catch(IOException e){ |
72 |
< |
} |
73 |
< |
// Log what machine/port we're listening on |
74 |
< |
try{ |
75 |
< |
_logger.write(toString(), Logger.SYSMSG, "Server listening on " |
62 |
> |
listenSocket = new ServerSocket(listenPort); |
63 |
> |
_logger.write(toString(), Logger.SYSMSG, "Server listening on " |
64 |
|
+InetAddress.getLocalHost().getHostName() |
65 |
|
+"/"+InetAddress.getLocalHost().getHostAddress() |
66 |
< |
+" port "+listenPort.getLocalPort()); |
67 |
< |
} |
68 |
< |
catch(UnknownHostException e){ |
66 |
> |
+" port "+listenSocket.getLocalPort()); |
67 |
> |
|
68 |
> |
} |
69 |
> |
catch(UnknownHostException e){ |
70 |
|
_logger.write(toString(), Logger.SYSMSG, "Server listening on UnknownHost " |
71 |
< |
+"port "+listenPort.getLocalPort()); |
71 |
> |
+"port "+listenSocket.getLocalPort()); |
72 |
|
} |
73 |
+ |
catch(IOException e){ |
74 |
+ |
_logger.write(toString(), Logger.FATAL, "IO Error, can't start FilterManager :"+e); |
75 |
+ |
run = false; |
76 |
+ |
} |
77 |
+ |
catch(PropertyNotFoundException e){ |
78 |
+ |
_logger.write(toString(), Logger.FATAL, "Fatal Error, can't find config :"+e); |
79 |
+ |
run = false; |
80 |
+ |
} |
81 |
+ |
catch(NumberFormatException e){ |
82 |
+ |
_logger.write(toString(), Logger.FATAL, "Invalid port configuration found :"+e); |
83 |
+ |
run = false; |
84 |
+ |
} |
85 |
+ |
|
86 |
|
// Loop round constantly until we decide to stop |
87 |
|
while(run){ |
88 |
|
Socket hostSocket=null; |
89 |
|
try{ |
90 |
|
_logger.write(toString(), Logger.SYSMSG, "Waiting for Connection"); |
91 |
|
// This will block until a host connects - at which point we get a Socket |
92 |
< |
hostSocket = listenPort.accept(); |
92 |
> |
hostSocket = listenSocket.accept(); |
93 |
|
_logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString()); |
94 |
|
} |
95 |
|
catch(IOException e){ |
100 |
|
if(run){ |
101 |
|
try { |
102 |
|
// Setup the HostInit so it can carry on communications with the host |
103 |
< |
HostInit init = new HostInit(hostSocket, _configManager, _logger, _ncRef); |
103 |
> |
HostInit init = new HostInit(hostSocket); |
104 |
|
// and start it |
105 |
|
init.start(); |
106 |
|
} catch (IOException e) { |
116 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
117 |
|
* method to provide clean logging (every class should have this). |
118 |
|
* |
119 |
+ |
* This uses the uk.org.iscream.util.NameFormat class |
120 |
+ |
* to format the toString() |
121 |
+ |
* |
122 |
|
* @return the name of this class and its CVS revision |
123 |
|
*/ |
124 |
|
public String toString() { |
125 |
< |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
125 |
> |
return FormatName.getName( |
126 |
> |
_name, |
127 |
> |
this.getClass().getName(), |
128 |
> |
REVISION); |
129 |
|
} |
130 |
|
|
131 |
|
//---PRIVATE METHODS--- |
135 |
|
//---ATTRIBUTES--- |
136 |
|
|
137 |
|
/** |
138 |
< |
* A reference to the logger the system is using |
138 |
> |
* This holds a reference to the |
139 |
> |
* system logger that is being used. |
140 |
|
*/ |
141 |
< |
Logger _logger; |
141 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
142 |
|
|
143 |
|
/** |
144 |
< |
* A reference to the configurator the system is using |
144 |
> |
* This is the friendly identifier of the |
145 |
> |
* component this class is running in. |
146 |
> |
* eg, a Filter may be called "filter1", |
147 |
> |
* If this class does not have an owning |
148 |
> |
* component, a name from the configuration |
149 |
> |
* can be placed here. This name could also |
150 |
> |
* be changed to null for utility classes. |
151 |
|
*/ |
152 |
< |
ConfigurationManager _configManager; |
138 |
< |
|
139 |
< |
/** |
140 |
< |
* The port on which the server should listen. |
141 |
< |
*/ |
142 |
< |
private int _port; |
143 |
< |
|
144 |
< |
private NamingContextExt _ncRef; |
152 |
> |
private String _name = FilterManager.NAME; |
153 |
|
|
154 |
|
//---STATIC ATTRIBUTES--- |
155 |
|
|