| 1 |
package org.astrogrid.samp.xmlrpc; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.lang.reflect.InvocationTargetException; |
| 5 |
import java.lang.reflect.Method; |
| 6 |
import java.net.MalformedURLException; |
| 7 |
import java.net.URL; |
| 8 |
import java.util.Arrays; |
| 9 |
import java.util.Collections; |
| 10 |
import java.util.HashMap; |
| 11 |
import java.util.List; |
| 12 |
import java.util.Map; |
| 13 |
import org.astrogrid.samp.RegInfo; |
| 14 |
import org.astrogrid.samp.SampUtils; |
| 15 |
import org.astrogrid.samp.client.ClientProfile; |
| 16 |
import org.astrogrid.samp.client.HubConnection; |
| 17 |
import org.astrogrid.samp.client.SampException; |
| 18 |
import org.astrogrid.samp.hub.KeyGenerator; |
| 19 |
|
| 20 |
/** |
| 21 |
* SampXmlRpcHandler implementation which passes Standard Profile-type XML-RPC |
| 22 |
* calls to a hub connection factory to provide a Standard Profile hub server. |
| 23 |
* |
| 24 |
* @author Mark Taylor |
| 25 |
* @since 15 Jul 2008 |
| 26 |
*/ |
| 27 |
class HubXmlRpcHandler extends ActorHandler { |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
* |
| 32 |
* @param xClientFactory XML-RPC client factory implementation |
| 33 |
* @param profile hub connection factory |
| 34 |
* @param secret password required for client registration |
| 35 |
*/ |
| 36 |
public HubXmlRpcHandler( SampXmlRpcClientFactory xClientFactory, |
| 37 |
ClientProfile profile, String secret, |
| 38 |
KeyGenerator keyGen ) { |
| 39 |
super( "samp.hub.", HubActor.class, |
| 40 |
new HubActorImpl( xClientFactory, profile, secret, keyGen ) ); |
| 41 |
} |
| 42 |
|
| 43 |
protected Object invokeMethod( Method method, Object obj, Object[] args ) |
| 44 |
throws IllegalAccessException, InvocationTargetException { |
| 45 |
return method.invoke( obj, args ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Implementation of the {@link HubActor} interface which does |
| 50 |
* the work for this class. |
| 51 |
* Apart from a few methods which have Standard-Profile-specific |
| 52 |
* aspects, the work is simply delegated to the hub connection factory. |
| 53 |
*/ |
| 54 |
private static class HubActorImpl implements HubActor { |
| 55 |
private final SampXmlRpcClientFactory xClientFactory_; |
| 56 |
private final ClientProfile profile_; |
| 57 |
private final String secret_; |
| 58 |
private final KeyGenerator keyGen_; |
| 59 |
private final Map clientMap_; |
| 60 |
|
| 61 |
/** |
| 62 |
* Constructor. |
| 63 |
* |
| 64 |
* @param xClientFactory XML-RPC client factory implementation |
| 65 |
* @param profile hub connection factory |
| 66 |
* @param secret password required for client registration |
| 67 |
* @param keyGen generator for private keys |
| 68 |
*/ |
| 69 |
HubActorImpl( SampXmlRpcClientFactory xClientFactory, |
| 70 |
ClientProfile profile, String secret, |
| 71 |
KeyGenerator keyGen ) { |
| 72 |
xClientFactory_ = xClientFactory; |
| 73 |
profile_ = profile; |
| 74 |
secret_ = secret; |
| 75 |
keyGen_ = keyGen; |
| 76 |
clientMap_ = Collections.synchronizedMap( new HashMap() ); |
| 77 |
} |
| 78 |
|
| 79 |
public Map register( String secret ) throws SampException { |
| 80 |
if ( ! profile_.isHubRunning() ) { |
| 81 |
throw new SampException( "Hub not running" ); |
| 82 |
} |
| 83 |
else if ( secret_.equals( secret ) ) { |
| 84 |
HubConnection connection = profile_.register(); |
| 85 |
if ( connection == null ) { |
| 86 |
throw new SampException( "Hub is not running" ); |
| 87 |
} |
| 88 |
String privateKey = keyGen_.next(); |
| 89 |
RegInfo regInfo = connection.getRegInfo(); |
| 90 |
regInfo.put( RegInfo.PRIVATEKEY_KEY, privateKey ); |
| 91 |
clientMap_.put( privateKey, connection ); |
| 92 |
return regInfo; |
| 93 |
} |
| 94 |
else { |
| 95 |
throw new SampException( "Bad password" ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public void unregister( String privateKey ) throws SampException { |
| 100 |
HubConnection connection = |
| 101 |
(HubConnection) clientMap_.remove( privateKey ); |
| 102 |
if ( connection == null ) { |
| 103 |
throw new SampException( "Unknown private key" ); |
| 104 |
} |
| 105 |
else { |
| 106 |
connection.unregister(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
public void ping( String privateKey ) throws SampException { |
| 111 |
getConnection( privateKey ).ping(); |
| 112 |
} |
| 113 |
|
| 114 |
public void setXmlrpcCallback( String privateKey, String surl ) |
| 115 |
throws SampException { |
| 116 |
SampXmlRpcClient xClient; |
| 117 |
try { |
| 118 |
xClient = xClientFactory_.createClient( new URL( surl ) ); |
| 119 |
} |
| 120 |
catch ( MalformedURLException e ) { |
| 121 |
throw new SampException( "Bad URL: " + surl, e ); |
| 122 |
} |
| 123 |
catch ( IOException e ) { |
| 124 |
throw new SampException( "No connection: " |
| 125 |
+ e.getMessage(), e ); |
| 126 |
} |
| 127 |
getConnection( privateKey ) |
| 128 |
.setCallable( new XmlRpcCallableClient( xClient, privateKey ) ); |
| 129 |
} |
| 130 |
|
| 131 |
public void declareMetadata( String privateKey, Map metadata ) |
| 132 |
throws SampException { |
| 133 |
getConnection( privateKey ).declareMetadata( metadata ); |
| 134 |
} |
| 135 |
|
| 136 |
public Map getMetadata( String privateKey, String clientId ) |
| 137 |
throws SampException { |
| 138 |
return getConnection( privateKey ).getMetadata( clientId ); |
| 139 |
} |
| 140 |
|
| 141 |
public void declareSubscriptions( String privateKey, Map subs ) |
| 142 |
throws SampException { |
| 143 |
getConnection( privateKey ).declareSubscriptions( subs ); |
| 144 |
} |
| 145 |
|
| 146 |
public Map getSubscriptions( String privateKey, String clientId ) |
| 147 |
throws SampException { |
| 148 |
return getConnection( privateKey ).getSubscriptions( clientId ); |
| 149 |
} |
| 150 |
|
| 151 |
public List getRegisteredClients( String privateKey ) |
| 152 |
throws SampException { |
| 153 |
return Arrays.asList( getConnection( privateKey ) |
| 154 |
.getRegisteredClients() ); |
| 155 |
} |
| 156 |
|
| 157 |
public Map getSubscribedClients( String privateKey, String mtype ) |
| 158 |
throws SampException { |
| 159 |
return getConnection( privateKey ).getSubscribedClients( mtype ); |
| 160 |
} |
| 161 |
|
| 162 |
public void notify( String privateKey, String recipientId, Map msg ) |
| 163 |
throws SampException { |
| 164 |
getConnection( privateKey ).notify( recipientId, msg ); |
| 165 |
} |
| 166 |
|
| 167 |
public List notifyAll( String privateKey, Map msg ) |
| 168 |
throws SampException { |
| 169 |
return getConnection( privateKey ).notifyAll( msg ); |
| 170 |
} |
| 171 |
|
| 172 |
public String call( String privateKey, String recipientId, |
| 173 |
String msgTag, Map msg ) |
| 174 |
throws SampException { |
| 175 |
return getConnection( privateKey ).call( recipientId, msgTag, msg ); |
| 176 |
} |
| 177 |
|
| 178 |
public Map callAll( String privateKey, String msgTag, Map msg ) |
| 179 |
throws SampException { |
| 180 |
return getConnection( privateKey ).callAll( msgTag, msg ); |
| 181 |
} |
| 182 |
|
| 183 |
public Map callAndWait( String privateKey, String recipientId, Map msg, |
| 184 |
String timeoutStr ) |
| 185 |
throws SampException { |
| 186 |
int timeout; |
| 187 |
try { |
| 188 |
timeout = SampUtils.decodeInt( timeoutStr ); |
| 189 |
} |
| 190 |
catch ( Exception e ) { |
| 191 |
throw new SampException( "Bad timeout format" |
| 192 |
+ " (should be SAMP int)", e ); |
| 193 |
} |
| 194 |
return getConnection( privateKey ) |
| 195 |
.callAndWait( recipientId, msg, timeout ); |
| 196 |
} |
| 197 |
|
| 198 |
public void reply( String privateKey, String msgId, Map response ) |
| 199 |
throws SampException { |
| 200 |
getConnection( privateKey ).reply( msgId, response ); |
| 201 |
} |
| 202 |
|
| 203 |
public void ping() throws SampException { |
| 204 |
if ( ! profile_.isHubRunning() ) { |
| 205 |
throw new SampException( "Hub is stopped" ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Returns the HubConnection associated with a private key used |
| 211 |
* by this hub actor. |
| 212 |
* |
| 213 |
* @param privateKey private key |
| 214 |
* @return connection for <code>privateKey</code> |
| 215 |
*/ |
| 216 |
private HubConnection getConnection( String privateKey ) |
| 217 |
throws SampException { |
| 218 |
HubConnection connection = |
| 219 |
(HubConnection) clientMap_.get( privateKey ); |
| 220 |
if ( connection == null ) { |
| 221 |
throw new SampException( "Unknown private key" ); |
| 222 |
} |
| 223 |
else { |
| 224 |
return connection; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
} |