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.2 by tdb, Sat Feb 3 04:44:09 2001 UTC vs.
Revision 1.19 by tdb, Sun Aug 1 10:40:48 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines