ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/filtermanager/HostInit.java
Revision: 1.15
Committed: Sun Jan 28 05:38:13 2001 UTC (23 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.14: +13 -3 lines
Log Message:
Some tidying up.

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2 tdb 1.9 package uk.ac.ukc.iscream.filtermanager;
3 tdb 1.1
4     //---IMPORTS---
5     import uk.ac.ukc.iscream.core.*;
6 tdb 1.7 import uk.ac.ukc.iscream.filter.*;
7 tdb 1.11 import uk.ac.ukc.iscream.util.*;
8 tdb 1.14 import uk.ac.ukc.iscream.componentmanager.*;
9 tdb 1.1 import java.net.Socket;
10     import java.io.*;
11    
12     /**
13 tdb 1.11 * Handles setting up a host.
14 ajm 1.12 * This class provides a host with appropriate configuration
15     * and a reference to a Filter to which it should pass data.
16 tdb 1.1 *
17 tdb 1.15 * @author $Author: tdb1 $
18     * @version $Id: HostInit.java,v 1.14 2001/01/18 23:15:09 tdb1 Exp $
19 tdb 1.1 */
20     class HostInit extends Thread {
21    
22     //---FINAL ATTRIBUTES---
23    
24     /**
25     * The current CVS revision of this class
26     */
27 tdb 1.15 public final String REVISION = "$Revision: 1.14 $";
28 tdb 1.1
29     //---STATIC METHODS---
30    
31     //---CONSTRUCTORS---
32    
33 tdb 1.15 /**
34     * Construct a new HostInit.
35     *
36     * @param socket The socket to which the host is connected
37     */
38 tdb 1.11 public HostInit(Socket socket) throws IOException {
39 tdb 1.1 _socket = socket;
40 tdb 1.15 // setup reader & writer
41 tdb 1.1 _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
42     _socketOut = new PrintWriter(_socket.getOutputStream());
43 tdb 1.11 _logger.write(toString(), Logger.SYSINIT, "created");
44 tdb 1.1 }
45    
46     //---PUBLIC METHODS---
47    
48 tdb 1.15 /**
49     * Main method in this class, which handles communicating with
50     * the host to determine it's setup.
51     */
52 tdb 1.1 public void run() {
53     try {
54     String inBound = _socketIn.readLine();
55     if (!inBound.equals("STARTCONFIG")) {
56     _socketOut.println("ERROR");
57     _socketOut.flush();
58 tdb 1.7 throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound);
59 tdb 1.1 }
60    
61 ajm 1.8 Configuration myConfig = _configManager.getConfiguration("Host." + _socket.getInetAddress().getHostName().toLowerCase());
62 tdb 1.1 if (myConfig == null) {
63     _socketOut.println("ERROR");
64 tdb 1.7 throw new IOException("error in host configuration");
65 tdb 1.1 } else {
66     _socketOut.println("OK");
67     _socketOut.flush();
68    
69 tdb 1.2 // get lastmodified
70     inBound = _socketIn.readLine();
71 tdb 1.4 if(!inBound.equals("LASTMODIFIED")) {
72     // protocol error
73 tdb 1.2 _socketOut.println("ERROR");
74     _socketOut.flush();
75 tdb 1.7 throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound);
76 tdb 1.4 }
77     else {
78     // send info
79     _socketOut.println(myConfig.getLastModified());
80     _socketOut.flush();
81 tdb 1.2 }
82    
83 tdb 1.7 // get config fileList
84     inBound = _socketIn.readLine();
85     if(!inBound.equals("FILELIST")) {
86     // protocol error
87     _socketOut.println("ERROR");
88     _socketOut.flush();
89     throw new IOException("protocol error - expected:FILELIST got:" + inBound);
90     }
91     else {
92     // send info
93     _socketOut.println(myConfig.getFileList());
94     _socketOut.flush();
95     }
96    
97 tdb 1.2 // get properties
98 tdb 1.1 inBound = _socketIn.readLine();
99     while(!inBound.equals("ENDCONFIG")) {
100    
101     // get the property
102     try {
103 tdb 1.10 String returnedProperty = myConfig.getProperty("Host."+inBound);
104 tdb 1.1
105     _socketOut.println(returnedProperty);
106     _socketOut.flush();
107    
108     } catch (org.omg.CORBA.MARSHAL e) {
109     _socketOut.println("ERROR");
110     _socketOut.flush();
111     }
112     inBound = _socketIn.readLine();
113     }
114 tdb 1.11 _logger.write(toString(), Logger.SYSMSG, "configured host");
115 tdb 1.1 _socketOut.println("OK");
116     _socketOut.flush();
117 tdb 1.7
118     // get filter reference
119     inBound = _socketIn.readLine();
120     if(!inBound.equals("FILTER")) {
121     // protocol error
122     _socketOut.println("ERROR");
123     _socketOut.flush();
124     throw new IOException("protocol error - expected:FILTER got:" + inBound);
125     }
126     else {
127     // send info
128     String parentFilter = myConfig.getProperty("Host.filter");
129 tdb 1.11 _logger.write(toString(), Logger.DEBUG, " looking for parent - " + parentFilter);
130     Filter filter = FilterHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.Filter." + parentFilter));
131 tdb 1.7 _socketOut.println(filter.getHostName() + ";"
132     + filter.getUDPPort() + ";"
133     + filter.getTCPPort());
134     _socketOut.flush();
135     }
136    
137     // confirm that all is ok
138     inBound = _socketIn.readLine();
139     if(!inBound.equals("END")) {
140     // protocol error
141     _socketOut.println("ERROR");
142     _socketOut.flush();
143     throw new IOException("protocol error - expected:END got:" + inBound);
144     }
145     else {
146     // send ok
147     _socketOut.println("OK");
148     _socketOut.flush();
149     }
150    
151 tdb 1.1 }
152    
153     } catch (Exception e) {
154 ajm 1.12 _logger.write(toString(), Logger.ERROR, "ERROR - " + e.getMessage());
155 tdb 1.1 }
156    
157     _socketOut.flush();
158     // Disconnect streams & socket
159     try {
160     _socketIn.close();
161     _socketOut.close();
162     _socket.close();
163     } catch (IOException e) {
164 tdb 1.11 _logger.write(toString(), Logger.ERROR, "exception on socket close");
165 tdb 1.1 }
166 tdb 1.11 _logger.write(toString(), Logger.SYSMSG, "finished");
167 tdb 1.1 }
168    
169     /**
170     * Overrides the {@link java.lang.Object#toString() Object.toString()}
171     * method to provide clean logging (every class should have this).
172     *
173 ajm 1.12 * This uses the uk.ac.ukc.iscream.util.NameFormat class
174     * to format the toString()
175     *
176 tdb 1.1 * @return the name of this class and its CVS revision
177     */
178     public String toString() {
179 ajm 1.12 return FormatName.getName(
180     _name,
181 ajm 1.13 getClass().getName(),
182 ajm 1.12 REVISION);
183 tdb 1.1 }
184    
185     //---PRIVATE METHODS---
186    
187     //---ACCESSOR/MUTATOR METHODS---
188    
189     //---ATTRIBUTES---
190    
191 tdb 1.11 /**
192 ajm 1.12 * This holds a reference to the
193     * system logger that is being used.
194 tdb 1.11 */
195 ajm 1.12 private Logger _logger = ReferenceManager.getInstance().getLogger();
196 tdb 1.11
197     /**
198     * A reference to the Configuration Manager the system is using
199     */
200 ajm 1.12 private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
201    
202     /**
203     * This is the friendly identifier of the
204     * component this class is running in.
205     * eg, a Filter may be called "filter1",
206     * If this class does not have an owning
207     * component, a name from the configuration
208     * can be placed here. This name could also
209     * be changed to null for utility classes.
210     */
211     private String _name = FilterManager.NAME;
212    
213     /**
214     * The socket this class uses
215     */
216     private Socket _socket;
217    
218     /**
219     * Used for the input stream of this socket
220     */
221     private BufferedReader _socketIn;
222 tdb 1.11
223 ajm 1.12 /**
224     * Used for the output stream of this socket
225     */
226     private PrintWriter _socketOut;
227 tdb 1.11
228 tdb 1.1 //---STATIC ATTRIBUTES---
229    
230     }