--- projects/cms/source/host/java/Config.java 2000/11/27 19:49:00 1.1 +++ projects/cms/source/host/java/Config.java 2000/11/27 20:36:13 1.2 @@ -1,141 +1,186 @@ +//---PACKAGE DECLARATION--- + +//---IMPORTS--- + import java.net.*; import java.util.*; import java.io.*; +/** + * Configurator object for the JavaHost + * Will connect to the configurator manager and collect its specific + * configuration + * + * @author $Author: ab11 $ + * @version $Id: Config.java,v 1.2 2000/11/27 20:36:13 ab11 Exp $ + */ class Config { - private String lastModified; - private int numProperties; - private Hashtable myProperties; - private String filterName; - private int filterUDPPort; - private int filterTCPPort; - private ArrayList aList; - private BufferedReader socketIn; - private PrintWriter socketOut; - +//---FINAL ATTRIBUTES--- - public Config( String serverName, int serverPort ){ - // takes in the master config settings and creates a connection with - // this computer, and downloads all the values listed in config.values.txt - // which should be in the local directory - - // read in from the file. - try { - BufferedReader inFile = new BufferedReader(new FileReader("config.values.txt")); - aList = new ArrayList(); - numProperties = 0; - String tmpIn = inFile.readLine(); - while ( tmpIn != null ){ - aList.add(tmpIn); - numProperties++; - tmpIn = inFile.readLine(); - } - } - catch ( FileNotFoundException e ){ - // do something - } - catch ( IOException e ){ - // do something - } - - // do the funky jibble - connect(serverName, serverPort); - - - } +//---STATIC METHODS--- - private void connect( String serverName, int serverPort ){ - Socket mySocket; - - // might throw a UnknownHostException - try { - 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"){ - // everything is fine - sendCommand("LASTMODIFIED"); - sendCommand("FILELIST"); - - // get all the properties - for ( int i = 0; i < numProperties; i++ ){ - String property = (String) aList.get(i); - myProperties.put(property, sendCommand(property)); - } - - sendCommand("ENDCONFIG"); - String filter_data = sendCommand("FILTER"); - StringTokenizer tok = new StringTokenizer(filter_data,";"); - filterName = tok.nextToken(); - filterUDPPort = Integer.parseInt(tok.nextToken()); - filterTCPPort = Integer.parseInt(tok.nextToken()); - - sendCommand("END"); - - } - - // close the socket - mySocket.close(); - - } - catch ( UnknownHostException e ){ - // what to do - } - catch ( IOException e ){ - // what to do - } - - } // connect +//---CONSTRUCTORS--- - public InetAddress getFilterName(){ - // will return the most recient IP address (if it is dynamic for whatever reason - try { - return InetAddress.getByName(filterName); - } - catch ( UnknownHostException e ){ - // do something - return null; - } - - } - - public int getFilterUDPPort(){ - - return filterUDPPort; - } - - public int getFilterTCPPort(){ - - return filterTCPPort; - } + public Config( String serverName, int serverPort ){ + // takes in the master config settings and creates a connection with + // this computer, and downloads all the values listed in config.values.txt + // which should be in the local directory + + // read in from the file. + try { + BufferedReader inFile = new BufferedReader(new FileReader("config.values.txt")); + aList = new ArrayList(); + numProperties = 0; + String tmpIn = inFile.readLine(); + while ( tmpIn != null ){ + aList.add(tmpIn); + numProperties++; + tmpIn = inFile.readLine(); + } + } + catch ( FileNotFoundException e ){ + // do something + } + catch ( IOException e ){ + // do something + } + + // do the funky jibble + connect(serverName, serverPort); + } - public String getProperty( String name ){ - - if ( myProperties.containsKey(name) ){ - // its in there, may return ERROR - return (String) myProperties.get(name); - } - else - { - // can't have been in the config.values.txt file! - return "ERROR"; - } - } - - private String sendCommand(String command){ - - socketOut.println(command); - try { - return socketIn.readLine(); - } - catch ( IOException e ){ - // something went wrong - return "ERROR"; - } - } +//---PUBLIC METHODS--- + + public InetAddress getFilterName(){ + // will return the most recient IP address (if it is dynamic for whatever reason + try { + return InetAddress.getByName(filterName); + } + catch ( UnknownHostException e ){ + // do something + return null; + } + + } + + /** + * Used to retrieve the port to send UDP packets to on the filter + * + * @return an integer corrisponding to the UDP port of the filter + */ + public int getFilterUDPPort(){ + + return filterUDPPort; + } + + /** + * Used to retrieve the port to send TCP heartbeats to on the filter + * + * @return an integer corrisponding to the TCP of the filter + */ + public int getFilterTCPPort(){ + + return filterTCPPort; + } + + /** + * Used to get the hostname of the filter we are to talk to. + * + * @return a string representing the hostname of the filter + */ + public String getProperty( String name ){ + + if ( myProperties.containsKey(name) ){ + // its in there, may return ERROR + return (String) myProperties.get(name); + } + else + { + // can't have been in the config.values.txt file! + return "ERROR"; + } + } + + +//---PRIVATE METHODS--- + + private void connect( String serverName, int serverPort ){ + Socket mySocket; + + // might throw a UnknownHostException + try { + 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"){ + // everything is fine + sendCommand("LASTMODIFIED"); + sendCommand("FILELIST"); + + // get all the properties + for ( int i = 0; i < numProperties; i++ ){ + String property = (String) aList.get(i); + myProperties.put(property, sendCommand(property)); + } + + sendCommand("ENDCONFIG"); + String filter_data = sendCommand("FILTER"); + StringTokenizer tok = new StringTokenizer(filter_data,";"); + filterName = tok.nextToken(); + filterUDPPort = Integer.parseInt(tok.nextToken()); + filterTCPPort = Integer.parseInt(tok.nextToken()); + + sendCommand("END"); + + } + + // close the socket + mySocket.close(); + + } + catch ( UnknownHostException e ){ + // what to do + } + catch ( IOException e ){ + // what to do + } + + } // connect + + + private String sendCommand(String command){ + + socketOut.println(command); + try { + return socketIn.readLine(); + } + catch ( IOException e ){ + // something went wrong + return "ERROR"; + } + } + +//---ACCESSOR/MUTATOR METHODS--- + +//---ATTRIBUTES--- + + + private String lastModified; + private int numProperties; + private Hashtable myProperties; + private String filterName; + private int filterUDPPort; + private int filterTCPPort; + private ArrayList aList; + private BufferedReader socketIn; + private PrintWriter socketOut; + + + +//---STATIC ATTRIBUTES--- } // class \ No newline at end of file