com.threerings.bureau.server
Class BureauRegistry

java.lang.Object
  extended by com.threerings.bureau.server.BureauRegistry

public class BureauRegistry
extends Object

Abstracts the launching and termination of external processes (bureaus) that host instances of server-side code (agents).


Nested Class Summary
protected static class BureauRegistry.AgentData
           
protected static class BureauRegistry.AgentState
           
protected static class BureauRegistry.Bureau
          Models a bureau, including the process handle, all running agents and their states.
static interface BureauRegistry.CommandGenerator
          Defines how to generate a command to launch a bureau in a local process.
protected static class BureauRegistry.FoundAgent
          Models the results of searching for an agent.
static interface BureauRegistry.Launcher
          Defines how a bureau is launched.
protected static class BureauRegistry.LauncherEntry
           
protected  class BureauRegistry.LauncherUnit
          Invoker unit to launch a bureau's process, then assign the result on the main thread.
 
Field Summary
protected  Map<String,BureauRegistry.Bureau> _bureaus
           
protected  Invoker _invoker
           
protected  Map<String,BureauRegistry.LauncherEntry> _launchers
           
protected  RootDObjectManager _omgr
           
 
Constructor Summary
BureauRegistry(InvocationManager invmgr, PresentsConnectionManager conmgr, ClientManager clmgr)
          Creates an uninitialized registry.
 
Method Summary
protected  void agentCreated(ClientObject client, int agentId)
          Callback for when the bureau client acknowledges the creation of an agent.
protected  void agentCreationFailed(ClientObject client, int agentId)
          Callback for when the bureau client acknowledges the failure to create an agent.
protected  void agentDestroyed(ClientObject client, int agentId)
          Callback for when the bureau client acknowledges the destruction of an agent.
protected  void bureauError(ClientObject caller, String message)
           
protected  void bureauInitialized(ClientObject client, String bureauId)
          Callback for when the bureau client acknowledges starting up.
 String checkToken(BureauCredentials creds)
          Check the credentials to make sure this is one of our bureaus.
protected  void clientDestroyed(BureauRegistry.Bureau bureau)
          Callback for when a client is destroyed.
 void destroyAgent(AgentObject agent)
          Destroys a previously started agent using the data in the given object.
protected  String generateToken(String bureauId)
          Create a hard-to-guess token that the bureau can use to authenticate itself when it tries to log in.
protected  Class<? extends BureauClientResolver> getClientResolverClass()
          Returns the class used to resolve bureau client data.
 Exception getLaunchError(AgentObject agentObj)
          If this agent's bureau encountered an error on launch, return it.
protected  Class<? extends BureauSession> getSessionClass()
          Returns the class used to handle bureau sessions.
protected  void handleLaunchError(BureauRegistry.Bureau bureau, Exception error, String cause)
          Called when something goes wrong with launching a bureau.
protected  void launchTimeoutExpired(BureauRegistry.Bureau bureau)
          Called by the launcher unit timeout time after launching.
 PresentsSession lookupClient(String bureauId)
          Returns the active session for a bureau of the given id.
protected  BureauRegistry.FoundAgent resolve(ClientObject client, int agentId, String resolver)
          Does lots of null checks and lookups and resolves the given information into FoundAgent.
protected  void sessionDidEnd(PresentsSession client, String id)
           
protected  void sessionDidStart(PresentsSession client, String id)
           
 void setCommandGenerator(String bureauType, BureauRegistry.CommandGenerator cmdGenerator)
          Registers a command generator for a given type.
 void setCommandGenerator(String bureauType, BureauRegistry.CommandGenerator cmdGenerator, int timeout)
          Registers a command generator for a given type.
 void setLauncher(String bureauType, BureauRegistry.Launcher launcher)
          Registers a launcher for a given type.
 void setLauncher(String bureauType, BureauRegistry.Launcher launcher, int timeout)
          Registers a launcher for a given type.
 void startAgent(AgentObject agent)
          Starts a new agent using the data in the given object, creating a new bureau if necessary.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_launchers

protected Map<String,BureauRegistry.LauncherEntry> _launchers

_bureaus

protected Map<String,BureauRegistry.Bureau> _bureaus

_omgr

@Inject
protected RootDObjectManager _omgr

_invoker

@Inject
protected Invoker _invoker
Constructor Detail

BureauRegistry

@Inject
public BureauRegistry(InvocationManager invmgr,
                             PresentsConnectionManager conmgr,
                             ClientManager clmgr)
Creates an uninitialized registry.

Method Detail

checkToken

