ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/java/Config.java
Revision: 1.9
Committed: Sat May 18 18:15:57 2002 UTC (22 years, 5 months ago) by tdb
Branch: MAIN
Changes since 1.8: +21 -2 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

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