| 1 |
package org.astrogrid.samp.hub; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.HashMap; |
| 5 |
import java.util.List; |
| 6 |
import java.util.Map; |
| 7 |
import org.astrogrid.samp.Message; |
| 8 |
import org.astrogrid.samp.Metadata; |
| 9 |
import org.astrogrid.samp.client.AbstractMessageHandler; |
| 10 |
import org.astrogrid.samp.client.HubConnection; |
| 11 |
|
| 12 |
/** |
| 13 |
* Implements MType for querying registered clients by metadata item. |
| 14 |
* |
| 15 |
* @author Mark Taylor |
| 16 |
* @since 21 Nov 2011 |
| 17 |
*/ |
| 18 |
class MetaQueryMessageHandler extends AbstractMessageHandler { |
| 19 |
|
| 20 |
private final ClientSet clientSet_; |
| 21 |
private static final String BASE_MTYPE = "query.by-meta"; |
| 22 |
|
| 23 |
/** |
| 24 |
* Constructor. |
| 25 |
* |
| 26 |
* @param clientSet hub client set object |
| 27 |
*/ |
| 28 |
public MetaQueryMessageHandler( ClientSet clientSet ) { |
| 29 |
super( new String[] { "samp." + BASE_MTYPE, "x-samp." + BASE_MTYPE } ); |
| 30 |
clientSet_ = clientSet; |
| 31 |
} |
| 32 |
|
| 33 |
public Map processCall( HubConnection conn, String senderId, Message msg ) { |
| 34 |
String key = (String) msg.getRequiredParam( "key" ); |
| 35 |
String value = (String) msg.getRequiredParam( "value" ); |
| 36 |
HubClient[] clients = clientSet_.getClients(); |
| 37 |
List foundList = new ArrayList(); |
| 38 |
for ( int ic = 0; ic < clients.length; ic++ ) { |
| 39 |
HubClient client = clients[ ic ]; |
| 40 |
Metadata meta = client.getMetadata(); |
| 41 |
if ( meta != null && value.equals( meta.get( key ) ) ) { |
| 42 |
foundList.add( client.getId() ); |
| 43 |
} |
| 44 |
} |
| 45 |
Map result = new HashMap(); |
| 46 |
result.put( "ids", foundList ); |
| 47 |
return result; |
| 48 |
} |
| 49 |
} |