| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.BorderLayout; |
| 4 |
import java.awt.Color; |
| 5 |
import java.awt.Dimension; |
| 6 |
import java.util.ArrayList; |
| 7 |
import java.util.Arrays; |
| 8 |
import java.util.Collection; |
| 9 |
import java.util.HashMap; |
| 10 |
import java.util.HashSet; |
| 11 |
import java.util.Iterator; |
| 12 |
import java.util.List; |
| 13 |
import java.util.Map; |
| 14 |
import java.util.WeakHashMap; |
| 15 |
import java.util.logging.Logger; |
| 16 |
import javax.swing.AbstractListModel; |
| 17 |
import javax.swing.BorderFactory; |
| 18 |
import javax.swing.Box; |
| 19 |
import javax.swing.BoxLayout; |
| 20 |
import javax.swing.JComponent; |
| 21 |
import javax.swing.JPanel; |
| 22 |
import javax.swing.JTabbedPane; |
| 23 |
import javax.swing.ListCellRenderer; |
| 24 |
import javax.swing.ListModel; |
| 25 |
import javax.swing.SwingUtilities; |
| 26 |
import javax.swing.UIManager; |
| 27 |
import javax.swing.event.ListDataEvent; |
| 28 |
import javax.swing.event.ListDataListener; |
| 29 |
import org.astrogrid.samp.Client; |
| 30 |
import org.astrogrid.samp.Message; |
| 31 |
import org.astrogrid.samp.Metadata; |
| 32 |
import org.astrogrid.samp.Response; |
| 33 |
import org.astrogrid.samp.Subscriptions; |
| 34 |
import org.astrogrid.samp.client.CallableClient; |
| 35 |
import org.astrogrid.samp.client.ClientProfile; |
| 36 |
import org.astrogrid.samp.client.HubConnection; |
| 37 |
import org.astrogrid.samp.client.SampException; |
| 38 |
import org.astrogrid.samp.client.TrackedClientSet; |
| 39 |
|
| 40 |
/** |
| 41 |
* HubConnector implementation which provides facilities for keeping track |
| 42 |
* of incoming and outgoing messages as well as the other GUI features. |
| 43 |
* |
| 44 |
* @author Mark Taylor |
| 45 |
* @since 26 Nov 2008 |
| 46 |
*/ |
| 47 |
public class MessageTrackerHubConnector extends GuiHubConnector |
| 48 |
implements ClientTransmissionHolder { |
| 49 |
|
| 50 |
private final TransmissionListModel txListModel_; |
| 51 |
private final TransmissionListModel rxListModel_; |
| 52 |
private final TransmissionTableModel txTableModel_; |
| 53 |
private final TransmissionTableModel rxTableModel_; |
| 54 |
private final Map callAllMap_; |
| 55 |
private final Map txModelMap_; |
| 56 |
private final Map rxModelMap_; |
| 57 |
private final ListDataListener transListListener_; |
| 58 |
private final int listRemoveDelay_; |
| 59 |
private static final Logger logger_ = |
| 60 |
Logger.getLogger( MessageTrackerHubConnector.class.getName() ); |
| 61 |
|
| 62 |
/** |
| 63 |
* Constructs a hub connector with default message tracker GUI expiry times. |
| 64 |
* |
| 65 |
* @param profile profile implementation |
| 66 |
*/ |
| 67 |
public MessageTrackerHubConnector( ClientProfile profile ) { |
| 68 |
this( profile, 500, 20000, 100 ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Constructs a hub connector with specified message tracker GUI |
| 73 |
* expiry times. |
| 74 |
* The delay times are times in milliseconds after message resolution |
| 75 |
* before message representations expire and hence remove themselves |
| 76 |
* from gui components. |
| 77 |
* |
| 78 |
* @param profile profile implementation |
| 79 |
* @param listRemoveDelay expiry delay for summary icons in client |
| 80 |
* list display |
| 81 |
* @param tableRemoveDelay expiry delay for rows in message |
| 82 |
* table display |
| 83 |
* @param tableMaxRows maximum number of rows in message table |
| 84 |
* (beyond this limit resolved messages may be |
| 85 |
* removed early) |
| 86 |
*/ |
| 87 |
public MessageTrackerHubConnector( ClientProfile profile, |
| 88 |
int listRemoveDelay, |
| 89 |
int tableRemoveDelay, |
| 90 |
int tableMaxRows ) { |
| 91 |
super( profile ); |
| 92 |
listRemoveDelay_ = listRemoveDelay; |
| 93 |
transListListener_ = new ClientTransmissionListListener(); |
| 94 |
txListModel_ = new TransmissionListModel( listRemoveDelay_ ); |
| 95 |
rxListModel_ = new TransmissionListModel( listRemoveDelay_ ); |
| 96 |
txListModel_.addListDataListener( transListListener_ ); |
| 97 |
rxListModel_.addListDataListener( transListListener_ ); |
| 98 |
txTableModel_ = |
| 99 |
new TransmissionTableModel( false, true, |
| 100 |
tableRemoveDelay, tableMaxRows ); |
| 101 |
rxTableModel_ = |
| 102 |
new TransmissionTableModel( true, false, |
| 103 |
tableRemoveDelay, tableMaxRows ); |
| 104 |
callAllMap_ = new HashMap(); // access only from EDT |
| 105 |
txModelMap_ = new WeakHashMap(); |
| 106 |
rxModelMap_ = new WeakHashMap(); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns a ListModel representing the pending messages sent using |
| 111 |
* this connector. |
| 112 |
* Elements of the model are {@link Transmission} objects. |
| 113 |
* |
| 114 |
* @return transmission list model |
| 115 |
*/ |
| 116 |
public ListModel getTxListModel() { |
| 117 |
return txListModel_; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Returns a ListModel representing the pending messages received using |
| 122 |
* this connector. |
| 123 |
* Elements of the model are {@link Transmission} objects. |
| 124 |
* |
| 125 |
* @return transmission list model |
| 126 |
*/ |
| 127 |
public ListModel getRxListModel() { |
| 128 |
return rxListModel_; |
| 129 |
} |
| 130 |
|
| 131 |
public ListModel getTxListModel( Client client ) { |
| 132 |
if ( ! txModelMap_.containsKey( client ) ) { |
| 133 |
TransmissionListModel listModel = |
| 134 |
new TransmissionListModel( listRemoveDelay_ ); |
| 135 |
listModel.addListDataListener( transListListener_ ); |
| 136 |
txModelMap_.put( client, listModel ); |
| 137 |
} |
| 138 |
return (ListModel) txModelMap_.get( client ); |
| 139 |
} |
| 140 |
|
| 141 |
public ListModel getRxListModel( Client client ) { |
| 142 |
if ( ! rxModelMap_.containsKey( client ) ) { |
| 143 |
TransmissionListModel listModel = |
| 144 |
new TransmissionListModel( listRemoveDelay_ ); |
| 145 |
listModel.addListDataListener( transListListener_ ); |
| 146 |
rxModelMap_.put( client, listModel ); |
| 147 |
} |
| 148 |
return (ListModel) rxModelMap_.get( client ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Returns a component which displays messages currently being |
| 153 |
* sent/received by this connector. |
| 154 |
* |
| 155 |
* @return iconSize height of icons in box |
| 156 |
*/ |
| 157 |
public JComponent createMessageBox( int iconSize ) { |
| 158 |
JComponent box = |
| 159 |
createMessageBox( iconSize, rxListModel_, txListModel_ ); |
| 160 |
registerUpdater( box, ENABLE_COMPONENT ); |
| 161 |
return box; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Returns a component which displays messages in receiver and/or sender |
| 166 |
* list models. |
| 167 |
* |
| 168 |
* @param iconSize height of icons |
| 169 |
* @param rxListModel list model containing received |
| 170 |
* {@link Transmission} objects |
| 171 |
* @param txListModel list model containing sent |
| 172 |
* {@link Transmission} objects |
| 173 |
*/ |
| 174 |
public static JComponent createMessageBox( int iconSize, |
| 175 |
ListModel rxListModel, |
| 176 |
ListModel txListModel ) { |
| 177 |
final Color dtColor = UIManager.getColor( "Label.disabledText" ); |
| 178 |
JComponent box = new JPanel() { |
| 179 |
final Color enabledFg = getForeground(); |
| 180 |
final Color enabledBg = Color.WHITE; |
| 181 |
final Color disabledFg = null; |
| 182 |
final Color disabledBg = getBackground(); |
| 183 |
public void setEnabled( boolean enabled ) { |
| 184 |
super.setEnabled( enabled ); |
| 185 |
setForeground( enabled ? enabledFg : disabledFg ); |
| 186 |
setBackground( enabled ? enabledBg : disabledFg ); |
| 187 |
} |
| 188 |
}; |
| 189 |
box.setLayout( new BoxLayout( box, BoxLayout.X_AXIS ) ); |
| 190 |
if ( rxListModel != null ) { |
| 191 |
IconBox rxBox = new IconBox( iconSize ); |
| 192 |
rxBox.setOpaque( false ); |
| 193 |
rxBox.setTrailing( true ); |
| 194 |
rxBox.setModel( rxListModel ); |
| 195 |
rxBox.setRenderer( new TransmissionCellRenderer() { |
| 196 |
public String getToolTipText( IconBox iconBox, Object value, |
| 197 |
int index ) { |
| 198 |
if ( value instanceof Transmission ) { |
| 199 |
Transmission trans = (Transmission) value; |
| 200 |
return new StringBuffer() |
| 201 |
.append( trans.getMessage().getMType() ) |
| 202 |
.append( " <- " ) |
| 203 |
.append( trans.getSender().toString() ) |
| 204 |
.toString(); |
| 205 |
} |
| 206 |
else { |
| 207 |
return super.getToolTipText( iconBox, value, index ); |
| 208 |
} |
| 209 |
} |
| 210 |
} ); |
| 211 |
Dimension prefSize = rxBox.getPreferredSize(); |
| 212 |
prefSize.width = iconSize * 3; |
| 213 |
rxBox.setPreferredSize( prefSize ); |
| 214 |
box.add( rxBox ); |
| 215 |
} |
| 216 |
IconBox cBox = new IconBox( iconSize ); |
| 217 |
cBox.setOpaque( false ); |
| 218 |
cBox.setBorder( BorderFactory.createEmptyBorder( 0, 2, 0, 2 ) ); |
| 219 |
cBox.setModel( new AbstractListModel() { |
| 220 |
public int getSize() { |
| 221 |
return 1; |
| 222 |
} |
| 223 |
public Object getElementAt( int index ) { |
| 224 |
return "app"; |
| 225 |
} |
| 226 |
} ); |
| 227 |
cBox.setRenderer( new TransmissionCellRenderer() ); |
| 228 |
Dimension cSize = cBox.getPreferredSize(); |
| 229 |
cBox.setMaximumSize( cSize ); |
| 230 |
cBox.setMinimumSize( cSize ); |
| 231 |
box.add( cBox ); |
| 232 |
if ( txListModel != null ) { |
| 233 |
IconBox txBox = new IconBox( iconSize ); |
| 234 |
txBox.setOpaque( false ); |
| 235 |
txBox.setModel( txListModel ); |
| 236 |
txBox.setRenderer( new TransmissionCellRenderer() { |
| 237 |
public String getToolTipText( IconBox iconBox, Object value, |
| 238 |
int index ) { |
| 239 |
if ( value instanceof Transmission ) { |
| 240 |
Transmission trans = (Transmission) value; |
| 241 |
return new StringBuffer() |
| 242 |
.append( trans.getMessage().getMType() ) |
| 243 |
.append( " -> " ) |
| 244 |
.append( trans.getReceiver().toString() ) |
| 245 |
.toString(); |
| 246 |
} |
| 247 |
else { |
| 248 |
return super.getToolTipText( iconBox, value, index ); |
| 249 |
} |
| 250 |
} |
| 251 |
} ); |
| 252 |
Dimension prefSize = txBox.getPreferredSize(); |
| 253 |
prefSize.width = iconSize * 3; |
| 254 |
txBox.setPreferredSize( prefSize ); |
| 255 |
box.add( txBox ); |
| 256 |
} |
| 257 |
box.setBackground( Color.WHITE ); |
| 258 |
box.setBorder( createBoxBorder() ); |
| 259 |
return box; |
| 260 |
} |
| 261 |
|
| 262 |
public ListCellRenderer createClientListCellRenderer() { |
| 263 |
MessageTrackerListCellRenderer renderer = |
| 264 |
new MessageTrackerListCellRenderer( this ); |
| 265 |
renderer.setTransmissionCellRenderer( new TransmissionCellRenderer() { |
| 266 |
public String getToolTipText( IconBox iconBox, Object value, |
| 267 |
int index ) { |
| 268 |
return value instanceof Transmission |
| 269 |
? ((Transmission) value).getMessage().getMType() |
| 270 |
: super.getToolTipText( iconBox, value, index ); |
| 271 |
} |
| 272 |
} ); |
| 273 |
return renderer; |
| 274 |
} |
| 275 |
|
| 276 |
public JComponent createMonitorPanel() { |
| 277 |
JTabbedPane tabber = new JTabbedPane(); |
| 278 |
|
| 279 |
// Add client view tab. |
| 280 |
HubView hubView = new HubView( false ); |
| 281 |
hubView.setClientListModel( getClientListModel() ); |
| 282 |
hubView.getClientList() |
| 283 |
.setCellRenderer( createClientListCellRenderer() ); |
| 284 |
tabber.add( "Clients", hubView ); |
| 285 |
|
| 286 |
// Add received message tab. |
| 287 |
tabber.add( "Received Messages", |
| 288 |
new TransmissionView( rxTableModel_ ) ); |
| 289 |
|
| 290 |
// Add sent message tab. |
| 291 |
tabber.add( "Sent Messages", |
| 292 |
new TransmissionView( txTableModel_ ) ); |
| 293 |
|
| 294 |
// Position and return. |
| 295 |
JComponent panel = new JPanel( new BorderLayout() ); |
| 296 |
panel.add( tabber, BorderLayout.CENTER ); |
| 297 |
return panel; |
| 298 |
} |
| 299 |
|
| 300 |
protected HubConnection createConnection() throws SampException { |
| 301 |
HubConnection connection = super.createConnection(); |
| 302 |
return connection == null |
| 303 |
? null |
| 304 |
: new MessageTrackerHubConnection( connection ); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Schedules a new transmission to add to the appropriate list models. |
| 309 |
* May be called from any thread. |
| 310 |
* |
| 311 |
* @param trans transmission |
| 312 |
* @param tx true for send, false for receive |
| 313 |
*/ |
| 314 |
private void scheduleAddTransmission( final Transmission trans, |
| 315 |
final boolean tx ) { |
| 316 |
SwingUtilities.invokeLater( new Runnable() { |
| 317 |
public void run() { |
| 318 |
( tx ? txTableModel_ |
| 319 |
: rxTableModel_ ).addTransmission( trans ); |
| 320 |
((TransmissionListModel) getTxListModel( trans.getSender() )) |
| 321 |
.addTransmission( trans ); |
| 322 |
((TransmissionListModel) getRxListModel( trans.getReceiver() )) |
| 323 |
.addTransmission( trans ); |
| 324 |
} |
| 325 |
} ); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Schedules a response to be registered for a previously added |
| 330 |
* transmission. |
| 331 |
* May be called from any thread. |
| 332 |
* |
| 333 |
* @param trans transmission |
| 334 |
* @param response response to associated with trans |
| 335 |
*/ |
| 336 |
private void scheduleSetResponse( final Transmission trans, |
| 337 |
final Response response ) { |
| 338 |
SwingUtilities.invokeLater( new Runnable() { |
| 339 |
public void run() { |
| 340 |
trans.setResponse( response ); |
| 341 |
} |
| 342 |
} ); |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Schedules an error to be registered for a previously added |
| 347 |
* transmission. |
| 348 |
* May be called from any thread. |
| 349 |
* |
| 350 |
* @param trans transmission |
| 351 |
* @param error exception |
| 352 |
*/ |
| 353 |
private void scheduleSetFailure( final Transmission trans, |
| 354 |
final Throwable error ) { |
| 355 |
SwingUtilities.invokeLater( new Runnable() { |
| 356 |
public void run() { |
| 357 |
trans.setError( error ); |
| 358 |
} |
| 359 |
} ); |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* HubConnection object which intercepts calls to keep track of |
| 364 |
* outgoing and incoming messages. |
| 365 |
*/ |
| 366 |
private class MessageTrackerHubConnection extends WrapperHubConnection { |
| 367 |
private Client selfClient_; |
| 368 |
|
| 369 |
/** |
| 370 |
* Constructor. |
| 371 |
* |
| 372 |
* @param base connection on which this one is based |
| 373 |
*/ |
| 374 |
MessageTrackerHubConnection( HubConnection base ) { |
| 375 |
super( base ); |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Returns a Client object for use in Transmission objects |
| 380 |
* which represents this connection's owner. |
| 381 |
* This has to be the same object as is used in the client set, |
| 382 |
* otherwise the various models don't get updated correctly. |
| 383 |
* For this reason, it has to be obtained lazily, after the client set |
| 384 |
* has been initialised. |
| 385 |
* |
| 386 |
* @return self client object |
| 387 |
*/ |
| 388 |
Client getSelfClient() { |
| 389 |
if ( selfClient_ == null ) { |
| 390 |
selfClient_ = |
| 391 |
(Client) getClientMap().get( getRegInfo().getSelfId() ); |
| 392 |
assert selfClient_ != null; |
| 393 |
txModelMap_.put( selfClient_, txListModel_ ); |
| 394 |
rxModelMap_.put( selfClient_, rxListModel_ ); |
| 395 |
} |
| 396 |
return selfClient_; |
| 397 |
} |
| 398 |
|
| 399 |
public void notify( final String recipientId, final Map msg ) |
| 400 |
throws SampException { |
| 401 |
|
| 402 |
// Construct a transmission corresponding to this notify and |
| 403 |
// add it to the send list. |
| 404 |
Client recipient = (Client) getClientMap().get( recipientId ); |
| 405 |
Transmission trans = recipient == null |
| 406 |
? null |
| 407 |
: new Transmission( getSelfClient(), recipient, |
| 408 |
Message.asMessage( msg ), |
| 409 |
null, null ); |
| 410 |
if ( trans != null ) { |
| 411 |
scheduleAddTransmission( trans, true ); |
| 412 |
} |
| 413 |
|
| 414 |
// Do the actual send. |
| 415 |
try { |
| 416 |
super.notify( recipientId, msg ); |
| 417 |
|
| 418 |
// Notify won't generate a response, so signal that now. |
| 419 |
if ( trans != null ) { |
| 420 |
scheduleSetResponse( trans, null ); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
// If the send failed, signal it. |
| 425 |
catch ( SampException e ) { |
| 426 |
if ( trans != null ) { |
| 427 |
scheduleSetFailure( trans, e ); |
| 428 |
} |
| 429 |
throw e; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
public List notifyAll( Map msg ) throws SampException { |
| 434 |
|
| 435 |
// Do the send. |
| 436 |
List recipientIdList = super.notifyAll( msg ); |
| 437 |
|
| 438 |
// Construct a list of transmissions corresponding to this notify |
| 439 |
// and add them to the send list. |
| 440 |
final List transList = new ArrayList(); |
| 441 |
Message message = Message.asMessage( msg ); |
| 442 |
Client sender = getSelfClient(); |
| 443 |
for ( Iterator it = recipientIdList.iterator(); it.hasNext(); ) { |
| 444 |
Client recipient = |
| 445 |
(Client) getClientMap().get( (String) it.next() ); |
| 446 |
if ( recipient != null ) { |
| 447 |
Transmission trans = |
| 448 |
new Transmission( sender, recipient, message, |
| 449 |
null, null ); |
| 450 |
scheduleAddTransmission( trans, true ); |
| 451 |
|
| 452 |
// Notify won't generate a response, so signal that now. |
| 453 |
scheduleSetResponse( trans, null ); |
| 454 |
} |
| 455 |
} |
| 456 |
return recipientIdList; |
| 457 |
} |
| 458 |
|
| 459 |
public String call( String recipientId, String msgTag, Map msg ) |
| 460 |
throws SampException { |
| 461 |
|
| 462 |
// Construct a transmission corresponding to this call |
| 463 |
// and add it to the send list. |
| 464 |
Client recipient = (Client) getClientMap().get( recipientId ); |
| 465 |
Transmission trans = recipient == null |
| 466 |
? null |
| 467 |
: new Transmission( getSelfClient(), recipient, |
| 468 |
Message.asMessage( msg ), |
| 469 |
msgTag, null ); |
| 470 |
if ( trans != null ) { |
| 471 |
scheduleAddTransmission( trans, true ); |
| 472 |
} |
| 473 |
|
| 474 |
// Do the actual call. |
| 475 |
try { |
| 476 |
return super.call( recipientId, msgTag, msg ); |
| 477 |
} |
| 478 |
|
| 479 |
// If the send failed, signal that since no reply will be |
| 480 |
// forthcoming. |
| 481 |
catch ( final SampException e ) { |
| 482 |
scheduleSetFailure( trans, e ); |
| 483 |
throw e; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
public Map callAll( final String msgTag, Map msg ) |
| 488 |
throws SampException { |
| 489 |
|
| 490 |
// This is a bit more complicated than the other cases. |
| 491 |
// We can't construct the list of transmissions before the send, |
| 492 |
// since we don't know which are the recipient clients. |
| 493 |
// But if we wait until after the delegated callAll() method |
| 494 |
// we may miss some early responses to it. So we have to |
| 495 |
// put in place a mechanism for dealing with responses before |
| 496 |
// we know exactly what they are responses to. |
| 497 |
// Prepare and store a CallAllHandler for this. |
| 498 |
final CallAllHandler cah = new CallAllHandler( msgTag ); |
| 499 |
SwingUtilities.invokeLater( new Runnable() { |
| 500 |
public void run() { |
| 501 |
callAllMap_.put( msgTag, cah ); |
| 502 |
} |
| 503 |
} ); |
| 504 |
|
| 505 |
// Do the actual call. |
| 506 |
Map callMap = super.callAll( msgTag, msg ); |
| 507 |
|
| 508 |
// Prepare a post-facto list of the transmissions which were sent. |
| 509 |
List transList = new ArrayList(); |
| 510 |
Message message = Message.asMessage( msg ); |
| 511 |
for ( Iterator it = callMap.entrySet().iterator(); it.hasNext(); ) { |
| 512 |
Map.Entry entry = (Map.Entry) it.next(); |
| 513 |
String recipientId = (String) entry.getKey(); |
| 514 |
Client sender = getSelfClient(); |
| 515 |
Client recipient = (Client) getClientMap().get( recipientId ); |
| 516 |
if ( recipient != null ) { |
| 517 |
String msgId = (String) entry.getValue(); |
| 518 |
Transmission trans = |
| 519 |
new Transmission( sender, recipient, message, |
| 520 |
msgTag, msgId ); |
| 521 |
scheduleAddTransmission( trans, true ); |
| 522 |
transList.add( trans ); |
| 523 |
} |
| 524 |
} |
| 525 |
final Transmission[] transmissions = |
| 526 |
(Transmission[]) transList.toArray( new Transmission[ 0 ] ); |
| 527 |
|
| 528 |
// And inform the CallAllHandler what the transmissions were, so |
| 529 |
// it knows how to process (possibly already received) responses. |
| 530 |
SwingUtilities.invokeLater( new Runnable() { |
| 531 |
public void run() { |
| 532 |
cah.setTransmissions( transmissions ); |
| 533 |
} |
| 534 |
} ); |
| 535 |
return callMap; |
| 536 |
} |
| 537 |
|
| 538 |
public Response callAndWait( String recipientId, Map msg, |
| 539 |
int timeout ) throws SampException { |
| 540 |
|
| 541 |
// Construct a transmission obejct corresponding to this call |
| 542 |
// and add it to the send list. |
| 543 |
Client recipient = (Client) getClientMap().get( recipientId ); |
| 544 |
Transmission trans = |
| 545 |
recipient == null |
| 546 |
? null |
| 547 |
: new Transmission( getSelfClient(), recipient, |
| 548 |
Message.asMessage( msg ), |
| 549 |
"<synchronous>", |
| 550 |
"<synchronous>" ); |
| 551 |
if ( trans != null ) { |
| 552 |
scheduleAddTransmission( trans, true ); |
| 553 |
} |
| 554 |
|
| 555 |
// Do the actual call. |
| 556 |
try { |
| 557 |
Response response = |
| 558 |
super.callAndWait( recipientId, msg, timeout ); |
| 559 |
|
| 560 |
// Inform the transmission of the response. |
| 561 |
if ( trans != null ) { |
| 562 |
scheduleSetResponse( trans, response ); |
| 563 |
} |
| 564 |
return response; |
| 565 |
} |
| 566 |
|
| 567 |
// In case of error, inform the transmission of failure. |
| 568 |
catch ( SampException e ) { |
| 569 |
if ( trans != null ) { |
| 570 |
scheduleSetFailure( trans, e ); |
| 571 |
} |
| 572 |
throw e; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
public void reply( final String msgId, final Map response ) |
| 577 |
throws SampException { |
| 578 |
|
| 579 |
// Do the actual reply. |
| 580 |
Throwable err; |
| 581 |
try { |
| 582 |
super.reply( msgId, response ); |
| 583 |
err = null; |
| 584 |
} |
| 585 |
catch ( Throwable e ) { |
| 586 |
err = e; |
| 587 |
} |
| 588 |
final Throwable error = err; |
| 589 |
|
| 590 |
// Inform the existing transmission on the receive list |
| 591 |
// that the reply has been made. |
| 592 |
SwingUtilities.invokeLater( new Runnable() { |
| 593 |
public void run() { |
| 594 |
int nt = rxListModel_.getSize(); |
| 595 |
for ( int i = 0; i < nt; i++ ) { |
| 596 |
Transmission trans = |
| 597 |
(Transmission) rxListModel_.getElementAt( i ); |
| 598 |
if ( msgId.equals( trans.getMessageId() ) ) { |
| 599 |
trans.setResponse( Response |
| 600 |
.asResponse( response ) ); |
| 601 |
if ( error != null ) { |
| 602 |
trans.setError( error ); |
| 603 |
} |
| 604 |
return; |
| 605 |
} |
| 606 |
} |
| 607 |
logger_.warning( "Orphan reply " + msgId |
| 608 |
+ " - replier programming error?" ); |
| 609 |
} |
| 610 |
} ); |
| 611 |
} |
| 612 |
|
| 613 |
public void setCallable( CallableClient callable ) |
| 614 |
throws SampException { |
| 615 |
|
| 616 |
// Install a wrapper-like callable client which can intercept |
| 617 |
// the calls to keep track of send/received messages. |
| 618 |
CallableClient mtCallable = |
| 619 |
new MessageTrackerCallableClient( callable, this ); |
| 620 |
super.setCallable( mtCallable ); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
/** |
| 625 |
* CallableClient wrapper class which intercepts calls to keep track |
| 626 |
* of sent and received messages. |
| 627 |
*/ |
| 628 |
private class MessageTrackerCallableClient implements CallableClient { |
| 629 |
private final CallableClient base_; |
| 630 |
private final MessageTrackerHubConnection connection_; |
| 631 |
|
| 632 |
/** |
| 633 |
* Constructor. |
| 634 |
* |
| 635 |
* @param base base callable |
| 636 |
* @param connection hub connection |
| 637 |
*/ |
| 638 |
MessageTrackerCallableClient( CallableClient base, |
| 639 |
MessageTrackerHubConnection connection ) { |
| 640 |
base_ = base; |
| 641 |
connection_ = connection; |
| 642 |
} |
| 643 |
|
| 644 |
/** |
| 645 |
* Returns a Client object for use in Transmission objects |
| 646 |
* which represents this connection's owner. |
| 647 |
* |
| 648 |
* @return self client object |
| 649 |
*/ |
| 650 |
private Client getSelfClient() { |
| 651 |
return connection_.getSelfClient(); |
| 652 |
} |
| 653 |
|
| 654 |
public void receiveCall( String senderId, String msgId, Message msg ) |
| 655 |
throws Exception { |
| 656 |
|
| 657 |
// Construct a transmission corresponding to the incoming call |
| 658 |
// and add it to the receive list. |
| 659 |
Client sender = (Client) getClientMap().get( senderId ); |
| 660 |
Transmission trans = |
| 661 |
sender == null ? null |
| 662 |
: new Transmission( sender, getSelfClient(), msg, |
| 663 |
null, msgId ); |
| 664 |
if ( trans != null ) { |
| 665 |
scheduleAddTransmission( trans, false ); |
| 666 |
} |
| 667 |
|
| 668 |
// Actually handle the call. |
| 669 |
try { |
| 670 |
base_.receiveCall( senderId, msgId, msg ); |
| 671 |
} |
| 672 |
|
| 673 |
// If the call handler fails, inform the transmission. |
| 674 |
catch ( Exception e ) { |
| 675 |
scheduleSetFailure( trans, e ); |
| 676 |
throw e; |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
public void receiveNotification( String senderId, Message msg ) |
| 681 |
throws Exception { |
| 682 |
Client sender = (Client) getClientMap().get( senderId ); |
| 683 |
|
| 684 |
// Actually handle the notification. |
| 685 |
base_.receiveNotification( senderId, msg ); |
| 686 |
|
| 687 |
// Construct a transmission corresponding to the incoming |
| 688 |
// notification and add it to the receive list. |
| 689 |
// Give it a null response immediately, since being a notify |
| 690 |
// it won't get another one. |
| 691 |
if ( sender != null ) { |
| 692 |
final Transmission trans = |
| 693 |
new Transmission( sender, getSelfClient(), msg, |
| 694 |
null, null ); |
| 695 |
scheduleAddTransmission( trans, false ); |
| 696 |
scheduleSetResponse( trans, null ); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
public void receiveResponse( final String responderId, |
| 701 |
final String msgTag, |
| 702 |
final Response response ) |
| 703 |
throws Exception { |
| 704 |
|
| 705 |
// Actually handle the response. |
| 706 |
base_.receiveResponse( responderId, msgTag, response ); |
| 707 |
|
| 708 |
// Update state of the send list. |
| 709 |
// This isn't foolproof - if a sender has re-used the same msgTag |
| 710 |
// for a call and a callAll this handling might get confused - |
| 711 |
// but then so would the sender. |
| 712 |
SwingUtilities.invokeLater( new Runnable() { |
| 713 |
public void run() { |
| 714 |
|
| 715 |
// If the message was sent using callAll, handle using |
| 716 |
// the registered CallAllHandler. |
| 717 |
CallAllHandler cah = |
| 718 |
(CallAllHandler) callAllMap_.get( msgTag ); |
| 719 |
if ( cah != null ) { |
| 720 |
cah.addResponse( responderId, response ); |
| 721 |
} |
| 722 |
|
| 723 |
// Otherwise find the relevant Transmission in the |
| 724 |
// send list and inform it of the response. |
| 725 |
else { |
| 726 |
int nt = txListModel_.getSize(); |
| 727 |
for ( int i = 0; i < nt; i++ ) { |
| 728 |
Transmission trans = |
| 729 |
(Transmission) txListModel_.getElementAt( i ); |
| 730 |
if ( responderId.equals( trans.getReceiver() |
| 731 |
.getId() ) && |
| 732 |
msgTag.equals( trans.getMessageTag() ) ) { |
| 733 |
trans.setResponse( response ); |
| 734 |
return; |
| 735 |
} |
| 736 |
} |
| 737 |
logger_.warning( "Orphan reply " + msgTag |
| 738 |
+ " - possible hub error?" ); |
| 739 |
} |
| 740 |
} |
| 741 |
} ); |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
/** |
| 746 |
* Class used to keep track of outgoing callAll() messages. |
| 747 |
* It needs to be able to match Responses with Transmissions, |
| 748 |
* but the complication is that a Response may arrive either before |
| 749 |
* or after its corresponding Transmission is known. |
| 750 |
*/ |
| 751 |
private class CallAllHandler { |
| 752 |
private final String msgTag_; |
| 753 |
private final Map responseMap_; |
| 754 |
private Collection transSet_; |
| 755 |
|
| 756 |
/** |
| 757 |
* Constructor. |
| 758 |
* |
| 759 |
* @param msgTag message tag labelling the callAll send |
| 760 |
*/ |
| 761 |
CallAllHandler( String msgTag ) { |
| 762 |
msgTag_ = msgTag; |
| 763 |
responseMap_ = new HashMap(); |
| 764 |
} |
| 765 |
|
| 766 |
/** |
| 767 |
* Called once when the list of transmissions corresponding to the |
| 768 |
* callAll invocation is known. |
| 769 |
* |
| 770 |
* @param transmissions list of transmission objects, one for each |
| 771 |
* callAll recipient |
| 772 |
*/ |
| 773 |
public void setTransmissions( Transmission[] transmissions ) { |
| 774 |
|
| 775 |
// Store transmissions for later. |
| 776 |
if ( transSet_ != null ) { |
| 777 |
throw new IllegalStateException(); |
| 778 |
} |
| 779 |
transSet_ = new HashSet( Arrays.asList( transmissions ) ); |
| 780 |
|
| 781 |
// Process any responses already in. |
| 782 |
for ( Iterator it = responseMap_.entrySet().iterator(); |
| 783 |
it.hasNext(); ) { |
| 784 |
Map.Entry entry = (Map.Entry) it.next(); |
| 785 |
String responderId = (String) entry.getKey(); |
| 786 |
Response response = (Response) entry.getValue(); |
| 787 |
processResponse( responderId, response ); |
| 788 |
} |
| 789 |
retireIfDone(); |
| 790 |
} |
| 791 |
|
| 792 |
/** |
| 793 |
* Supplies a response to the callAll invocation handled by this object. |
| 794 |
* |
| 795 |
* @param responderId client ID of responder |
| 796 |
* @param response response |
| 797 |
*/ |
| 798 |
public void addResponse( String responderId, Response response ) { |
| 799 |
|
| 800 |
// If we know what transmissions have been sent, we can process |
| 801 |
// this response directly. |
| 802 |
if ( transSet_ != null ) { |
| 803 |
processResponse( responderId, response ); |
| 804 |
retireIfDone(); |
| 805 |
} |
| 806 |
|
| 807 |
// Otherwise store the response and defer processing until we do. |
| 808 |
else { |
| 809 |
responseMap_.put( responderId, response ); |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
* Does the work of passing a received response to the relevant |
| 815 |
* member of the transmission list. |
| 816 |
* May only be called following {@link #setTransmissions}. |
| 817 |
* |
| 818 |
* @param responderId client ID of responder |
| 819 |
* @param response response |
| 820 |
*/ |
| 821 |
private void processResponse( String responderId, Response response ) { |
| 822 |
assert transSet_ != null; |
| 823 |
for ( Iterator it = transSet_.iterator(); it.hasNext(); ) { |
| 824 |
Transmission trans = (Transmission) it.next(); |
| 825 |
if ( trans.getReceiver().getId().equals( responderId ) ) { |
| 826 |
trans.setResponse( response ); |
| 827 |
it.remove(); |
| 828 |
return; |
| 829 |
} |
| 830 |
} |
| 831 |
logger_.warning( "Orphan reply " + msgTag_ |
| 832 |
+ " - possible hub error?" ); |
| 833 |
} |
| 834 |
|
| 835 |
/** |
| 836 |
* Checks whether this object has any further work to do |
| 837 |
* (any more responses are expected) and if not uninstalls itself, |
| 838 |
* at which point it becomes unreachable and can be garbage collected. |
| 839 |
* May only be called following {@link #setTransmissions}. |
| 840 |
*/ |
| 841 |
private void retireIfDone() { |
| 842 |
assert transSet_ != null; |
| 843 |
if ( transSet_.isEmpty() ) { |
| 844 |
assert callAllMap_.containsKey( msgTag_ ); |
| 845 |
callAllMap_.remove( msgTag_ ); |
| 846 |
} |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
/** |
| 851 |
* ListDataListener implementation which responds to transmission list |
| 852 |
* events and passes them on to the client list, since any changes to |
| 853 |
* the transmission list may change the way that a client is renderered |
| 854 |
* in the JList. |
| 855 |
*/ |
| 856 |
private class ClientTransmissionListListener implements ListDataListener { |
| 857 |
|
| 858 |
public void contentsChanged( ListDataEvent evt ) { |
| 859 |
transmissionChanged( evt ); |
| 860 |
} |
| 861 |
|
| 862 |
public void intervalAdded( ListDataEvent evt ) { |
| 863 |
transmissionChanged( evt ); |
| 864 |
} |
| 865 |
|
| 866 |
public void intervalRemoved( ListDataEvent evt ) { |
| 867 |
transmissionChanged( evt ); |
| 868 |
} |
| 869 |
|
| 870 |
/** |
| 871 |
* Called when there is any change to a known transmission. |
| 872 |
*/ |
| 873 |
private void transmissionChanged( ListDataEvent evt ) { |
| 874 |
Object src = evt.getSource(); |
| 875 |
assert src instanceof Transmission; |
| 876 |
if ( src instanceof Transmission ) { |
| 877 |
Transmission trans = (Transmission) src; |
| 878 |
TrackedClientSet clientSet = getClientSet(); |
| 879 |
clientSet.updateClient( trans.getReceiver(), false, false ); |
| 880 |
clientSet.updateClient( trans.getSender(), false, false ); |
| 881 |
} |
| 882 |
} |
| 883 |
} |
| 884 |
} |