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
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/filter/TCPReaderInit.java (file contents):
Revision 1.9 by ajm, Thu Nov 30 02:38:09 2000 UTC vs.
Revision 1.26 by tdb, Thu Mar 21 13:01:21 2002 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.filter;
2 > package uk.org.iscream.cms.server.filter;
3  
4   //---IMPORTS---
5 < import uk.ac.ukc.iscream.core.*;
6 < import uk.ac.ukc.iscream.filter.*;
5 > import uk.org.iscream.cms.server.core.*;
6 > import uk.org.iscream.cms.server.filter.*;
7 > import uk.org.iscream.cms.server.componentmanager.*;
8   import java.net.Socket;
9   import java.io.InputStream;
10   import java.io.OutputStream;
11   import java.io.IOException;
12   import java.io.*;
13 < import org.omg.CORBA.*;
13 < import org.omg.CosNaming.*;
14 < import uk.ac.ukc.iscream.util.*;
13 > import uk.org.iscream.cms.server.util.*;
14  
15   /**
16 < * <ONE LINE DESCRIPTION>
18 < * <DETAILED DESCRIPTION>
16 > * This provides Host heartbeat functionality
17   *
18   * @author  $Author$
19   * @version $Id$
# Line 32 | Line 30 | class TCPReaderInit extends Thread {
30   //---STATIC METHODS---
31  
32   //---CONSTRUCTORS---
33 <
34 <    public TCPReaderInit(Socket socket, Filter parent) throws IOException {
33 >    
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 >    public TCPReaderInit(Socket socket, Queue queue) throws IOException {
42 >        // set the Thread name
43 >        setName("filter.TCPReaderInit");
44 >        
45          _socket = socket;
46 <        _parent = parent;
46 >        _queue = queue;
47 >        // setup the reader & writer
48          _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
49 <        _socketOut = new PrintWriter(_socket.getOutputStream());
49 >        _socketOut = new PrintWriter(_socket.getOutputStream(), true);
50          _logger.write(toString(), Logger.SYSINIT, "created");
51      }
52      
53   //---PUBLIC METHODS---
54 <
54 >    
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      public void run() {
61          try {
62              //variables
# Line 50 | Line 64 | class TCPReaderInit extends Thread {
64              String lastModified = "";
65              String inBound = "";
66              
67 <            inBound = _socketIn.readLine();
68 <            if(!inBound.equals("HEARTBEAT")) {
69 <                _socketOut.println("ERROR");
56 <                _socketOut.flush();
57 <                throw new IOException("protocol error - expecting:HEARTBEAT got:" + inBound);
58 <            } else {
59 <                _socketOut.println("OK");
60 <                _socketOut.flush();
61 <            }
67 >            // try for HEARTBEAT
68 >            getInBound("HEARTBEAT");
69 >            _socketOut.println("OK");
70              
71 <            inBound = _socketIn.readLine();
72 <            if(!inBound.equals("CONFIG")) {
73 <                _socketOut.println("ERROR");
66 <                _socketOut.flush();
67 <                throw new IOException("protocol error - expecting:CONFIG got:" + inBound);
68 <            } else {
69 <                _socketOut.println("OK");
70 <                _socketOut.flush();
71 <            }
71 >            // try for CONFIG
72 >            getInBound("CONFIG");
73 >            _socketOut.println("OK");
74              
75 <            inBound = _socketIn.readLine();
76 <            filelist = inBound;
75 >            // try for {filelist}
76 >            filelist = getInBound();
77              _socketOut.println("OK");
76            _socketOut.flush();
78              
79 <            inBound = _socketIn.readLine();
80 <            lastModified = inBound;
81 <            
79 >            // try for {lastModified}
80 >            lastModified = getInBound();
81 >            // check to see if a config update has happen
82              boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified));
82            
83              if(newConfig) {
84 +                // new config !
85                  _socketOut.println("ERROR");
86              }
87              else {
88 +                // nothing has changed
89                  _socketOut.println("OK");
90              }
89            _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");
98 <                _socketOut.flush();
99 <            }
100 <
101 <            String date = new Long(System.currentTimeMillis()).toString();
102 <            String hostname = _socket.getInetAddress().getHostName();
92 >            // 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 >            String hostname = _socket.getInetAddress().getHostName().toLowerCase();
99              String ipadd = _socket.getInetAddress().getHostAddress();
104            String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\"></packet>";
100              
101 <            FilterThread t = new FilterThread(xml, _parent);
102 <            t.start();
101 >            // run the service checks for this host
102 >            _logger.write(toString(), Logger.DEBUG, "Running service checks");
103 >            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 +            _queue.add(xml);
110 +            
111          } catch (Exception e) {
112 <            _logger.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage());
112 >            _logger.write(toString(), Logger.ERROR, "ERROR: " + e);
113          }
114          
113        _socketOut.flush();
115          // Disconnect streams & socket
116          try {
117              _socketIn.close();
# Line 119 | Line 120 | class TCPReaderInit extends Thread {
120          } catch (IOException e) {
121              _logger.write(toString(), Logger.ERROR, "exception on socket close");
122          }
123 <        _logger.write(toString(), Logger.SYSMSG, "finished");
123 >        _logger.write(toString(), Logger.DEBUG, "finished");
124      }
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 +     * This uses the uk.org.iscream.cms.server.util.NameFormat class
131 +     * to format the toString()
132 +     *
133       * @return the name of this class and its CVS revision
134       */
135      public String toString() {
136 <        return this.getClass().getName() + "{" + _socket.getInetAddress().getHostName()
137 <         + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")";
138 <        
136 >        return FormatName.getName(
137 >            _name,
138 >            getClass().getName(),
139 >            REVISION);
140      }
141  
142   //---PRIVATE METHODS---
143  
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 from "+_socket.getInetAddress().getHostName()+" - 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 +
166   //---ACCESSOR/MUTATOR METHODS---
167  
168   //---ATTRIBUTES---
169  
170 +    /**
171 +     * A reference to the configuration manager
172 +     */
173      ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
174 <    Logger _logger = ReferenceManager.getInstance().getLogger();
174 >    
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      Socket _socket;
196 +    
197 +    /**
198 +     * The input from the socket
199 +     */
200      BufferedReader _socketIn;
201 +    
202 +    /**
203 +     * The output from the socket
204 +     */
205      PrintWriter _socketOut;
206 <    Filter _parent;
206 >    
207 >    /**
208 >     * A reference to our Queue
209 >     */
210 >    Queue _queue;
211   //---STATIC ATTRIBUTES---
212  
213   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines