| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.BorderLayout; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Comparator; |
| 6 |
import java.util.HashMap; |
| 7 |
import java.util.HashSet; |
| 8 |
import java.util.List; |
| 9 |
import java.util.Map; |
| 10 |
import java.util.Random; |
| 11 |
import javax.swing.ImageIcon; |
| 12 |
import javax.swing.ListModel; |
| 13 |
import javax.swing.ListSelectionModel; |
| 14 |
import javax.swing.JComponent; |
| 15 |
import javax.swing.JList; |
| 16 |
import javax.swing.JFrame; |
| 17 |
import javax.swing.JPanel; |
| 18 |
import javax.swing.JTabbedPane; |
| 19 |
import javax.swing.SwingUtilities; |
| 20 |
import javax.swing.event.ListDataEvent; |
| 21 |
import javax.swing.event.ListDataListener; |
| 22 |
import org.astrogrid.samp.Client; |
| 23 |
import org.astrogrid.samp.Message; |
| 24 |
import org.astrogrid.samp.Response; |
| 25 |
import org.astrogrid.samp.client.CallableClient; |
| 26 |
import org.astrogrid.samp.client.SampException; |
| 27 |
import org.astrogrid.samp.hub.ClientSet; |
| 28 |
import org.astrogrid.samp.hub.HubClient; |
| 29 |
import org.astrogrid.samp.hub.ProfileToken; |
| 30 |
|
| 31 |
/** |
| 32 |
* GuiHubService subclass which additionally keeps track of which messages |
| 33 |
* have been sent and received, and can provide a graphical display of these. |
| 34 |
* The overhead in maintaining the GUI display can be significant if there is |
| 35 |
* high volume of message traffic. |
| 36 |
* |
| 37 |
* @author Mark Taylor |
| 38 |
* @since 20 Nov 2008 |
| 39 |
*/ |
| 40 |
public class MessageTrackerHubService extends GuiHubService |
| 41 |
implements ClientTransmissionHolder { |
| 42 |
|
| 43 |
private final CallMap callMap_; |
| 44 |
private final TransmissionTableModel transTableModel_; |
| 45 |
private final int listRemoveDelay_; |
| 46 |
private final int tableRemoveDelay_; |
| 47 |
private final int tableMaxRows_; |
| 48 |
private MessageTrackerClientSet clientSet_; |
| 49 |
private ListSelectionModel selectionModel_; |
| 50 |
|
| 51 |
/** |
| 52 |
* Constructs a hub service with default message tracker GUI expiry times. |
| 53 |
* |
| 54 |
* @param random random number generator |
| 55 |
*/ |
| 56 |
public MessageTrackerHubService( Random random ) { |
| 57 |
this( random, 0, 20000, 100 ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Constructs a hub service with specified message tracker GUI expiry times. |
| 62 |
* The delay times are times in milliseconds after message resolution |
| 63 |
* before message representations expire and hence remove themselves |
| 64 |
* from gui components. |
| 65 |
* |
| 66 |
* @param random random number generator |
| 67 |
* @param listRemoveDelay expiry delay for summary icons in client |
| 68 |
* list display |
| 69 |
* @param tableRemoveDelay expiry delay for rows in message |
| 70 |
* table display |
| 71 |
* @param tableMaxRows maximum number of rows in message table |
| 72 |
* (beyond this limit resolved messages may be |
| 73 |
* removed early) |
| 74 |
*/ |
| 75 |
public MessageTrackerHubService( Random random, int listRemoveDelay, |
| 76 |
int tableRemoveDelay, int tableMaxRows ) { |
| 77 |
super( random ); |
| 78 |
listRemoveDelay_ = listRemoveDelay; |
| 79 |
tableRemoveDelay_ = tableRemoveDelay; |
| 80 |
tableMaxRows_ = tableMaxRows; |
| 81 |
callMap_ = new CallMap(); // access only from EDT; |
| 82 |
transTableModel_ = |
| 83 |
new TransmissionTableModel( true, true, tableRemoveDelay_, |
| 84 |
tableMaxRows_ ); |
| 85 |
} |
| 86 |
|
| 87 |
public void start() { |
| 88 |
super.start(); |
| 89 |
clientSet_ = (MessageTrackerClientSet) getClientSet(); |
| 90 |
} |
| 91 |
|
| 92 |
public ClientSet createClientSet() { |
| 93 |
return new MessageTrackerClientSet( getIdComparator() ); |
| 94 |
} |
| 95 |
|
| 96 |
public HubClient createClient( String publicId, ProfileToken ptoken ) { |
| 97 |
return new MessageTrackerHubClient( publicId, ptoken ); |
| 98 |
} |
| 99 |
|
| 100 |
public JComponent createHubPanel() { |
| 101 |
JTabbedPane tabber = new JTabbedPane(); |
| 102 |
|
| 103 |
// Add client view tab. |
| 104 |
HubView hubView = new HubView( true ); |
| 105 |
hubView.setClientListModel( getClientListModel() ); |
| 106 |
JList jlist = hubView.getClientList(); |
| 107 |
jlist.setCellRenderer( new MessageTrackerListCellRenderer( this ) ); |
| 108 |
jlist.addMouseListener( new HubClientPopupListener( this ) ); |
| 109 |
selectionModel_ = jlist.getSelectionModel(); |
| 110 |
tabber.add( "Clients", hubView ); |
| 111 |
|
| 112 |
// Add messages tab. |
| 113 |
tabber.add( "Messages", new TransmissionView( transTableModel_ ) ); |
| 114 |
|
| 115 |
// Position and return. |
| 116 |
JComponent panel = new JPanel( new BorderLayout() ); |
| 117 |
panel.add( tabber, BorderLayout.CENTER ); |
| 118 |
return panel; |
| 119 |
} |
| 120 |
|
| 121 |
public ListSelectionModel getClientSelectionModel() { |
| 122 |
return selectionModel_; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Returns a ListModel representing the pending messages sent |
| 127 |
* from a given client. |
| 128 |
* Elements of the model are {@link Transmission} objects. |
| 129 |
* |
| 130 |
* @param client client owned by this hub service |
| 131 |
* @return transmission list model |
| 132 |
*/ |
| 133 |
public ListModel getTxListModel( Client client ) { |
| 134 |
return client instanceof MessageTrackerHubClient |
| 135 |
? ((MessageTrackerHubClient) client).txListModel_ |
| 136 |
: null; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Returns a ListModel representing the pending messages received |
| 141 |
* by a given client. |
| 142 |
* Elements of the model are {@link Transmission} objects. |
| 143 |
* |
| 144 |
* @param client client owned by this hub service |
| 145 |
* @return transmission list model |
| 146 |
*/ |
| 147 |
public ListModel getRxListModel( Client client ) { |
| 148 |
return client instanceof MessageTrackerHubClient |
| 149 |
? ((MessageTrackerHubClient) client).rxListModel_ |
| 150 |
: null; |
| 151 |
} |
| 152 |
|
| 153 |
protected void reply( HubClient caller, String msgId, final Map response ) |
| 154 |
throws SampException { |
| 155 |
|
| 156 |
// Notify the transmission object corresponding to this response |
| 157 |
// that the response has been received. |
| 158 |
final Object callKey = getCallKey( caller, msgId ); |
| 159 |
SwingUtilities.invokeLater( new Runnable() { |
| 160 |
public void run() { |
| 161 |
Transmission trans = callMap_.remove( callKey ); |
| 162 |
if ( trans != null ) { |
| 163 |
trans.setResponse( Response.asResponse( response ) ); |
| 164 |
} |
| 165 |
} |
| 166 |
} ); |
| 167 |
|
| 168 |
// Forward the call to the base implementation. |
| 169 |
super.reply( caller, msgId, response ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Registers a newly created transmission with internal data models |
| 174 |
* as required. |
| 175 |
* |
| 176 |
* @param trans new transmission to track |
| 177 |
*/ |
| 178 |
private void addTransmission( Transmission trans ) { |
| 179 |
((MessageTrackerHubClient) trans.getSender()) |
| 180 |
.txListModel_.addTransmission( trans ); |
| 181 |
((MessageTrackerHubClient) trans.getReceiver()) |
| 182 |
.rxListModel_.addTransmission( trans ); |
| 183 |
transTableModel_.addTransmission( trans ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Returns a key for use in the call map. |
| 188 |
* Identifies a call/response mode transmission. |
| 189 |
* |
| 190 |
* @param receiver message receiver |
| 191 |
* @param msgId message ID |
| 192 |
*/ |
| 193 |
private static Object getCallKey( Client receiver, String msgId ) { |
| 194 |
return new StringBuffer() |
| 195 |
.append( msgId ) |
| 196 |
.append( "->" ) |
| 197 |
.append( receiver.getId() ) |
| 198 |
.toString(); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* HubClient class used by this HubService implementation. |
| 203 |
*/ |
| 204 |
private class MessageTrackerHubClient extends HubClient { |
| 205 |
final TransmissionListModel rxListModel_; |
| 206 |
final TransmissionListModel txListModel_; |
| 207 |
|
| 208 |
/** |
| 209 |
* Constructor. |
| 210 |
* |
| 211 |
* @param publicId public ID |
| 212 |
* @param ptoken connection source |
| 213 |
*/ |
| 214 |
public MessageTrackerHubClient( String publicId, ProfileToken ptoken ) { |
| 215 |
super( publicId, ptoken ); |
| 216 |
|
| 217 |
// Prepare list models for the transmissions sent/received by |
| 218 |
// a given client. These models are updated as the hub forwards |
| 219 |
// messages and responses. The contents of these models are |
| 220 |
// Transmission objects. |
| 221 |
txListModel_ = new TransmissionListModel( listRemoveDelay_ ); |
| 222 |
rxListModel_ = new TransmissionListModel( listRemoveDelay_ ); |
| 223 |
} |
| 224 |
|
| 225 |
public void setCallable( CallableClient callable ) { |
| 226 |
super.setCallable( callable == null |
| 227 |
? null |
| 228 |
: new MessageTrackerCallableClient( callable, this ) ); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Wrapper implementation for the CallableClient class which intercepts |
| 234 |
* calls to update sent and received transmission list models. |
| 235 |
*/ |
| 236 |
private class MessageTrackerCallableClient implements CallableClient { |
| 237 |
private final CallableClient base_; |
| 238 |
private final MessageTrackerHubClient client_; |
| 239 |
|
| 240 |
/** |
| 241 |
* Constructor. |
| 242 |
* |
| 243 |
* @param base callable on which this one is based |
| 244 |
* @param client hub client for which this receiver is operating |
| 245 |
*/ |
| 246 |
MessageTrackerCallableClient( CallableClient base, |
| 247 |
MessageTrackerHubClient client ) { |
| 248 |
base_ = base; |
| 249 |
client_ = client; |
| 250 |
} |
| 251 |
|
| 252 |
public void receiveCall( String senderId, String msgId, Message msg ) |
| 253 |
throws SampException { |
| 254 |
|
| 255 |
// When a call is received, create a corresponding Transmission |
| 256 |
// object and add it to both the send list of the sender and |
| 257 |
// the receive list of the recipient. |
| 258 |
MessageTrackerHubClient sender = |
| 259 |
(MessageTrackerHubClient) |
| 260 |
clientSet_.getFromPublicId( senderId ); |
| 261 |
MessageTrackerHubClient recipient = client_; |
| 262 |
final Transmission trans = |
| 263 |
new Transmission( sender, recipient, msg, null, msgId ); |
| 264 |
final Object callKey = getCallKey( recipient, msgId ); |
| 265 |
SwingUtilities.invokeLater( new Runnable() { |
| 266 |
public void run() { |
| 267 |
callMap_.add( callKey, trans ); |
| 268 |
addTransmission( trans ); |
| 269 |
} |
| 270 |
} ); |
| 271 |
|
| 272 |
// Forward the call to the base implementation. |
| 273 |
try { |
| 274 |
base_.receiveCall( senderId, msgId, msg ); |
| 275 |
} |
| 276 |
catch ( final Exception e ) { |
| 277 |
SwingUtilities.invokeLater( new Runnable() { |
| 278 |
public void run() { |
| 279 |
trans.setError( e ); |
| 280 |
} |
| 281 |
} ); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
public void receiveNotification( String senderId, Message msg ) |
| 286 |
throws SampException { |
| 287 |
|
| 288 |
// When a notification is received, create a corresponding |
| 289 |
// Transmission object and add it to both the send list of the |
| 290 |
// sender and the receive list of the recipient. |
| 291 |
MessageTrackerHubClient sender = |
| 292 |
(MessageTrackerHubClient) |
| 293 |
clientSet_.getFromPublicId( senderId ); |
| 294 |
MessageTrackerHubClient recipient = client_; |
| 295 |
final Transmission trans = |
| 296 |
new Transmission( sender, recipient, msg, null, null ); |
| 297 |
SwingUtilities.invokeLater( new Runnable() { |
| 298 |
public void run() { |
| 299 |
addTransmission( trans ); |
| 300 |
} |
| 301 |
} ); |
| 302 |
|
| 303 |
// Forward the call to the base implementation. |
| 304 |
Exception error; |
| 305 |
try { |
| 306 |
base_.receiveNotification( senderId, msg ); |
| 307 |
error = null; |
| 308 |
} |
| 309 |
catch ( Exception e ) { |
| 310 |
error = e; |
| 311 |
} |
| 312 |
|
| 313 |
// Since it's a notify, no response will be forthcoming. |
| 314 |
// So signal a no-response (or send failure) directly. |
| 315 |
final Throwable err2 = error; |
| 316 |
SwingUtilities.invokeLater( new Runnable() { |
| 317 |
public void run() { |
| 318 |
if ( err2 == null ) { |
| 319 |
trans.setResponse( null ); |
| 320 |
} |
| 321 |
else { |
| 322 |
trans.setError( err2 ); |
| 323 |
} |
| 324 |
} |
| 325 |
} ); |
| 326 |
} |
| 327 |
|
| 328 |
public void receiveResponse( String responderId, String msgTag, |
| 329 |
Response response ) |
| 330 |
throws Exception { |
| 331 |
|
| 332 |
// Just forward the call to the base implementation. |
| 333 |
// Handling the responses happens elsewhere (where we have the |
| 334 |
// msgId not the msgTag). |
| 335 |
base_.receiveResponse( responderId, msgTag, response ); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* ClientSet implementation used by this hub service. |
| 341 |
*/ |
| 342 |
private class MessageTrackerClientSet extends GuiClientSet { |
| 343 |
private final ListDataListener transListener_; |
| 344 |
|
| 345 |
/** |
| 346 |
* Constructor. |
| 347 |
* |
| 348 |
* @param clientIdComparator comparator for client IDs |
| 349 |
*/ |
| 350 |
MessageTrackerClientSet( Comparator clientIdComparator ) { |
| 351 |
super( clientIdComparator ); |
| 352 |
|
| 353 |
// Prepare a listener which will be notified when a client's |
| 354 |
// send or receive list changes, or when any of the Transmission |
| 355 |
// objects in those lists changes state. |
| 356 |
transListener_ = new ListDataListener() { |
| 357 |
public void contentsChanged( ListDataEvent evt ) { |
| 358 |
transmissionChanged( evt ); |
| 359 |
} |
| 360 |
public void intervalAdded( ListDataEvent evt ) { |
| 361 |
transmissionChanged( evt ); |
| 362 |
} |
| 363 |
public void intervalRemoved( ListDataEvent evt ) { |
| 364 |
transmissionChanged( evt ); |
| 365 |
} |
| 366 |
private void transmissionChanged( ListDataEvent evt ) { |
| 367 |
Object src = evt.getSource(); |
| 368 |
assert src instanceof Transmission; |
| 369 |
if ( src instanceof Transmission ) { |
| 370 |
Transmission trans = (Transmission) src; |
| 371 |
int nclient = getSize(); |
| 372 |
for ( int ic = 0; ic < nclient; ic++ ) { |
| 373 |
Client client = (Client) getElementAt( ic ); |
| 374 |
if ( trans.getSender().equals( client ) || |
| 375 |
trans.getReceiver().equals( client ) ) { |
| 376 |
ListDataEvent clientEvt = |
| 377 |
new ListDataEvent( trans, |
| 378 |
ListDataEvent |
| 379 |
.CONTENTS_CHANGED, |
| 380 |
ic, ic ); |
| 381 |
fireListDataEvent( clientEvt ); |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
}; |
| 387 |
} |
| 388 |
|
| 389 |
public void add( HubClient client ) { |
| 390 |
MessageTrackerHubClient mtClient = (MessageTrackerHubClient) client; |
| 391 |
final ListModel txListModel = mtClient.txListModel_; |
| 392 |
final ListModel rxListModel = mtClient.rxListModel_; |
| 393 |
SwingUtilities.invokeLater( new Runnable() { |
| 394 |
public void run() { |
| 395 |
txListModel.addListDataListener( transListener_ ); |
| 396 |
rxListModel.addListDataListener( transListener_ ); |
| 397 |
} |
| 398 |
} ); |
| 399 |
super.add( client ); |
| 400 |
} |
| 401 |
|
| 402 |
public void remove( HubClient client ) { |
| 403 |
super.remove( client ); |
| 404 |
MessageTrackerHubClient mtClient = (MessageTrackerHubClient) client; |
| 405 |
final TransmissionListModel txListModel = mtClient.txListModel_; |
| 406 |
final TransmissionListModel rxListModel = mtClient.rxListModel_; |
| 407 |
SwingUtilities.invokeLater( new Runnable() { |
| 408 |
public void run() { |
| 409 |
for ( int i = 0; i < txListModel.getSize(); i++ ) { |
| 410 |
((Transmission) txListModel.getElementAt( i )) |
| 411 |
.setSenderUnregistered(); |
| 412 |
} |
| 413 |
for ( int i = 0; i < rxListModel.getSize(); i++ ) { |
| 414 |
((Transmission) rxListModel.getElementAt( i )) |
| 415 |
.setReceiverUnregistered(); |
| 416 |
} |
| 417 |
txListModel.removeListDataListener( transListener_ ); |
| 418 |
rxListModel.removeListDataListener( transListener_ ); |
| 419 |
} |
| 420 |
} ); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Keeps track of transmissions by key. |
| 426 |
* It works somewhat like a Map, but with the difference that multiple |
| 427 |
* values may be stored under a single key. |
| 428 |
*/ |
| 429 |
private static class CallMap { |
| 430 |
private final Map map_ = new HashMap(); |
| 431 |
|
| 432 |
/** |
| 433 |
* Adds a new entry. |
| 434 |
* |
| 435 |
* @param key key |
| 436 |
* @param trans value |
| 437 |
*/ |
| 438 |
public void add( Object key, Transmission trans ) { |
| 439 |
assert SwingUtilities.isEventDispatchThread(); |
| 440 |
if ( ! map_.containsKey( key ) ) { |
| 441 |
map_.put( key, new ArrayList( 1 ) ); |
| 442 |
} |
| 443 |
((List) map_.get( key )).add( trans ); |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* Reads and removes an entry. If multiple values are stored under |
| 448 |
* the given key, one of them (the first to have been stored) |
| 449 |
* is returned, and any others are unaffected. |
| 450 |
* |
| 451 |
* @param key key |
| 452 |
* @returh a value correspondig to key |
| 453 |
*/ |
| 454 |
public Transmission remove( Object key ) { |
| 455 |
assert SwingUtilities.isEventDispatchThread(); |
| 456 |
List transList = (List) map_.get( key ); |
| 457 |
if ( transList == null ) { |
| 458 |
return null; |
| 459 |
} |
| 460 |
else { |
| 461 |
assert ! transList.isEmpty(); |
| 462 |
Transmission trans = (Transmission) transList.remove( 0 ); |
| 463 |
if ( transList.isEmpty() ) { |
| 464 |
map_.remove( key ); |
| 465 |
} |
| 466 |
return trans; |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
} |