ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/java/Config.java
(Generate patch)

Comparing projects/cms/source/host/java/Config.java (file contents):
Revision 1.1 by ab11, Mon Nov 27 19:49:00 2000 UTC vs.
Revision 1.9 by tdb, Sat May 18 18:15:57 2002 UTC

# Line 1 | Line 1
1 + /*
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 + //---PACKAGE DECLARATION---
21 +
22 + //---IMPORTS---
23 +
24   import java.net.*;
25   import java.util.*;
26   import java.io.*;
27  
28 + /**
29 + * Configurator object for the JavaHost
30 + * Will connect to the filter manager and collect its specific
31 + * configuration
32 + *
33 + * @author  $Author$
34 + * @version $Id$
35 + */
36   class Config {
37  
38 <        private String lastModified;
8 <        private int numProperties;
9 <        private Hashtable myProperties;
10 <        private String filterName;
11 <        private int filterUDPPort;
12 <        private int filterTCPPort;
13 <        private ArrayList aList;
14 <        private BufferedReader socketIn;
15 <        private PrintWriter socketOut;
16 <        
38 > //---FINAL ATTRIBUTES---
39  
40 <        public Config( String serverName, int serverPort ){
19 <                // takes in the master config settings and creates a connection with
20 <                // this computer, and downloads all the values listed in config.values.txt
21 <                // which should be in the local directory
22 <                
23 <                // read in from the file.
24 <                try {
25 <                        BufferedReader inFile = new BufferedReader(new FileReader("config.values.txt"));
26 <                        aList = new ArrayList();
27 <                        numProperties = 0;
28 <                        String tmpIn = inFile.readLine();
29 <                        while ( tmpIn != null ){
30 <                                aList.add(tmpIn);
31 <                                numProperties++;
32 <                                tmpIn = inFile.readLine();      
33 <                        }
34 <                }
35 <                catch ( FileNotFoundException e ){
36 <                        // do something
37 <                }
38 <                catch ( IOException e ){
39 <                        // do something
40 <                }
41 <        
42 <                // do the funky jibble
43 <                connect(serverName, serverPort);
44 <                
45 <                
46 <        }
40 > //---STATIC METHODS---
41  
42 <        private void connect( String serverName, int serverPort ){
43 <                Socket mySocket;
44 <                
45 <                // might throw a UnknownHostException
46 <                try {
47 <                        mySocket = new Socket(serverName, serverPort );
48 <                        // ok we have our socket connected ok. grab their input and output streams
49 <                    socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
50 <                    socketOut = new PrintWriter(mySocket.getOutputStream());
51 <        
52 <                
53 <                if (sendCommand("STARTCONFIG") == "OK"){
54 <                    // everything is fine
55 <                    sendCommand("LASTMODIFIED");
56 <                    sendCommand("FILELIST");
57 <                    
58 <                    // get all the properties
59 <                    for ( int i = 0; i < numProperties; i++ ){
60 <                        String property = (String) aList.get(i);
61 <                        myProperties.put(property, sendCommand(property));  
62 <                    }
63 <                    
64 <                    sendCommand("ENDCONFIG");
65 <                    String filter_data = sendCommand("FILTER");
66 <                    StringTokenizer tok = new StringTokenizer(filter_data,";");
67 <                    filterName = tok.nextToken();
68 <                    filterUDPPort = Integer.parseInt(tok.nextToken());
69 <                    filterTCPPort = Integer.parseInt(tok.nextToken());
70 <                        
71 <                sendCommand("END");                
72 <                    
79 <                }
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 >            System.out.println("Reading Config file");
52 >            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 >                aList.add(numProperties, tmpIn);
58 >                numProperties++;
59 >                tmpIn = inFile.readLine();  
60 >            }
61 >            System.out.println("Added "+numProperties+" properties");
62 >        }
63 >        catch ( FileNotFoundException e ){
64 >            // do something
65 >            System.out.println("Config file not found");
66 >        }
67 >        catch ( IOException e ){
68 >            // do something
69 >        }
70 >    
71 >        myProperties = new HashMap();
72 >        configChanged = false;
73          
74 <                // close the socket
75 <                mySocket.close();
74 >        // time in seconds before first retry
75 >        filterManagerRetryTime = 10;
76 >    
77 >        // do the funky jibble
78 >        connect(serverName, serverPort);
79 >    }
80 >
81 > //---PUBLIC METHODS---
82 >
83 >    public InetAddress getFilterName(){
84 >        // will return the most recent IP address (if it is dynamic for whatever reason)
85 >        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 >     * @return an integer corresponding to the UDP port of the filter
99 >     */
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 >     * @return an integer corresponding to the TCP of the filter
109 >     */    
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 >        configChanged = false;
139 >        
140 >        System.out.println("Establishing connection with filter manager");
141 >        
142 >        // might throw a UnknownHostException
143 >        try {
144 >            mySocket = new Socket( serverName, serverPort );
145 >             // ok we have our socket connected ok. grab their input and output streams
146 >            socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
147 >            socketOut = new PrintWriter(mySocket.getOutputStream());
148 >
149 >            if (sendCommand("STARTCONFIG").equals("OK")){
150 >                // everything is fine
151 >                // sendCommand("LASTMODIFIED");
152 >                lastModified = sendCommand("LASTMODIFIED");
153 >                    
154 >                fileList = sendCommand("FILELIST");
155 >                
156 >                fQDN = sendCommand("FQDN");                
157 >                // get all the properties
158 >                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 >                }
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 >                                    
173 >                sendCommand("END");        
174 >                
175 >            }
176 >          
177 >        
178 >            // close the socket
179 >            mySocket.close();
180 >            System.out.println("Completed communication with filter manager");
181 >        
182 >        }
183 >        catch ( UnknownHostException e ){
184 >            // what to do  
185 >            System.out.println("Host not found");    
186 >        }
187 >        catch ( IOException e ){
188 >            // what to do
189 >            System.out.println("Unable to read from socket, might not be open");
190 >            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 >        }
203 >            
204 >    } // connect
205 >
206 >
207 >    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 <                }
232 <                catch ( IOException e ){
233 <                        // what to do
234 <                }
235 <                        
236 <        } // connect
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 >            System.out.println("Re-establishing contact with filter manager");
237 >            configChanged = true;
238 >        }
239 >    }
240  
241 <        public InetAddress getFilterName(){
242 <                // will return the most recient IP address (if it is dynamic for whatever reason
243 <                try {
244 <                        return InetAddress.getByName(filterName);
245 <                }
246 <                catch ( UnknownHostException e ){
247 <                        // do something
248 <                        return null;
249 <                }
250 <                
251 <        }
252 <        
253 <        public int getFilterUDPPort(){
254 <                
255 <                return filterUDPPort;
256 <        }
110 <        
111 <        public int getFilterTCPPort(){
112 <                
113 <                return filterTCPPort;
114 <        }
241 >    private String sendCommand(String command){
242 >        
243 >        // System.out.println("Writing command: "+command);
244 >        socketOut.println(command);
245 >        socketOut.flush();
246 >        try {
247 >           String response = socketIn.readLine();
248 >           // System.out.println("Got: "+response);
249 >           return response;
250 >        }
251 >        catch ( IOException e ){
252 >            // something went wrong
253 >            System.out.println("Error recieving response");
254 >            return "ERROR";  
255 >        }
256 >    }
257  
258 <        public String getProperty( String name ){
259 <                
260 <                if ( myProperties.containsKey(name) ){
261 <                        // its in there, may return ERROR
262 <                        return (String) myProperties.get(name);
263 <                }
264 <                else
265 <                {
266 <                        // can't have been in the config.values.txt file!
267 <                        return "ERROR";
268 <                }
269 <        }
270 <        
271 <        private String sendCommand(String command){
272 <            
273 <            socketOut.println(command);
274 <            try {
275 <                return socketIn.readLine();
276 <            }
277 <            catch ( IOException e ){
278 <                // something went wrong
279 <                return "ERROR";  
280 <            }
281 <        }
258 > //---ACCESSOR/MUTATOR METHODS---
259 >
260 >    public boolean reloadConfig(){
261 >        return configChanged;
262 >    }
263 >
264 > //---ATTRIBUTES---
265 >
266 >    private boolean configChanged;
267 >    private String lastModified;
268 >    private String fileList;
269 >    private String fQDN;
270 >    private int numProperties;
271 >    private HashMap myProperties;
272 >    private String filterName;
273 >    private int filterUDPPort;
274 >    private int filterTCPPort;
275 >    private ArrayList aList;
276 >    private BufferedReader socketIn;
277 >    private PrintWriter socketOut;
278 >    private int filterManagerRetryTime;
279 >    
280 >
281 >
282 > //---STATIC ATTRIBUTES---
283  
284 < } // class
284 > } // class

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines