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/TCPReader.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/filter/TCPReader.java (file contents):
Revision 1.1 by tdb, Mon Nov 27 00:43:17 2000 UTC vs.
Revision 1.19 by tdb, Wed Feb 5 16:43:47 2003 UTC

# Line 1 | Line 1
1 + /*
2 + * i-scream central monitoring system
3 + * http://www.i-scream.org.uk
4 + * Copyright (C) 2000-2002 i-scream
5 + *
6 + * This program is free software; you can redistribute it and/or
7 + * modify it under the terms of the GNU General Public License
8 + * as published by the Free Software Foundation; either version 2
9 + * of the License, or (at your option) any later version.
10 + *
11 + * This program is distributed in the hope that it will be useful,
12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 + * GNU General Public License for more details.
15 + *
16 + * You should have received a copy of the GNU General Public License
17 + * along with this program; if not, write to the Free Software
18 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 + */
20 +
21   //---PACKAGE DECLARATION---
22 + package uk.org.iscream.cms.server.filter;
23  
24   //---IMPORTS---
25 < import uk.ac.ukc.iscream.core.*;
26 < import uk.ac.ukc.iscream.filter.*;
25 > import uk.org.iscream.cms.server.core.*;
26 > import uk.org.iscream.cms.server.componentmanager.*;
27 > import uk.org.iscream.cms.server.filter.*;
28   import java.net.Socket;
29   import java.net.ServerSocket;
30   import java.io.OutputStream;
31   import java.io.IOException;
32   import java.net.InetAddress;
33   import java.net.UnknownHostException;
34 < import org.omg.CORBA.*;
13 < import org.omg.CosNaming.*;
34 > import uk.org.iscream.cms.util.*;
35  
36   /**
37 < * A socket listener to listen for new hosts registering with the system.
17 < * When a host makes a connection, the connecton is past to an instance
18 < * of the HostInit class, which handles further communication.
37 > * Reads TCP Heartbeats from the host applications.
38   *
39   * @author  $Author$
40   * @version $Id$
# Line 33 | Line 52 | class TCPReader extends Thread {
52  
53   //---CONSTRUCTORS---
54  
55 <        /**
56 <     * Constructs a new listener
55 >    /**
56 >     * Constructs a new TCPReader
57       *
58 <     * @param logger a reference to the logger we are using
59 <     * @param configManager a reference to the ConfigurationManager we are using
60 <         * @param port The port that the server will listen on.
61 <         */
62 <    public TCPReader(Logger logger, ConfigurationManager configManager, int port, Filter parent) {
63 <        _logger = logger;
64 <        _configManager = configManager;
58 >     * @param queue A reference to our Queue
59 >     * @param port The port that the TCPReader will listen on
60 >     */
61 >    public TCPReader(int port, Queue queue) {
62 >        // set the Thread name
63 >        setName("filter.TCPReader");
64 >        
65          _port = port;
66 <        _parent = parent;
66 >        _queue = queue;
67          _logger.write(toString(), Logger.SYSINIT, "started");
68      }
69  
# Line 52 | Line 71 | class TCPReader extends Thread {
71  
72   //---PUBLIC METHODS---
73  
74 <        /**
75 <         * The run() method is the main loop for this thread, and we
76 <         * will remain in here until such a point as something goes
77 <         * wrong with the listening. After initially setting up the
78 <         * ServerSocket we go round a while loop receiving connections
79 <         * and then passing them off to other processes to deal with.
80 <         */
74 >    /**
75 >     * The run() method is the main loop for this thread, and we
76 >     * will remain in here until such a point as something goes
77 >     * wrong with the listening. After initially setting up the
78 >     * ServerSocket we go round a while loop receiving connections
79 >     * and then passing them off to other processes to deal with.
80 >     */
81      public void run(){
82 +        // setup an empty ACL defaulting to ALLOW
83 +        ACL acl = new ACL(ACL.ALLOW);
84 +        
85 +        // Setup the ServerSocket so that clients can connect
86          ServerSocket listenPort=null;
64                // We use this boolean so we can break out of the while loop if we want
65        boolean run = true;
87          try{
88 <                        // Setup the ServerSocket so that clients can connect
68 <            listenPort = new ServerSocket(_port);
88 >            listenPort = new ACLServerSocket(acl, _port);
89          }
90          catch(IOException e){
91 +            _logger.write(toString(), Logger.FATAL, "Failed to setup ServerSocket: " + e);
92 +            return;
93          }
94 <                // Log what machine/port we're listening on
95 <                try{
96 <                        _logger.write(toString(), Logger.SYSMSG, "TCPReader listening on "
97 <                                                                                +InetAddress.getLocalHost().getHostName()
98 <                                                                                +"/"+InetAddress.getLocalHost().getHostAddress()
99 <                                                                                +" port "+listenPort.getLocalPort());
100 <                }
101 <                catch(UnknownHostException e){
102 <                        _logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost "
103 <                                                                                +"port "+listenPort.getLocalPort());
104 <                }
105 <                // Loop round constantly until we decide to stop
94 >        
95 >        // Log what machine/port we're listening on
96 >        try{
97 >            _logger.write(toString(), Logger.SYSMSG, "TCPReader listening on "
98 >                                        +InetAddress.getLocalHost().getHostName()
99 >                                        +"/"+InetAddress.getLocalHost().getHostAddress()
100 >                                        +" port "+listenPort.getLocalPort());
101 >        }
102 >        catch(UnknownHostException e){
103 >            _logger.write(toString(), Logger.SYSMSG, "TCPReader listening on UnknownHost "
104 >                                        +"port "+listenPort.getLocalPort());
105 >        }
106 >        
107 >        // We use this boolean so we can break out of the while loop if we want
108 >        boolean run = true;
109 >        
110 >        // Loop round constantly until we decide to stop
111 >        ConfigurationProxy cp = ConfigurationProxy.getInstance();
112 >        String stringACL = "";
113 >        String newStringACL = "";
114          while(run){
115 <            Socket hostSocket=null;
115 >            // get hold of the ACL in the configuration
116 >            try {
117 >                newStringACL = cp.getProperty("Filter." + FilterMain.NAME, "Filter.TCPACL");
118 >            }
119 >            catch(PropertyNotFoundException e) {
120 >                // if we can't find it, we'll just use a null ACL
121 >                newStringACL = "";
122 >                _logger.write(toString(), Logger.WARNING, "No ACL found for TCPReader, using empty ACL instead: " + e);
123 >            }
124 >            // check to see if the ACL has changed
125 >            if(!newStringACL.equals(stringACL)) {
126 >                _logger.write(toString(), Logger.SYSMSG, "Reloading Access Control List");
127 >                // clear the ACL
128 >                acl.clear();
129 >                // set the default to something sane
130 >                acl.setDefaultMode(ACL.ALLOW);
131 >                // add the new ACL (this may change the default)
132 >                acl.add(newStringACL);
133 >                stringACL = newStringACL;
134 >            }
135 >            Socket hostSocket=null;
136              try{
137 <                _logger.write(toString(), Logger.SYSMSG, "Waiting for Connection");
138 <                                // This will block until a host connects - at which point we get a Socket
137 >                _logger.write(toString(), Logger.DEBUG, "Waiting for Connection");
138 >                // This will block until a host connects - at which point we get a Socket
139                  hostSocket = listenPort.accept();
140 <                _logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString());
140 >                _logger.write(toString(), Logger.DEBUG, "Connection accepted from: " + hostSocket.toString());
141              }
142              catch(IOException e){
143 <                                // Something went wrong with the ServerSocket, so we'll stop listening
143 >                // Something went wrong with the ServerSocket, so we'll stop listening
144                  run=false;
145              }
146 <                        // If we've stopped on the line above we won't want to try this !
146 >            // If we've stopped on the line above we won't want to try this !
147              if(run){
148 <                                try {
149 <                                    // Setup the HostInit so it can carry on communications with the host
150 <                    TCPReaderInit init = new TCPReaderInit(hostSocket, _configManager, _logger, _parent);
151 <                                    // and start it
148 >                try {
149 >                    // Setup the TCPReaderInit and start it
150 >                    TCPReaderInit init = new TCPReaderInit(hostSocket, _queue);
151 >                    // and start it
152                      init.start();
153                  } catch (IOException e) {
154                      _logger.write(toString(), Logger.ERROR, e.toString());
155                  }
156              }
157          }
158 <                // Best log the fact that we're stopping
158 >        // Best log the fact that we're stopping
159          _logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending");
160      }
161  
# Line 113 | Line 163 | class TCPReader extends Thread {
163       * Overrides the {@link java.lang.Object#toString() Object.toString()}
164       * method to provide clean logging (every class should have this).
165       *
166 +     * This uses the uk.org.iscream.cms.util.NameFormat class
167 +     * to format the toString()
168 +     *
169       * @return the name of this class and its CVS revision
170       */
171      public String toString() {
172 <        return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")";
172 >        return FormatName.getName(
173 >            _name,
174 >            getClass().getName(),
175 >            REVISION);
176      }
177  
178   //---PRIVATE METHODS---
# Line 126 | Line 182 | class TCPReader extends Thread {
182   //---ATTRIBUTES---
183      
184      /**
185 <     * A reference to the logger the system is using
185 >     * This is the friendly identifier of the
186 >     * component this class is running in.
187 >     * eg, a Filter may be called "filter1",
188 >     * If this class does not have an owning
189 >     * component,  a name from the configuration
190 >     * can be placed here.  This name could also
191 >     * be changed to null for utility classes.
192       */
193 <    Logger _logger;
194 <    
193 >    private String _name = FilterMain.NAME;
194 >
195      /**
196 <     * A reference to the configurator the system is using
196 >     * This holds a reference to the
197 >     * system logger that is being used.
198       */
199 <    ConfigurationManager _configManager;
199 >    private Logger _logger = ReferenceManager.getInstance().getLogger();
200      
201      /**
202 <         * The port on which the server should listen.
203 <         */
202 >     * The port on which the server should listen.
203 >     */
204      private int _port;
205      
206 <    private Filter _parent;
206 >    /**
207 >     * A reference to our Queue
208 >     */
209 >    private Queue _queue;
210      
211   //---STATIC ATTRIBUTES---
212  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines