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.1 by tdb, Mon Nov 27 00:43:26 2000 UTC vs.
Revision 1.22 by tdb, Tue Mar 13 16:25:57 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 + package uk.ac.ukc.iscream.filter;
3  
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.*;
12 < import org.omg.CosNaming.*;
13 > import uk.ac.ukc.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 <            String inBound = _socketIn.readLine();
63 <            if (!inBound.equals("STARTCONFIG")) {
62 >            //variables
63 >            String filelist = "";
64 >            String lastModified = "";
65 >            String inBound = "";
66 >            
67 >            // try for HEARTBEAT
68 >            getInBound("STARTCONFIG");
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");
52                _socketOut.flush();
53                throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound);
86              }
87 <
88 <            Configuration myConfig = _configManager.getConfiguration(_socket.getInetAddress().getHostName().toLowerCase());
57 <            if (myConfig == null) {
58 <                _socketOut.println("ERROR");
59 <                throw new IOException("error in host configuration");
60 <            } else {
87 >            else {
88 >                // nothing has changed
89                  _socketOut.println("OK");
62                _socketOut.flush();
63                
64                // get lastmodified
65                inBound = _socketIn.readLine();
66                if(!inBound.equals("LASTMODIFIED")) {
67                        // protocol error
68                    _socketOut.println("ERROR");
69                    _socketOut.flush();
70                    throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound);
71                }
72                else {
73                        // send info
74                        _socketOut.println(myConfig.getLastModified());
75                        _socketOut.flush();
76                }
77                
78                // get config fileList
79                inBound = _socketIn.readLine();
80                if(!inBound.equals("FILELIST")) {
81                        // protocol error
82                    _socketOut.println("ERROR");
83                    _socketOut.flush();
84                    throw new IOException("protocol error - expected:FILELIST got:" + inBound);
85                }
86                else {
87                        // send info
88                        _socketOut.println(myConfig.getFileList());
89                        _socketOut.flush();
90                }
91
92                // get properties
93                inBound = _socketIn.readLine();
94                while(!inBound.equals("ENDCONFIG")) {
95                          
96                    // get the property
97                    try {
98                        String returnedProperty = myConfig.getProperty(inBound);    
99                        
100                        _socketOut.println(returnedProperty);
101                        _socketOut.flush();
102    
103                    } catch (org.omg.CORBA.MARSHAL e) {
104                        _socketOut.println("ERROR");
105                        _socketOut.flush();
106                    }
107                    inBound = _socketIn.readLine();
108                }
109                _logRef.write(toString(), Logger.SYSMSG, "configured host");
110                _socketOut.println("OK");
111                _socketOut.flush();
112                
113                // get filter reference
114                inBound = _socketIn.readLine();
115                if(!inBound.equals("FILTER")) {
116                        // protocol error
117                    _socketOut.println("ERROR");
118                    _socketOut.flush();
119                    throw new IOException("protocol error - expected:FILTER got:" + inBound);
120                }
121                else {
122                        // send info
123                        String parentFilter =  myConfig.getProperty("Host.filter");
124                        _logRef.write(toString(), Logger.DEBUG, " looking for parent - " + parentFilter);
125                        org.omg.CORBA.Object objRef = _ncRef.resolve(_ncRef.to_name("iscream.Filter." + parentFilter));
126                    Filter filter = FilterHelper.narrow(objRef);
127                        _socketOut.println(filter.getHostName() + ";"
128                                         + filter.getUDPPort() + ";"
129                                         + filter.getTCPPort());
130                        _socketOut.flush();
131                }
132                
133                // confirm that all is ok
134                inBound = _socketIn.readLine();
135                if(!inBound.equals("END")) {
136                        // protocol error
137                    _socketOut.println("ERROR");
138                    _socketOut.flush();
139                    throw new IOException("protocol error - expected:END got:" + inBound);
140                }
141                else {
142                        // send ok
143                        _socketOut.println("OK");
144                        _socketOut.flush();
145                }
146
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          
153        _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.ac.ukc.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