| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.Component; |
| 4 |
import java.awt.event.ActionEvent; |
| 5 |
import java.util.Collections; |
| 6 |
import java.util.HashMap; |
| 7 |
import java.util.Iterator; |
| 8 |
import java.util.Map; |
| 9 |
import java.util.logging.Logger; |
| 10 |
import javax.swing.AbstractAction; |
| 11 |
import javax.swing.Action; |
| 12 |
import javax.swing.ListModel; |
| 13 |
import javax.swing.event.ChangeEvent; |
| 14 |
import javax.swing.event.ChangeListener; |
| 15 |
import org.astrogrid.samp.Client; |
| 16 |
import org.astrogrid.samp.Message; |
| 17 |
import org.astrogrid.samp.Response; |
| 18 |
import org.astrogrid.samp.client.HubConnection; |
| 19 |
import org.astrogrid.samp.client.LogResultHandler; |
| 20 |
import org.astrogrid.samp.client.ResponseHandler; |
| 21 |
import org.astrogrid.samp.client.ResultHandler; |
| 22 |
|
| 23 |
/** |
| 24 |
* Partial SendActionManager implementation which |
| 25 |
* uses the Asynchronous Call/Response delivery pattern. |
| 26 |
* It supplies most of the machinery required for tracking what happened |
| 27 |
* to responses to messages sent at the same time, but does not |
| 28 |
* implement the actual {@link #createBroadcastAction} method. |
| 29 |
* Subclasses are provided which do this. |
| 30 |
* |
| 31 |
* @author Mark Taylor |
| 32 |
* @since 11 Nov 2008 |
| 33 |
*/ |
| 34 |
public abstract class AbstractCallActionManager extends SendActionManager { |
| 35 |
|
| 36 |
private final Component parent_; |
| 37 |
private final GuiHubConnector connector_; |
| 38 |
private final CallResponseHandler responder_; |
| 39 |
private static final Logger logger_ = |
| 40 |
Logger.getLogger( AbstractCallActionManager.class.getName() ); |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor. |
| 44 |
* |
| 45 |
* @param parent parent component |
| 46 |
* @param connector hub connector |
| 47 |
* @param clientListModel list model containing only those clients |
| 48 |
* which are suitable recipients; |
| 49 |
* all elements must be {@link Client}s |
| 50 |
*/ |
| 51 |
public AbstractCallActionManager( Component parent, |
| 52 |
GuiHubConnector connector, |
| 53 |
ListModel clientListModel ) { |
| 54 |
super( connector, clientListModel ); |
| 55 |
parent_ = parent; |
| 56 |
connector_ = connector; |
| 57 |
responder_ = new CallResponseHandler(); |
| 58 |
connector_.addResponseHandler( responder_ ); |
| 59 |
connector_.addConnectionListener( responder_ ); |
| 60 |
updateState(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Must be implemented by concrete subclasses. |
| 65 |
*/ |
| 66 |
abstract protected Action createBroadcastAction(); |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns an object which will be informed of the results of a single- |
| 70 |
* or multiple-recipient send as they arrive. |
| 71 |
* This method will be called from the event dispatch thread. |
| 72 |
* |
| 73 |
* <p>The default implementation returns an instance of |
| 74 |
* {@link org.astrogrid.samp.client.LogResultHandler}. |
| 75 |
* |
| 76 |
* @param connection connection object |
| 77 |
* @param msg the message which was sent |
| 78 |
* @param recipients the recipients to whom the message was sent |
| 79 |
* @return result handler object |
| 80 |
*/ |
| 81 |
protected ResultHandler createResultHandler( HubConnection connection, |
| 82 |
Message msg, |
| 83 |
Client[] recipients ) { |
| 84 |
return new LogResultHandler( msg ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Releases resources associated with this object. |
| 89 |
* Specifically, it removes listeners from the hub connector. |
| 90 |
* Following a call to this method, this object should not be used again. |
| 91 |
*/ |
| 92 |
public void dispose() { |
| 93 |
connector_.removeResponseHandler( responder_ ); |
| 94 |
connector_.removeConnectionListener( responder_ ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns the Message object which is to be transmitted by this manager |
| 99 |
* to a given client. This is called by the action returned by |
| 100 |
* {@link #getSendAction}. |
| 101 |
* |
| 102 |
* @param client target |
| 103 |
* @return message |
| 104 |
*/ |
| 105 |
protected abstract Map createMessage( Client client ) throws Exception; |
| 106 |
|
| 107 |
protected Action getSendAction( Client client ) { |
| 108 |
return new SendAction( client ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Creates and returns a new tag which will be attached to |
| 113 |
* an outgoing message, and updates internal structures so that |
| 114 |
* it will be recognised in the future. |
| 115 |
* A subsequent call to {@link #registerHandler} should be made for the |
| 116 |
* returned tag. |
| 117 |
* |
| 118 |
* @return new tag |
| 119 |
*/ |
| 120 |
public String createTag() { |
| 121 |
return responder_.createTag(); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Registers a result handler to handle results corresponding to a |
| 126 |
* message tag. |
| 127 |
* |
| 128 |
* @param tag tag returned by an earlier invocation of |
| 129 |
* {@link #createTag} |
| 130 |
* @param recipients clients from which responses are expected |
| 131 |
* @param handler result handler for responses; may be null |
| 132 |
* if no handling is required |
| 133 |
*/ |
| 134 |
public void registerHandler( String tag, Client[] recipients, |
| 135 |
ResultHandler handler ) { |
| 136 |
responder_.registerHandler( tag, recipients, handler ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Action which performs a send to a particular client. |
| 141 |
*/ |
| 142 |
private class SendAction extends AbstractAction { |
| 143 |
private final Client client_; |
| 144 |
private final String cName_; |
| 145 |
|
| 146 |
/** |
| 147 |
* Constructor. |
| 148 |
* |
| 149 |
* @param client target client |
| 150 |
*/ |
| 151 |
SendAction( Client client ) { |
| 152 |
client_ = client; |
| 153 |
cName_ = client.toString(); |
| 154 |
putValue( NAME, cName_ ); |
| 155 |
putValue( SHORT_DESCRIPTION, |
| 156 |
"Transmit to " + cName_ + " using SAMP protocol" ); |
| 157 |
} |
| 158 |
|
| 159 |
public void actionPerformed( ActionEvent evt ) { |
| 160 |
boolean sent = false; |
| 161 |
Message msg = null; |
| 162 |
HubConnection connection = null; |
| 163 |
String tag = null; |
| 164 |
|
| 165 |
// Attempt to send the messsage. |
| 166 |
try { |
| 167 |
msg = Message.asMessage( createMessage( client_ ) ); |
| 168 |
msg.check(); |
| 169 |
connection = connector_.getConnection(); |
| 170 |
if ( connection != null ) { |
| 171 |
tag = responder_.createTag(); |
| 172 |
connection.call( client_.getId(), tag, msg ); |
| 173 |
sent = true; |
| 174 |
} |
| 175 |
} |
| 176 |
catch ( Exception e ) { |
| 177 |
ErrorDialog.showError( parent_, "Send Error", |
| 178 |
"Send failure " + e.getMessage(), e ); |
| 179 |
} |
| 180 |
|
| 181 |
// If it was sent, arrange for the result to be processed by |
| 182 |
// a suitable result handler. |
| 183 |
if ( sent ) { |
| 184 |
assert connection != null; |
| 185 |
assert msg != null; |
| 186 |
assert tag != null; |
| 187 |
Client[] recipients = new Client[] { client_ }; |
| 188 |
ResultHandler handler = |
| 189 |
createResultHandler( connection, msg, recipients ); |
| 190 |
responder_.registerHandler( tag, recipients, handler ); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
public boolean equals( Object o ) { |
| 195 |
if ( o instanceof SendAction ) { |
| 196 |
SendAction other = (SendAction) o; |
| 197 |
return this.client_.equals( other.client_ ) |
| 198 |
&& this.cName_.equals( other.cName_ ); |
| 199 |
} |
| 200 |
else { |
| 201 |
return false; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
public int hashCode() { |
| 206 |
return client_.hashCode() * 23 + cName_.hashCode(); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* ResponseHandler implementation for use by this class. |
| 212 |
* It handles all SAMP responses for calls which have been made by |
| 213 |
* this object and passes them on to the appropriate ResultHandlers. |
| 214 |
*/ |
| 215 |
private class CallResponseHandler implements ResponseHandler, |
| 216 |
ChangeListener { |
| 217 |
private int iCall_; |
| 218 |
private final Map tagMap_; |
| 219 |
|
| 220 |
/** |
| 221 |
* Constructor. |
| 222 |
*/ |
| 223 |
CallResponseHandler() { |
| 224 |
tagMap_ = Collections.synchronizedMap( new HashMap() ); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Creates and returns a new tag which will be attached to |
| 229 |
* an outgoing message, and updates internal structures so that |
| 230 |
* it will be recognised in the future. |
| 231 |
* A subsequent call to {@link #registerHandler} should be made for the |
| 232 |
* returned tag. |
| 233 |
* |
| 234 |
* @return new tag |
| 235 |
*/ |
| 236 |
public synchronized String createTag() { |
| 237 |
String tag = connector_.createTag( this ); |
| 238 |
tagMap_.put( tag, null ); |
| 239 |
return tag; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Registers a result handler to handle results corresponding to a |
| 244 |
* message tag. |
| 245 |
* |
| 246 |
* @param tag tag returned by an earlier invocation of |
| 247 |
* {@link #createTag} |
| 248 |
* @param recipients clients from which responses are expected |
| 249 |
* @param handler result handler for responses; may be null |
| 250 |
* if no handling is required |
| 251 |
*/ |
| 252 |
public void registerHandler( String tag, Client[] recipients, |
| 253 |
ResultHandler handler ) { |
| 254 |
synchronized ( tagMap_ ) { |
| 255 |
if ( handler != null ) { |
| 256 |
tagMap_.put( tag, new TagInfo( recipients, handler ) ); |
| 257 |
} |
| 258 |
else { |
| 259 |
tagMap_.remove( tag ); |
| 260 |
} |
| 261 |
tagMap_.notifyAll(); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
public boolean ownsTag( String tag ) { |
| 266 |
return tagMap_.containsKey( tag ); |
| 267 |
} |
| 268 |
|
| 269 |
public void receiveResponse( HubConnection connection, |
| 270 |
final String responderId, final String tag, |
| 271 |
final Response response ) { |
| 272 |
synchronized ( tagMap_ ) { |
| 273 |
if ( tagMap_.containsKey( tag ) ) { |
| 274 |
|
| 275 |
// If the result handler is already registered, pass the |
| 276 |
// result on to it. |
| 277 |
TagInfo info = (TagInfo) tagMap_.get( tag ); |
| 278 |
if ( info != null ) { |
| 279 |
processResponse( tag, info, responderId, response ); |
| 280 |
} |
| 281 |
|
| 282 |
// If the response was received very quickly, it's possible |
| 283 |
// that the handler has not been registered yet. |
| 284 |
// In this case, wait until it is. |
| 285 |
// Do this in a separate thread so that the |
| 286 |
// receiveResponse can return quickly (not essential, but |
| 287 |
// good behaviour). |
| 288 |
else { |
| 289 |
new Thread( "TagWaiter-" + tag ) { |
| 290 |
public void run() { |
| 291 |
TagInfo tinfo; |
| 292 |
try { |
| 293 |
synchronized ( tagMap_ ) { |
| 294 |
do { |
| 295 |
tinfo = |
| 296 |
(TagInfo) tagMap_.get( tag ); |
| 297 |
if ( tinfo == null ) { |
| 298 |
tagMap_.wait(); |
| 299 |
} |
| 300 |
} while ( tinfo == null ); |
| 301 |
} |
| 302 |
processResponse( tag, tinfo, responderId, |
| 303 |
response ); |
| 304 |
} |
| 305 |
catch ( InterruptedException e ) { |
| 306 |
logger_.warning( "Interrupted??" ); |
| 307 |
} |
| 308 |
} |
| 309 |
}.start(); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
// Shouldn't happen - HubConnector should not have invoked |
| 314 |
// in this case. |
| 315 |
else { |
| 316 |
logger_.warning( "Receive response for unknown tag " |
| 317 |
+ tag + "??" ); |
| 318 |
return; |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Does the work of passing on a received response to a registered |
| 325 |
* result handler. |
| 326 |
* |
| 327 |
* @param tag message tag |
| 328 |
* @param info tag handling information object |
| 329 |
* @param responderId client ID of responder |
| 330 |
* @param response response object |
| 331 |
*/ |
| 332 |
private void processResponse( String tag, TagInfo info, |
| 333 |
String responderId, Response response ) { |
| 334 |
ResultHandler handler = info.handler_; |
| 335 |
Map recipientMap = info.recipientMap_; |
| 336 |
synchronized ( info ) { |
| 337 |
Client responder = (Client) recipientMap.remove( responderId ); |
| 338 |
|
| 339 |
// Pass response on to handler. |
| 340 |
if ( responder != null ) { |
| 341 |
handler.result( responder, response ); |
| 342 |
} |
| 343 |
|
| 344 |
// If there are no more to come, notify the handler of this. |
| 345 |
if ( recipientMap.isEmpty() ) { |
| 346 |
handler.done(); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
// Unregister the handler if no more responses are expected for it. |
| 351 |
synchronized ( tagMap_ ) { |
| 352 |
if ( recipientMap.isEmpty() ) { |
| 353 |
tagMap_.remove( tag ); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
public void stateChanged( ChangeEvent evt ) { |
| 359 |
if ( ! connector_.isConnected() ) { |
| 360 |
hubDisconnected(); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Called when the connection to the hub disappears. |
| 366 |
*/ |
| 367 |
private void hubDisconnected() { |
| 368 |
synchronized ( tagMap_ ) { |
| 369 |
|
| 370 |
// Notify all result handlers that they will receive no more |
| 371 |
// responses, then unregister them all. |
| 372 |
for ( Iterator it = tagMap_.entrySet().iterator(); |
| 373 |
it.hasNext(); ) { |
| 374 |
Map.Entry entry = (Map.Entry) it.next(); |
| 375 |
String tag = (String) entry.getKey(); |
| 376 |
TagInfo info = (TagInfo) entry.getValue(); |
| 377 |
if ( info != null ) { |
| 378 |
info.handler_.done(); |
| 379 |
} |
| 380 |
it.remove(); |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Aggregates information required for handling responses which |
| 388 |
* correspond to a particular message tag. |
| 389 |
*/ |
| 390 |
private static class TagInfo { |
| 391 |
final Map recipientMap_; |
| 392 |
final ResultHandler handler_; |
| 393 |
|
| 394 |
/** |
| 395 |
* Constructor. |
| 396 |
* |
| 397 |
* @param recipients recipients of message |
| 398 |
* @param handler handler for responses |
| 399 |
*/ |
| 400 |
public TagInfo( Client[] recipients, ResultHandler handler ) { |
| 401 |
recipientMap_ = Collections.synchronizedMap( new HashMap() ); |
| 402 |
for ( int i = 0; i < recipients.length; i++ ) { |
| 403 |
Client recipient = recipients[ i ]; |
| 404 |
recipientMap_.put( recipient.getId(), recipient ); |
| 405 |
} |
| 406 |
handler_ = handler; |
| 407 |
} |
| 408 |
} |
| 409 |
} |