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.12 by tdb, Fri Mar 16 02:14:40 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.clientinterface;
2 > package uk.org.iscream.clientinterface;
3  
4   //---IMPORTS---
5 < import uk.ac.ukc.iscream.util.*;
6 < import uk.ac.ukc.iscream.componentmanager.*;
7 < import uk.ac.ukc.iscream.core.*;
8 < import uk.ac.ukc.iscream.client.*;
5 > import uk.org.iscream.util.*;
6 > import uk.org.iscream.componentmanager.*;
7 > import uk.org.iscream.core.*;
8 > import uk.org.iscream.client.*;
9  
10  
11   /**
12 < * Acts as a Handler to a CORBA based client.
12 > * Acts as a Control Handler to a CORBA based client.
13   *
14   * !!! FUNDAMENTAL DESIGN PROBLEM !!!
15   * !!! Need a way to "shutdown" this class !!!
# Line 17 | Line 17 | import uk.ac.ukc.iscream.client.*;
17   * @author  $Author$
18   * @version $Id$
19   */
20 < class CorbaHandlerServant extends Thread implements CorbaHandlerOperations {
20 > class CorbaControlHandlerServant extends CorbaControlHandlerPOA {
21  
22   //---FINAL ATTRIBUTES---
23  
# Line 31 | Line 31 | class CorbaHandlerServant extends Thread implements Co
31   //---CONSTRUCTORS---
32      
33      /**
34 <     * Construct a new CorbaHandlerServant.
34 >     * Construct a new CorbaControlHandlerServant.
35       *
36       * @param packetSorter A reference to the PacketSorter in the component
37     * @param name The name of the client
37       * @param client A reference to the "servant" part of the connecting client.
38 +     * @param queueMonitorInterval The interval at which to monitor our Queue.
39 +     * @param clientname A name to identify the client.
40       */
41 <    public CorbaHandlerServant(PacketSorter packetSorter, String name, Client client) {
41 >    public CorbaControlHandlerServant(PacketSorter packetSorter, Client client, String clientname) {
42          _packetSorter = packetSorter;
42        _clientName = name;
43          _hostList = "";
44        _queue = new Queue();
44          _client = client;
45 +        _clientname = clientname;
46 +        _dataHandler = null;
47          _logger.write(toString(), Logger.SYSINIT, "created");
48      }
49      
50   //---PUBLIC METHODS---
51      
52      /**
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    /**
53       * Start sending data to the client.
54       *
55       * @return a boolean stating whether the attempt to start succeeded
56       */
57      public boolean startData() {
58 <        if(!_sendingData) {
58 >        if(_dataHandler == null) {
59 >            // create a new DataHandler
60 >            CorbaDataHandler dh = new CorbaDataHandler(_client, this);
61              // register the Queue
62 <            _packetSorter.register(_queue, _hostList);
63 <            // mark the running flag
64 <            _sendingData = true;
65 <            // bump the loop
66 <            synchronized(this) {
67 <                try { notifyAll(); } catch(Exception e) {}
62 >            _packetSorter.register(dh.getQueue(), _hostList);
63 >            try {
64 >                // startup a monitor on the DataHandler's queue
65 >                ConfigurationProxy cp = ConfigurationProxy.getInstance();
66 >                int queueMonitorInterval = Integer.parseInt(cp.getProperty("ClientInterface", "Queue.MonitorInterval"));
67 >                String queueName = _name + " CorbaHandler:"+_clientname;
68 >                dh.getQueue().startMonitor(queueMonitorInterval*1000, _packetSorter.getQueue(), queueName);
69 >            } catch (PropertyNotFoundException e) {
70 >                _logger.write(toString(), Logger.WARNING, "failed to find queue monitor config, disabling. " + e);
71              }
72 +            // start the DataHandler running
73 +            dh.start();
74 +            // keep a reference
75 +            _dataHandler = dh;
76              return true;
77          }
78          else {
# Line 117 | Line 86 | class CorbaHandlerServant extends Thread implements Co
86       * @return a boolean stating whether the attempt to stop succeeded
87       */
88      public boolean stopData() {
89 <        if(_sendingData) {
89 >        if(_dataHandler != null) {
90              // deregister the Queue
91 <            _packetSorter.deregister(_queue, _hostList);
92 <            // stop ourselves running
93 <            _sendingData = false;
94 <            // if the main loop is waiting for data it won't notice the
95 <            // above flag to stop. This bumps it out of the blocked get().
127 <            _queue.releaseQueue(_queueID);
91 >            _packetSorter.deregister(_dataHandler.getQueue(), _hostList);
92 >            // stop the DataHandler
93 >            _dataHandler.shutdown();
94 >            // destroy the reference
95 >            _dataHandler = null;
96              return true;
97          }
98          else {
# Line 140 | Line 108 | class CorbaHandlerServant extends Thread implements Co
108       * @return Whether the request succeeded.
109       */
110      public boolean setHostList(String hostList) {
111 <        if(!_sendingData) {
111 >        if(_dataHandler == null) {
112              _hostList = hostList;
113              return true;
114          }
# Line 150 | Line 118 | class CorbaHandlerServant extends Thread implements Co
118      }
119      
120      /**
121 +     * Disconnect, this will shutdown the data and unhook from
122 +     * the CORBA ORB.
123 +     */
124 +    public void disconnect() {
125 +        // close the data handler
126 +        stopData();
127 +        // disconnect from the ORB
128 +        try {
129 +            byte[] oid = _refman.getRootPOA().servant_to_id(this);
130 +            _refman.getRootPOA().deactivate_object(oid);
131 +        } catch(Exception e) {
132 +            _logger.write(this.toString(), Logger.ERROR, "disconnect failed: "+e);
133 +        }
134 +    }
135 +    
136 +    /**
137       * Overrides the {@link java.lang.Object#toString() Object.toString()}
138       * method to provide clean logging (every class should have this).
139       *
140 <     * This uses the uk.ac.ukc.iscream.util.NameFormat class
140 >     * This uses the uk.org.iscream.util.NameFormat class
141       * to format the toString()
142       *
143       * @return the name of this class and its CVS revision
# Line 167 | Line 151 | class CorbaHandlerServant extends Thread implements Co
151  
152   //---PRIVATE METHODS---
153  
154 +    /**
155 +     * Overridden for debugging purposes
156 +     * to see when an instance of this class
157 +     * is destroyed
158 +     */
159 +    protected void finalize() throws Throwable {
160 +        _logger.write(this.toString(), Logger.DEBUG, "finalized by GC");
161 +    }
162 +
163   //---ACCESSOR/MUTATOR METHODS---
164  
165   //---ATTRIBUTES---
# Line 187 | Line 180 | class CorbaHandlerServant extends Thread implements Co
180       * system logger that is being used.
181       */
182      private Logger _logger = ReferenceManager.getInstance().getLogger();
183 <    
183 >
184      /**
185 <     * A reference to the Configuration Manager the system is using
185 >     * A reference to the reference manager in use
186       */
187 <    private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
187 >    private ReferenceManager _refman = ReferenceManager.getInstance();
188      
189      /**
190           * A reference to the PacketSorter.
# Line 204 | Line 197 | class CorbaHandlerServant extends Thread implements Co
197      private String _hostList;
198      
199      /**
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    /**
200       * The "servant" part of the client we're connected to.
201       */
202      private Client _client;
203      
204      /**
205 <     * Our queue number within our Queue
205 >     * A reference to our DataHandler, if we have one
206       */
207 <    private int _queueID;
207 >    private CorbaDataHandler _dataHandler;
208      
209      /**
210 <     * The flag that dictates whether the main loop should *completely* exit
210 >     * A name to identify the client
211       */
212 <    private boolean run;
212 >    private String _clientname;
213      
214   //---STATIC ATTRIBUTES---
215  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines