ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/filter/TCPReaderInit.java
Revision: 1.28
Committed: Thu Mar 21 22:09:13 2002 UTC (22 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.27: +2 -12 lines
Log Message:
Removed a few lines of unused code.

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2 tdb 1.25 package uk.org.iscream.cms.server.filter;
3 tdb 1.1
4     //---IMPORTS---
5 tdb 1.25 import uk.org.iscream.cms.server.core.*;
6     import uk.org.iscream.cms.server.filter.*;
7     import uk.org.iscream.cms.server.componentmanager.*;
8 tdb 1.1 import java.net.Socket;
9     import java.io.*;
10 tdb 1.27 import java.util.Random;
11 tdb 1.25 import uk.org.iscream.cms.server.util.*;
12 tdb 1.1
13     /**
14 ajm 1.10 * This provides Host heartbeat functionality
15 tdb 1.1 *
16 tdb 1.26 * @author $Author: tdb $
17 tdb 1.28 * @version $Id: TCPReaderInit.java,v 1.27 2002/03/21 17:44:51 tdb Exp $
18 tdb 1.1 */
19     class TCPReaderInit extends Thread {
20    
21     //---FINAL ATTRIBUTES---
22    
23     /**
24     * The current CVS revision of this class
25     */
26 tdb 1.28 public final String REVISION = "$Revision: 1.27 $";
27 tdb 1.1
28     //---STATIC METHODS---
29    
30     //---CONSTRUCTORS---
31 tdb 1.13
32     /**
33     * Construct a new TCPReaderInit.
34     *
35     * @param socket the Socket to which the host is connected
36     * @param queue the Queue to which we'll add data
37     * @throws IOException if something goes badly wrong
38     */
39 tdb 1.11 public TCPReaderInit(Socket socket, Queue queue) throws IOException {
40 tdb 1.21 // set the Thread name
41     setName("filter.TCPReaderInit");
42    
43 tdb 1.1 _socket = socket;
44 tdb 1.11 _queue = queue;
45 tdb 1.13 // setup the reader & writer
46 tdb 1.1 _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
47 tdb 1.15 _socketOut = new PrintWriter(_socket.getOutputStream(), true);
48 tdb 1.8 _logger.write(toString(), Logger.SYSINIT, "created");
49 tdb 1.1 }
50    
51     //---PUBLIC METHODS---
52 tdb 1.13
53     /**
54     * Main run method. Will communicate with the host, inform it
55     * if any updates to it's configuration are needed, and send
56     * a heartbeat packet into the system.
57     */
58 tdb 1.1 public void run() {
59     try {
60 tdb 1.27 // get an instance of the KeyManager
61     KeyManager keyman = KeyManager.getInstance();
62    
63     // get some information about the host
64     String hostname = _socket.getInetAddress().getHostName().toLowerCase();
65     String ipadd = _socket.getInetAddress().getHostAddress();
66 tdb 1.2
67 tdb 1.22 // try for HEARTBEAT
68 tdb 1.23 getInBound("HEARTBEAT");
69 tdb 1.22 _socketOut.println("OK");
70 tdb 1.3
71 tdb 1.27 // look for a command:
72     // CONFIG - to check config
73     // KEY - to get the key
74     // ENDHEARTBEAT - to finish
75     String cmd = getInBound();
76     while(!cmd.equals("ENDHEARTBEAT")) {
77     if(cmd.equals("CONFIG")) {
78     // respond to CONFIG
79     _socketOut.println("OK");
80    
81     // try for {filelist}
82     String filelist = getInBound();
83     _socketOut.println("OK");
84    
85     // try for {lastModified}
86     String lastModified = getInBound();
87     // check to see if a config update has happen
88     boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified));
89     if(newConfig) {
90     // new config !
91     _socketOut.println("ERROR");
92     }
93     else {
94     // nothing has changed
95     _socketOut.println("OK");
96     }
97     }
98     else if(cmd.equals("KEY")) {
99     // repsond to KEY
100     // generate a key
101     String key = keyman.genKey();
102     // add it to the key manager
103     keyman.addKey(hostname, key);
104     _socketOut.println(key);
105     }
106     else {
107     _socketOut.println("ERROR");
108     }
109     // get the next command
110     cmd = getInBound();
111 tdb 1.3 }
112    
113 tdb 1.27 // respond to ENDHEARTBEAT
114 tdb 1.22 _socketOut.println("OK");
115    
116     // work out some information for our heartbeat packet
117     String date = new Long(System.currentTimeMillis()/((long) 1000)).toString(); //seconds
118 tdb 1.6
119 ajm 1.16 // run the service checks for this host
120 ajm 1.17 _logger.write(toString(), Logger.DEBUG, "Running service checks");
121 ajm 1.16 String checks = PluginServiceCheckManager.getInstance().runServiceChecks(hostname);
122    
123     // build the heartbeat packet
124     String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\">" + checks + "</packet>";
125    
126     // get it to be sent on
127 tdb 1.11 _queue.add(xml);
128 tdb 1.1
129     } catch (Exception e) {
130 tdb 1.20 _logger.write(toString(), Logger.ERROR, "ERROR: " + e);
131 tdb 1.1 }
132    
133     // Disconnect streams & socket
134     try {
135     _socketIn.close();
136     _socketOut.close();
137     _socket.close();
138     } catch (IOException e) {
139 tdb 1.8 _logger.write(toString(), Logger.ERROR, "exception on socket close");
140 tdb 1.1 }
141 tdb 1.14 _logger.write(toString(), Logger.DEBUG, "finished");
142 tdb 1.1 }
143    
144     /**
145     * Overrides the {@link java.lang.Object#toString() Object.toString()}
146     * method to provide clean logging (every class should have this).
147     *
148 tdb 1.25 * This uses the uk.org.iscream.cms.server.util.NameFormat class
149 ajm 1.10 * to format the toString()
150     *
151 tdb 1.1 * @return the name of this class and its CVS revision
152     */
153     public String toString() {
154 ajm 1.10 return FormatName.getName(
155     _name,
156     getClass().getName(),
157     REVISION);
158 tdb 1.1 }
159    
160     //---PRIVATE METHODS---
161 tdb 1.22
162     private String getInBound(String expected) throws IOException {
163     // grab the input
164     String inBound = getInBound();
165     // check if it's what we're expecting
166     if(!inBound.equals(expected)) {
167 tdb 1.26 throw new IOException("protocol error from "+_socket.getInetAddress().getHostName()+" - expected:"+expected+" got:" + inBound);
168 tdb 1.22 }
169     // it should be ok then
170     return inBound;
171     }
172    
173     private String getInBound() throws IOException {
174     // grab the input
175     String inBound = _socketIn.readLine();
176     // check for null's, likely disconnection
177     if(inBound == null) {
178     throw new IOException("got null from host, maybe it died");
179     }
180     // it's a valid message it seems
181     return inBound;
182     }
183 tdb 1.27
184 tdb 1.1 //---ACCESSOR/MUTATOR METHODS---
185    
186     //---ATTRIBUTES---
187    
188 ajm 1.10 /**
189     * A reference to the configuration manager
190     */
191 tdb 1.8 ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
192 ajm 1.10
193     /**
194     * This is the friendly identifier of the
195     * component this class is running in.
196     * eg, a Filter may be called "filter1",
197     * If this class does not have an owning
198     * component, a name from the configuration
199     * can be placed here. This name could also
200     * be changed to null for utility classes.
201     */
202     private String _name = FilterMain.NAME;
203    
204     /**
205     * This holds a reference to the
206     * system logger that is being used.
207     */
208     private Logger _logger = ReferenceManager.getInstance().getLogger();
209    
210     /**
211     * The socket we are talking on
212     */
213 tdb 1.1 Socket _socket;
214 ajm 1.10
215     /**
216     * The input from the socket
217     */
218 tdb 1.1 BufferedReader _socketIn;
219 ajm 1.10
220     /**
221     * The output from the socket
222     */
223 tdb 1.1 PrintWriter _socketOut;
224 ajm 1.10
225     /**
226 tdb 1.11 * A reference to our Queue
227 ajm 1.10 */
228 tdb 1.11 Queue _queue;
229 tdb 1.27
230 tdb 1.1 //---STATIC ATTRIBUTES---
231    
232     }