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.28 by tdb, Thu Mar 21 22:09:13 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;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.io.IOException;
9   import java.io.*;
10 < import uk.ac.ukc.iscream.util.*;
10 > import java.util.Random;
11 > import uk.org.iscream.cms.server.util.*;
12  
13   /**
14   * This provides Host heartbeat functionality
# Line 29 | Line 28 | class TCPReaderInit extends Thread {
28   //---STATIC METHODS---
29  
30   //---CONSTRUCTORS---
31 <
31 >    
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      public TCPReaderInit(Socket socket, Queue queue) throws IOException {
40 +        // set the Thread name
41 +        setName("filter.TCPReaderInit");
42 +        
43          _socket = socket;
44          _queue = queue;
45 +        // setup the reader & writer
46          _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
47 <        _socketOut = new PrintWriter(_socket.getOutputStream());
47 >        _socketOut = new PrintWriter(_socket.getOutputStream(), true);
48          _logger.write(toString(), Logger.SYSINIT, "created");
49      }
50      
51   //---PUBLIC METHODS---
52 <
52 >    
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      public void run() {
59          try {
60 <            //variables
61 <            String filelist = "";
47 <            String lastModified = "";
48 <            String inBound = "";
60 >            // get an instance of the KeyManager
61 >            KeyManager keyman = KeyManager.getInstance();
62              
63 <            inBound = _socketIn.readLine();
64 <            if(!inBound.equals("HEARTBEAT")) {
65 <                _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 <            }
63 >            // get some information about the host
64 >            String hostname = _socket.getInetAddress().getHostName().toLowerCase();
65 >            String ipadd = _socket.getInetAddress().getHostAddress();
66              
67 <            inBound = _socketIn.readLine();
68 <            if(!inBound.equals("CONFIG")) {
69 <                _socketOut.println("ERROR");
70 <                _socketOut.flush();
71 <                throw new IOException("protocol error - expecting:CONFIG got:" + inBound);
72 <            } else {
73 <                _socketOut.println("OK");
74 <                _socketOut.flush();
67 >            // try for HEARTBEAT
68 >            getInBound("HEARTBEAT");
69 >            _socketOut.println("OK");
70 >            
71 >            // 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              }
112              
113 <            inBound = _socketIn.readLine();
71 <            filelist = inBound;
113 >            // respond to ENDHEARTBEAT
114              _socketOut.println("OK");
73            _socketOut.flush();
115              
116 <            inBound = _socketIn.readLine();
117 <            lastModified = inBound;
116 >            // work out some information for our heartbeat packet
117 >            String date = new Long(System.currentTimeMillis()/((long) 1000)).toString(); //seconds
118              
119 <            boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified));
119 >            // run the service checks for this host
120 >            _logger.write(toString(), Logger.DEBUG, "Running service checks");
121 >            String checks = PluginServiceCheckManager.getInstance().runServiceChecks(hostname);
122              
123 <            if(newConfig) {
124 <                _socketOut.println("ERROR");
82 <            }
83 <            else {
84 <                _socketOut.println("OK");
85 <            }
86 <            _socketOut.flush();
123 >            // build the heartbeat packet
124 >            String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\">" + checks + "</packet>";
125              
126 <            inBound = _socketIn.readLine();
89 <            if(!inBound.equals("ENDHEARTBEAT")) {
90 <                _socketOut.println("ERROR");
91 <                _socketOut.flush();
92 <                throw new IOException("protocol error - expecting:ENDHEARTBEAT got:" + inBound);
93 <            } else {
94 <                _socketOut.println("OK");
95 <                _socketOut.flush();
96 <            }
97 <
98 <            String date = new Long(System.currentTimeMillis()).toString();
99 <            String hostname = _socket.getInetAddress().getHostName();
100 <            String ipadd = _socket.getInetAddress().getHostAddress();
101 <            String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\"></packet>";
102 <            
126 >            // get it to be sent on
127              _queue.add(xml);
128              
129          } catch (Exception e) {
130 <            _logger.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage());
130 >            _logger.write(toString(), Logger.ERROR, "ERROR: " + e);
131          }
132          
109        _socketOut.flush();
133          // Disconnect streams & socket
134          try {
135              _socketIn.close();
# Line 115 | Line 138 | class TCPReaderInit extends Thread {
138          } catch (IOException e) {
139              _logger.write(toString(), Logger.ERROR, "exception on socket close");
140          }
141 <        _logger.write(toString(), Logger.SYSMSG, "finished");
141 >        _logger.write(toString(), Logger.DEBUG, "finished");
142      }
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 <     * This uses the uk.ac.ukc.iscream.util.NameFormat class
148 >     * This uses the uk.org.iscream.cms.server.util.NameFormat class
149       * to format the toString()
150       *
151       * @return the name of this class and its CVS revision
# Line 136 | Line 159 | class TCPReaderInit extends Thread {
159  
160   //---PRIVATE METHODS---
161  
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 +            throw new IOException("protocol error from "+_socket.getInetAddress().getHostName()+" - expected:"+expected+" got:" + inBound);
168 +        }
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 +    
184   //---ACCESSOR/MUTATOR METHODS---
185  
186   //---ATTRIBUTES---
# Line 181 | Line 226 | class TCPReaderInit extends Thread {
226       * A reference to our Queue
227       */
228      Queue _queue;
229 +    
230   //---STATIC ATTRIBUTES---
231  
232   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines