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.24
Committed: Wed Mar 14 23:25:29 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
CVS Tags: PROJECT_COMPLETION
Changes since 1.23: +8 -8 lines
Log Message:
The whole server package structure has been changed.
Old Package: uk.ac.ukc.iscream.*
New Package: uk.org.iscream.*

File Contents

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