ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/java/Config.java
Revision: 1.6
Committed: Mon Jan 22 04:08:36 2001 UTC (23 years, 8 months ago) by tdb
Branch: MAIN
Changes since 1.5: +5 -5 lines
Log Message:
Just a few typos.

File Contents

# User Rev Content
1 ab11 1.2 //---PACKAGE DECLARATION---
2    
3     //---IMPORTS---
4    
5 ab11 1.1 import java.net.*;
6     import java.util.*;
7     import java.io.*;
8    
9 ab11 1.2 /**
10     * Configurator object for the JavaHost
11 tdb 1.5 * Will connect to the filter manager and collect its specific
12 ab11 1.2 * configuration
13     *
14 tdb 1.6 * @author $Author: tdb1 $
15     * @version $Id: Config.java,v 1.5 2000/12/07 23:18:28 tdb1 Exp $
16 ab11 1.2 */
17 ab11 1.1 class Config {
18    
19 ab11 1.2 //---FINAL ATTRIBUTES---
20    
21     //---STATIC METHODS---
22    
23     //---CONSTRUCTORS---
24    
25     public Config( String serverName, int serverPort ){
26     // takes in the master config settings and creates a connection with
27     // this computer, and downloads all the values listed in config.values.txt
28     // which should be in the local directory
29    
30     // read in from the file.
31     try {
32 ab11 1.3 System.out.println("Reading Config file");
33 ab11 1.2 BufferedReader inFile = new BufferedReader(new FileReader("config.values.txt"));
34     aList = new ArrayList();
35     numProperties = 0;
36     String tmpIn = inFile.readLine();
37     while ( tmpIn != null ){
38 ab11 1.3 aList.add(numProperties, tmpIn);
39 ab11 1.2 numProperties++;
40     tmpIn = inFile.readLine();
41     }
42 ab11 1.3 System.out.println("Added "+numProperties+" properties");
43 ab11 1.2 }
44     catch ( FileNotFoundException e ){
45     // do something
46 ab11 1.3 System.out.println("Config file not found");
47 ab11 1.2 }
48     catch ( IOException e ){
49     // do something
50     }
51    
52 ab11 1.3 myProperties = new HashMap();
53     configChanged = false;
54    
55 ab11 1.4 // time in seconds before first retry
56     filterManagerRetryTime = 10;
57 ab11 1.3
58 ab11 1.2 // do the funky jibble
59     connect(serverName, serverPort);
60     }
61    
62     //---PUBLIC METHODS---
63    
64     public InetAddress getFilterName(){
65 tdb 1.6 // will return the most recent IP address (if it is dynamic for whatever reason)
66 ab11 1.2 try {
67     return InetAddress.getByName(filterName);
68     }
69     catch ( UnknownHostException e ){
70     // do something
71     return null;
72     }
73    
74     }
75    
76     /**
77     * Used to retrieve the port to send UDP packets to on the filter
78     *
79 tdb 1.6 * @return an integer corresponding to the UDP port of the filter
80 ab11 1.2 */
81     public int getFilterUDPPort(){
82    
83     return filterUDPPort;
84     }
85    
86     /**
87     * Used to retrieve the port to send TCP heartbeats to on the filter
88     *
89 tdb 1.6 * @return an integer corresponding to the TCP of the filter
90 ab11 1.2 */
91     public int getFilterTCPPort(){
92    
93     return filterTCPPort;
94     }
95    
96     /**
97     * Used to get the hostname of the filter we are to talk to.
98     *
99     * @return a string representing the hostname of the filter
100     */
101     public String getProperty( String name ){
102    
103     if ( myProperties.containsKey(name) ){
104     // its in there, may return ERROR
105     return (String) myProperties.get(name);
106     }
107     else
108     {
109     // can't have been in the config.values.txt file!
110     return "ERROR";
111     }
112     }
113    
114    
115     //---PRIVATE METHODS---
116    
117     private void connect( String serverName, int serverPort ){
118     Socket mySocket;
119 ab11 1.3 configChanged = false;
120    
121 tdb 1.5 System.out.println("Establishing connection with filter manager");
122 ab11 1.2
123     // might throw a UnknownHostException
124     try {
125 ab11 1.3 mySocket = new Socket( serverName, serverPort );
126     // ok we have our socket connected ok. grab their input and output streams
127 ab11 1.2 socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
128     socketOut = new PrintWriter(mySocket.getOutputStream());
129 ab11 1.3
130     if (sendCommand("STARTCONFIG").equals("OK")){
131 ab11 1.2 // everything is fine
132 ab11 1.3 // sendCommand("LASTMODIFIED");
133     lastModified = sendCommand("LASTMODIFIED");
134 ab11 1.2
135 ab11 1.3 fileList = sendCommand("FILELIST");
136 ab11 1.2 // get all the properties
137 ab11 1.3 if ( numProperties > 0 ){
138     // sendCommand("CONFIG");
139     for ( int i = 0; i < numProperties; i++ ){
140     String property = (String) aList.get(i);
141     myProperties.put(property, sendCommand(property));
142     }
143 ab11 1.2 }
144    
145     sendCommand("ENDCONFIG");
146     String filter_data = sendCommand("FILTER");
147     StringTokenizer tok = new StringTokenizer(filter_data,";");
148     filterName = tok.nextToken();
149     filterUDPPort = Integer.parseInt(tok.nextToken());
150     filterTCPPort = Integer.parseInt(tok.nextToken());
151 ab11 1.4
152 ab11 1.2 sendCommand("END");
153 ab11 1.3
154 ab11 1.2 }
155 ab11 1.3
156 ab11 1.2
157     // close the socket
158     mySocket.close();
159 tdb 1.5 System.out.println("Completed communication with filter manager");
160 ab11 1.2
161     }
162     catch ( UnknownHostException e ){
163 ab11 1.3 // what to do
164     System.out.println("Host not found");
165 ab11 1.2 }
166     catch ( IOException e ){
167     // what to do
168 ab11 1.3 System.out.println("Unable to read from socket, might not be open");
169 ab11 1.4 System.out.println("Retrying in "+filterManagerRetryTime+" seconds");
170     configChanged = true;
171     try {
172     Thread.sleep(filterManagerRetryTime*1000);
173     }
174     catch( InterruptedException f ){
175     System.out.println("Sleep interrupted");
176     }
177     filterManagerRetryTime = filterManagerRetryTime * 2;
178     // warning this WILL cause a stack overflow after a while..
179     // need to fix it.
180     connect(serverName, serverPort);
181 ab11 1.2 }
182    
183     } // connect
184    
185    
186 ab11 1.3 public void sendHeartBeat(){
187    
188     Socket mySocket;
189     try {
190    
191     mySocket = new Socket( filterName, filterTCPPort );
192     // ok we have our socket connected ok. grab their input and output streams
193     socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
194     socketOut = new PrintWriter(mySocket.getOutputStream());
195    
196    
197     if ( sendCommand("HEARTBEAT").equals("OK") ){
198     if ( sendCommand("CONFIG").equals("OK") ){
199     sendCommand(fileList);
200     if ( sendCommand(lastModified).equals("ERROR") ){
201     // config has changed
202     configChanged = true;
203     }
204     sendCommand("ENDHEARTBEAT");
205     }
206     }
207     }
208     catch ( UnknownHostException e ){
209     // what to do
210     System.out.println("Host not found");
211     }
212     catch ( IOException e ){
213     // what to do
214     System.out.println("Unable to read from socket, might not be open");
215 ab11 1.4 System.out.println("Re-establishing contact with filter manager");
216     configChanged = true;
217 ab11 1.3 }
218     }
219    
220 ab11 1.2 private String sendCommand(String command){
221    
222 ab11 1.3 // System.out.println("Writing command: "+command);
223 ab11 1.2 socketOut.println(command);
224 ab11 1.3 socketOut.flush();
225 ab11 1.2 try {
226 ab11 1.3 String response = socketIn.readLine();
227     // System.out.println("Got: "+response);
228     return response;
229 ab11 1.2 }
230     catch ( IOException e ){
231     // something went wrong
232 ab11 1.3 System.out.println("Error recieving response");
233 ab11 1.2 return "ERROR";
234     }
235     }
236    
237     //---ACCESSOR/MUTATOR METHODS---
238    
239 ab11 1.3 public boolean reloadConfig(){
240     return configChanged;
241     }
242    
243 ab11 1.2 //---ATTRIBUTES---
244    
245 ab11 1.3 private boolean configChanged;
246 ab11 1.2 private String lastModified;
247 ab11 1.3 private String fileList;
248 ab11 1.2 private int numProperties;
249 ab11 1.3 private HashMap myProperties;
250 ab11 1.2 private String filterName;
251     private int filterUDPPort;
252     private int filterTCPPort;
253     private ArrayList aList;
254     private BufferedReader socketIn;
255     private PrintWriter socketOut;
256 ab11 1.4 private int filterManagerRetryTime;
257 ab11 1.2
258    
259    
260     //---STATIC ATTRIBUTES---
261 ab11 1.1
262     } // class