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.27 by tdb, Thu Mar 21 17:44:51 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 26 | Line 25 | class TCPReaderInit extends Thread {
25       */
26      public final String REVISION = "$Revision$";
27      
28 +    /**
29 +     * The set of characters to be used for our key
30 +     */
31 +    private final char[] KEYSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
32 +    
33 +    /**
34 +     * The length of our key
35 +     */
36 +    private final int KEYLEN = 15;
37 +    
38   //---STATIC METHODS---
39  
40   //---CONSTRUCTORS---
41 <
41 >    
42 >    /**
43 >     * Construct a new TCPReaderInit.
44 >     *
45 >     * @param socket the Socket to which the host is connected
46 >     * @param queue the Queue to which we'll add data
47 >     * @throws IOException if something goes badly wrong
48 >     */
49      public TCPReaderInit(Socket socket, Queue queue) throws IOException {
50 +        // set the Thread name
51 +        setName("filter.TCPReaderInit");
52 +        
53          _socket = socket;
54          _queue = queue;
55 +        // setup the reader & writer
56          _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
57 <        _socketOut = new PrintWriter(_socket.getOutputStream());
57 >        _socketOut = new PrintWriter(_socket.getOutputStream(), true);
58          _logger.write(toString(), Logger.SYSINIT, "created");
59      }
60      
61   //---PUBLIC METHODS---
62 <
62 >    
63 >    /**
64 >     * Main run method. Will communicate with the host, inform it
65 >     * if any updates to it's configuration are needed, and send
66 >     * a heartbeat packet into the system.
67 >     */
68      public void run() {
69          try {
70 <            //variables
71 <            String filelist = "";
47 <            String lastModified = "";
48 <            String inBound = "";
70 >            // get an instance of the KeyManager
71 >            KeyManager keyman = KeyManager.getInstance();
72              
73 <            inBound = _socketIn.readLine();
74 <            if(!inBound.equals("HEARTBEAT")) {
75 <                _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 <            }
73 >            // get some information about the host
74 >            String hostname = _socket.getInetAddress().getHostName().toLowerCase();
75 >            String ipadd = _socket.getInetAddress().getHostAddress();
76              
77 <            inBound = _socketIn.readLine();
78 <            if(!inBound.equals("CONFIG")) {
79 <                _socketOut.println("ERROR");
80 <                _socketOut.flush();
81 <                throw new IOException("protocol error - expecting:CONFIG got:" + inBound);
82 <            } else {
83 <                _socketOut.println("OK");
84 <                _socketOut.flush();
77 >            // try for HEARTBEAT
78 >            getInBound("HEARTBEAT");
79 >            _socketOut.println("OK");
80 >            
81 >            // look for a command:
82 >            // CONFIG - to check config
83 >            // KEY - to get the key
84 >            // ENDHEARTBEAT - to finish            
85 >            String cmd = getInBound();
86 >            while(!cmd.equals("ENDHEARTBEAT")) {
87 >                if(cmd.equals("CONFIG")) {
88 >                    // respond to CONFIG
89 >                    _socketOut.println("OK");
90 >                        
91 >                    // try for {filelist}
92 >                    String filelist = getInBound();
93 >                    _socketOut.println("OK");
94 >                    
95 >                    // try for {lastModified}
96 >                    String lastModified = getInBound();
97 >                    // check to see if a config update has happen
98 >                    boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified));
99 >                    if(newConfig) {
100 >                        // new config !
101 >                        _socketOut.println("ERROR");
102 >                    }
103 >                    else {
104 >                        // nothing has changed
105 >                        _socketOut.println("OK");
106 >                    }
107 >                }
108 >                else if(cmd.equals("KEY")) {
109 >                    // repsond to KEY
110 >                    // generate a key
111 >                    String key = keyman.genKey();
112 >                    // add it to the key manager
113 >                    keyman.addKey(hostname, key);
114 >                    _socketOut.println(key);
115 >                }
116 >                else {
117 >                    _socketOut.println("ERROR");
118 >                }
119 >                // get the next command
120 >                cmd = getInBound();
121              }
122              
123 <            inBound = _socketIn.readLine();
71 <            filelist = inBound;
123 >            // respond to ENDHEARTBEAT
124              _socketOut.println("OK");
73            _socketOut.flush();
125              
126 <            inBound = _socketIn.readLine();
127 <            lastModified = inBound;
126 >            // work out some information for our heartbeat packet
127 >            String date = new Long(System.currentTimeMillis()/((long) 1000)).toString(); //seconds
128              
129 <            boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified));
129 >            // run the service checks for this host
130 >            _logger.write(toString(), Logger.DEBUG, "Running service checks");
131 >            String checks = PluginServiceCheckManager.getInstance().runServiceChecks(hostname);
132              
133 <            if(newConfig) {
134 <                _socketOut.println("ERROR");
82 <            }
83 <            else {
84 <                _socketOut.println("OK");
85 <            }
86 <            _socketOut.flush();
133 >            // build the heartbeat packet
134 >            String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\">" + checks + "</packet>";
135              
136 <            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 <            
136 >            // get it to be sent on
137              _queue.add(xml);
138              
139          } catch (Exception e) {
140 <            _logger.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage());
140 >            _logger.write(toString(), Logger.ERROR, "ERROR: " + e);
141          }
142          
109        _socketOut.flush();
143          // Disconnect streams & socket
144          try {
145              _socketIn.close();
# Line 115 | Line 148 | class TCPReaderInit extends Thread {
148          } catch (IOException e) {
149              _logger.write(toString(), Logger.ERROR, "exception on socket close");
150          }
151 <        _logger.write(toString(), Logger.SYSMSG, "finished");
151 >        _logger.write(toString(), Logger.DEBUG, "finished");
152      }
153      
154      /**
155       * Overrides the {@link java.lang.Object#toString() Object.toString()}
156       * method to provide clean logging (every class should have this).
157       *
158 <     * This uses the uk.ac.ukc.iscream.util.NameFormat class
158 >     * This uses the uk.org.iscream.cms.server.util.NameFormat class
159       * to format the toString()
160       *
161       * @return the name of this class and its CVS revision
# Line 136 | Line 169 | class TCPReaderInit extends Thread {
169  
170   //---PRIVATE METHODS---
171  
172 +    private String getInBound(String expected) throws IOException {
173 +        // grab the input
174 +        String inBound = getInBound();
175 +        // check if it's what we're expecting
176 +        if(!inBound.equals(expected)) {
177 +            throw new IOException("protocol error from "+_socket.getInetAddress().getHostName()+" - expected:"+expected+" got:" + inBound);
178 +        }
179 +        // it should be ok then
180 +        return inBound;
181 +    }
182 +    
183 +    private String getInBound() throws IOException {
184 +        // grab the input
185 +        String inBound = _socketIn.readLine();
186 +        // check for null's, likely disconnection
187 +        if(inBound == null) {
188 +            throw new IOException("got null from host, maybe it died");
189 +        }
190 +        // it's a valid message it seems
191 +        return inBound;
192 +    }
193 +    
194   //---ACCESSOR/MUTATOR METHODS---
195  
196   //---ATTRIBUTES---
# Line 181 | Line 236 | class TCPReaderInit extends Thread {
236       * A reference to our Queue
237       */
238      Queue _queue;
239 +    
240   //---STATIC ATTRIBUTES---
241  
242   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines