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.2 by tdb, Mon Nov 27 10:23:32 2000 UTC vs.
Revision 1.24 by tdb, Wed Mar 14 23:25:29 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 + package uk.org.iscream.filter;
3  
4   //---IMPORTS---
5 < import uk.ac.ukc.iscream.core.*;
6 < import uk.ac.ukc.iscream.filter.*;
5 > import uk.org.iscream.core.*;
6 > import uk.org.iscream.filter.*;
7 > import uk.org.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.*;
12 < import org.omg.CosNaming.*;
13 > import uk.org.iscream.util.*;
14  
15   /**
16 < * <ONE LINE DESCRIPTION>
16 < * <DETAILED DESCRIPTION>
16 > * This provides Host heartbeat functionality
17   *
18   * @author  $Author$
19   * @version $Id$
# Line 30 | 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 >        // 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());
50 <        _logRef.write(toString(), Logger.SYSINIT, "created");
49 >        _socketOut = new PrintWriter(_socket.getOutputStream(), true);
50 >        _logger.write(toString(), Logger.SYSINIT, "created");
51      }
52      
53   //---PUBLIC METHODS---
54 <
55 <    //NEED TO MAJORILY CHANGE THIS BIT !
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
63 +            String filelist = "";
64 +            String lastModified = "";
65 +            String inBound = "";
66              
67 +            // try for HEARTBEAT
68 +            getInBound("HEARTBEAT");
69 +            _socketOut.println("OK");
70              
71 +            // try for CONFIG
72 +            getInBound("CONFIG");
73 +            _socketOut.println("OK");
74 +            
75 +            // try for {filelist}
76 +            filelist = getInBound();
77 +            _socketOut.println("OK");
78 +            
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));
83 +            if(newConfig) {
84 +                // new config !
85 +                _socketOut.println("ERROR");
86 +            }
87 +            else {
88 +                // nothing has changed
89 +                _socketOut.println("OK");
90 +            }
91 +            
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();
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 <            _logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage());
112 >            _logger.write(toString(), Logger.ERROR, "ERROR: " + e);
113          }
114          
55        _socketOut.flush();
115          // Disconnect streams & socket
116          try {
117              _socketIn.close();
118              _socketOut.close();
119              _socket.close();
120          } catch (IOException e) {
121 <            _logRef.write(toString(), Logger.ERROR, "exception on socket close");
121 >            _logger.write(toString(), Logger.ERROR, "exception on socket close");
122          }
123 <        _logRef.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.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 - 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 <    ConfigurationManager _configManager;
171 <    Logger _logRef;
170 >    /**
171 >     * A reference to the configuration manager
172 >     */
173 >    ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
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