1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.filtermanager; |
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 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. |
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 |
* @author $Author: tdb1 $ |
18 |
* @version $Id: HostListener.java,v 1.5 2001/01/18 23:17:54 tdb1 Exp $ |
19 |
*/ |
20 |
class HostListener extends Thread { |
21 |
|
22 |
//---FINAL ATTRIBUTES--- |
23 |
|
24 |
/** |
25 |
* The current CVS revision of this class |
26 |
*/ |
27 |
public final String REVISION = "$Revision: 1.5 $"; |
28 |
|
29 |
//---STATIC METHODS--- |
30 |
|
31 |
//---CONSTRUCTORS--- |
32 |
|
33 |
/** |
34 |
* Constructs a new listener |
35 |
* |
36 |
* @param listenPort The port that the server will listen on. |
37 |
*/ |
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 |
|
46 |
|
47 |
|
48 |
//---PUBLIC METHODS--- |
49 |
|
50 |
/** |
51 |
* The run() method is the main loop for this thread, and we |
52 |
* will remain in here until such a point as something goes |
53 |
* wrong with the listening. After initially setting up the |
54 |
* ServerSocket we go round a while loop receiving connections |
55 |
* and then passing them off to other processes to deal with. |
56 |
*/ |
57 |
public void run(){ |
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 |
listenSocket = new ServerSocket(_listenPort); |
64 |
} |
65 |
catch(IOException e){ |
66 |
} |
67 |
// Log what machine/port we're listening on |
68 |
try{ |
69 |
_logger.write(toString(), Logger.SYSMSG, "Server listening on " |
70 |
+InetAddress.getLocalHost().getHostName() |
71 |
+"/"+InetAddress.getLocalHost().getHostAddress() |
72 |
+" port "+listenSocket.getLocalPort()); |
73 |
} |
74 |
catch(UnknownHostException e){ |
75 |
_logger.write(toString(), Logger.SYSMSG, "Server listening on UnknownHost " |
76 |
+"port "+listenSocket.getLocalPort()); |
77 |
} |
78 |
// Loop round constantly until we decide to stop |
79 |
while(run){ |
80 |
Socket hostSocket=null; |
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 = listenSocket.accept(); |
85 |
_logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString()); |
86 |
} |
87 |
catch(IOException e){ |
88 |
// Something went wrong with the ServerSocket, so we'll stop listening |
89 |
run=false; |
90 |
} |
91 |
// If we've stopped on the line above we won't want to try this ! |
92 |
if(run){ |
93 |
try { |
94 |
// Setup the HostInit so it can carry on communications with the host |
95 |
HostInit init = new HostInit(hostSocket); |
96 |
// and start it |
97 |
init.start(); |
98 |
} catch (IOException e) { |
99 |
_logger.write(toString(), Logger.ERROR, e.toString()); |
100 |
} |
101 |
} |
102 |
} |
103 |
// Best log the fact that we're stopping |
104 |
_logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending"); |
105 |
} |
106 |
|
107 |
/** |
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 FormatName.getName( |
118 |
_name, |
119 |
this.getClass().getName(), |
120 |
REVISION); |
121 |
} |
122 |
|
123 |
//---PRIVATE METHODS--- |
124 |
|
125 |
//---ACCESSOR/MUTATOR METHODS--- |
126 |
|
127 |
//---ATTRIBUTES--- |
128 |
|
129 |
/** |
130 |
* This holds a reference to the |
131 |
* system logger that is being used. |
132 |
*/ |
133 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
134 |
|
135 |
/** |
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 |
private String _name = FilterManager.NAME; |
145 |
|
146 |
/** |
147 |
* The port on which the server should listen. |
148 |
*/ |
149 |
private int _listenPort; |
150 |
|
151 |
//---STATIC ATTRIBUTES--- |
152 |
|
153 |
} |