| 1 |
package org.astrogrid.samp.hub; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Collections; |
| 5 |
import java.util.HashMap; |
| 6 |
import java.util.List; |
| 7 |
import java.util.Map; |
| 8 |
import java.util.logging.Logger; |
| 9 |
import org.astrogrid.samp.Message; |
| 10 |
import org.astrogrid.samp.RegInfo; |
| 11 |
import org.astrogrid.samp.Subscriptions; |
| 12 |
import org.astrogrid.samp.client.CallableClient; |
| 13 |
import org.astrogrid.samp.client.ClientProfile; |
| 14 |
import org.astrogrid.samp.client.HubConnection; |
| 15 |
import org.astrogrid.samp.client.SampException; |
| 16 |
|
| 17 |
/** |
| 18 |
* HubService that provides hub functionality by accessing an existing |
| 19 |
* hub service. The existing hub service is defined by a supplied |
| 20 |
* ClientProfile object. |
| 21 |
* |
| 22 |
* @author Mark Taylor |
| 23 |
* @since 1 Feb 2011 |
| 24 |
*/ |
| 25 |
public class FacadeHubService implements HubService { |
| 26 |
|
| 27 |
private final ClientProfile profile_; |
| 28 |
private final Map connectionMap_; // FacadeHubConnection -> ProfileToken |
| 29 |
private static final Logger logger_ = |
| 30 |
Logger.getLogger( FacadeHubService.class.getName() ); |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor. |
| 34 |
* |
| 35 |
* @param profile defines the hub connection factory on which this |
| 36 |
* service is based |
| 37 |
*/ |
| 38 |
public FacadeHubService( ClientProfile profile ) { |
| 39 |
profile_ = profile; |
| 40 |
connectionMap_ = Collections.synchronizedMap( new HashMap() ); |
| 41 |
} |
| 42 |
|
| 43 |
public boolean isHubRunning() { |
| 44 |
return profile_.isHubRunning(); |
| 45 |
} |
| 46 |
|
| 47 |
public HubConnection register( ProfileToken profileToken ) |
| 48 |
throws SampException { |
| 49 |
|
| 50 |
// Mostly delegate registration to the underlying client profile, |
| 51 |
// but put in place machinery to keep track of which clients |
| 52 |
// are registered via this service. This will be required so that |
| 53 |
// they can be messaged if the underlying hub shuts down. |
| 54 |
HubConnection baseConnection = profile_.register(); |
| 55 |
if ( baseConnection != null ) { |
| 56 |
HubConnection conn = new FacadeHubConnection( baseConnection ) { |
| 57 |
final HubConnection hubConn = this; |
| 58 |
public void ping() throws SampException { |
| 59 |
if ( FacadeHubService.this.isHubRunning() ) { |
| 60 |
super.ping(); |
| 61 |
} |
| 62 |
else { |
| 63 |
throw new SampException( "Hub underlying facade " |
| 64 |
+ "is not running" ); |
| 65 |
} |
| 66 |
} |
| 67 |
public void unregister() throws SampException { |
| 68 |
connectionMap_.keySet().remove( hubConn ); |
| 69 |
super.unregister(); |
| 70 |
} |
| 71 |
}; |
| 72 |
connectionMap_.put( conn, profileToken ); |
| 73 |
return conn; |
| 74 |
} |
| 75 |
|
| 76 |
// Or return null if there is no underlying hub. |
| 77 |
else { |
| 78 |
return null; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public void disconnectAll( ProfileToken profileToken ) { |
| 83 |
Map.Entry[] entries = (Map.Entry[]) |
| 84 |
connectionMap_.entrySet() |
| 85 |
.toArray( new Map.Entry[ 0 ] ); |
| 86 |
List ejectList = new ArrayList(); |
| 87 |
for ( int ie = 0; ie < entries.length; ie++ ) { |
| 88 |
if ( profileToken.equals( entries[ ie ].getValue() ) ) { |
| 89 |
ejectList.add( entries[ ie ].getKey() ); |
| 90 |
} |
| 91 |
} |
| 92 |
FacadeHubConnection[] ejectConns = |
| 93 |
(FacadeHubConnection[]) |
| 94 |
ejectList.toArray( new FacadeHubConnection[ 0 ] ); |
| 95 |
int nc = ejectConns.length; |
| 96 |
Message discoMsg = new Message( "samp.hub.event.shutdown" ); |
| 97 |
String[] ejectIds = new String[ nc ]; |
| 98 |
for ( int ic = 0; ic < nc; ic++ ) { |
| 99 |
FacadeHubConnection conn = ejectConns[ ic ]; |
| 100 |
ejectIds[ ic ] = conn.getRegInfo().getSelfId(); |
| 101 |
conn.hubEvent( discoMsg ); |
| 102 |
connectionMap_.remove( conn ); |
| 103 |
} |
| 104 |
for ( int ic = 0; ic < nc; ic++ ) { |
| 105 |
hubEvent( new Message( "samp.hub.event.unregister" ) |
| 106 |
.addParam( "id", ejectIds[ ic ] ) ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* No-op. |
| 112 |
*/ |
| 113 |
public void start() { |
| 114 |
} |
| 115 |
|
| 116 |
public void shutdown() { |
| 117 |
hubEvent( new Message( "samp.hub.event.shutdown" ) ); |
| 118 |
connectionMap_.clear(); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Sends a given message by notification, as if from the hub, |
| 123 |
* to all the clients which have registered through this service. |
| 124 |
* |
| 125 |
* @param msg message to send |
| 126 |
*/ |
| 127 |
private void hubEvent( Message msg ) { |
| 128 |
String mtype = msg.getMType(); |
| 129 |
FacadeHubConnection[] connections = |
| 130 |
(FacadeHubConnection[]) |
| 131 |
connectionMap_.keySet().toArray( new FacadeHubConnection[ 0 ] ); |
| 132 |
for ( int ic = 0; ic < connections.length; ic++ ) { |
| 133 |
connections[ ic ].hubEvent( msg ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Utility HubConnection class which allows hub event notifications |
| 139 |
* to be sent to clients. |
| 140 |
*/ |
| 141 |
private static class FacadeHubConnection extends WrapperHubConnection { |
| 142 |
private CallableClient callable_; |
| 143 |
private Subscriptions subs_; |
| 144 |
|
| 145 |
/** |
| 146 |
* Constructor. |
| 147 |
* |
| 148 |
* @param base base connection |
| 149 |
*/ |
| 150 |
FacadeHubConnection( HubConnection base ) { |
| 151 |
super( base ); |
| 152 |
} |
| 153 |
|
| 154 |
public void setCallable( CallableClient callable ) |
| 155 |
throws SampException { |
| 156 |
super.setCallable( callable ); |
| 157 |
callable_ = callable; |
| 158 |
} |
| 159 |
|
| 160 |
public void declareSubscriptions( Map subs ) throws SampException { |
| 161 |
super.declareSubscriptions( subs ); |
| 162 |
subs_ = subs == null ? null |
| 163 |
: Subscriptions.asSubscriptions( subs ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Sends a given message as a notification, as if from the hub, |
| 168 |
* to this connection if it is able to receive it. |
| 169 |
* |
| 170 |
* @param msg message to send |
| 171 |
*/ |
| 172 |
void hubEvent( Message msg ) { |
| 173 |
String mtype = msg.getMType(); |
| 174 |
CallableClient callable = callable_; |
| 175 |
if ( callable != null && subs_.isSubscribed( mtype ) ) { |
| 176 |
RegInfo regInfo = getRegInfo(); |
| 177 |
try { |
| 178 |
callable.receiveNotification( regInfo.getHubId(), msg ); |
| 179 |
} |
| 180 |
catch ( Throwable e ) { |
| 181 |
logger_.info( "Failed " + mtype + " notification to " |
| 182 |
+ regInfo.getSelfId() ); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |