1 |
//---PACKAGE DECLARATION--- |
2 |
|
3 |
//---IMPORTS--- |
4 |
import uk.ac.ukc.iscream.core.*; |
5 |
import uk.ac.ukc.iscream.filter.*; |
6 |
import java.net.Socket; |
7 |
import java.io.InputStream; |
8 |
import java.io.OutputStream; |
9 |
import java.io.IOException; |
10 |
import java.io.*; |
11 |
import org.omg.CORBA.*; |
12 |
import org.omg.CosNaming.*; |
13 |
|
14 |
/** |
15 |
* <ONE LINE DESCRIPTION> |
16 |
* <DETAILED DESCRIPTION> |
17 |
* |
18 |
* @author $Author: tdb1 $ |
19 |
* @version $Id: TCPReaderInit.java,v 1.3 2000/11/27 21:48:44 tdb1 Exp $ |
20 |
*/ |
21 |
class TCPReaderInit extends Thread { |
22 |
|
23 |
//---FINAL ATTRIBUTES--- |
24 |
|
25 |
/** |
26 |
* The current CVS revision of this class |
27 |
*/ |
28 |
public final String REVISION = "$Revision: 1.3 $"; |
29 |
|
30 |
//---STATIC METHODS--- |
31 |
|
32 |
//---CONSTRUCTORS--- |
33 |
|
34 |
public TCPReaderInit(Socket socket, ConfigurationManager configManager, Logger logRef, Filter parent) throws IOException { |
35 |
_configManager = configManager; |
36 |
_logRef = logRef; |
37 |
_socket = socket; |
38 |
_parent = parent; |
39 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
40 |
_socketOut = new PrintWriter(_socket.getOutputStream()); |
41 |
_logRef.write(toString(), Logger.SYSINIT, "created"); |
42 |
} |
43 |
|
44 |
//---PUBLIC METHODS--- |
45 |
|
46 |
//NEED TO MAJORILY CHANGE THIS BIT ! |
47 |
public void run() { |
48 |
try { |
49 |
//variables |
50 |
String filelist = ""; |
51 |
String lastModified = ""; |
52 |
String inBound = ""; |
53 |
|
54 |
inBound = _socketIn.readLine(); |
55 |
if(!inBound.equals("HEARTBEAT")) { |
56 |
_socketOut.println("ERROR"); |
57 |
_socketOut.flush(); |
58 |
throw new IOException("protocol error - expecting:HEARTBEAT got:" + inBound); |
59 |
} else { |
60 |
_socketOut.println("OK"); |
61 |
_socketOut.flush(); |
62 |
} |
63 |
|
64 |
inBound = _socketIn.readLine(); |
65 |
if(!inBound.equals("CONFIG")) { |
66 |
_socketOut.println("ERROR"); |
67 |
_socketOut.flush(); |
68 |
throw new IOException("protocol error - expecting:CONFIG got:" + inBound); |
69 |
} else { |
70 |
_socketOut.println("OK"); |
71 |
_socketOut.flush(); |
72 |
} |
73 |
|
74 |
inBound = _socketIn.readLine(); |
75 |
filelist = inBound; |
76 |
_socketOut.println("OK"); |
77 |
_socketOut.flush(); |
78 |
|
79 |
inBound = _socketIn.readLine(); |
80 |
lastModified = inBound; |
81 |
|
82 |
boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified)); |
83 |
|
84 |
if(newConfig) { |
85 |
_socketOut.println("ERROR"); |
86 |
} |
87 |
else { |
88 |
_socketOut.println("OK"); |
89 |
} |
90 |
_socketOut.flush(); |
91 |
|
92 |
inBound = _socketIn.readLine(); |
93 |
if(!inBound.equals("ENDHEARTBEAT")) { |
94 |
_socketOut.println("ERROR"); |
95 |
_socketOut.flush(); |
96 |
throw new IOException("protocol error - expecting:ENDHEARTBEAT got:" + inBound); |
97 |
} else { |
98 |
_socketOut.println("OK"); |
99 |
_socketOut.flush(); |
100 |
} |
101 |
|
102 |
// NEED TO LOG THE HEARTBEAT IN XML HERE. |
103 |
|
104 |
} catch (Exception e) { |
105 |
_logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
106 |
} |
107 |
|
108 |
_socketOut.flush(); |
109 |
// Disconnect streams & socket |
110 |
try { |
111 |
_socketIn.close(); |
112 |
_socketOut.close(); |
113 |
_socket.close(); |
114 |
} catch (IOException e) { |
115 |
_logRef.write(toString(), Logger.ERROR, "exception on socket close"); |
116 |
} |
117 |
_logRef.write(toString(), Logger.SYSMSG, "finished"); |
118 |
} |
119 |
|
120 |
/** |
121 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
122 |
* method to provide clean logging (every class should have this). |
123 |
* |
124 |
* @return the name of this class and its CVS revision |
125 |
*/ |
126 |
public String toString() { |
127 |
return this.getClass().getName() + "{" + _socket.getInetAddress().getHostName() |
128 |
+ "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
129 |
|
130 |
} |
131 |
|
132 |
//---PRIVATE METHODS--- |
133 |
|
134 |
//---ACCESSOR/MUTATOR METHODS--- |
135 |
|
136 |
//---ATTRIBUTES--- |
137 |
|
138 |
ConfigurationManager _configManager; |
139 |
Logger _logRef; |
140 |
Socket _socket; |
141 |
BufferedReader _socketIn; |
142 |
PrintWriter _socketOut; |
143 |
Filter _parent; |
144 |
//---STATIC ATTRIBUTES--- |
145 |
|
146 |
} |