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