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.34
Committed: Wed Feb 5 16:43:47 2003 UTC (21 years, 3 months ago) by tdb
Branch: MAIN
Changes since 1.33: +4 -4 lines
Log Message:
Changed the server to use the external util package. Quite a minor change,
but does affect a lot of files.

File Contents

# User Rev Content
1 tdb 1.30 /*
2     * i-scream central monitoring system
3 tdb 1.32 * http://www.i-scream.org.uk
4 tdb 1.30 * Copyright (C) 2000-2002 i-scream
5     *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19     */
20    
21 tdb 1.1 //---PACKAGE DECLARATION---
22 tdb 1.28 package uk.org.iscream.cms.server.filtermanager;
23 tdb 1.1
24     //---IMPORTS---
25 tdb 1.28 import uk.org.iscream.cms.server.core.*;
26     import uk.org.iscream.cms.server.filter.*;
27 tdb 1.34 import uk.org.iscream.cms.util.*;
28 tdb 1.28 import uk.org.iscream.cms.server.componentmanager.*;
29 tdb 1.18 import java.net.*;
30 tdb 1.1 import java.io.*;
31 tdb 1.22 import java.util.*;
32 tdb 1.1
33     /**
34 tdb 1.11 * Handles setting up a host.
35 ajm 1.12 * This class provides a host with appropriate configuration
36     * and a reference to a Filter to which it should pass data.
37 tdb 1.1 *
38 tdb 1.29 * @author $Author: tdb $
39 tdb 1.34 * @version $Id: HostInit.java,v 1.33 2002/09/06 15:10:48 tdb Exp $
40 tdb 1.1 */
41     class HostInit extends Thread {
42    
43     //---FINAL ATTRIBUTES---
44    
45     /**
46     * The current CVS revision of this class
47     */
48 tdb 1.34 public final String REVISION = "$Revision: 1.33 $";
49 tdb 1.1
50     //---STATIC METHODS---
51    
52     //---CONSTRUCTORS---
53    
54 tdb 1.15 /**
55     * Construct a new HostInit.
56     *
57     * @param socket The socket to which the host is connected
58     */
59 tdb 1.11 public HostInit(Socket socket) throws IOException {
60 tdb 1.25 // set the Thread name
61     setName("filtermanager.HostInit");
62    
63 tdb 1.1 _socket = socket;
64 tdb 1.15 // setup reader & writer
65 tdb 1.1 _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
66 tdb 1.16 _socketOut = new PrintWriter(_socket.getOutputStream(), true);
67 tdb 1.11 _logger.write(toString(), Logger.SYSINIT, "created");
68 tdb 1.1 }
69    
70     //---PUBLIC METHODS---
71    
72 tdb 1.15 /**
73     * Main method in this class, which handles communicating with
74     * the host to determine it's setup.
75     */
76 tdb 1.1 public void run() {
77 tdb 1.26 // get a hook on the config proxy
78     ConfigurationProxy cp = ConfigurationProxy.getInstance();
79     // this is our config name
80     String configName = "Host." + _socket.getInetAddress().getHostName().toLowerCase();
81 tdb 1.1 try {
82 tdb 1.26 // try for STARTCONFIG
83     getInBound("STARTCONFIG");
84     _socketOut.println("OK");
85    
86     // try for LASTMODIFIED
87     getInBound("LASTMODIFIED");
88     _socketOut.println(cp.getLastModified(configName));
89    
90     // try for FILELIST
91     getInBound("FILELIST");
92     _socketOut.println(cp.getFileList(configName));
93    
94     // try for FQDN
95     getInBound("FQDN");
96     _socketOut.println(_socket.getInetAddress().getHostName().toLowerCase());
97    
98     // get properties
99     String reqProperty = getInBound();
100     while(!reqProperty.equals("ENDCONFIG")) {
101     // get the property
102     try {
103     String returnedProperty = cp.getProperty(configName, "Host."+reqProperty);
104     _socketOut.println(returnedProperty);
105     } catch (PropertyNotFoundException e) {
106 tdb 1.2 _socketOut.println("ERROR");
107 tdb 1.4 }
108 tdb 1.26 // get the next request
109     reqProperty = _socketIn.readLine();
110     }
111     _logger.write(toString(), Logger.SYSMSG, "configured host");
112     _socketOut.println("OK");
113    
114     // get filter reference
115     getInBound("FILTER");
116     // send info
117     String filterList = cp.getProperty(configName, "Host.filter");
118 tdb 1.29 FilterInfo filterInfoRef = null;
119 tdb 1.26 String filter = null;
120     StringTokenizer st = new StringTokenizer(filterList, ";");
121 tdb 1.29 while (filterInfoRef==null && st.hasMoreTokens()) {
122 tdb 1.26 filter = st.nextToken();
123     _logger.write(toString(), Logger.DEBUG, " looking for filter- " + filter);
124     try {
125 tdb 1.33 filterInfoRef = FilterInfoHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.FilterInfo\\." + filter));
126 tdb 1.26 } catch (ComponentCORBAException e) {
127     _logger.write(toString(), Logger.DEBUG, " unable to find filter- " + filter);
128 tdb 1.7 }
129 tdb 1.1 }
130 tdb 1.26
131 tdb 1.31 // hopefully we found a filter
132 tdb 1.29 if(filterInfoRef != null) {
133 tdb 1.26 _logger.write(toString(), Logger.DEBUG, " found filter- " + filter);
134 tdb 1.31 try {
135     // tell the host about it...
136     _socketOut.println(filterInfoRef.getHostName() + ";"
137     + filterInfoRef.getUDPPort() + ";"
138     + filterInfoRef.getTCPPort());
139     }
140     catch(org.omg.CORBA.COMM_FAILURE e) {
141     // failed to talk to filter, lets signal an error
142     _socketOut.println("ERROR");
143     throw new IOException("error communicating with filter - " + e);
144     }
145 tdb 1.26 }
146     else {
147     // ...or throw a wobbly (and tell the host!)
148     _socketOut.println("ERROR");
149     throw new IOException("unable to find filter for host");
150     }
151    
152     // confirm ok with END
153     getInBound("END");
154     _socketOut.println("OK");
155 tdb 1.1
156     } catch (Exception e) {
157 tdb 1.17 _logger.write(toString(), Logger.ERROR, "ERROR - " + e);
158 tdb 1.1 }
159    
160     // Disconnect streams & socket
161     try {
162     _socketIn.close();
163     _socketOut.close();
164     _socket.close();
165     } catch (IOException e) {
166 tdb 1.11 _logger.write(toString(), Logger.ERROR, "exception on socket close");
167 tdb 1.1 }
168 tdb 1.11 _logger.write(toString(), Logger.SYSMSG, "finished");
169 tdb 1.1 }
170    
171     /**
172     * Overrides the {@link java.lang.Object#toString() Object.toString()}
173     * method to provide clean logging (every class should have this).
174     *
175 tdb 1.34 * This uses the uk.org.iscream.cms.util.NameFormat class
176 ajm 1.12 * to format the toString()
177     *
178 tdb 1.1 * @return the name of this class and its CVS revision
179     */
180     public String toString() {
181 ajm 1.12 return FormatName.getName(
182     _name,
183 ajm 1.13 getClass().getName(),
184 ajm 1.12 REVISION);
185 tdb 1.1 }
186    
187     //---PRIVATE METHODS---
188 tdb 1.26
189     private String getInBound(String expected) throws IOException {
190     // grab the input
191     String inBound = getInBound();
192     // check if it's what we're expecting
193     if(!inBound.equals(expected)) {
194     throw new IOException("protocol error - expected:"+expected+" got:" + inBound);
195     }
196     // it should be ok then
197     return inBound;
198     }
199    
200     private String getInBound() throws IOException {
201     // grab the input
202     String inBound = _socketIn.readLine();
203     // check for null's, likely disconnection
204     if(inBound == null) {
205     throw new IOException("got null from host, maybe it died");
206     }
207     // it's a valid message it seems
208     return inBound;
209     }
210 tdb 1.1
211     //---ACCESSOR/MUTATOR METHODS---
212    
213     //---ATTRIBUTES---
214    
215 tdb 1.11 /**
216 ajm 1.12 * This holds a reference to the
217     * system logger that is being used.
218 tdb 1.11 */
219 ajm 1.12 private Logger _logger = ReferenceManager.getInstance().getLogger();
220 tdb 1.11
221     /**
222     * A reference to the Configuration Manager the system is using
223     */
224 ajm 1.12 private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
225    
226     /**
227     * This is the friendly identifier of the
228     * component this class is running in.
229     * eg, a Filter may be called "filter1",
230     * If this class does not have an owning
231     * component, a name from the configuration
232     * can be placed here. This name could also
233     * be changed to null for utility classes.
234     */
235     private String _name = FilterManager.NAME;
236    
237     /**
238     * The socket this class uses
239     */
240     private Socket _socket;
241    
242     /**
243     * Used for the input stream of this socket
244     */
245     private BufferedReader _socketIn;
246 tdb 1.11
247 ajm 1.12 /**
248     * Used for the output stream of this socket
249     */
250     private PrintWriter _socketOut;
251 tdb 1.11
252 tdb 1.1 //---STATIC ATTRIBUTES---
253    
254     }