Debian Codegraph @

src/java/org/astrogrid/samp/bridge/ProxyManager.java

raw
1 package org.astrogrid.samp.bridge;
2
3 import java.awt.Image;
4 import java.awt.AlphaComposite;
5 import java.awt.Composite;
6 import java.awt.Graphics2D;
7 import java.awt.image.BufferedImage;
8 import java.awt.image.RenderedImage;
9 import java.io.IOException;
10 import java.net.URL;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.logging.Logger;
19 import org.astrogrid.samp.Client;
20 import org.astrogrid.samp.ErrInfo;
21 import org.astrogrid.samp.Message;
22 import org.astrogrid.samp.Metadata;
23 import org.astrogrid.samp.RegInfo;
24 import org.astrogrid.samp.Response;
25 import org.astrogrid.samp.Subscriptions;
26 import org.astrogrid.samp.client.CallableClient;
27 import org.astrogrid.samp.client.ClientProfile;
28 import org.astrogrid.samp.client.HubConnection;
29 import org.astrogrid.samp.client.HubConnector;
30 import org.astrogrid.samp.client.SampException;
31 import org.astrogrid.samp.client.TrackedClientSet;
32 import org.astrogrid.samp.httpd.HttpServer;
33 import org.astrogrid.samp.httpd.UtilServer;
34
35 /**
36 * Takes care of client connections for the SAMP Bridge.
37 * An instance of this class is associated with a given 'local' hub
38 * participating in the bridge, and makes the following connections:
39 * <ol>
40 * <li>On the local hub, one connection which is there to monitor
41 * client changes</li>
42 * <li>On each remote hub participating in the bridge, one 'proxy' connection
43 * for every client on this manager's local hub.</li>
44 * </ol>
45 * Callbacks from the hub to the proxy clients can be tunnelled by this
46 * proxy manager to their true destination on the local hub.
47 * Note that each proxy manager needs the cooperation of all the other
48 * proxy managers (the ones associated with the other bridged hubs) to
49 * make this work, so each instance of this class must be made aware of
50 * the other ProxyMangers before use (see {@link #init}).
51 *
52 * @author Mark Taylor
53 * @since 15 Jul 2009
54 */
55 class ProxyManager {
56
57 private final ClientProfile localProfile_;
58 private final UtilServer server_;
59 private final HubConnector pmConnector_;
60 private final Map connectionMap_; // local client ID -> HubConnection[]
61 private final Map tagMap_;
62 private final IconAdjuster iconAdjuster_;
63 private ProxyManager[] remoteManagers_;
64 private UrlExporter exporter_;
65 private boolean useProxyHub_;
66 private int nRemote_;
67
68 private static final Logger logger_ =
69 Logger.getLogger( ProxyManager.class.getName() );
70
71 /**
72 * Constructor.
73 *
74 * @param localProfile profile for connection to this manager's local hub
75 * @param server server instance
76 */
77 public ProxyManager( ClientProfile localProfile, UtilServer server ) {
78 localProfile_ = localProfile;
79 server_ = server;
80
81 // Set up the local hub connection to monitor client list changes.
82 pmConnector_ =
83 new HubConnector( localProfile, new ProxyManagerClientSet() ) {
84 protected void connectionChanged( boolean isConnected ) {
85 super.connectionChanged( isConnected );
86 managerConnectionChanged( isConnected );
87 }
88 };
89 Metadata meta = new Metadata();
90 meta.setName( "bridge" );
91 meta.setDescriptionText( "Bridge between hubs" );
92 try {
93 meta.setIconUrl( server_
94 .exportResource( "/org/astrogrid/samp/images/"
95 + "bridge.png" )
96 .toString() );
97 }
98 catch ( IOException e ) {
99 logger_.warning( "Couldn't set icon" );
100 }
101 meta.put( "author.name", "Mark Taylor" );
102 meta.put( "author.email", "[email protected]" );
103 pmConnector_.declareMetadata( meta );
104 Subscriptions subs = pmConnector_.computeSubscriptions();
105 pmConnector_.declareSubscriptions( subs );
106
107 // Set up other required data structures.
108 connectionMap_ = Collections.synchronizedMap( new HashMap() );
109 tagMap_ = Collections.synchronizedMap( new HashMap() );
110 iconAdjuster_ = new ProxyIconAdjuster();
111 server_.getServer().addHandler( iconAdjuster_ );
112 }
113
114 /**
115 * Returns the profile for this manager's local hub.
116 *
117 * @return profile
118 */
119 public ClientProfile getProfile() {
120 return localProfile_;
121 }
122
123 /**
124 * Returns the hub connector used by this manager for client monitoring
125 * on the local hub.
126 *
127 * @return hub connector
128 */
129 public HubConnector getManagerConnector() {
130 return pmConnector_;
131 }
132
133 /**
134 * Sets an object which is used to export SAMP data contents for use
135 * in remote contexts.
136 *
137 * @param exporter new exporter; may be null
138 */
139 public void setExporter( UrlExporter exporter ) {
140 exporter_ = exporter;
141 }
142
143 /**
144 * Sets whether remote proxy should be generated for the local client
145 * representing the local hub.
146 * Default is not, since they are not very interesting to talk to.
147 *
148 * @param useProxyHub true iff the client representing the local hub
149 * should be proxied remotely
150 */
151 public void setUseProxyHub( boolean useProxyHub ) {
152 useProxyHub_ = useProxyHub;
153 }
154
155 /**
156 * Prepares this manager for use by informing it about all its sibling
157 * managers. This must be done before the bridge can start operations.
158 *
159 * @param allManagers array of ProxyManagers including this one,
160 * one for each hub participating in the bridge
161 */
162 public void init( ProxyManager[] allManagers ) {
163
164 // Store an array of all the other managers, excluding this one,
165 // for later use.
166 List remoteList = new ArrayList();
167 int selfCount = 0;
168 for ( int im = 0; im < allManagers.length; im++ ) {
169 ProxyManager pm = allManagers[ im ];
170 if ( pm == this ) {
171 selfCount++;
172 }
173 else {
174 remoteList.add( pm );
175 }
176 }
177 if ( selfCount != 1 ) {
178 throw new IllegalArgumentException( "Self not in list once" );
179 }
180 remoteManagers_ =
181 (ProxyManager[]) remoteList.toArray( new ProxyManager[ 0 ] );
182 nRemote_ = remoteManagers_.length;
183 assert nRemote_ == allManagers.length - 1;
184 }
185
186 public String toString() {
187 return localProfile_.toString();
188 }
189
190 /**
191 * Returns the connection on the hub associated with a remote
192 * proxy manager which is the proxy for a given local client.
193 *
194 * @param remoteManager proxy manager for a remote bridged hub
195 * @param localClientId client ID of a client registered with
196 * this manager's local hub
197 * @return proxy connection
198 */
199 private HubConnection getProxyConnection( ProxyManager remoteManager,
200 String localClientId ) {
201 HubConnection[] proxyConnections =
202 (HubConnection[]) connectionMap_.get( localClientId );
203 return proxyConnections == null
204 ? null
205 : proxyConnections[ getManagerIndex( remoteManager ) ];
206 }
207
208 /**
209 * Deletes the record of the connection on the hub associated with
210 * a remote proxy manager which is the proxy for a given local client.
211 * This proxy can no longer be used.
212 *
213 * @param remoteManager proxy manager for a remote bridged hub
214 * @param localClientId client ID of a client registered with
215 * this manager's local hub
216 */
217 private void removeProxyConnection( ProxyManager remoteManager,
218 String localClientId ) {
219 HubConnection[] proxyConnections =
220 (HubConnection[]) connectionMap_.get( localClientId );
221 if ( proxyConnections != null ) {
222 proxyConnections[ getManagerIndex( remoteManager ) ] = null;
223 }
224 }
225
226 /**
227 * Returns the index by which this manager labels a given remote
228 * proxy manager.
229 *
230 * @param remoteManager manager to locate
231 * @return index of <code>remoteManager</code> in the list
232 */
233 private int getManagerIndex( ProxyManager remoteManager ) {
234 return Arrays.asList( remoteManagers_ ).indexOf( remoteManager );
235 }
236
237 /**
238 * Returns the metadata to use for the remote proxy of a local client.
239 * This resembles the metadata of the local client itself, but may
240 * have some adjustments.
241 *
242 * @param localClient local client
243 * @return metadata to use for client's remote proxy
244 */
245 private Metadata getProxyMetadata( Client localClient ) {
246 Metadata meta = localClient.getMetadata();
247 if ( meta == null ) {
248 return null;
249 }
250 else {
251 meta = new Metadata( meta );
252 if ( exporter_ != null ) {
253 exporter_.exportMap( meta );
254 }
255 meta.setName( proxyName( meta.getName() ) );
256 if ( meta.getIconUrl() != null ) {
257 URL iconUrl = proxyIconUrl( meta.getIconUrl() );
258 meta.setIconUrl( iconUrl == null ? null : iconUrl.toString() );
259 }
260 meta.put( "bridge.proxy.source", ProxyManager.this.toString() );
261 }
262 return meta;
263 }
264
265 /**
266 * Returns the name to be used for a proxy client given its local name.
267 *
268 * @param localName local name
269 * @return proxy name
270 */
271 private String proxyName( String localName ) {
272 return localName == null ? "(proxy)"
273 : localName + " (proxy)";
274 }
275
276 /**
277 * Returns the icon to be used for a proxy client given its local icon.
278 *
279 * @param localIconUrl URL for local icon
280 * @return URL for proxy icon
281 */
282 private URL proxyIconUrl( URL localIconUrl ) {
283 return localIconUrl != null
284 ? iconAdjuster_.exportAdjustedIcon( localIconUrl )
285 : localIconUrl;
286 }
287
288 /**
289 * Returns the subscriptions to use for the remote proxy of a local client.
290 * This resembles the subscriptions of the local client itself, but may
291 * have some adjustments.
292 *
293 * @param localClient local client
294 * @return subscriptions to use for client's remote proxy
295 */
296 private Subscriptions getProxySubscriptions( Client client ) {
297 Subscriptions subs = client.getSubscriptions();
298 if ( subs == null ) {
299 return null;
300 }
301 else {
302
303 // Remove subscriptions to most hub administrative MTypes.
304 // These should not be delivered from the remote hub to the
305 // local client, since the local client should only receive
306 // such messages from its own hub. Note this does not mean
307 // that the local client will not be informed about changes
308 // to clients on the remote hubs; this information will be
309 // relayed by the local hub as a consequence of proxies from
310 // other ProxyManagers making register/declare/etc calls
311 // on this manager's local hub.
312 subs = new Subscriptions( subs );
313 subs.remove( "samp.hub.event.shutdown" );
314 subs.remove( "samp.hub.event.register" );
315 subs.remove( "samp.hub.event.unregister" );
316 subs.remove( "samp.hub.event.metadata" );
317 subs.remove( "samp.hub.event.subscriptions" );
318 if ( exporter_ != null ) {
319 exporter_.exportMap( subs );
320 }
321 return subs;
322 }
323 }
324
325 /**
326 * Called when this ProxyManager's connector has been disconnected
327 * (for whatever reason) from its local hub.
328 * It makes sure that any proxies from other ProxyManagers to the local
329 * hub are unregistered, so that no further bridge activity takes
330 * place on the local hub.
331 *
332 * @param isConnected true for a connection; false for a disconnection
333 */
334 protected void managerConnectionChanged( boolean isConnected ) {
335 if ( ! isConnected ) {
336 for ( int ir = 0; ir < nRemote_; ir++ ) {
337 ProxyManager remoteManager = remoteManagers_[ ir ];
338 int im = remoteManager.getManagerIndex( this );
339 for ( Iterator it = remoteManager.connectionMap_.values()
340 .iterator();
341 it.hasNext(); ) {
342 HubConnection[] connections = (HubConnection[]) it.next();
343 if ( connections != null ) {
344 HubConnection connection = connections[ im ];
345 if ( connection != null ) {
346 connections[ im ] = null;
347 try {
348 connection.unregister();
349 }
350 catch ( SampException e ) {
351 logger_.info( "Unregister failed" );
352 }
353 }
354 }
355 }
356 }
357 }
358 else {
359 // not expected except for initial connection
360 }
361 }
362
363 /**
364 * Invoked when a client is added to the local hub.
365 *
366 * @param client newly added client
367 */
368 private void localClientAdded( Client client ) {
369 if ( ! isProxiedClient( client ) ) {
370 return;
371 }
372
373 // Register a proxy for the new local client on all the remote hubs
374 // in the bridge.
375 Metadata meta = getProxyMetadata( client );
376 Subscriptions subs = getProxySubscriptions( client );
377 HubConnection[] proxyConnections = new HubConnection[ nRemote_ ];
378 connectionMap_.put( client.getId(), proxyConnections );
379 for ( int ir = 0; ir < nRemote_; ir++ ) {
380 ProxyManager remoteManager = remoteManagers_[ ir ];
381 try {
382
383 // This synchronization is here so that the isProxy method
384 // can work reliably. isProxy may ask whether a client seen
385 // on a remote hub is a proxy controlled by this one.
386 // It can only ask after the registration has been done,
387 // and the determination is synchronized on connectionMap_.
388 // By synchronizing here, we can ensure that it can't ask
389 // after the registration, but before the information has
390 // been recorded in the connectionMap.
391 final HubConnection proxyConnection;
392 synchronized ( connectionMap_ ) {
393 proxyConnection = remoteManager.getProfile().register();
394 if ( proxyConnection != null ) {
395 CallableClient callable =
396 new ProxyCallableClient( client, proxyConnection,
397 remoteManager );
398 proxyConnection.setCallable( callable );
399 proxyConnections[ ir ] = proxyConnection;
400 }
401 }
402 if ( proxyConnection != null ) {
403 if ( meta != null ) {
404 try {
405 proxyConnection.declareMetadata( meta );
406 }
407 catch ( SampException e ) {
408 logger_.warning( "proxy declareMetadata failed for "
409 + client );
410 }
411 }
412 if ( subs != null ) {
413 try {
414 proxyConnection.declareSubscriptions( subs );
415 }
416 catch ( SampException e ) {
417 logger_.warning( "proxy declareSubscriptions failed"
418 + " for " + client );
419 }
420 }
421 }
422 }
423 catch ( SampException e ) {
424 logger_.warning( "proxy registration failed for " + client );
425 }
426 }
427 }
428
429 /**
430 * Invoked when a client is removed from the local hub.
431 *
432 * @param client recently removed client
433 */
434 private void localClientRemoved( Client client ) {
435 if ( ! isProxiedClient( client ) ) {
436 return;
437 }
438
439 // Remove all the proxies which were registered on remote hubs
440 // on behalf of the removed client.
441 HubConnection[] proxyConnections =
442 (HubConnection[]) connectionMap_.remove( client.getId() );
443 if ( proxyConnections != null ) {
444 for ( int ir = 0; ir < nRemote_; ir++ ) {
445 HubConnection connection = proxyConnections[ ir ];
446 if ( connection != null ) {
447 try {
448 connection.unregister();
449 }
450 catch ( SampException e ) {
451 logger_.warning( "proxy unregister failed for "
452 + client );
453 }
454 }
455 }
456 }
457 }
458
459 /**
460 * Invoked when information (metadata or subscriptions) have been
461 * updated for a client on the local hub.
462 *
463 * @param client updated client
464 * @param metaChanged true if metadata may have changed
465 * (false if known unchanged)
466 * @param subsChanged true if subscriptions may have changed
467 * (false if known unchanged)
468 */
469 private void localClientUpdated( Client client, boolean metaChanged,
470 boolean subsChanged ) {
471 if ( ! isProxiedClient( client ) ) {
472 return;
473 }
474
475 // Cause each of the local client's proxies on remote hubs to
476 // declare subscription/metadata updates appropriately.
477 HubConnection[] proxyConnections =
478 (HubConnection[]) connectionMap_.get( client.getId() );
479 Metadata meta =
480 metaChanged ? getProxyMetadata( client ) : null;
481 Subscriptions subs =
482 subsChanged ? getProxySubscriptions( client ) : null;
483 if ( proxyConnections != null ) {
484 for ( int ir = 0; ir < nRemote_; ir++ ) {
485 HubConnection connection = proxyConnections[ ir ];
486 if ( connection != null ) {
487 if ( meta != null ) {
488 try {
489 connection.declareMetadata( meta );
490 }
491 catch ( SampException e ) {
492 logger_.warning( "proxy declareMetadata failed "
493 + "for " + client );
494 }
495 }
496 if ( subs != null ) {
497 try {
498 connection.declareSubscriptions( subs );
499 }
500 catch ( SampException e ) {
501 logger_.warning( "proxy declareSubscriptions "
502 + "failed for " + client );
503 }
504 }
505 }
506 }
507 }
508 }
509
510 /**
511 * Determines whether a local client is a genuine third party client
512 * which requires a remote proxy. Will return false for clients which
513 * are operating on behalf of this bridge, including the ProxyManager's
514 * client tracking connection and any proxies controlled by remote
515 * ProxyManagers. Unless useProxyHub is true, will also return false
516 * for the hub client on remote hubs, since these are not very
517 * interesting to talk to.
518 *
519 * @param client local client
520 * @param true if <code>client</code> has or should have a proxy;
521 * false if it's an organ of the bridge administration
522 */
523 private boolean isProxiedClient( Client client ) {
524
525 // Is it a client on the local hub that we want to exclude?
526 try {
527 if ( pmConnector_.isConnected() ) {
528 HubConnection connection = pmConnector_.getConnection();
529 if ( connection != null ) {
530 String clientId = client.getId();
531 RegInfo regInfo = connection.getRegInfo();
532 if ( clientId.equals( regInfo.getSelfId() ) ||
533 ( ! useProxyHub_ &&
534 clientId.equals( regInfo.getHubId() ) ) ) {
535 return false;
536 }
537 }
538 }
539 }
540 catch ( SampException e ) {
541 }
542
543 // Is it a proxy controlled by one of the remote managers?
544 for ( int ir = 0; ir < nRemote_; ir++ ) {
545 if ( remoteManagers_[ ir ].isProxy( client, ProxyManager.this ) ) {
546 return false;
547 }
548 }
549
550 // No, then it's a genuine local client requiring a proxy.
551 return true;
552 }
553
554 /**
555 * Determines whether a given local client is a proxy controlled by
556 * a given remote ProxyManager.
557 *
558 * @param client local client
559 * @param remoteManager remote proxy manager
560 * @return true iff <code>client</code> is one of
561 * <code>remoteManager</code>'s proxies
562 */
563 private boolean isProxy( Client client, ProxyManager remoteManager ) {
564 int ir = getManagerIndex( remoteManager );
565 synchronized ( connectionMap_ ) {
566 for ( Iterator it = connectionMap_.values().iterator();
567 it.hasNext(); ) {
568 HubConnection[] proxyConnections = (HubConnection[]) it.next();
569 if ( proxyConnections != null ) {
570 HubConnection proxyConnection = proxyConnections[ ir ];
571 if ( proxyConnection != null ) {
572 RegInfo proxyReg = proxyConnection.getRegInfo();
573 if ( proxyReg.getSelfId().equals( client.getId() ) ) {
574 return true;
575 }
576 }
577 }
578 }
579 }
580 return false;
581 }
582
583 /**
584 * CallableClient implementation used by remote proxy connections on
585 * behalf of local clients. This is the core of the proxy manager.
586 * Callbacks received by the remote proxy client are tunnelled back
587 * to the local hub and forwarded by the local proxy of the remote
588 * sender client to the appropriate local non-proxy client.
589 * Since local proxies are managed by other proxy managers
590 * (this one manages remote proxies of local clients)
591 * this means getting the other proxy managers to do some of the work.
592 */
593 private class ProxyCallableClient implements CallableClient {
594 private final String localClientId_;
595 private final HubConnection remoteProxy_;
596 private final ProxyManager remoteManager_;
597 private final ProxyManager localManager_;
598
599 /**
600 * Constructor.
601 *
602 * @param localClient local client
603 * @param remoteProxy hub connection to the remote hub for the proxy
604 * @param remoteManager remote ProxyManager associated with the
605 * hub where this proxy is connected
606 */
607 ProxyCallableClient( Client localClient, HubConnection remoteProxy,
608 ProxyManager remoteManager ) {
609 localClientId_ = localClient.getId();
610 remoteProxy_ = remoteProxy;
611 remoteManager_ = remoteManager;
612 localManager_ = ProxyManager.this;
613 }
614
615 public void receiveNotification( String remoteSenderId, Message msg )
616 throws SampException {
617
618 // Forward the notification.
619 if ( remoteManager_.exporter_ != null ) {
620 msg = new Message( msg );
621 remoteManager_.exporter_.exportMap( msg );
622 }
623 HubConnection localProxy = getLocalProxy( remoteSenderId );
624 if ( localProxy != null ) {
625 localProxy.notify( localClientId_, msg );
626 }
627 proxyProcessMessage( remoteSenderId, msg );
628 }
629
630 public void receiveCall( String remoteSenderId, String remoteMsgId,
631 Message msg )
632 throws SampException {
633
634 // Choose a tag; use the message ID as its value.
635 // These things are different, but we are free to choose any
636 // form for the tag, and we need something which will allow
637 // us to recover the message ID from it later.
638 // Making them identical is the easiest way to do that.
639 String localMsgTag = remoteMsgId;
640
641 // Forward the call.
642 if ( remoteManager_.exporter_ != null ) {
643 msg = new Message( msg );
644 remoteManager_.exporter_.exportMap( msg );
645 }
646 HubConnection localProxy = getLocalProxy( remoteSenderId );
647 if ( localProxy != null ) {
648 localProxy.call( localClientId_, localMsgTag, msg );
649 }
650 else {
651 ErrInfo errInfo = new ErrInfo();
652 errInfo.setErrortxt( "Bridge can't forward message" );
653 Client senderClient =
654 (Client) remoteManager_.getManagerConnector()
655 .getClientMap().get( remoteSenderId );
656 String usertxt = new StringBuffer()
657 .append( "Bridge can't forward message to recipient;\n" )
658 .append( "sender client " )
659 .append( senderClient )
660 .append( " has no proxy on remote hub" )
661 .toString();
662 errInfo.setUsertxt( usertxt );
663 new ErrInfo( "Client " + remoteSenderId + " not present"
664 + " on other side of bridge" );
665 remoteProxy_.reply( remoteMsgId,
666 Response.createErrorResponse( errInfo ) );
667 }
668 proxyProcessMessage( remoteSenderId, msg );
669 }
670
671 public void receiveResponse( String remoteResponderId,
672 String remoteMsgTag, Response response )
673 throws SampException {
674
675 // The message ID we need for forwarding is the one we encoded
676 // (by identity) earlier in the tag.
677 String localMsgId = remoteMsgTag;
678
679 // Forward the reply appropriately.
680 if ( remoteManager_.exporter_ != null ) {
681 response = new Response( response );
682 remoteManager_.exporter_.exportMap( response );
683 }
684 HubConnection localProxy = getLocalProxy( remoteResponderId );
685 if ( localProxy != null ) {
686 localProxy.reply( localMsgId, response );
687 }
688 else {
689
690 // Should only happen if the proxy has been disconnected
691 // between send and receive.
692 logger_.warning( "Bridge can't forward response: "
693 + " missing proxy" );
694 }
695 }
696
697 /**
698 * Returns the hub connection for the proxy on the local hub
699 * which corresponds to a given remote client.
700 *
701 * @param remoteClientId client ID of remote client
702 * @return hub connection for local proxy
703 */
704 private HubConnection getLocalProxy( String remoteClientId ) {
705 return remoteManager_
706 .getProxyConnection( localManager_, remoteClientId );
707 }
708
709 /**
710 * Performs housekeeping tasks for an incoming message if any.
711 * This is in addition to forwarding the message to the client
712 * for which we are proxying.
713 *
714 * @param remoteSenderId id of sending client on remote hub
715 * @param msg message
716 */
717 private void proxyProcessMessage( String remoteSenderId, Message msg ) {
718 String mtype = msg.getMType();
719 boolean fromHub =
720 remoteSenderId.equals( remoteProxy_.getRegInfo().getHubId() );
721 if ( "samp.hub.disconnect".equals( mtype ) ) {
722 if ( fromHub ) {
723 removeProxyConnection( remoteManager_, localClientId_ );
724 }
725 else {
726 logger_.warning( mtype + " from non-hub client "
727 + remoteSenderId + " - ignored" );
728 }
729 }
730 }
731 }
732
733 /**
734 * TrackedClientSet implementation used by a Proxy Manager.
735 * Apart from inheriting default behaviour, this triggers
736 * calls to ProxyManager methods when there are status changes
737 * to local clients.
738 */
739 private class ProxyManagerClientSet extends TrackedClientSet {
740
741 /**
742 * Constructor.
743 */
744 private ProxyManagerClientSet() {
745 super();
746 }
747
748 public void addClient( Client client ) {
749 super.addClient( client );
750 localClientAdded( client );
751 }
752
753 public void removeClient( Client client ) {
754 localClientRemoved( client );
755 super.removeClient( client );
756 }
757
758 public void updateClient( Client client,
759 boolean metaChanged, boolean subsChanged ) {
760 super.updateClient( client, metaChanged, subsChanged );
761 localClientUpdated( client, metaChanged, subsChanged );
762 }
763
764 public void setClients( Client[] clients ) {
765 for ( Iterator it = getClientMap().entrySet().iterator();
766 it.hasNext(); ) {
767 Map.Entry entry = (Map.Entry) it.next();
768 Client client = (Client) entry.getValue();
769 localClientRemoved( client );
770 }
771 super.setClients( clients );
772 for ( int i = 0; i < clients.length; i++ ) {
773 Client client = clients[ i ];
774 localClientAdded( client );
775 }
776 }
777 }
778
779 /**
780 * Class which can turn a client's icon into the icon for the proxy of
781 * the same client. Some visually distinctive adjustment is made to
782 * make it obvious from the icon that it's a proxy.
783 */
784 private class ProxyIconAdjuster extends IconAdjuster {
785
786 /**
787 * Constructor.
788 */
789 ProxyIconAdjuster() {
790 super( server_.getServer(),
791 server_.getBasePath( "proxy" + "-" + localProfile_ ) );
792 }
793
794 public RenderedImage adjustImage( BufferedImage inImage ) {
795 int w = inImage.getWidth();
796 int h = inImage.getHeight();
797
798 // Copy the image to a new image. It would be possible to write
799 // directly into the input BufferedImage, but this might not
800 // have the correct image type, so could end up getting the
801 // transparency wrong or something.
802 BufferedImage outImage =
803 new BufferedImage( w, h, BufferedImage.TYPE_4BYTE_ABGR );
804 Graphics2D g2 = outImage.createGraphics();
805 g2.drawImage( inImage, null, 0, 0 );
806
807 // Slice off half of the image diagonally.
808 int[] xs = new int[] { 0, w, w, };
809 int[] ys = new int[] { h, h, 0, };
810 Composite compos = g2.getComposite();
811 g2.setComposite( AlphaComposite.Clear );
812 g2.fillPolygon( xs, ys, 3 );
813 g2.setComposite( compos );
814
815 // Return the result.
816 return outImage;
817 }
818 }
819 }