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.29
Committed: Fri Mar 22 10:43:06 2002 UTC (22 years, 1 month ago) by tdb
Branch: MAIN
Changes since 1.28: +10 -10 lines
Log Message:
Split the CORBA side of the filter in two. The Filter interface still does
the receiving of data, just like it used to. This can optionally be turned
off in the configuration if required. The new interface, FilterInfo, is
used to provide information (host and ports) to other parts of the server
about the Filter - namely the FilterManager. This split has been done so
that the data receiving part of the Filter can be turned off, without
breaking the FilterManager.

File Contents

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