Class RmiHostedService

All Implemented Interfaces:
ConnectionListener, HostedService, Service<HostedServiceManager>

public class RmiHostedService extends AbstractHostedService
A service that can be added to the host to support a simple shared objects protocol.

Objects are shared by adding them to the RmiRegistry with one of the share() methods. Shared objects must have a separate interface and implementation. The interface is what the other end of the connection will use to interact with the object and that interface class must be available on both ends of the connection. The implementing class need only be on the sharing end.

Shared objects can be accessed on the other end of the connection by using one of the RmiRegistry's getRemoteObject() methods. These can be used to lookup an object by class if it is a shared singleton or by name if it was registered with a name.

On the hosting side, a special shardGlobal() method is provided that will register shared objects that will automatically be provided to every new joining client and they will all be calling the same server-side instance. Normally, shared objects themselves are connection specific and handled at the connection layer. The shareGlobal() space is a way to have global resources passed directly though the need is relatively rare.

Note: This RMI implementation is not as advanced as Java's regular RMI as it won't marshall shared references, ie: you can't pass a shared objects as an argument to another shared object's method.

Author:
Paul Speed
  • Field Details

    • ATTRIBUTE_NAME

      public static final String ATTRIBUTE_NAME
      Hosted connection attribute used to store the connection-specific RMI registry.
      See Also:
  • Constructor Details

    • RmiHostedService

      public RmiHostedService()
      Creates an RMI hosted service using the default object id and reliable channel.
    • RmiHostedService

      public RmiHostedService(byte defaultChannel)
      Creates an RMI hosted service using the specified default channel.
      Parameters:
      defaultChannel - the default network channel for RMI traffic
    • RmiHostedService

      public RmiHostedService(short rmiId, byte defaultChannel, boolean autoHost)
      Creates an RMI hosted service.
      Parameters:
      rmiId - the shared RMI object id
      defaultChannel - the default network channel for RMI traffic
      autoHost - true to start hosting automatically for new connections
  • Method Details

    • shareGlobal

      public <T> void shareGlobal(T object, Class<? super T> type)
      Shares a server-wide object associated with the specified type. All connections with RMI hosting started will have access to this shared object as soon as they connect, and they will all share the same instance. It is up to the shared object to handle any multithreading that might be required.
      Type Parameters:
      T - the shared object type
      Parameters:
      object - the object to share
      type - the shared interface type
    • shareGlobal

      public <T> void shareGlobal(String name, T object, Class<? super T> type)
      Shares a server-wide object associated with the specified name. All connections with RMI hosting started will have access to this shared object as soon as they connect, and they will all share the same instance. It is up to the shared object to handle any multithreading that might be required.
      Type Parameters:
      T - the shared object type
      Parameters:
      name - the exported object name
      object - the object to share
      type - the shared interface type
    • shareGlobal

      public <T> void shareGlobal(byte channel, String name, T object, Class<? super T> type)
      Shares a server-wide object associated with the specified name over the specified channel. All connections with RMI hosting started will have access to this shared object as soon as they connect, and they will all share the same instance. It is up to the shared object to handle any multithreading that might be required. All network communication associated with the shared object will be done over the specified channel.
      Type Parameters:
      T - the shared object type
      Parameters:
      channel - the network channel to use
      name - the exported object name
      object - the object to share
      type - the shared interface type
    • setAutoHost

      public void setAutoHost(boolean b)
      Set to true if all new connections should automatically have RMI hosting started. Set to false if the game-specific connection setup will call startHostingOnConnection() after some connection setup is done (for example, logging in). Note: generally is safe to autohost RMI as long as callers are careful about what they've added using shareGlobal(). One reasonable use-case is to shareGlobal() some kind of login service and nothing else. All other shared objects would then be added as connection-specific objects during successful login processing.
      Parameters:
      b - true to auto-host RMI on new connections
    • getAutoHost

      public boolean getAutoHost()
      Returns true if RMI hosting is automatically started for all new connections.
      Returns:
      true if auto-hosting is enabled
    • getRmiRegistry

      public RmiRegistry getRmiRegistry(HostedConnection hc)
      Returns the RMI registry for the specific HostedConnection. Each connection has its own registry with its own connection-specific shared objects.
      Parameters:
      hc - the hosted connection
      Returns:
      the connection-specific RMI registry, or null if not started
    • startHostingOnConnection

      public void startHostingOnConnection(HostedConnection hc)
      Sets up RMI hosting services for the hosted connection allowing getRmiRegistry() to return a valid RmiRegistry object. This method is called automatically for all new connections if autohost is set to true.
      Parameters:
      hc - the hosted connection to initialize
    • stopHostingOnConnection

      public void stopHostingOnConnection(HostedConnection hc)
      Removes any RMI hosting services associated with the specified connection. Calls to getRmiRegistry() will return null for this connection. This method is called automatically for all leaving connections if autohost is set to true.
      Parameters:
      hc - the hosted connection to stop hosting on
    • onInitialize

      protected void onInitialize(HostedServiceManager s)
      Description copied from class: AbstractService
      Called during initialize() for the subclass to perform implementation specific initialization.
      Specified by:
      onInitialize in class AbstractService<HostedServiceManager>
      Parameters:
      s - the service manager initializing this service
    • connectionAdded

      public void connectionAdded(Server server, HostedConnection hc)
      Called internally when a new connection is detected for the server. If the current autoHost property is true then startHostingOnConnection(hc) is called.
      Specified by:
      connectionAdded in interface ConnectionListener
      Overrides:
      connectionAdded in class AbstractHostedService
      Parameters:
      server - the server that accepted the connection
      hc - the newly connected client
    • connectionRemoved

      public void connectionRemoved(Server server, HostedConnection hc)
      Called internally when an existing connection is leaving the server. If the current autoHost property is true then stopHostingOnConnection(hc) is called.
      Specified by:
      connectionRemoved in interface ConnectionListener
      Overrides:
      connectionRemoved in class AbstractHostedService
      Parameters:
      server - the server that removed the connection
      hc - the disconnected client