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, 11 months ago) by ab11
Branch: MAIN
Changes since 1.2: +76 -17 lines
Log Message:
Added Heartbeat functionality, and reaload function

File Contents

# Content
1 //---PACKAGE DECLARATION---
2
3 //---IMPORTS---
4
5 import java.net.*;
6 import java.util.*;
7 import java.io.*;
8
9 /**
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 * @version $Id: Config.java,v 1.2 2000/11/27 20:36:13 ab11 Exp $
16 */
17 class Config {
18
19 //---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 System.out.println("Reading Config file");
33 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 aList.add(numProperties, tmpIn);
39 numProperties++;
40 tmpIn = inFile.readLine();
41 }
42 System.out.println("Added "+numProperties+" properties");
43 }
44 catch ( FileNotFoundException e ){
45 // do something
46 System.out.println("Config file not found");
47 }
48 catch ( IOException e ){
49 // do something
50 }
51
52 myProperties = new HashMap();
53 configChanged = false;
54
55
56 // 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 configChanged = false;
118
119 System.out.println("Establishing connection with config manager");
120
121 // might throw a UnknownHostException
122 try {
123 mySocket = new Socket( serverName, serverPort );
124 // ok we have our socket connected ok. grab their input and output streams
125 socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
126 socketOut = new PrintWriter(mySocket.getOutputStream());
127
128 if (sendCommand("STARTCONFIG").equals("OK")){
129 // everything is fine
130 // sendCommand("LASTMODIFIED");
131 lastModified = sendCommand("LASTMODIFIED");
132
133 fileList = sendCommand("FILELIST");
134 // get all the properties
135 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 }
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
152 }
153
154
155 // close the socket
156 mySocket.close();
157 System.out.println("Completed communication with config manager");
158
159 }
160 catch ( UnknownHostException e ){
161 // what to do
162 System.out.println("Host not found");
163 }
164 catch ( IOException e ){
165 // what to do
166 System.out.println("Unable to read from socket, might not be open");
167 }
168
169 } // connect
170
171
172 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 private String sendCommand(String command){
205
206 // System.out.println("Writing command: "+command);
207 socketOut.println(command);
208 socketOut.flush();
209 try {
210 String response = socketIn.readLine();
211 // System.out.println("Got: "+response);
212 return response;
213 }
214 catch ( IOException e ){
215 // something went wrong
216 System.out.println("Error recieving response");
217 return "ERROR";
218 }
219 }
220
221 //---ACCESSOR/MUTATOR METHODS---
222
223 public boolean reloadConfig(){
224 return configChanged;
225 }
226
227 //---ATTRIBUTES---
228
229 private boolean configChanged;
230 private String lastModified;
231 private String fileList;
232 private int numProperties;
233 private HashMap myProperties;
234 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
245 } // class