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.11 by tdb, Fri Jan 12 00:45:25 2001 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 uk.ac.ukc.iscream.util.*;
13 > import uk.org.iscream.cms.server.util.*;
14  
15   /**
16   * This provides Host heartbeat functionality
# Line 29 | Line 30 | class TCPReaderInit extends Thread {
30   //---STATIC METHODS---
31  
32   //---CONSTRUCTORS---
33 <
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          _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 47 | 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");
53 <                _socketOut.flush();
54 <                throw new IOException("protocol error - expecting:HEARTBEAT got:" + inBound);
55 <            } else {
56 <                _socketOut.println("OK");
57 <                _socketOut.flush();
58 <            }
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");
63 <                _socketOut.flush();
64 <                throw new IOException("protocol error - expecting:CONFIG got:" + inBound);
65 <            } else {
66 <                _socketOut.println("OK");
67 <                _socketOut.flush();
68 <            }
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");
73            _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));
79            
83              if(newConfig) {
84 +                // new config !
85                  _socketOut.println("ERROR");
86              }
87              else {
88 +                // nothing has changed
89                  _socketOut.println("OK");
90              }
86            _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");
95 <                _socketOut.flush();
96 <            }
97 <
98 <            String date = new Long(System.currentTimeMillis()).toString();
99 <            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();
101            String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\"></packet>";
100              
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          
109        _socketOut.flush();
115          // Disconnect streams & socket
116          try {
117              _socketIn.close();
# Line 115 | 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.ac.ukc.iscream.util.NameFormat class
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
# Line 135 | Line 140 | class TCPReaderInit extends Thread {
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  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines