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$ |
15 |
+ |
* @version $Id$ |
16 |
+ |
*/ |
17 |
|
class Config { |
18 |
|
|
19 |
< |
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 |
< |
|
19 |
> |
//---FINAL ATTRIBUTES--- |
20 |
|
|
21 |
< |
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 |
< |
} |
21 |
> |
//---STATIC METHODS--- |
22 |
|
|
23 |
< |
private void connect( String serverName, int serverPort ){ |
49 |
< |
Socket mySocket; |
50 |
< |
|
51 |
< |
// might throw a UnknownHostException |
52 |
< |
try { |
53 |
< |
mySocket = new Socket(serverName, serverPort ); |
54 |
< |
// ok we have our socket connected ok. grab their input and output streams |
55 |
< |
socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream())); |
56 |
< |
socketOut = new PrintWriter(mySocket.getOutputStream()); |
57 |
< |
|
58 |
< |
|
59 |
< |
if (sendCommand("STARTCONFIG") == "OK"){ |
60 |
< |
// everything is fine |
61 |
< |
sendCommand("LASTMODIFIED"); |
62 |
< |
sendCommand("FILELIST"); |
63 |
< |
|
64 |
< |
// get all the properties |
65 |
< |
for ( int i = 0; i < numProperties; i++ ){ |
66 |
< |
String property = (String) aList.get(i); |
67 |
< |
myProperties.put(property, sendCommand(property)); |
68 |
< |
} |
69 |
< |
|
70 |
< |
sendCommand("ENDCONFIG"); |
71 |
< |
String filter_data = sendCommand("FILTER"); |
72 |
< |
StringTokenizer tok = new StringTokenizer(filter_data,";"); |
73 |
< |
filterName = tok.nextToken(); |
74 |
< |
filterUDPPort = Integer.parseInt(tok.nextToken()); |
75 |
< |
filterTCPPort = Integer.parseInt(tok.nextToken()); |
76 |
< |
|
77 |
< |
sendCommand("END"); |
78 |
< |
|
79 |
< |
} |
80 |
< |
|
81 |
< |
// close the socket |
82 |
< |
mySocket.close(); |
83 |
< |
|
84 |
< |
} |
85 |
< |
catch ( UnknownHostException e ){ |
86 |
< |
// what to do |
87 |
< |
} |
88 |
< |
catch ( IOException e ){ |
89 |
< |
// what to do |
90 |
< |
} |
91 |
< |
|
92 |
< |
} // connect |
23 |
> |
//---CONSTRUCTORS--- |
24 |
|
|
25 |
< |
public InetAddress getFilterName(){ |
26 |
< |
// will return the most recient IP address (if it is dynamic for whatever reason |
27 |
< |
try { |
28 |
< |
return InetAddress.getByName(filterName); |
29 |
< |
} |
30 |
< |
catch ( UnknownHostException e ){ |
31 |
< |
// do something |
32 |
< |
return null; |
33 |
< |
} |
34 |
< |
|
35 |
< |
} |
36 |
< |
|
37 |
< |
public int getFilterUDPPort(){ |
38 |
< |
|
39 |
< |
return filterUDPPort; |
40 |
< |
} |
41 |
< |
|
42 |
< |
public int getFilterTCPPort(){ |
43 |
< |
|
44 |
< |
return filterTCPPort; |
45 |
< |
} |
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 |
> |
BufferedReader inFile = new BufferedReader(new FileReader("config.values.txt")); |
33 |
> |
aList = new ArrayList(); |
34 |
> |
numProperties = 0; |
35 |
> |
String tmpIn = inFile.readLine(); |
36 |
> |
while ( tmpIn != null ){ |
37 |
> |
aList.add(tmpIn); |
38 |
> |
numProperties++; |
39 |
> |
tmpIn = inFile.readLine(); |
40 |
> |
} |
41 |
> |
} |
42 |
> |
catch ( FileNotFoundException e ){ |
43 |
> |
// do something |
44 |
> |
} |
45 |
> |
catch ( IOException e ){ |
46 |
> |
// do something |
47 |
> |
} |
48 |
> |
|
49 |
> |
// do the funky jibble |
50 |
> |
connect(serverName, serverPort); |
51 |
> |
} |
52 |
|
|
53 |
< |
public String getProperty( String name ){ |
54 |
< |
|
55 |
< |
if ( myProperties.containsKey(name) ){ |
56 |
< |
// its in there, may return ERROR |
57 |
< |
return (String) myProperties.get(name); |
58 |
< |
} |
59 |
< |
else |
60 |
< |
{ |
61 |
< |
// can't have been in the config.values.txt file! |
62 |
< |
return "ERROR"; |
63 |
< |
} |
64 |
< |
} |
65 |
< |
|
66 |
< |
private String sendCommand(String command){ |
67 |
< |
|
68 |
< |
socketOut.println(command); |
69 |
< |
try { |
70 |
< |
return socketIn.readLine(); |
71 |
< |
} |
72 |
< |
catch ( IOException e ){ |
73 |
< |
// something went wrong |
74 |
< |
return "ERROR"; |
75 |
< |
} |
76 |
< |
} |
53 |
> |
//---PUBLIC METHODS--- |
54 |
> |
|
55 |
> |
public InetAddress getFilterName(){ |
56 |
> |
// will return the most recient IP address (if it is dynamic for whatever reason |
57 |
> |
try { |
58 |
> |
return InetAddress.getByName(filterName); |
59 |
> |
} |
60 |
> |
catch ( UnknownHostException e ){ |
61 |
> |
// do something |
62 |
> |
return null; |
63 |
> |
} |
64 |
> |
|
65 |
> |
} |
66 |
> |
|
67 |
> |
/** |
68 |
> |
* Used to retrieve the port to send UDP packets to on the filter |
69 |
> |
* |
70 |
> |
* @return an integer corrisponding to the UDP port of the filter |
71 |
> |
*/ |
72 |
> |
public int getFilterUDPPort(){ |
73 |
> |
|
74 |
> |
return filterUDPPort; |
75 |
> |
} |
76 |
> |
|
77 |
> |
/** |
78 |
> |
* Used to retrieve the port to send TCP heartbeats to on the filter |
79 |
> |
* |
80 |
> |
* @return an integer corrisponding to the TCP of the filter |
81 |
> |
*/ |
82 |
> |
public int getFilterTCPPort(){ |
83 |
> |
|
84 |
> |
return filterTCPPort; |
85 |
> |
} |
86 |
> |
|
87 |
> |
/** |
88 |
> |
* Used to get the hostname of the filter we are to talk to. |
89 |
> |
* |
90 |
> |
* @return a string representing the hostname of the filter |
91 |
> |
*/ |
92 |
> |
public String getProperty( String name ){ |
93 |
> |
|
94 |
> |
if ( myProperties.containsKey(name) ){ |
95 |
> |
// its in there, may return ERROR |
96 |
> |
return (String) myProperties.get(name); |
97 |
> |
} |
98 |
> |
else |
99 |
> |
{ |
100 |
> |
// can't have been in the config.values.txt file! |
101 |
> |
return "ERROR"; |
102 |
> |
} |
103 |
> |
} |
104 |
> |
|
105 |
> |
|
106 |
> |
//---PRIVATE METHODS--- |
107 |
> |
|
108 |
> |
private void connect( String serverName, int serverPort ){ |
109 |
> |
Socket mySocket; |
110 |
> |
|
111 |
> |
// might throw a UnknownHostException |
112 |
> |
try { |
113 |
> |
mySocket = new Socket(serverName, serverPort ); |
114 |
> |
// ok we have our socket connected ok. grab their input and output streams |
115 |
> |
socketIn = new BufferedReader(new InputStreamReader(mySocket.getInputStream())); |
116 |
> |
socketOut = new PrintWriter(mySocket.getOutputStream()); |
117 |
> |
|
118 |
> |
|
119 |
> |
if (sendCommand("STARTCONFIG") == "OK"){ |
120 |
> |
// everything is fine |
121 |
> |
sendCommand("LASTMODIFIED"); |
122 |
> |
sendCommand("FILELIST"); |
123 |
> |
|
124 |
> |
// get all the properties |
125 |
> |
for ( int i = 0; i < numProperties; i++ ){ |
126 |
> |
String property = (String) aList.get(i); |
127 |
> |
myProperties.put(property, sendCommand(property)); |
128 |
> |
} |
129 |
> |
|
130 |
> |
sendCommand("ENDCONFIG"); |
131 |
> |
String filter_data = sendCommand("FILTER"); |
132 |
> |
StringTokenizer tok = new StringTokenizer(filter_data,";"); |
133 |
> |
filterName = tok.nextToken(); |
134 |
> |
filterUDPPort = Integer.parseInt(tok.nextToken()); |
135 |
> |
filterTCPPort = Integer.parseInt(tok.nextToken()); |
136 |
> |
|
137 |
> |
sendCommand("END"); |
138 |
> |
|
139 |
> |
} |
140 |
> |
|
141 |
> |
// close the socket |
142 |
> |
mySocket.close(); |
143 |
> |
|
144 |
> |
} |
145 |
> |
catch ( UnknownHostException e ){ |
146 |
> |
// what to do |
147 |
> |
} |
148 |
> |
catch ( IOException e ){ |
149 |
> |
// what to do |
150 |
> |
} |
151 |
> |
|
152 |
> |
} // connect |
153 |
> |
|
154 |
> |
|
155 |
> |
private String sendCommand(String command){ |
156 |
> |
|
157 |
> |
socketOut.println(command); |
158 |
> |
try { |
159 |
> |
return socketIn.readLine(); |
160 |
> |
} |
161 |
> |
catch ( IOException e ){ |
162 |
> |
// something went wrong |
163 |
> |
return "ERROR"; |
164 |
> |
} |
165 |
> |
} |
166 |
> |
|
167 |
> |
//---ACCESSOR/MUTATOR METHODS--- |
168 |
> |
|
169 |
> |
//---ATTRIBUTES--- |
170 |
> |
|
171 |
> |
|
172 |
> |
private String lastModified; |
173 |
> |
private int numProperties; |
174 |
> |
private Hashtable myProperties; |
175 |
> |
private String filterName; |
176 |
> |
private int filterUDPPort; |
177 |
> |
private int filterTCPPort; |
178 |
> |
private ArrayList aList; |
179 |
> |
private BufferedReader socketIn; |
180 |
> |
private PrintWriter socketOut; |
181 |
> |
|
182 |
> |
|
183 |
> |
|
184 |
> |
//---STATIC ATTRIBUTES--- |
185 |
|
|
186 |
|
} // class |