Debian Codegraph @

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

raw
1 package org.astrogrid.samp.xmlrpc;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6 import org.astrogrid.samp.Message;
7 import org.astrogrid.samp.Response;
8 import org.astrogrid.samp.ShutdownManager;
9 import org.astrogrid.samp.client.CallableClient;
10 import org.astrogrid.samp.client.SampException;
11
12 /**
13 * CallableClient implementation used to communicate with XML-RPC-based
14 * callable clients.
15 *
16 * @author Mark Taylor
17 * @since 28 Jan 2011
18 */
19 class XmlRpcCallableClient implements CallableClient {
20
21 private final SampXmlRpcClient xClient_;
22 private final String privateKey_;
23 private static volatile boolean isShutdown_;
24 static {
25 ShutdownManager.getInstance()
26 .registerHook( XmlRpcCallableClient.class,
27 ShutdownManager.PREPARE_SEQUENCE,
28 new Runnable() {
29 public void run() {
30 isShutdown_ = true;
31 }
32 } );
33 }
34
35 /**
36 * Constructor.
37 *
38 * @param xClient XML-RPC client implementation
39 * @param SAMP client's private key
40 */
41 public XmlRpcCallableClient( SampXmlRpcClient xClient, String privateKey ) {
42 xClient_ = xClient;
43 privateKey_ = privateKey;
44 }
45
46 public void receiveCall( String senderId, String msgId, Message msg )
47 throws SampException {
48 exec( "receiveCall", new Object[] { senderId, msgId, msg, } );
49 }
50
51 public void receiveNotification( String senderId, Message msg )
52 throws SampException {
53 exec( "receiveNotification", new Object[] { senderId, msg, } );
54 }
55
56 public void receiveResponse( String responderId, String msgTag,
57 Response response )
58 throws SampException {
59 exec( "receiveResponse",
60 new Object[] { responderId, msgTag, response, } );
61 }
62
63 /**
64 * Makes an XML-RPC call to the SAMP callable client represented
65 * by this receiver.
66 *
67 * @param methodName unqualified SAMP callable client API method name
68 * @param params array of method parameters
69 */
70 private void exec( String methodName, Object[] params )
71 throws SampException {
72 List paramList = new ArrayList();
73 paramList.add( privateKey_ );
74 for ( int ip = 0; ip < params.length; ip++ ) {
75 paramList.add( params[ ip ] );
76 }
77 try {
78 rawExec( "samp.client." + methodName, paramList );
79 }
80 catch ( IOException e ) {
81 throw new SampException( e.getMessage(), e );
82 }
83 }
84
85 /**
86 * Actually makes an XML-RPC call to the SAMP callable client
87 * represented by this receiver.
88 *
89 * @param fqName fully qualified SAMP callable client API method name
90 * @param paramList list of method parameters
91 */
92 private void rawExec( String fqName, List paramList ) throws IOException {
93
94 // In most cases, callAndForget is adequate.
95 // However, if the JVM is in the process of shutting down, the
96 // hub shutdown that this triggers will attempt to message clients
97 // to tell them so, and the threads that callAndForget uses to
98 // handle those calls will die during the communications, meaning
99 // that the clients are not properly informed of shutdown.
100 // So in the case of shutdown, do it synchronously.
101 if ( isShutdown_ ) {
102 xClient_.callAndWait( fqName, paramList );
103 }
104 else {
105 xClient_.callAndForget( fqName, paramList );
106 }
107 }
108 }