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.14 by tdb, Tue May 29 17:02:34 2001 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines