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