Debian Codegraph @

src/java/org/astrogrid/samp/xmlrpc/XmlRpcHubConnection.java

raw
1 package org.astrogrid.samp.xmlrpc;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.logging.Logger;
9 import org.astrogrid.samp.Metadata;
10 import org.astrogrid.samp.RegInfo;
11 import org.astrogrid.samp.Response;
12 import org.astrogrid.samp.ShutdownManager;
13 import org.astrogrid.samp.SampUtils;
14 import org.astrogrid.samp.Subscriptions;
15 import org.astrogrid.samp.client.HubConnection;
16 import org.astrogrid.samp.client.SampException;
17
18 /**
19 * Partial HubConnection implementation based on XML-RPC.
20 * No implementation is provided for the {@link #setCallable} method.
21 * This is a useful base class for XML-RPC-based profile implementations,
22 * but it is not perfectly general: some assumptions, compatible
23 * with the Standard Profile, are made about the way that XML-RPC
24 * calls are mapped on to SAMP hub interface calls.
25 *
26 * @author Mark Taylor
27 * @author Sylvain Lafrasse
28 * @since 16 Jul 2008
29 */
30 public abstract class XmlRpcHubConnection implements HubConnection {
31
32 private final SampXmlRpcClient xClient_;
33 private final String prefix_;
34 private final RegInfo regInfo_;
35 private boolean unregistered_;
36 private static final Logger logger_ =
37 Logger.getLogger( XmlRpcHubConnection.class.getName() );
38
39 /**
40 * Constructor.
41 *
42 * @param xClient XML-RPC client
43 * @param prefix string prepended to all hub interface method names
44 * to turn them into XML-RPC method names
45 * @param registerArgs arguments to the profile-specific "register"
46 * method to initiate this connection
47 */
48 public XmlRpcHubConnection( SampXmlRpcClient xClient, String prefix,
49 List registerArgs )
50 throws SampException {
51 xClient_ = xClient;
52 prefix_ = prefix;
53 Object regInfo = rawExec( prefix_ + "register", registerArgs );
54 if ( regInfo instanceof Map ) {
55 regInfo_ = RegInfo.asRegInfo( Collections
56 .unmodifiableMap( asMap( regInfo ) ) );
57 }
58 else {
59 throw new SampException( "Bad return value from hub register method" + " - not a map" );
60 }
61 ShutdownManager.getInstance()
62 .registerHook( this, ShutdownManager.CLIENT_SEQUENCE,
63 new Runnable() {
64 public void run() {
65 finish();
66 }
67 } );
68 }
69
70 public RegInfo getRegInfo() {
71 return regInfo_;
72 }
73
74 public void ping() throws SampException {
75 rawExec( prefix_ + "ping", new ArrayList() );
76 }
77
78 public void unregister() throws SampException {
79 exec( "unregister", new Object[ 0 ] );
80 ShutdownManager.getInstance().unregisterHook( this );
81 unregistered_ = true;
82 }
83
84 public void declareMetadata( Map meta ) throws SampException {
85 exec( "declareMetadata", new Object[] { meta } );
86 }
87
88 public Metadata getMetadata( String clientId ) throws SampException {
89 return Metadata
90 .asMetadata( asMap( exec( "getMetadata",
91 new Object[] { clientId } ) ) );
92 }
93
94 public void declareSubscriptions( Map subs ) throws SampException {
95 exec( "declareSubscriptions", new Object[] { subs } );
96 }
97
98 public Subscriptions getSubscriptions( String clientId )
99 throws SampException {
100 return Subscriptions
101 .asSubscriptions( asMap( exec( "getSubscriptions",
102 new Object[] { clientId } ) ) );
103 }
104
105 public String[] getRegisteredClients() throws SampException {
106 return (String[])
107 asList( exec( "getRegisteredClients", new Object[ 0 ] ) )
108 .toArray( new String[ 0 ] );
109 }
110
111 public Map getSubscribedClients( String mtype ) throws SampException {
112 return asMap( exec( "getSubscribedClients", new Object[] { mtype } ) );
113 }
114
115 public void notify( String recipientId, Map msg ) throws SampException {
116 exec( "notify", new Object[] { recipientId, msg } );
117 }
118
119 public List notifyAll( Map msg ) throws SampException {
120 return asList( exec( "notifyAll", new Object[] { msg } ) );
121 }
122
123 public String call( String recipientId, String msgTag, Map msg )
124 throws SampException {
125 return asString( exec( "call",
126 new Object[] { recipientId, msgTag, msg } ) );
127 }
128
129 public Map callAll( String msgTag, Map msg ) throws SampException {
130 return asMap( exec( "callAll", new Object[] { msgTag, msg } ) );
131 }
132
133 public Response callAndWait( String recipientId, Map msg, int timeout )
134 throws SampException {
135 return Response
136 .asResponse(
137 asMap( exec( "callAndWait",
138 new Object[] { recipientId, msg, SampUtils
139 .encodeInt( timeout ) } ) ) );
140 }
141
142 public void reply( String msgId, Map response ) throws SampException {
143 exec( "reply", new Object[] { msgId, response } );
144 }
145
146 /**
147 * Returns an object which is used as the first argument of most
148 * XML-RPC calls to the hub.
149 *
150 * @return SAMP-friendly object to identify this client
151 */
152 public abstract Object getClientKey();
153
154 /**
155 * Makes an XML-RPC call to the SAMP hub represented by this connection.
156 * The result of {@link #getClientKey} is passed as the first argument
157 * of the XML-RPC call.
158 *
159 * @param methodName unqualified SAMP hub API method name
160 * @param params array of method parameters
161 * @return XML-RPC call return value
162 */
163 public Object exec( String methodName, Object[] params )
164 throws SampException {
165 List paramList = new ArrayList();
166 paramList.add( getClientKey() );
167 for ( int ip = 0; ip < params.length; ip++ ) {
168 paramList.add( params[ ip ] );
169 }
170 return rawExec( prefix_ + methodName, paramList );
171 }
172
173 /**
174 * Actually makes an XML-RPC call to the SAMP hub represented by this
175 * connection.
176 *
177 * @param fqName fully qualified SAMP hub API method name
178 * @param paramList list of method parameters
179 * @return XML-RPC call return value
180 */
181 public Object rawExec( String fqName, List paramList )
182 throws SampException {
183 try {
184 return xClient_.callAndWait( fqName, paramList );
185 }
186 catch ( IOException e ) {
187 throw new SampException( e.getMessage(), e );
188 }
189 }
190
191 /**
192 * Unregisters if not already unregistered.
193 * May harmlessly be called multiple times.
194 */
195 private void finish() {
196 if ( ! unregistered_ ) {
197 try {
198 unregister();
199 }
200 catch ( SampException e ) {
201 }
202 }
203 }
204
205 /**
206 * Utility method to cast an object to a given SAMP-like type.
207 *
208 * @param obj object to cast
209 * @param clazz class to cast to
210 * @param name SAMP name of type
211 * @return obj
212 * @throws SampException if cast attempt failed
213 */
214 private static Object asType( Object obj, Class clazz, String name )
215 throws SampException {
216 if ( clazz.isAssignableFrom( obj.getClass() ) ) {
217 return obj;
218 }
219 else {
220 throw new SampException( "Hub returned unexpected type ("
221 + obj.getClass().getName() + " not "
222 + name );
223 }
224 }
225
226 /**
227 * Utility method to cast an object to a string.
228 *
229 * @param obj object
230 * @return object as string
231 * @throws SampException if cast attempt failed
232 */
233 private String asString( Object obj ) throws SampException {
234 return (String) asType( obj, String.class, "string" );
235 }
236
237 /**
238 * Utility method to cast an object to a list.
239 *
240 * @param obj object
241 * @return object as list
242 * @throws SampException if cast attempt failed
243 */
244 private List asList( Object obj ) throws SampException {
245 return (List) asType( obj, List.class, "list" );
246 }
247
248 /**
249 * Utility method to cast an object to a map.
250 *
251 * @param obj object
252 * @return object as map
253 * @throws SampException if cast attempt failed
254 */
255 private Map asMap( Object obj ) throws SampException {
256 return (Map) asType( obj, Map.class, "map" );
257 }
258 }