public String checkToken(BureauCredentials creds)
Check the credentials to make sure this is one of our bureaus.

Returns:
null if all's well, otherwise a string describing the authentication failure

setCommandGenerator

public void setCommandGenerator(String bureauType,
                                BureauRegistry.CommandGenerator cmdGenerator)
Registers a command generator for a given type. When an agent is started and no bureaus are running, the bureauType is used to determine the CommandGenerator instance to call. The registry will wait indefinitely for the bureau to connect back.

Parameters:
bureauType - the type of bureau that will be launched
cmdGenerator - the generator to be used for bureaus of bureauType

setCommandGenerator

public void setCommandGenerator(String bureauType,
                                BureauRegistry.CommandGenerator cmdGenerator,
                                int timeout)
Registers a command generator for a given type. When an agent is started and no bureaus are running, the bureauType is used to determine the CommandGenerator instance to call. If the launched bureau does not connect within the given number of milliseconds, it will be logged as an error and future attempts to launch the bureau will try launching the command again.

Parameters:
bureauType - the type of bureau that will be launched
cmdGenerator - the generator to be used for bureaus of bureauType
timeout - milliseconds to wait for the bureau or 0 to wait forever

setLauncher

public void setLauncher(String bureauType,
                        BureauRegistry.Launcher launcher)
Registers a launcher for a given type. When an agent is started and no bureaus are running, the bureauType is used to determine the Launcher instance to call. The registry will wait indefinitely for the launched bureau to connect back.

Parameters:
bureauType - the type of bureau that will be launched
launcher - the launcher to be used for bureaus of bureauType

setLauncher

public void setLauncher(String bureauType,
                        BureauRegistry.Launcher launcher,
                        int timeout)
Registers a launcher for a given type. When an agent is started and no bureaus are running, the bureauType is used to determine the Launcher instance to call. If the launched bureau does not connect within the given number of milliseconds, it will be logged as an error and future attempts to launch the bureau will invoke the launch method again.

Parameters:
bureauType - the type of bureau that will be launched
launcher - the launcher to be used for bureaus of bureauType
timeout - milliseconds to wait for the bureau or 0 to wait forever

startAgent

public void startAgent(AgentObject agent)
Starts a new agent using the data in the given object, creating a new bureau if necessary.


destroyAgent

public void destroyAgent(AgentObject agent)
Destroys a previously started agent using the data in the given object.


lookupClient

public PresentsSession lookupClient(String bureauId)
Returns the active session for a bureau of the given id.


getLaunchError

public Exception getLaunchError(AgentObject agentObj)
If this agent's bureau encountered an error on launch, return it.


sessionDidStart

protected void sessionDidStart(PresentsSession client,
                               String id)

sessionDidEnd

protected void sessionDidEnd(PresentsSession client,
                             String id)

bureauInitialized

protected void bureauInitialized(ClientObject client,
                                 String bureauId)
Callback for when the bureau client acknowledges starting up. Starts all pending agents and causes subsequent agent start requests to be sent directly to the bureau.


bureauError

protected void bureauError(ClientObject caller,
                           String message)

agentCreated

protected void agentCreated(ClientObject client,
                            int agentId)
Callback for when the bureau client acknowledges the creation of an agent.


agentCreationFailed

protected void agentCreationFailed(ClientObject client,
                                   int agentId)
Callback for when the bureau client acknowledges the failure to create an agent.


agentDestroyed

protected void agentDestroyed(ClientObject client,
                              int agentId)
Callback for when the bureau client acknowledges the destruction of an agent.


clientDestroyed

protected void clientDestroyed(BureauRegistry.Bureau bureau)
Callback for when a client is destroyed.


resolve

protected BureauRegistry.FoundAgent resolve(ClientObject client,
                                            int agentId,
                                            String resolver)
Does lots of null checks and lookups and resolves the given information into FoundAgent.


generateToken

protected String generateToken(String bureauId)
Create a hard-to-guess token that the bureau can use to authenticate itself when it tries to log in.


launchTimeoutExpired

protected void launchTimeoutExpired(BureauRegistry.Bureau bureau)
Called by the launcher unit timeout time after launching.

Parameters:
bureau - bureau whose launch occurred

handleLaunchError

protected void handleLaunchError(BureauRegistry.Bureau bureau,
                                 Exception error,
                                 String cause)
Called when something goes wrong with launching a bureau.


getSessionClass

protected Class<? extends BureauSession> getSessionClass()
Returns the class used to handle bureau sessions.


getClientResolverClass

protected Class<? extends BureauClientResolver> getClientResolverClass()
Returns the class used to resolve bureau client data.