Debian Codegraph @

src/java/org/astrogrid/samp/hub/HubCallableClient.java

raw
1 package org.astrogrid.samp.hub;
2
3 import org.astrogrid.samp.Message;
4 import org.astrogrid.samp.Response;
5 import org.astrogrid.samp.Subscriptions;
6 import org.astrogrid.samp.client.AbstractMessageHandler;
7 import org.astrogrid.samp.client.CallableClient;
8 import org.astrogrid.samp.client.HubConnection;
9 import org.astrogrid.samp.client.SampException;
10
11 /**
12 * CallableClient implementation used by the hub client.
13 * This isn't exactly essential, but it enables the hub client
14 * (the client which represents the hub itself) to subscribe to some MTypes.
15 * Possibly useful for testing purposes etc.
16 *
17 * @author Mark Taylor
18 * @since 28 Jan 2011
19 */
20 class HubCallableClient implements CallableClient {
21
22 private final HubConnection connection_;
23 private final AbstractMessageHandler[] handlers_;
24
25 /**
26 * Constructs a HubCallableClient with a given set of handlers.
27 *
28 * @param connection connection to hub service
29 * @param handlers array of message handlers
30 */
31 public HubCallableClient( HubConnection connection,
32 AbstractMessageHandler[] handlers ) {
33 connection_ = connection;
34 handlers_ = handlers;
35 }
36
37 public void receiveCall( String senderId, String msgId, Message msg )
38 throws SampException {
39 msg.check();
40 getHandler( msg.getMType() )
41 .receiveCall( connection_, senderId, msgId, msg );
42 }
43
44 public void receiveNotification( String senderId, Message msg )
45 throws SampException {
46 msg.check();
47 getHandler( msg.getMType() )
48 .receiveNotification( connection_, senderId, msg );
49 }
50
51 public void receiveResponse( String responderId, String msgTag,
52 Response response ) throws SampException {
53 }
54
55 /**
56 * Returns the subscriptions corresponding to the messages that this
57 * receiver can deal with.
58 *
59 * @return subscriptions list
60 */
61 public Subscriptions getSubscriptions() {
62 Subscriptions subs = new Subscriptions();
63 for ( int i = 0; i < handlers_.length; i++ ) {
64 subs.putAll( handlers_[ i ].getSubscriptions() );
65 }
66 return subs;
67 }
68
69 /**
70 * Returns a handler owned by this callable client which can handle
71 * a given MType. If more than one applies, the first one encountered
72 * is returned.
73 *
74 * @param mtype MType to handle
75 * @return handler for <code>mtype</code>
76 * @throws SampException if no suitable handler exists
77 */
78 private AbstractMessageHandler getHandler( String mtype )
79 throws SampException {
80 for ( int i = 0; i < handlers_.length; i++ ) {
81 AbstractMessageHandler handler = handlers_[ i ];
82 if ( Subscriptions.asSubscriptions( handler.getSubscriptions() )
83 .isSubscribed( mtype ) ) {
84 return handler;
85 }
86 }
87 throw new SampException( "Not subscribed to " + mtype );
88 }
89 }