Debian Codegraph @

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

raw
1 package org.astrogrid.samp.hub;
2
3 import java.util.Map;
4 import org.astrogrid.samp.Client;
5 import org.astrogrid.samp.Message;
6 import org.astrogrid.samp.Metadata;
7 import org.astrogrid.samp.Response;
8 import org.astrogrid.samp.SampUtils;
9 import org.astrogrid.samp.Subscriptions;
10 import org.astrogrid.samp.client.CallableClient;
11 import org.astrogrid.samp.client.SampException;
12
13 /**
14 * Represents a client registered with a hub.
15 *
16 * @author Mark Taylor
17 * @since 15 Jul 2008
18 */
19 public class HubClient implements Client {
20
21 private final String publicId_;
22 private final ProfileToken profileToken_;
23 private volatile Subscriptions subscriptions_;
24 private volatile Metadata metadata_;
25 private volatile CallableClient callable_;
26
27 /**
28 * Constructor.
29 *
30 * @param publicId client public ID
31 * @param profileToken identifier for the source of the hub connection
32 */
33 public HubClient( String publicId, ProfileToken profileToken ) {
34 publicId_ = publicId;
35 profileToken_ = profileToken;
36 subscriptions_ = new Subscriptions();
37 metadata_ = new Metadata();
38 callable_ = new NoCallableClient();
39 }
40
41 public String getId() {
42 return publicId_;
43 }
44
45 public Metadata getMetadata() {
46 return metadata_;
47 }
48
49 public Subscriptions getSubscriptions() {
50 return subscriptions_;
51 }
52
53 /**
54 * Returns a token identifying the source of this client's connection
55 * to the hub.
56 *
57 * @return profile token
58 */
59 public ProfileToken getProfileToken() {
60 return profileToken_;
61 }
62
63 /**
64 * Sets this client's metadata map.
65 *
66 * @param meta metadata map
67 */
68 public void setMetadata( Map meta ) {
69 metadata_ = new Metadata( meta );
70 }
71
72 /**
73 * Sets this client's subscriptions list.
74 *
75 * @param subs subscriptions map
76 */
77 public void setSubscriptions( Map subs ) {
78 subscriptions_ = Subscriptions.asSubscriptions( subs );
79 }
80
81 /**
82 * Indicates whether this client is subscribed to a given MType.
83 *
84 * @param mtype MType
85 * @return true iff subscribed to MType
86 */
87 public boolean isSubscribed( String mtype ) {
88 return isCallable() && subscriptions_.isSubscribed( mtype );
89 }
90
91 /**
92 * Returns the subscription information for a given MType for this client.
93 *
94 * @param mtype MType
95 * @return subscriptions map value for key <code>mtype</code>,
96 * or null if not subscribed
97 */
98 public Map getSubscription( String mtype ) {
99 return isCallable() ? subscriptions_.getSubscription( mtype )
100 : null;
101 }
102
103 /**
104 * Sets the callable object which allows this client to receive
105 * callbacks. If null is used, a no-op callable object is installed.
106 *
107 * @param callable new callable interface, or null
108 */
109 public void setCallable( CallableClient callable ) {
110 callable_ = callable == null ? new NoCallableClient() : callable;
111 }
112
113 /**
114 * Returns the callable object which allows this client to receive
115 * callbacks. It is never null.
116 *
117 * @return callable object
118 */
119 public CallableClient getCallable() {
120 return callable_;
121 }
122
123 /**
124 * Indicates whether this client is callable.
125 *
126 * @return true iff this client has a non-useless callback handler
127 * installed
128 */
129 public boolean isCallable() {
130 return ! ( callable_ instanceof NoCallableClient );
131 }
132
133 public String toString() {
134 return SampUtils.toString( this );
135 }
136
137 /**
138 * No-op callback handler implementation.
139 * Any attempt to call its methods results in an exception.
140 */
141 private class NoCallableClient implements CallableClient {
142 public void receiveNotification( String senderId, Message message )
143 throws SampException {
144 refuse();
145 }
146 public void receiveCall( String senderId, String msgId,
147 Message message )
148 throws SampException {
149 refuse();
150 }
151 public void receiveResponse( String responderId, String msgId,
152 Response response )
153 throws SampException {
154 refuse();
155 }
156 private void refuse() throws SampException {
157 throw new SampException( "Client " + getId() + " is not callable" );
158 }
159 }
160 }