1 |
ab11 |
1.1 |
import java.net.*; |
2 |
|
|
import java.util.*; |
3 |
|
|
import java.io.*; |
4 |
|
|
|
5 |
|
|
class Config { |
6 |
|
|
|
7 |
|
|
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 |
|
|
|
17 |
|
|
|
18 |
|
|
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 |
|
|
} |
47 |
|
|
|
48 |
|
|
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 |
93 |
|
|
|
94 |
|
|
public InetAddress getFilterName(){ |
95 |
|
|
// will return the most recient IP address (if it is dynamic for whatever reason |
96 |
|
|
try { |
97 |
|
|
return InetAddress.getByName(filterName); |
98 |
|
|
} |
99 |
|
|
catch ( UnknownHostException e ){ |
100 |
|
|
// do something |
101 |
|
|
return null; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
public int getFilterUDPPort(){ |
107 |
|
|
|
108 |
|
|
return filterUDPPort; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
public int getFilterTCPPort(){ |
112 |
|
|
|
113 |
|
|
return filterTCPPort; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
public String getProperty( String name ){ |
117 |
|
|
|
118 |
|
|
if ( myProperties.containsKey(name) ){ |
119 |
|
|
// its in there, may return ERROR |
120 |
|
|
return (String) myProperties.get(name); |
121 |
|
|
} |
122 |
|
|
else |
123 |
|
|
{ |
124 |
|
|
// can't have been in the config.values.txt file! |
125 |
|
|
return "ERROR"; |
126 |
|
|
} |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
private String sendCommand(String command){ |
130 |
|
|
|
131 |
|
|
socketOut.println(command); |
132 |
|
|
try { |
133 |
|
|
return socketIn.readLine(); |
134 |
|
|
} |
135 |
|
|
catch ( IOException e ){ |
136 |
|
|
// something went wrong |
137 |
|
|
return "ERROR"; |
138 |
|
|
} |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
} // class |