public class BasicHubService extends java.lang.Object implements HubService
Modifier and Type | Class and Description |
---|---|
private static class |
BasicHubService.ClientIdGenerator
Generates client public IDs.
|
private static class |
BasicHubService.MessageId
Encapsulates information about a MessageId.
|
Modifier and Type | Field and Description |
---|---|
private ClientSet |
clientSet_ |
private static char |
ID_DELIMITER |
private BasicHubService.ClientIdGenerator |
idGen_ |
private static ProfileToken |
INTERNAL_PROFILE |
private KeyGenerator |
keyGen_ |
private java.util.logging.Logger |
logger_ |
static int |
MAX_TIMEOUT
The maximum timeout for a synchronous call permitted in seconds.
|
static int |
MAX_WAITERS
The maximum number of concurrently pending synchronous calls.
|
private HubClient |
serviceClient_ |
private HubConnection |
serviceClientConnection_ |
private boolean |
shutdown_ |
private boolean |
started_ |
private java.util.Map |
waiterMap_ |
Constructor and Description |
---|
BasicHubService(java.util.Random random)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.String |
call(HubClient caller,
java.lang.String recipientId,
java.lang.String msgTag,
java.util.Map message)
Does the work for the
call method of connections
registered with this service. |
protected java.util.Map |
callAll(HubClient caller,
java.lang.String msgTag,
java.util.Map message)
Does the work for the
call method of connections
registered with this service. |
protected Response |
callAndWait(HubClient caller,
java.lang.String recipientId,
java.util.Map message,
int timeout)
Does the work for the
callAndWait method of connections
registered with this service. |
private boolean |
canSend(HubClient sender,
HubClient recipient,
java.lang.String mtype)
Indicates whether a given send is permitted.
|
private void |
checkSend(HubClient sender,
HubClient recipient,
java.lang.String mtype)
Checks if a given send is permitted.
|
protected HubClient |
createClient(java.lang.String publicId,
ProfileToken ptoken)
Factory method used to create all the client objects which will
be used by this hub service.
|
protected ClientSet |
createClientSet()
Factory method used to create the client set used by this hub service.
|
protected HubConnection |
createConnection(HubClient caller)
Returns a new HubConnection for use by a given hub client.
|
protected AbstractMessageHandler[] |
createHubMessageHandlers()
Constructs a list of MessageHandlers to use for the client
provided by the Hub.
|
protected void |
declareMetadata(HubClient caller,
java.util.Map meta)
Does the work for the
declareMetadata method of connections
registered with this service. |
protected void |
declareSubscriptions(HubClient caller,
java.util.Map subscriptions)
Does the work for the
declareSubscriptions method of
connections registered with this service. |
private void |
disconnect(java.lang.String[] clientIds,
Message discoMsg)
Forcibly disconnects a number of clients for the same reason.
|
void |
disconnect(java.lang.String clientId,
java.lang.String reason)
Forcibly disconnects a given client.
|
void |
disconnectAll(ProfileToken profileToken)
Forcibly terminates any connections created by a previous call of
HubService.register(org.astrogrid.samp.hub.ProfileToken)
with a particular profileToken . |
private HubClient |
getClient(java.lang.String id)
Returns the client object corresponding to a public client ID.
|
ClientSet |
getClientSet()
Returns the structure which keeps track of registered clients.
|
java.util.Comparator |
getIdComparator()
Returns a comparator which will order client IDs.
|
protected Metadata |
getMetadata(HubClient caller,
java.lang.String clientId)
Does the work for the
getMetadata method of connections
registered with this service. |
protected java.lang.String[] |
getRegisteredClients(HubClient caller)
Does the work for the
getRegisteredClients method of
connections registered with this service. |
private java.lang.String |
getSendError(HubClient sender,
HubClient recipient,
java.lang.String mtype)
Does the work to determine whether a given sending client is
permitted to send a message with a given MType to a given recipient.
|
HubConnection |
getServiceConnection()
Returns the HubConnection object used by the hub itself to send
and receive messages.
|
protected java.util.Map |
getSubscribedClients(HubClient caller,
java.lang.String mtype)
Does the work for the
getSubscribedClients method of
connections registered with this service. |
protected Subscriptions |
getSubscriptions(HubClient caller,
java.lang.String clientId)
Does the work for the
getSubscriptions method of connections
registered with this service. |
private Subscriptions |
getSubscriptionsFor(HubClient client,
Subscriptions subs)
Returns the view of a given subscriptions map to be presented to
a sending client.
|
private void |
hubEvent(Message msg)
Broadcast an event message to all subscribed clients.
|
boolean |
isHubRunning()
Indicates whether this hub service is currently open for operations.
|
protected void |
notify(HubClient caller,
java.lang.String recipientId,
java.util.Map message)
Does the work for the
notify method of connections
registered with this service. |
protected java.util.List |
notifyAll(HubClient caller,
java.util.Map message)
Does the work for the
notifyAll method of connections
registered with this service. |
HubConnection |
register(ProfileToken ptoken)
Creates a new connection to this hub service, thereby initiating
a new registered client.
|
protected void |
reply(HubClient caller,
java.lang.String msgIdStr,
java.util.Map resp)
Does the work for the
reply method of connections
registered with this service. |
protected void |
setCallable(HubClient caller,
CallableClient callable)
Does the work for the
setCallable method of connections
registered with this service. |
void |
shutdown()
Tidies up any resources owned by this object.
|
void |
start()
Begin operation.
|
protected void |
unregister(HubClient caller)
Does the work for the
unregister method of conections
registered with this service. |
private final KeyGenerator keyGen_
private final BasicHubService.ClientIdGenerator idGen_
private final java.util.Map waiterMap_
private ClientSet clientSet_
private HubClient serviceClient_
private HubConnection serviceClientConnection_
private volatile boolean started_
private volatile boolean shutdown_
private static final char ID_DELIMITER
private final java.util.logging.Logger logger_
private static final ProfileToken INTERNAL_PROFILE
public static int MAX_TIMEOUT
public static int MAX_WAITERS
public BasicHubService(java.util.Random random)
random
- random number generator used for message tags etcpublic void start()
HubService
HubService.register(org.astrogrid.samp.hub.ProfileToken)
method should not be
called until the hub has been started.start
in interface HubService
protected ClientSet createClientSet()
protected HubClient createClient(java.lang.String publicId, ProfileToken ptoken)
publicId
- client public IDptoken
- connection sourceprotected AbstractMessageHandler[] createHubMessageHandlers()
public java.util.Comparator getIdComparator()
public ClientSet getClientSet()
public HubConnection register(ProfileToken ptoken) throws SampException
HubService
It is the responsibility of the returned connection, not the
user of that connection, to broadcast the various
samp.hub.event.*
notifications at the appropriate times.
Most of the HubConnection
methods are declared to
throw SampException
, however, implementations may
throw unchecked exceptions if that is more convenient;
users of the connection should be prepared to catch these if
they occur.
register
in interface HubService
ptoken
- identifier for the profile acting as gatekeeper
for this connectionSampException
public void disconnectAll(ProfileToken profileToken)
HubService
HubService.register(org.astrogrid.samp.hub.ProfileToken)
with a particular profileToken
.
Any necessary hub events will be sent.disconnectAll
in interface HubService
profileToken
- previous argument to register
protected HubConnection createConnection(HubClient caller)
caller
- client requiring a connectioncaller
protected void unregister(HubClient caller) throws SampException
unregister
method of conections
registered with this service.caller
- client to unregisterSampException
HubConnection.unregister()
protected void setCallable(HubClient caller, CallableClient callable) throws SampException
setCallable
method of connections
registered with this service.caller
- clientcallable
- callable objectSampException
HubConnection.setCallable(org.astrogrid.samp.client.CallableClient)
protected void declareMetadata(HubClient caller, java.util.Map meta) throws SampException
declareMetadata
method of connections
registered with this service.caller
- clientmeta
- new metadata for clientSampException
HubConnection.declareMetadata(java.util.Map)
protected Metadata getMetadata(HubClient caller, java.lang.String clientId) throws SampException
getMetadata
method of connections
registered with this service.caller
- calling clientclientId
- id of client being queriedSampException
HubConnection.getMetadata(java.lang.String)
protected void declareSubscriptions(HubClient caller, java.util.Map subscriptions) throws SampException
declareSubscriptions
method of
connections registered with this service.caller
- clientsubscriptions
- new subscriptions for clientSampException
HubConnection.declareSubscriptions(java.util.Map)
protected Subscriptions getSubscriptions(HubClient caller, java.lang.String clientId) throws SampException
getSubscriptions
method of connections
registered with this service.caller
- calling clientclientId
- id of client being queriedSampException
HubConnection.getSubscriptions(java.lang.String)
protected java.lang.String[] getRegisteredClients(HubClient caller) throws SampException
getRegisteredClients
method of
connections registered with this service.caller
- calling clientcaller
'sSampException
HubConnection.getRegisteredClients()
protected java.util.Map getSubscribedClients(HubClient caller, java.lang.String mtype) throws SampException
getSubscribedClients
method of
connections registered with this service.caller
- calling clientmtype
- message typemtype
SampException
HubConnection.getSubscribedClients(java.lang.String)
protected void notify(HubClient caller, java.lang.String recipientId, java.util.Map message) throws SampException
notify
method of connections
registered with this service.caller
- calling clientrecipientId
- public ID of client to receive messagemessage
- messageSampException
HubConnection.notify(java.lang.String, java.util.Map)
protected java.lang.String call(HubClient caller, java.lang.String recipientId, java.lang.String msgTag, java.util.Map message) throws SampException
call
method of connections
registered with this service.caller
- calling clientrecipientId
- client ID of recipientmsgTag
- message tagmessage
- messageSampException
HubConnection.call(java.lang.String, java.lang.String, java.util.Map)
protected java.util.List notifyAll(HubClient caller, java.util.Map message) throws SampException
notifyAll
method of connections
registered with this service.caller
- calling clientmessage
- messageSampException
HubConnection.notifyAll(java.util.Map)
protected java.util.Map callAll(HubClient caller, java.lang.String msgTag, java.util.Map message) throws SampException
call
method of connections
registered with this service.caller
- calling clientmsgTag
- message tagmessage
- messageSampException
HubConnection.callAll(java.lang.String, java.util.Map)
protected void reply(HubClient caller, java.lang.String msgIdStr, java.util.Map resp) throws SampException
reply
method of connections
registered with this service.caller
- calling clientmsgIdStr
- message IDresp
- response to forwardSampException
HubConnection.reply(java.lang.String, java.util.Map)
protected Response callAndWait(HubClient caller, java.lang.String recipientId, java.util.Map message, int timeout) throws SampException
callAndWait
method of connections
registered with this service.caller
- calling clientrecipientId
- client ID of recipientmessage
- messagetimeout
- timeout in secondsSampException
HubConnection.callAndWait(java.lang.String, java.util.Map, int)
public HubConnection getServiceConnection()
public void disconnect(java.lang.String clientId, java.lang.String reason)
samp.hub.disconnect
message to the
client which is about to be ejected, if the client is
subscribed to that MTypesamp.hub.unregister
message to all
remaining clients indicating that the client has disappearedclientId
- public-id of client to ejectreason
- short text string indicating reason for ejectionprivate void disconnect(java.lang.String[] clientIds, Message discoMsg)
clientIds
- public-ids of clients to disconnectdiscoMsg
- message to send to clients which will effect
disconnection (samp.hub.disconnect or samp.hub.event.shutdown)public boolean isHubRunning()
HubService
isHubRunning
in interface HubService
HubService.start()
and HubService.shutdown()
public void shutdown()
HubService
shutdown
in interface HubService
private void hubEvent(Message msg)
msg
- message to broadcastprivate HubClient getClient(java.lang.String id) throws SampException
id
- client public idSampException
private void checkSend(HubClient sender, HubClient recipient, java.lang.String mtype) throws SampException
sender
- sending clientrecipient
- receiving clientmtype
- MTypeSampException
- if the send is not permittedprivate boolean canSend(HubClient sender, HubClient recipient, java.lang.String mtype)
sender
- sending clientrecipient
- receiving clientmtype
- MTypeprivate java.lang.String getSendError(HubClient sender, HubClient recipient, java.lang.String mtype)
canSend(org.astrogrid.samp.hub.HubClient, org.astrogrid.samp.hub.HubClient, java.lang.String)
and checkSend(org.astrogrid.samp.hub.HubClient, org.astrogrid.samp.hub.HubClient, java.lang.String)
.sender
- sending clientrecipient
- receiving clientmtype
- MTypeprivate Subscriptions getSubscriptionsFor(HubClient client, Subscriptions subs)
client
- client to view subscriptionssubs
- basic subscription mapclient