ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/java/Config.java
Revision: 1.3
Committed: Thu Nov 30 04:06:52 2000 UTC (23 years, 9 months ago) by ab11
Branch: MAIN
Changes since 1.2: +76 -17 lines
Log Message:
Added Heartbeat functionality, and reaload function

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     * Will connect to the configurator manager and collect its specific
12     * configuration
13     *
14     * @author $Author: ab11 $
15 ab11 1.3 * @version $Id: Config.java,v 1.2 2000/11/27 20:36:13 ab11 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    
56 ab11 1.2 // do the funky jibble
57     connect(serverName, serverPort);
58     }
59    
60     //---PUBLIC METHODS---
61    
62     public InetAddress getFilterName(){
63     // will return the most recient IP address (if it is dynamic for whatever reason
64     try {
65     return InetAddress.getByName(filterName);
66     }
67     catch ( UnknownHostException e ){
68     // do something
69     return null;
70     }
71    
72     }
73    
74     /**
75     * Used to retrieve the port to send UDP packets to on the filter
76     *
77     * @return an integer corrisponding to the UDP port of the filter
78     */
79     public int getFilterUDPPort(){
80    
81     return filterUDPPort;
82     }
83    
84     /**
85     * Used to retrieve the port to send TCP heartbeats to on the filter
86     *
87     * @return an integer corrisponding to the TCP of the filter
88     */
89     public int getFilterTCPPort(){
90    
91     return filterTCPPort;
92     }
93    
94     /**
95     * Used to get the hostname of the filter we are to talk to.
96     *
97     * @return a string representing the hostname of the filter
98     */
99     public String getProperty( String name ){
100    
101     if ( myProperties.containsKey(name) ){
102     // its in there, may return ERROR
103     return (String) myProperties.get(name);
104     }
105     else
106     {
107     // can't have been in the config.values.txt file!
108     return "ERROR";
109     }
110     }
111    
112    
113     //---PRIVATE METHODS---
114    
115     private void connect( String serverName, int serverPort ){
116     Socket mySocket;
117 ab11 1.3 configChanged = false;
118    
119     System.out.println("Establishing connection with config manager");
120 ab11 1.2
121     // might throw a UnknownHostException
122     try {
123 ab11 1.3 mySocket = new Socket( serverName, serverPort );
124     // ok we have our socket connected ok. grab their input and output streams
125 ab11 1.2 socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
126     socketOut = new PrintWriter(mySocket.getOutputStream());
127 ab11 1.3
128     if (sendCommand("STARTCONFIG").equals("OK")){
129 ab11 1.2 // everything is fine
130 ab11 1.3 // sendCommand("LASTMODIFIED");
131     lastModified = sendCommand("LASTMODIFIED");
132 ab11 1.2
133 ab11 1.3 fileList = sendCommand("FILELIST");
134 ab11 1.2 // get all the properties
135 ab11 1.3 if ( numProperties > 0 ){
136     // sendCommand("CONFIG");
137     for ( int i = 0; i < numProperties; i++ ){
138     String property = (String) aList.get(i);
139     myProperties.put(property, sendCommand(property));
140     }
141 ab11 1.2 }
142    
143     sendCommand("ENDCONFIG");
144     String filter_data = sendCommand("FILTER");
145     StringTokenizer tok = new StringTokenizer(filter_data,";");
146     filterName = tok.nextToken();
147     filterUDPPort = Integer.parseInt(tok.nextToken());
148     filterTCPPort = Integer.parseInt(tok.nextToken());
149    
150     sendCommand("END");
151 ab11 1.3
152 ab11 1.2 }
153 ab11 1.3
154 ab11 1.2
155     // close the socket
156     mySocket.close();
157 ab11 1.3 System.out.println("Completed communication with config manager");
158 ab11 1.2
159     }
160     catch ( UnknownHostException e ){
161 ab11 1.3 // what to do
162     System.out.println("Host not found");
163 ab11 1.2 }
164     catch ( IOException e ){
165     // what to do
166 ab11 1.3 System.out.println("Unable to read from socket, might not be open");
167 ab11 1.2 }
168    
169     } // connect
170    
171    
172 ab11 1.3 public void sendHeartBeat(){
173    
174     Socket mySocket;
175     try {
176    
177     mySocket = new Socket( filterName, filterTCPPort );
178     // ok we have our socket connected ok. grab their input and output streams
179     socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
180     socketOut = new PrintWriter(mySocket.getOutputStream());
181    
182    
183     if ( sendCommand("HEARTBEAT").equals("OK") ){
184     if ( sendCommand("CONFIG").equals("OK") ){
185     sendCommand(fileList);
186     if ( sendCommand(lastModified).equals("ERROR") ){
187     // config has changed
188     configChanged = true;
189     }
190     sendCommand("ENDHEARTBEAT");
191     }
192     }
193     }
194     catch ( UnknownHostException e ){
195     // what to do
196     System.out.println("Host not found");
197     }
198     catch ( IOException e ){
199     // what to do
200     System.out.println("Unable to read from socket, might not be open");
201     }
202     }
203    
204 ab11 1.2 private String sendCommand(String command){
205    
206 ab11 1.3 // System.out.println("Writing command: "+command);
207 ab11 1.2 socketOut.println(command);
208 ab11 1.3 socketOut.flush();
209 ab11 1.2 try {
210 ab11 1.3 String response = socketIn.readLine();
211     // System.out.println("Got: "+response);
212     return response;
213 ab11 1.2 }
214     catch ( IOException e ){
215     // something went wrong
216 ab11 1.3 System.out.println("Error recieving response");
217 ab11 1.2 return "ERROR";
218     }
219     }
220    
221     //---ACCESSOR/MUTATOR METHODS---
222    
223 ab11 1.3 public boolean reloadConfig(){
224     return configChanged;
225     }
226    
227 ab11 1.2 //---ATTRIBUTES---
228    
229 ab11 1.3 private boolean configChanged;
230 ab11 1.2 private String lastModified;
231 ab11 1.3 private String fileList;
232 ab11 1.2 private int numProperties;
233 ab11 1.3 private HashMap myProperties;
234 ab11 1.2 private String filterName;
235     private int filterUDPPort;
236     private int filterTCPPort;
237     private ArrayList aList;
238     private BufferedReader socketIn;
239     private PrintWriter socketOut;
240    
241    
242    
243     //---STATIC ATTRIBUTES---
244 ab11 1.1
245     } // class