Debian Codegraph @

src/java/org/astrogrid/samp/web/WebHubConnection.java

raw
1 package org.astrogrid.samp.web;
2
3 import java.util.Collections;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.logging.Level;
8 import java.util.logging.Logger;
9 import org.astrogrid.samp.Metadata;
10 import org.astrogrid.samp.SampUtils;
11 import org.astrogrid.samp.client.CallableClient;
12 import org.astrogrid.samp.client.SampException;
13 import org.astrogrid.samp.xmlrpc.SampXmlRpcClient;
14 import org.astrogrid.samp.xmlrpc.XmlRpcHubConnection;
15
16 /**
17 * HubConnection implementation for the Web Profile.
18 *
19 * @author Mark Taylor
20 * @since 3 Feb 2011
21 */
22 class WebHubConnection extends XmlRpcHubConnection {
23
24 private final String appName_;
25 private final String clientKey_;
26 private CallWorker callWorker_;
27 private static Logger logger_ =
28 Logger.getLogger( WebHubConnection.class.getName() );
29
30 /**
31 * Constructor.
32 *
33 * @param xClient XML-RPC client
34 * @param securityMap security information map
35 * @param appName client's declared name
36 */
37 public WebHubConnection( SampXmlRpcClient xClient, Map securityMap )
38 throws SampException {
39 super( xClient, WebClientProfile.WEBSAMP_HUB_PREFIX,
40 Collections.singletonList( securityMap ) );
41 Object nameObj = securityMap.get( Metadata.NAME_KEY );
42 appName_ = nameObj instanceof String ? (String) nameObj : "??";
43 clientKey_ = getRegInfo().getPrivateKey();
44 }
45
46 public Object getClientKey() {
47 return clientKey_;
48 }
49
50 public void setCallable( CallableClient client ) throws SampException {
51 CallWorker oldWorker = callWorker_;
52 callWorker_ = null;
53 if ( oldWorker != null ) {
54 oldWorker.stopped_ = true;
55 }
56 exec( "allowReverseCallbacks",
57 new Object[] { SampUtils.encodeBoolean( client != null ) } );
58 if ( client != null ) {
59 CallWorker callWorker = new CallWorker( this, client, appName_ );
60 callWorker.start();
61 callWorker_ = callWorker;
62 }
63 }
64
65 /**
66 * Thread that performs repeated long polls to pull callbacks from the
67 * hub and passes them on to this connection's CallableClient for
68 * execution.
69 */
70 private static class CallWorker extends Thread {
71
72 private final XmlRpcHubConnection xconn_;
73 private final CallableClient client_;
74 private final int timeoutSec_ = 60 * 10;
75 private final long minWaitMillis_ = 5 * 1000;
76 private volatile boolean stopped_;
77
78 /**
79 * Constructor.
80 *
81 * @param xconn hub connection
82 * @parma client callable client
83 * @param appName client's name
84 */
85 CallWorker( XmlRpcHubConnection xconn, CallableClient client,
86 String appName ) {
87 super( "Web Profile Callback Puller for " + appName );
88 xconn_ = xconn;
89 client_ = client;
90 setDaemon( true );
91 }
92
93 public void run() {
94 String stimeout = SampUtils.encodeInt( timeoutSec_ );
95 while ( true && ! stopped_ ) {
96 long start = System.currentTimeMillis();
97 Object result;
98 try {
99 result = xconn_.exec( "pullCallbacks",
100 new Object[] { stimeout } );
101 }
102 catch ( Exception e ) {
103 long wait = System.currentTimeMillis() - start;
104 if ( wait < minWaitMillis_ ) {
105 seriousError( e );
106 }
107 else {
108 logger_.config( "pullCallbacks timeout? "
109 + ( wait / 1000 ) + "s" );
110 }
111 break;
112 }
113 catch ( Throwable e ) {
114 seriousError( e );
115 break;
116 }
117 if ( ! stopped_ ) {
118 if ( result instanceof List ) {
119 List resultList = (List) result;
120 for ( Iterator it = resultList.iterator();
121 it.hasNext(); ) {
122 try {
123 final Callback cb =
124 new Callback( (Map) it.next() );
125 new Thread( "Web Profile Callback" ) {
126 public void run() {
127 try {
128 ClientCallbackOperation
129 .invoke( cb, client_ );
130 }
131 catch ( Throwable e ) {
132 logger_.log( Level.WARNING,
133 "Callback failure: "
134 + e.getMessage(), e );
135 }
136 }
137 }.start();
138 }
139 catch ( Throwable e ) {
140 logger_.log( Level.WARNING, e.getMessage(), e );
141 }
142 }
143 }
144 else {
145 logger_.warning( "pullCallbacks result "
146 + "is not a List - ignore" );
147 }
148 }
149 }
150 }
151
152 /**
153 * Invoked if there is a serious (non-timeout) error when polling
154 * for callbacks. This currently stops the polling for good.
155 * That may be a drastic response, but at least it prevents
156 * repeated high-frequency polling attempts to a broken server,
157 * which might otherwise result.
158 *
159 * @parm e error which caused the trouble
160 */
161 private void seriousError( Throwable e ) {
162 stopped_ = true;
163 logger_.log( Level.WARNING,
164 "Fatal pullCallbacks error - stopped listening", e );
165 }
166 }
167 }