1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.clientinterface; |
3 |
|
4 |
//---IMPORTS--- |
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 Data Handler to a CORBA based client. |
13 |
* |
14 |
* @author $Author: tdb1 $ |
15 |
* @version $Id: CorbaDataHandler.java,v 1.8 2001/03/16 02:14:40 tdb1 Exp $ |
16 |
*/ |
17 |
class CorbaDataHandler extends Thread { |
18 |
|
19 |
//---FINAL ATTRIBUTES--- |
20 |
|
21 |
/** |
22 |
* The current CVS revision of this class |
23 |
*/ |
24 |
public final String REVISION = "$Revision: 1.8 $"; |
25 |
|
26 |
//---STATIC METHODS--- |
27 |
|
28 |
//---CONSTRUCTORS--- |
29 |
|
30 |
/** |
31 |
* Construct a new CorbaDataHandler. |
32 |
* |
33 |
* @param client A reference to the "servant" part of the connecting client. |
34 |
* @param cchServer A reference to the class that is control us. |
35 |
*/ |
36 |
public CorbaDataHandler(Client client, CorbaControlHandlerServant cchServant) { |
37 |
// set the Thread name |
38 |
setName("clientinterface.CorbaDataHandler"); |
39 |
|
40 |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
41 |
String configName = "ClientInterface"; |
42 |
// see if this Queue needs a size limit |
43 |
try { |
44 |
int queueSizeLimit = Integer.parseInt(cp.getProperty(configName, "Queue.SizeLimit")); |
45 |
String queueRemoveAlgorithm = cp.getProperty(configName, "Queue.RemoveAlgorithm"); |
46 |
int algorithm = StringUtils.getStringPos(queueRemoveAlgorithm, Queue.algorithms); |
47 |
if(algorithm != -1) { |
48 |
_logger.write(toString(), Logger.DEBUG, "Starting Queue with size limit of "+queueSizeLimit+", using remove algorithm "+queueRemoveAlgorithm); |
49 |
// we have valid values, so lets start it. |
50 |
_queue = new Queue(queueSizeLimit, algorithm); |
51 |
} |
52 |
else { |
53 |
_logger.write(toString(), Logger.WARNING, "Bad Queue Algorithm configuration, not known: "+queueRemoveAlgorithm); |
54 |
// just don't activate a limit |
55 |
_queue = new Queue(); |
56 |
} |
57 |
|
58 |
} catch (PropertyNotFoundException e) { |
59 |
_logger.write(toString(), Logger.DEBUG, "Optional config not set: "+e); |
60 |
// just don't activate a limit |
61 |
_queue = new Queue(); |
62 |
} catch (NumberFormatException e) { |
63 |
_logger.write(toString(), Logger.WARNING, "Bad Queue SizeLimit configuration: "+e); |
64 |
// just don't activate a limit |
65 |
_queue = new Queue(); |
66 |
} |
67 |
|
68 |
_client = client; |
69 |
_cchServant = cchServant; |
70 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
71 |
} |
72 |
|
73 |
//---PUBLIC METHODS--- |
74 |
|
75 |
/** |
76 |
* This method loops round until such a point as we shutdown. It keeps |
77 |
* grabbing data items from it's Queue, then sends them on to the connected |
78 |
* client over CORBA. |
79 |
*/ |
80 |
public void run() { |
81 |
// get a queue |
82 |
_queueID = _queue.getQueue(); |
83 |
// we'll keep going until someone implements a "shutdown" :) |
84 |
run = true; |
85 |
// loop sending data |
86 |
while(run) { |
87 |
try { |
88 |
String xml = (String) _queue.get(_queueID); |
89 |
// if it's not null (which could happen if we're "released") |
90 |
// send it on to the client that we're connected to. |
91 |
if(xml != null) { |
92 |
try { |
93 |
_client.receiveXML(xml); |
94 |
} |
95 |
catch (org.omg.CORBA.COMM_FAILURE e) { |
96 |
// lets stop sending, the client has quit |
97 |
run = false; |
98 |
_logger.write(toString(), Logger.ERROR, "Connection failure, client shutdown? : "+e); |
99 |
// disconnect the servant above us, or at least try |
100 |
_cchServant.disconnect(); |
101 |
} |
102 |
} |
103 |
} |
104 |
catch(InvalidQueueException e) { |
105 |
// lets stop sending. |
106 |
run = false; |
107 |
_logger.write(toString(), Logger.ERROR, "Queue failure: "+e); |
108 |
} |
109 |
} |
110 |
// if we get here we've been told to stop |
111 |
_logger.write(toString(), Logger.SYSMSG, "Shutting Down"); |
112 |
// remove ourselves from the queue |
113 |
_queue.removeQueue(_queueID); |
114 |
_queue.stopMonitor(); |
115 |
} |
116 |
|
117 |
/** |
118 |
* Method to shutdown this Data Handler. All this actually does |
119 |
* is set a flag which the main loop will see and commence shutting |
120 |
* down. This method will return before the main loop stops. |
121 |
*/ |
122 |
public void shutdown() { |
123 |
// set the main loop to stop |
124 |
run=false; |
125 |
// if the main loop is waiting for data it won't notice the |
126 |
// above flag to stop. This bumps it out of the blocked get. |
127 |
_queue.releaseQueue(_queueID); |
128 |
} |
129 |
|
130 |
/** |
131 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
132 |
* method to provide clean logging (every class should have this). |
133 |
* |
134 |
* This uses the uk.org.iscream.util.NameFormat class |
135 |
* to format the toString() |
136 |
* |
137 |
* @return the name of this class and its CVS revision |
138 |
*/ |
139 |
public String toString() { |
140 |
return FormatName.getName( |
141 |
_name, |
142 |
getClass().getName(), |
143 |
REVISION); |
144 |
} |
145 |
|
146 |
//---PRIVATE METHODS--- |
147 |
|
148 |
/** |
149 |
* Overridden for debugging purposes |
150 |
* to see when an instance of this class |
151 |
* is destroyed |
152 |
*/ |
153 |
protected void finalize() throws Throwable { |
154 |
_logger.write(this.toString(), Logger.DEBUG, "finalized by GC"); |
155 |
} |
156 |
|
157 |
//---ACCESSOR/MUTATOR METHODS--- |
158 |
|
159 |
/** |
160 |
* Accessor to our Queue. The Control Handler needs to |
161 |
* get this reference to register us. |
162 |
* |
163 |
* @return a reference to our Queue |
164 |
*/ |
165 |
public Queue getQueue() { |
166 |
return _queue; |
167 |
} |
168 |
|
169 |
//---ATTRIBUTES--- |
170 |
|
171 |
/** |
172 |
* This is the friendly identifier of the |
173 |
* component this class is running in. |
174 |
* eg, a Filter may be called "filter1", |
175 |
* If this class does not have an owning |
176 |
* component, a name from the configuration |
177 |
* can be placed here. This name could also |
178 |
* be changed to null for utility classes. |
179 |
*/ |
180 |
private String _name = ClientInterfaceMain.NAME; |
181 |
|
182 |
/** |
183 |
* This holds a reference to the |
184 |
* system logger that is being used. |
185 |
*/ |
186 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
187 |
|
188 |
/** |
189 |
* The Queue we'll use for buffering data to the client. |
190 |
*/ |
191 |
private Queue _queue; |
192 |
|
193 |
/** |
194 |
* The "servant" part of the client we're connected to. |
195 |
*/ |
196 |
private Client _client; |
197 |
|
198 |
/** |
199 |
* Our queue number within our Queue |
200 |
*/ |
201 |
private int _queueID; |
202 |
|
203 |
/** |
204 |
* The flag that dictates whether the main loop should *completely* exit |
205 |
*/ |
206 |
private boolean run; |
207 |
|
208 |
/** |
209 |
* A reference to our controlling class |
210 |
*/ |
211 |
private CorbaControlHandlerServant _cchServant; |
212 |
|
213 |
//---STATIC ATTRIBUTES--- |
214 |
|
215 |
} |