--- projects/cms/source/host/java/Config.java 2000/11/27 20:36:13 1.2 +++ projects/cms/source/host/java/Config.java 2002/05/21 16:47:12 1.10 @@ -1,3 +1,23 @@ +/* + * i-scream central monitoring system + * http://www.i-scream.org.uk + * Copyright (C) 2000-2002 i-scream + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + //---PACKAGE DECLARATION--- //---IMPORTS--- @@ -8,11 +28,11 @@ import java.io.*; /** * Configurator object for the JavaHost - * Will connect to the configurator manager and collect its specific + * Will connect to the filter manager and collect its specific * configuration * - * @author $Author: ab11 $ - * @version $Id: Config.java,v 1.2 2000/11/27 20:36:13 ab11 Exp $ + * @author $Author: tdb $ + * @version $Id: Config.java,v 1.10 2002/05/21 16:47:12 tdb Exp $ */ class Config { @@ -29,23 +49,32 @@ class Config { // read in from the file. try { + System.out.println("Reading Config file"); BufferedReader inFile = new BufferedReader(new FileReader("config.values.txt")); aList = new ArrayList(); numProperties = 0; String tmpIn = inFile.readLine(); while ( tmpIn != null ){ - aList.add(tmpIn); + aList.add(numProperties, tmpIn); numProperties++; tmpIn = inFile.readLine(); } + System.out.println("Added "+numProperties+" properties"); } catch ( FileNotFoundException e ){ // do something + System.out.println("Config file not found"); } catch ( IOException e ){ // do something } + myProperties = new HashMap(); + configChanged = false; + + // time in seconds before first retry + filterManagerRetryTime = 10; + // do the funky jibble connect(serverName, serverPort); } @@ -53,7 +82,7 @@ class Config { //---PUBLIC METHODS--- public InetAddress getFilterName(){ - // will return the most recient IP address (if it is dynamic for whatever reason + // will return the most recent IP address (if it is dynamic for whatever reason) try { return InetAddress.getByName(filterName); } @@ -67,7 +96,7 @@ class Config { /** * Used to retrieve the port to send UDP packets to on the filter * - * @return an integer corrisponding to the UDP port of the filter + * @return an integer corresponding to the UDP port of the filter */ public int getFilterUDPPort(){ @@ -77,7 +106,7 @@ class Config { /** * Used to retrieve the port to send TCP heartbeats to on the filter * - * @return an integer corrisponding to the TCP of the filter + * @return an integer corresponding to the TCP of the filter */ public int getFilterTCPPort(){ @@ -107,24 +136,32 @@ class Config { private void connect( String serverName, int serverPort ){ Socket mySocket; + configChanged = false; + System.out.println("Establishing connection with filter manager"); + // might throw a UnknownHostException try { - mySocket = new Socket(serverName, serverPort ); - // ok we have our socket connected ok. grab their input and output streams + mySocket = new Socket( serverName, serverPort ); + // ok we have our socket connected ok. grab their input and output streams socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream())); socketOut = new PrintWriter(mySocket.getOutputStream()); - - - if (sendCommand("STARTCONFIG") == "OK"){ + + if (sendCommand("STARTCONFIG").equals("OK")){ // everything is fine - sendCommand("LASTMODIFIED"); - sendCommand("FILELIST"); + // sendCommand("LASTMODIFIED"); + lastModified = sendCommand("LASTMODIFIED"); + + fileList = sendCommand("FILELIST"); + fQDN = sendCommand("FQDN"); // get all the properties - for ( int i = 0; i < numProperties; i++ ){ - String property = (String) aList.get(i); - myProperties.put(property, sendCommand(property)); + if ( numProperties > 0 ){ + // sendCommand("CONFIG"); + for ( int i = 0; i < numProperties; i++ ){ + String property = (String) aList.get(i); + myProperties.put(property, sendCommand(property)); + } } sendCommand("ENDCONFIG"); @@ -133,54 +170,116 @@ class Config { filterName = tok.nextToken(); filterUDPPort = Integer.parseInt(tok.nextToken()); filterTCPPort = Integer.parseInt(tok.nextToken()); - + sendCommand("END"); - + } + // close the socket mySocket.close(); + System.out.println("Completed communication with filter manager"); } catch ( UnknownHostException e ){ - // what to do + // what to do + System.out.println("Host not found"); } catch ( IOException e ){ // what to do + System.out.println("Unable to read from socket, might not be open"); + System.out.println("Retrying in "+filterManagerRetryTime+" seconds"); + configChanged = true; + try { + Thread.sleep(filterManagerRetryTime*1000); + } + catch( InterruptedException f ){ + System.out.println("Sleep interrupted"); + } + filterManagerRetryTime = filterManagerRetryTime * 2; + // warning this WILL cause a stack overflow after a while.. + // need to fix it. + connect(serverName, serverPort); } } // connect + public void sendHeartBeat(){ + + Socket mySocket; + try { + + mySocket = new Socket( filterName, filterTCPPort ); + // ok we have our socket connected ok. grab their input and output streams + socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream())); + socketOut = new PrintWriter(mySocket.getOutputStream()); + + + if ( sendCommand("HEARTBEAT").equals("OK") ){ + if ( sendCommand("CONFIG").equals("OK") ){ + sendCommand(fileList); + if ( sendCommand(lastModified).equals("ERROR") ){ + // config has changed + configChanged = true; + } + sendCommand("ENDHEARTBEAT"); + } + } + } + catch ( UnknownHostException e ){ + // what to do + System.out.println("Host not found"); + } + catch ( IOException e ){ + // what to do + System.out.println("Unable to read from socket, might not be open"); + System.out.println("Re-establishing contact with filter manager"); + configChanged = true; + } + } + private String sendCommand(String command){ + // System.out.println("Writing command: "+command); socketOut.println(command); + socketOut.flush(); try { - return socketIn.readLine(); + String response = socketIn.readLine(); + // System.out.println("Got: "+response); + return response; } catch ( IOException e ){ // something went wrong + System.out.println("Error recieving response"); return "ERROR"; } } //---ACCESSOR/MUTATOR METHODS--- + public boolean reloadConfig(){ + return configChanged; + } + //---ATTRIBUTES--- - + private boolean configChanged; private String lastModified; + private String fileList; + private String fQDN; private int numProperties; - private Hashtable myProperties; + private HashMap myProperties; private String filterName; private int filterUDPPort; private int filterTCPPort; private ArrayList aList; private BufferedReader socketIn; private PrintWriter socketOut; + private int filterManagerRetryTime; //---STATIC ATTRIBUTES--- -} // class \ No newline at end of file +} // class