Package org.astrogrid.samp.hub
Class BasicHubService
java.lang.Object
org.astrogrid.samp.hub.BasicHubService
- All Implemented Interfaces:
HubService
- Direct Known Subclasses:
GuiHubService
HubService implementation.
- Since:
- 15 Jul 2008
- Author:
- Mark Taylor
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic intThe maximum timeout for a synchronous call permitted in seconds.static intThe maximum number of concurrently pending synchronous calls. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringDoes the work for thecallmethod of connections registered with this service.protected MapDoes the work for thecallmethod of connections registered with this service.protected ResponsecallAndWait(HubClient caller, String recipientId, Map message, int timeout) Does the work for thecallAndWaitmethod of connections registered with this service.protected HubClientcreateClient(String publicId, ProfileToken ptoken) Factory method used to create all the client objects which will be used by this hub service.protected ClientSetFactory method used to create the client set used by this hub service.protected HubConnectioncreateConnection(HubClient caller) Returns a new HubConnection for use by a given hub client.protected AbstractMessageHandler[]Constructs a list of MessageHandlers to use for the client provided by the Hub.protected voiddeclareMetadata(HubClient caller, Map meta) Does the work for thedeclareMetadatamethod of connections registered with this service.protected voiddeclareSubscriptions(HubClient caller, Map subscriptions) Does the work for thedeclareSubscriptionsmethod of connections registered with this service.voiddisconnect(String clientId, String reason) Forcibly disconnects a given client.voiddisconnectAll(ProfileToken profileToken) Forcibly terminates any connections created by a previous call ofHubService.register(org.astrogrid.samp.hub.ProfileToken)with a particularprofileToken.Returns the structure which keeps track of registered clients.Returns a comparator which will order client IDs.protected MetadatagetMetadata(HubClient caller, String clientId) Does the work for thegetMetadatamethod of connections registered with this service.protected String[]getRegisteredClients(HubClient caller) Does the work for thegetRegisteredClientsmethod of connections registered with this service.Returns the HubConnection object used by the hub itself to send and receive messages.protected MapgetSubscribedClients(HubClient caller, String mtype) Does the work for thegetSubscribedClientsmethod of connections registered with this service.protected SubscriptionsgetSubscriptions(HubClient caller, String clientId) Does the work for thegetSubscriptionsmethod of connections registered with this service.booleanIndicates whether this hub service is currently open for operations.protected voidDoes the work for thenotifymethod of connections registered with this service.protected ListDoes the work for thenotifyAllmethod of connections registered with this service.register(ProfileToken ptoken) Creates a new connection to this hub service, thereby initiating a new registered client.protected voidDoes the work for thereplymethod of connections registered with this service.protected voidsetCallable(HubClient caller, CallableClient callable) Does the work for thesetCallablemethod of connections registered with this service.voidshutdown()Tidies up any resources owned by this object.voidstart()Begin operation.protected voidunregister(HubClient caller) Does the work for theunregistermethod of conections registered with this service.
-
Field Details
-
MAX_TIMEOUT
public static int MAX_TIMEOUTThe maximum timeout for a synchronous call permitted in seconds. Default is 43200 = 12 hours. -
MAX_WAITERS
public static int MAX_WAITERSThe maximum number of concurrently pending synchronous calls. Default is 100.
-
-
Constructor Details
-
BasicHubService
Constructor.- Parameters:
random- random number generator used for message tags etc
-
-
Method Details
-
start
public void start()Description copied from interface:HubServiceBegin operation. TheHubService.register(org.astrogrid.samp.hub.ProfileToken)method should not be called until the hub has been started.- Specified by:
startin interfaceHubService
-
createClientSet
Factory method used to create the client set used by this hub service.- Returns:
- client set
-
createClient
Factory method used to create all the client objects which will be used by this hub service.- Parameters:
publicId- client public IDptoken- connection source- Returns:
- hub client
-
createHubMessageHandlers
Constructs a list of MessageHandlers to use for the client provided by the Hub.- Returns:
- hub message handler list
-
getIdComparator
Returns a comparator which will order client IDs. The ordering is in creation sequence.- Returns:
- public ID comparator
-
getClientSet
Returns the structure which keeps track of registered clients.- Returns:
- client set
-
register
Description copied from interface:HubServiceCreates a new connection to this hub service, thereby initiating a new registered client.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
HubConnectionmethods are declared to throwSampException, however, implementations may throw unchecked exceptions if that is more convenient; users of the connection should be prepared to catch these if they occur.- Specified by:
registerin interfaceHubService- Parameters:
ptoken- identifier for the profile acting as gatekeeper for this connection- Returns:
- new hub connection representing registration of a new client
- Throws:
SampException
-
disconnectAll
Description copied from interface:HubServiceForcibly terminates any connections created by a previous call ofHubService.register(org.astrogrid.samp.hub.ProfileToken)with a particularprofileToken. Any necessary hub events will be sent.- Specified by:
disconnectAllin interfaceHubService- Parameters:
profileToken- previous argument toregister
-
createConnection
Returns a new HubConnection for use by a given hub client. The instance methods of the returned object delegate to similarly named protected methods of this BasicHubService object. These BasicHubService methods may therefore be overridden to modify the behaviour of such returned connections.- Parameters:
caller- client requiring a connection- Returns:
- connection whose methods may be called by or on behalf of
caller
-
unregister
Does the work for theunregistermethod of conections registered with this service.- Parameters:
caller- client to unregister- Throws:
SampException- See Also:
-
setCallable
Does the work for thesetCallablemethod of connections registered with this service.- Parameters:
caller- clientcallable- callable object- Throws:
SampException- See Also:
-
declareMetadata
Does the work for thedeclareMetadatamethod of connections registered with this service.- Parameters:
caller- clientmeta- new metadata for client- Throws:
SampException- See Also:
-
getMetadata
Does the work for thegetMetadatamethod of connections registered with this service.- Parameters:
caller- calling clientclientId- id of client being queried- Returns:
- metadata for client
- Throws:
SampException- See Also:
-
declareSubscriptions
Does the work for thedeclareSubscriptionsmethod of connections registered with this service.- Parameters:
caller- clientsubscriptions- new subscriptions for client- Throws:
SampException- See Also:
-
getSubscriptions
Does the work for thegetSubscriptionsmethod of connections registered with this service.- Parameters:
caller- calling clientclientId- id of client being queried- Returns:
- subscriptions for client
- Throws:
SampException- See Also:
-
getRegisteredClients
Does the work for thegetRegisteredClientsmethod of connections registered with this service.- Parameters:
caller- calling client- Returns:
- array of registered client IDs excluding
caller's - Throws:
SampException- See Also:
-
getSubscribedClients
Does the work for thegetSubscribedClientsmethod of connections registered with this service.- Parameters:
caller- calling clientmtype- message type- Returns:
- map in which the keys are the public IDs of clients
subscribed to
mtype - Throws:
SampException- See Also:
-
notify
Does the work for thenotifymethod of connections registered with this service.- Parameters:
caller- calling clientrecipientId- public ID of client to receive messagemessage- message- Throws:
SampException- See Also:
-
call
protected String call(HubClient caller, String recipientId, String msgTag, Map message) throws SampException Does the work for thecallmethod of connections registered with this service.- Parameters:
caller- calling clientrecipientId- client ID of recipientmsgTag- message tagmessage- message- Returns:
- message ID
- Throws:
SampException- See Also:
-
notifyAll
Does the work for thenotifyAllmethod of connections registered with this service.- Parameters:
caller- calling clientmessage- message- Returns:
- list of public IDs for clients to which the notify will be sent
- Throws:
SampException- See Also:
-
callAll
Does the work for thecallmethod of connections registered with this service.- Parameters:
caller- calling clientmsgTag- message tagmessage- message- Returns:
- publicId->msgId map for clients to which an attempt to send the call will be made
- Throws:
SampException- See Also:
-
reply
Does the work for thereplymethod of connections registered with this service.- Parameters:
caller- calling clientmsgIdStr- message IDresp- response to forward- Throws:
SampException- See Also:
-
callAndWait
protected Response callAndWait(HubClient caller, String recipientId, Map message, int timeout) throws SampException Does the work for thecallAndWaitmethod of connections registered with this service.- Parameters:
caller- calling clientrecipientId- client ID of recipientmessage- messagetimeout- timeout in seconds- Returns:
- response response
- Throws:
SampException- See Also:
-
getServiceConnection
Returns the HubConnection object used by the hub itself to send and receive messages. This is the one which apparently sends samp.hub.event.shutdown messages etc.- Returns:
- hub service's own hub connection
-
disconnect
Forcibly disconnects a given client. This call does three things:- sends a
samp.hub.disconnectmessage to the client which is about to be ejected, if the client is subscribed to that MType - removes that client from this hub's client set so that any further communication attempts to or from it will fail
- broadcasts a
samp.hub.unregistermessage to all remaining clients indicating that the client has disappeared
- Parameters:
clientId- public-id of client to ejectreason- short text string indicating reason for ejection
- sends a
-
isHubRunning
public boolean isHubRunning()Description copied from interface:HubServiceIndicates whether this hub service is currently open for operations.- Specified by:
isHubRunningin interfaceHubService- Returns:
- true iff called between
HubService.start()andHubService.shutdown()
-
shutdown
public void shutdown()Description copied from interface:HubServiceTidies up any resources owned by this object. Should be called when no longer required.- Specified by:
shutdownin interfaceHubService
-