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.

  • Field Details

  • Constructor Details

    • RmiHostedService

      public RmiHostedService()
    • RmiHostedService

      public RmiHostedService(byte defaultChannel)
    • RmiHostedService

      public RmiHostedService(short rmiId, byte defaultChannel, boolean autoHost)
  • 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.
    • 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.
    • 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.
    • 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.
    • getAutoHost

      public boolean getAutoHost()
      Returns true if RMI hosting is automatically started for all new connections.
    • 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.
    • 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.
    • 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.
    • 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>
    • 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
    • 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