| 1 |
package org.astrogrid.samp.xmlrpc; |
| 2 |
|
| 3 |
import java.lang.reflect.InvocationTargetException; |
| 4 |
import java.lang.reflect.Method; |
| 5 |
import java.util.HashMap; |
| 6 |
import java.util.Map; |
| 7 |
import java.util.logging.Level; |
| 8 |
import java.util.logging.Logger; |
| 9 |
import org.astrogrid.samp.ErrInfo; |
| 10 |
import org.astrogrid.samp.Message; |
| 11 |
import org.astrogrid.samp.Response; |
| 12 |
import org.astrogrid.samp.client.CallableClient; |
| 13 |
import org.astrogrid.samp.client.HubConnection; |
| 14 |
|
| 15 |
/** |
| 16 |
* SampXmlRpcHandler implementation which passes Standard Profile-like XML-RPC |
| 17 |
* calls to one or more {@link CallableClient}s to provide client callbacks |
| 18 |
* from the hub. |
| 19 |
* |
| 20 |
* @author Mark Taylor |
| 21 |
* @since 16 Jul 2008 |
| 22 |
*/ |
| 23 |
class ClientXmlRpcHandler extends ActorHandler { |
| 24 |
|
| 25 |
private final ClientActorImpl clientActor_; |
| 26 |
private static final Logger logger_ = |
| 27 |
Logger.getLogger( ClientXmlRpcHandler.class.getName() ); |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
*/ |
| 32 |
public ClientXmlRpcHandler() { |
| 33 |
super( "samp.client.", ClientActor.class, new ClientActorImpl() ); |
| 34 |
clientActor_ = (ClientActorImpl) getActor(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Adds a CallableClient object to this server. |
| 39 |
* |
| 40 |
* @param connection hub connection for the registered client on behalf |
| 41 |
* of which the client will operate |
| 42 |
* @param callable callable client object |
| 43 |
*/ |
| 44 |
public void addClient( HubConnection connection, CallableClient callable ) { |
| 45 |
clientActor_.entryMap_.put( connection.getRegInfo().getPrivateKey(), |
| 46 |
new Entry( connection, callable ) ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Removes a CallableClient object from this server. |
| 51 |
* |
| 52 |
* @param privateKey hub connection for which this client was added |
| 53 |
*/ |
| 54 |
public void removeClient( HubConnection connection ) { |
| 55 |
clientActor_.entryMap_ |
| 56 |
.remove( connection.getRegInfo().getPrivateKey() ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns the number of clients currently owned by this handler. |
| 61 |
* |
| 62 |
* @return client count |
| 63 |
*/ |
| 64 |
public int getClientCount() { |
| 65 |
return clientActor_.entryMap_.size(); |
| 66 |
} |
| 67 |
|
| 68 |
protected Object invokeMethod( Method method, Object obj, Object[] args ) |
| 69 |
throws IllegalAccessException, InvocationTargetException { |
| 70 |
return method.invoke( obj, args ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Implementation of the {@link ClientActor} interface which does the |
| 75 |
* work for this class. |
| 76 |
* The correct CallableClient is determined from the private key, |
| 77 |
* and the work is then delegated to it. |
| 78 |
*/ |
| 79 |
private static class ClientActorImpl implements ClientActor { |
| 80 |
|
| 81 |
private Map entryMap_ = new HashMap(); // String -> Entry |
| 82 |
|
| 83 |
public void receiveNotification( String privateKey, |
| 84 |
final String senderId, Map msg ) { |
| 85 |
Entry entry = getEntry( privateKey ); |
| 86 |
final CallableClient callable = entry.callable_; |
| 87 |
final Message message = Message.asMessage( msg ); |
| 88 |
final String label = "Notify " + senderId + " " |
| 89 |
+ message.getMType(); |
| 90 |
new Thread( label ) { |
| 91 |
public void run() { |
| 92 |
try { |
| 93 |
callable.receiveNotification( senderId, message ); |
| 94 |
} |
| 95 |
catch ( Throwable e ) { |
| 96 |
logger_.log( Level.INFO, label + " error", e ); |
| 97 |
} |
| 98 |
} |
| 99 |
}.start(); |
| 100 |
} |
| 101 |
|
| 102 |
public void receiveCall( String privateKey, final String senderId, |
| 103 |
final String msgId, Map msg ) |
| 104 |
throws Exception { |
| 105 |
Entry entry = getEntry( privateKey ); |
| 106 |
final CallableClient callable = entry.callable_; |
| 107 |
final HubConnection connection = entry.connection_; |
| 108 |
final Message message = Message.asMessage( msg ); |
| 109 |
final String label = "Call " + senderId + " " + message.getMType(); |
| 110 |
new Thread( label ) { |
| 111 |
public void run() { |
| 112 |
try { |
| 113 |
callable.receiveCall( senderId, msgId, message ); |
| 114 |
} |
| 115 |
catch ( Throwable e ) { |
| 116 |
try { |
| 117 |
Response response = |
| 118 |
Response |
| 119 |
.createErrorResponse( new ErrInfo( e ) ); |
| 120 |
connection.reply( msgId, response ); |
| 121 |
} |
| 122 |
catch ( Throwable e2 ) { |
| 123 |
logger_.log( Level.INFO, |
| 124 |
label + " error replying", e2 ); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
}.start(); |
| 129 |
} |
| 130 |
|
| 131 |
public void receiveResponse( String privateKey, |
| 132 |
final String responderId, |
| 133 |
final String msgTag, Map resp ) |
| 134 |
throws Exception { |
| 135 |
Entry entry = getEntry( privateKey ); |
| 136 |
final CallableClient callable = entry.callable_; |
| 137 |
final Response response = Response.asResponse( resp ); |
| 138 |
final String label = "Reply " + responderId; |
| 139 |
new Thread( label ) { |
| 140 |
public void run() { |
| 141 |
try { |
| 142 |
callable.receiveResponse( responderId, msgTag, |
| 143 |
response ); |
| 144 |
} |
| 145 |
catch ( Throwable e ) { |
| 146 |
logger_.log( Level.INFO, label + " error replying", e ); |
| 147 |
} |
| 148 |
} |
| 149 |
}.start(); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Returns the CallableClient corresponding to a given private key. |
| 154 |
* |
| 155 |
* @param privateKey private key for client |
| 156 |
* @return entry identified by privateKey |
| 157 |
* @throws IllegalStateException if <code>privateKey</code> is unknown |
| 158 |
*/ |
| 159 |
private Entry getEntry( String privateKey ) { |
| 160 |
Object ent = entryMap_.get( privateKey ); |
| 161 |
if ( ent instanceof Entry ) { |
| 162 |
return (Entry) ent; |
| 163 |
} |
| 164 |
else { |
| 165 |
throw new IllegalStateException( "Client is not listening" ); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Utility class to aggregate information about a client. |
| 172 |
*/ |
| 173 |
private static class Entry { |
| 174 |
final HubConnection connection_; |
| 175 |
final CallableClient callable_; |
| 176 |
|
| 177 |
/** |
| 178 |
* Constructor. |
| 179 |
* |
| 180 |
* @param connection hub connection |
| 181 |
* @param callable callable client |
| 182 |
*/ |
| 183 |
Entry( HubConnection connection, CallableClient callable ) { |
| 184 |
connection_ = connection; |
| 185 |
callable_ = callable; |
| 186 |
} |
| 187 |
} |
| 188 |
} |