ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/clientinterface/CorbaControlHandlerServant.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/clientinterface/CorbaControlHandlerServant.java (file contents):
Revision 1.1 by tdb, Sat Feb 3 00:42:54 2001 UTC vs.
Revision 1.16 by tdb, Tue May 21 16:47:16 2002 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.ac.ukc.iscream.clientinterface;
22 > package uk.org.iscream.cms.server.clientinterface;
23  
24   //---IMPORTS---
25 < import uk.ac.ukc.iscream.util.*;
26 < import uk.ac.ukc.iscream.componentmanager.*;
27 < import uk.ac.ukc.iscream.core.*;
28 < import uk.ac.ukc.iscream.client.*;
25 > import uk.org.iscream.cms.server.util.*;
26 > import uk.org.iscream.cms.server.componentmanager.*;
27 > import uk.org.iscream.cms.server.core.*;
28 > import uk.org.iscream.cms.server.client.*;
29  
30  
31   /**
32 < * Acts as a Handler to a CORBA based client.
32 > * Acts as a Control Handler to a CORBA based client.
33   *
14 * !!! FUNDAMENTAL DESIGN PROBLEM !!!
15 * !!! Need a way to "shutdown" this class !!!
16 *
34   * @author  $Author$
35   * @version $Id$
36   */
37 < class CorbaHandlerServant extends Thread implements CorbaHandlerOperations {
37 > class CorbaControlHandlerServant extends CorbaControlHandlerPOA {
38  
39   //---FINAL ATTRIBUTES---
40  
# Line 31 | Line 48 | class CorbaHandlerServant extends Thread implements Co
48   //---CONSTRUCTORS---
49      
50      /**
51 <     * Construct a new CorbaHandlerServant.
51 >     * Construct a new CorbaControlHandlerServant.
52       *
53       * @param packetSorter A reference to the PacketSorter in the component
37     * @param name The name of the client
54       * @param client A reference to the "servant" part of the connecting client.
55 +     * @param queueMonitorInterval The interval at which to monitor our Queue.
56 +     * @param clientname A name to identify the client.
57       */
58 <    public CorbaHandlerServant(PacketSorter packetSorter, String name, Client client) {
58 >    public CorbaControlHandlerServant(PacketSorter packetSorter, Client client, String clientname) {
59          _packetSorter = packetSorter;
42        _clientName = name;
60          _hostList = "";
44        _queue = new Queue();
61          _client = client;
62 +        _clientname = clientname;
63 +        _dataHandler = null;
64          _logger.write(toString(), Logger.SYSINIT, "created");
65      }
66      
67   //---PUBLIC METHODS---
68      
69      /**
52     * This method loops round until we "shutdown" (not implemented yet). Inside
53     * the main loop we will first wait until the startData() method is called,
54     * then we'll send data constantly until stopData() is called.
55     */
56    public void run() {
57        // get a queue
58        _queueID = _queue.getQueue();
59        // we'll keep going until someone implements a "shutdown" :)
60        run = true;
61        while(run) {
62            // initially lock until someone tells us to start
63            synchronized(this) {
64                try { wait(); } catch(Exception e) {}
65            }
66            // loop sending data
67            while(_sendingData) {
68                try {
69                    String xml = (String) _queue.get(_queueID);
70                    // if it's not null (which could happen if we're "released")
71                    // send it on to the client that we're connected to.
72                    if(xml != null) {
73                        _client.receiveXML(xml);
74                    }
75                }
76                catch(InvalidQueueException e) {
77                    // lets stop sending - maybe stop altogether ?
78                    stopData();
79                    _logger.write(toString(), Logger.ERROR, "Queue failure: "+e);
80                }
81            }
82            // now, we've obviously been stopped, so we'll head back round this
83            // while loop until someone starts us again.
84        }
85        // if we get here we've been told to stop
86        _logger.write(toString(), Logger.SYSMSG, "Shutting Down");
87        // remove ourselves from the queue
88        _queue.removeQueue(_queueID);
89    }
90    
91    /**
70       * Start sending data to the client.
71       *
72       * @return a boolean stating whether the attempt to start succeeded
73       */
74      public boolean startData() {
75 <        if(!_sendingData) {
75 >        if(_dataHandler == null) {
76 >            // create a new DataHandler
77 >            CorbaDataHandler dh = new CorbaDataHandler(_client, this);
78              // register the Queue
79 <            _packetSorter.register(_queue, _hostList);
80 <            // mark the running flag
81 <            _sendingData = true;
82 <            // bump the loop
83 <            synchronized(this) {
84 <                try { notifyAll(); } catch(Exception e) {}
79 >            _packetSorter.register(dh.getQueue(), _hostList);
80 >            try {
81 >                // startup a monitor on the DataHandler's queue
82 >                ConfigurationProxy cp = ConfigurationProxy.getInstance();
83 >                int queueMonitorInterval = Integer.parseInt(cp.getProperty("ClientInterface", "Queue.MonitorInterval"));
84 >                String queueName = _name + " CorbaHandler:"+_clientname;
85 >                dh.getQueue().startMonitor(queueMonitorInterval*1000, _packetSorter.getQueue(), queueName);
86 >            } catch (PropertyNotFoundException e) {
87 >                _logger.write(toString(), Logger.WARNING, "failed to find queue monitor config, disabling. " + e);
88              }
89 +            // start the DataHandler running
90 +            dh.start();
91 +            // keep a reference
92 +            _dataHandler = dh;
93              return true;
94          }
95          else {
# Line 116 | Line 103 | class CorbaHandlerServant extends Thread implements Co
103       * @return a boolean stating whether the attempt to stop succeeded
104       */
105      public boolean stopData() {
106 <        if(_sendingData) {
106 >        if(_dataHandler != null) {
107              // deregister the Queue
108 <            _packetSorter.deregister(_queue, _hostList);
109 <            // stop ourselves running
110 <            _sendingData = false;
111 <            // if the main loop is waiting for data it won't notice the
112 <            // above flag to stop. This bumps it out of the blocked get().
126 <            _queue.releaseQueue(_queueID);
108 >            _packetSorter.deregister(_dataHandler.getQueue(), _hostList);
109 >            // stop the DataHandler
110 >            _dataHandler.shutdown();
111 >            // destroy the reference
112 >            _dataHandler = null;
113              return true;
114          }
115          else {
# Line 139 | Line 125 | class CorbaHandlerServant extends Thread implements Co
125       * @return Whether the request succeeded.
126       */
127      public boolean setHostList(String hostList) {
128 <        if(!_sendingData) {
128 >        if(_dataHandler == null) {
129              _hostList = hostList;
130              return true;
131          }
# Line 149 | Line 135 | class CorbaHandlerServant extends Thread implements Co
135      }
136      
137      /**
138 +     * Disconnect, this will shutdown the data and unhook from
139 +     * the CORBA ORB.
140 +     */
141 +    public void disconnect() {
142 +        // close the data handler
143 +        stopData();
144 +        // disconnect from the ORB
145 +        try {
146 +            byte[] oid = _refman.getRootPOA().servant_to_id(this);
147 +            _refman.getRootPOA().deactivate_object(oid);
148 +        } catch(Exception e) {
149 +            _logger.write(this.toString(), Logger.ERROR, "disconnect failed: "+e);
150 +        }
151 +    }
152 +    
153 +    /**
154       * Overrides the {@link java.lang.Object#toString() Object.toString()}
155       * method to provide clean logging (every class should have this).
156       *
157 <     * This uses the uk.ac.ukc.iscream.util.NameFormat class
157 >     * This uses the uk.org.iscream.cms.server.util.NameFormat class
158       * to format the toString()
159       *
160       * @return the name of this class and its CVS revision
# Line 166 | Line 168 | class CorbaHandlerServant extends Thread implements Co
168  
169   //---PRIVATE METHODS---
170  
171 +    /**
172 +     * Overridden for debugging purposes
173 +     * to see when an instance of this class
174 +     * is destroyed
175 +     */
176 +    protected void finalize() throws Throwable {
177 +        _logger.write(this.toString(), Logger.DEBUG, "finalized by GC");
178 +    }
179 +
180   //---ACCESSOR/MUTATOR METHODS---
181  
182   //---ATTRIBUTES---
# Line 186 | Line 197 | class CorbaHandlerServant extends Thread implements Co
197       * system logger that is being used.
198       */
199      private Logger _logger = ReferenceManager.getInstance().getLogger();
200 <    
200 >
201      /**
202 <     * A reference to the Configuration Manager the system is using
202 >     * A reference to the reference manager in use
203       */
204 <    private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
204 >    private ReferenceManager _refman = ReferenceManager.getInstance();
205      
206      /**
207           * A reference to the PacketSorter.
# Line 203 | Line 214 | class CorbaHandlerServant extends Thread implements Co
214      private String _hostList;
215      
216      /**
206     * The name of the connected client.
207     */
208    private String _clientName;
209    
210    /**
211     * Whether we are active and sending data.
212     */
213    private boolean _sendingData;
214    
215    /**
216     * The Queue we'll use for buffering data to the client.
217     */
218    private Queue _queue;
219    
220    /**
217       * The "servant" part of the client we're connected to.
218       */
219      private Client _client;
220      
221      /**
222 <     * Our queue number within our Queue
222 >     * A reference to our DataHandler, if we have one
223       */
224 <    private int _queueID;
224 >    private CorbaDataHandler _dataHandler;
225      
226      /**
227 <     * The flag that dictates whether the main loop should *completely* exit
227 >     * A name to identify the client
228       */
229 <    private boolean run;
229 >    private String _clientname;
230      
231   //---STATIC ATTRIBUTES---
232  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines