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.5 by tdb, Wed Nov 29 19:26:00 2000 UTC vs.
Revision 1.16 by ajm, Sun Feb 11 18:01:43 2001 UTC

# Line 4 | Line 4 | package uk.ac.ukc.iscream.filter;
4   //---IMPORTS---
5   import uk.ac.ukc.iscream.core.*;
6   import uk.ac.ukc.iscream.filter.*;
7 + import uk.ac.ukc.iscream.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.*;
13 > import uk.ac.ukc.iscream.util.*;
14  
15   /**
16 < * <ONE LINE DESCRIPTION>
17 < * <DETAILED DESCRIPTION>
16 > * This provides Host heartbeat functionality
17   *
18   * @author  $Author$
19   * @version $Id$
# Line 31 | Line 30 | class TCPReaderInit extends Thread {
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;
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          _socket = socket;
43 <        _parent = parent;
43 >        _queue = queue;
44 >        // setup the reader & writer
45          _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
46 <        _socketOut = new PrintWriter(_socket.getOutputStream());
47 <        _logRef.write(toString(), Logger.SYSINIT, "created");
46 >        _socketOut = new PrintWriter(_socket.getOutputStream(), true);
47 >        _logger.write(toString(), Logger.SYSINIT, "created");
48      }
49      
50   //---PUBLIC METHODS---
51 <
52 <    //NEED TO MAJORILY CHANGE THIS BIT !
51 >    
52 >    /**
53 >     * Main run method. Will communicate with the host, inform it
54 >     * if any updates to it's configuration are needed, and send
55 >     * a heartbeat packet into the system.
56 >     */
57      public void run() {
58          try {
59              //variables
# Line 55 | Line 64 | class TCPReaderInit extends Thread {
64              inBound = _socketIn.readLine();
65              if(!inBound.equals("HEARTBEAT")) {
66                  _socketOut.println("ERROR");
58                _socketOut.flush();
67                  throw new IOException("protocol error - expecting:HEARTBEAT got:" + inBound);
68              } else {
69                  _socketOut.println("OK");
62                _socketOut.flush();
70              }
71              
72              inBound = _socketIn.readLine();
73              if(!inBound.equals("CONFIG")) {
74                  _socketOut.println("ERROR");
68                _socketOut.flush();
75                  throw new IOException("protocol error - expecting:CONFIG got:" + inBound);
76              } else {
77                  _socketOut.println("OK");
72                _socketOut.flush();
78              }
79              
80              inBound = _socketIn.readLine();
81              filelist = inBound;
82              _socketOut.println("OK");
78            _socketOut.flush();
83              
84              inBound = _socketIn.readLine();
85              lastModified = inBound;
# Line 88 | Line 92 | class TCPReaderInit extends Thread {
92              else {
93                  _socketOut.println("OK");
94              }
91            _socketOut.flush();
95              
96              inBound = _socketIn.readLine();
97              if(!inBound.equals("ENDHEARTBEAT")) {
98                  _socketOut.println("ERROR");
96                _socketOut.flush();
99                  throw new IOException("protocol error - expecting:ENDHEARTBEAT got:" + inBound);
100              } else {
101                  _socketOut.println("OK");
100                _socketOut.flush();
102              }
103  
104 <            // NEED TO LOG THE HEARTBEAT IN XML HERE.
104 >            String date = new Long(System.currentTimeMillis()).toString();
105 >            String hostname = _socket.getInetAddress().getHostName();
106 >            String ipadd = _socket.getInetAddress().getHostAddress();
107              
108 +            // run the service checks for this host
109 +            String checks = PluginServiceCheckManager.getInstance().runServiceChecks(hostname);
110 +            
111 +            // build the heartbeat packet
112 +            String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\">" + checks + "</packet>";
113 +            
114 +            // get it to be sent on
115 +            _queue.add(xml);
116 +            
117          } catch (Exception e) {
118 <            _logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage());
118 >            _logger.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage());
119          }
120          
109        _socketOut.flush();
121          // Disconnect streams & socket
122          try {
123              _socketIn.close();
124              _socketOut.close();
125              _socket.close();
126          } catch (IOException e) {
127 <            _logRef.write(toString(), Logger.ERROR, "exception on socket close");
127 >            _logger.write(toString(), Logger.ERROR, "exception on socket close");
128          }
129 <        _logRef.write(toString(), Logger.SYSMSG, "finished");
129 >        _logger.write(toString(), Logger.DEBUG, "finished");
130      }
131      
132      /**
133       * Overrides the {@link java.lang.Object#toString() Object.toString()}
134       * method to provide clean logging (every class should have this).
135       *
136 +     * This uses the uk.ac.ukc.iscream.util.NameFormat class
137 +     * to format the toString()
138 +     *
139       * @return the name of this class and its CVS revision
140       */
141      public String toString() {
142 <        return this.getClass().getName() + "{" + _socket.getInetAddress().getHostName()
143 <         + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")";
144 <        
142 >        return FormatName.getName(
143 >            _name,
144 >            getClass().getName(),
145 >            REVISION);
146      }
147  
148   //---PRIVATE METHODS---
# Line 136 | Line 151 | class TCPReaderInit extends Thread {
151  
152   //---ATTRIBUTES---
153  
154 <    ConfigurationManager _configManager;
155 <    Logger _logRef;
154 >    /**
155 >     * A reference to the configuration manager
156 >     */
157 >    ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
158 >    
159 >    /**
160 >     * This is the friendly identifier of the
161 >     * component this class is running in.
162 >     * eg, a Filter may be called "filter1",
163 >     * If this class does not have an owning
164 >     * component,  a name from the configuration
165 >     * can be placed here.  This name could also
166 >     * be changed to null for utility classes.
167 >     */
168 >    private String _name = FilterMain.NAME;
169 >
170 >    /**
171 >     * This holds a reference to the
172 >     * system logger that is being used.
173 >     */
174 >    private Logger _logger = ReferenceManager.getInstance().getLogger();
175 >    
176 >    /**
177 >     * The socket we are talking on
178 >     */
179      Socket _socket;
180 +    
181 +    /**
182 +     * The input from the socket
183 +     */
184      BufferedReader _socketIn;
185 +    
186 +    /**
187 +     * The output from the socket
188 +     */
189      PrintWriter _socketOut;
190 <    Filter _parent;
190 >    
191 >    /**
192 >     * A reference to our Queue
193 >     */
194 >    Queue _queue;
195   //---STATIC ATTRIBUTES---
196  
197   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines