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.15 by tdb, Wed Feb 7 13:47:11 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          _socket = socket;
43 <        _parent = parent;
43 >        _queue = queue;
44 >        // setup the reader & writer
45          _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
46 <        _socketOut = new PrintWriter(_socket.getOutputStream());
47 <        _logRef.write(toString(), Logger.SYSINIT, "created");
46 >        _socketOut = new PrintWriter(_socket.getOutputStream(), true);
47 >        _logger.write(toString(), Logger.SYSINIT, "created");
48      }
49      
50   //---PUBLIC METHODS---
51 <
52 <    //NEED TO MAJORILY CHANGE THIS BIT !
51 >    
52 >    /**
53 >     * Main run method. Will communicate with the host, inform it
54 >     * if any updates to it's configuration are needed, and send
55 >     * a heartbeat packet into the system.
56 >     */
57      public void run() {
58          try {
59 +            //variables
60 +            String filelist = "";
61 +            String lastModified = "";
62 +            String inBound = "";
63              
64 +            inBound = _socketIn.readLine();
65 +            if(!inBound.equals("HEARTBEAT")) {
66 +                _socketOut.println("ERROR");
67 +                throw new IOException("protocol error - expecting:HEARTBEAT got:" + inBound);
68 +            } else {
69 +                _socketOut.println("OK");
70 +            }
71              
72 +            inBound = _socketIn.readLine();
73 +            if(!inBound.equals("CONFIG")) {
74 +                _socketOut.println("ERROR");
75 +                throw new IOException("protocol error - expecting:CONFIG got:" + inBound);
76 +            } else {
77 +                _socketOut.println("OK");
78 +            }
79 +            
80 +            inBound = _socketIn.readLine();
81 +            filelist = inBound;
82 +            _socketOut.println("OK");
83 +            
84 +            inBound = _socketIn.readLine();
85 +            lastModified = inBound;
86 +            
87 +            boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified));
88 +            
89 +            if(newConfig) {
90 +                _socketOut.println("ERROR");
91 +            }
92 +            else {
93 +                _socketOut.println("OK");
94 +            }
95 +            
96 +            inBound = _socketIn.readLine();
97 +            if(!inBound.equals("ENDHEARTBEAT")) {
98 +                _socketOut.println("ERROR");
99 +                throw new IOException("protocol error - expecting:ENDHEARTBEAT got:" + inBound);
100 +            } else {
101 +                _socketOut.println("OK");
102 +            }
103 +
104 +            String date = new Long(System.currentTimeMillis()).toString();
105 +            String hostname = _socket.getInetAddress().getHostName();
106 +            String ipadd = _socket.getInetAddress().getHostAddress();
107 +            String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\"></packet>";
108 +            
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.getMessage());
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.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---
# Line 82 | Line 145 | class TCPReaderInit extends Thread {
145  
146   //---ATTRIBUTES---
147  
148 <    ConfigurationManager _configManager;
149 <    Logger _logRef;
148 >    /**
149 >     * A reference to the configuration manager
150 >     */
151 >    ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
152 >    
153 >    /**
154 >     * This is the friendly identifier of the
155 >     * component this class is running in.
156 >     * eg, a Filter may be called "filter1",
157 >     * If this class does not have an owning
158 >     * component,  a name from the configuration
159 >     * can be placed here.  This name could also
160 >     * be changed to null for utility classes.
161 >     */
162 >    private String _name = FilterMain.NAME;
163 >
164 >    /**
165 >     * This holds a reference to the
166 >     * system logger that is being used.
167 >     */
168 >    private Logger _logger = ReferenceManager.getInstance().getLogger();
169 >    
170 >    /**
171 >     * The socket we are talking on
172 >     */
173      Socket _socket;
174 +    
175 +    /**
176 +     * The input from the socket
177 +     */
178      BufferedReader _socketIn;
179 +    
180 +    /**
181 +     * The output from the socket
182 +     */
183      PrintWriter _socketOut;
184 <    Filter _parent;
184 >    
185 >    /**
186 >     * A reference to our Queue
187 >     */
188 >    Queue _queue;
189   //---STATIC ATTRIBUTES---
190  
191   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines