Class AbstractHostedConnectionService

All Implemented Interfaces:
ConnectionListener, HostedService, Service<HostedServiceManager>
Direct Known Subclasses:

public abstract class AbstractHostedConnectionService extends AbstractHostedService
Convenient base class for HostedServices providing some default HostedService interface implementations as well as a few convenience methods such as getServiceManager() and getService(type). This implementation enhances the default capabilities provided by AbstractHostedService by adding automatic connection management.

Subclasses must at least override the onInitialize(), startHostingOnConnection(), and stopHostingOnConnection() methods to handle service and connection initialization.

An autoHost flag controls whether startHostingOnConnection() is called automatically when new connections are detected. If autoHost is false then it is up to the implementation or application to specifically start hosting at some point.

  • Constructor Details

    • AbstractHostedConnectionService

      protected AbstractHostedConnectionService()
      Creates a new HostedService that will autohost connections when detected.
    • AbstractHostedConnectionService

      protected AbstractHostedConnectionService(boolean autoHost)
      Creates a new HostedService that will automatically host connections only if autoHost is true.
  • Method Details

    • setAutoHost

      public void setAutoHost(boolean b)
      When set to true, all new connections will automatically have hosting services attached to them by calling startHostingOnConnection(). If this is set to false then it is up to the application or other services to eventually call startHostingOnConnection().

      Reasons for doing this vary but usually would be because the client shouldn't be allowed to perform any service-related calls until it has provided more information... for example, logging in.

    • getAutoHost

      public boolean getAutoHost()
      Returns true if this service automatically attaches hosting capabilities to new connections.
    • startHostingOnConnection

      public abstract void startHostingOnConnection(HostedConnection hc)
      Performs implementation specific connection hosting setup. Generally this involves setting up some handlers or session attributes on the connection. If autoHost is true then this method is called automatically during connectionAdded() processing.
    • stopHostingOnConnection

      public abstract void stopHostingOnConnection(HostedConnection hc)
      Performs implementation specific connection tear-down. This will be called automatically when the connectionRemoved() event occurs... whether the application has already called it or not.
    • 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
      connectionAdded in class AbstractHostedService
    • connectionRemoved

      public void connectionRemoved(Server server, HostedConnection hc)
      Called internally when an existing connection is leaving the server. This method always calls stopHostingOnConnection(hc). Implementations should be aware that if they stopHostingOnConnection() early that they will get a second call when the connection goes away.
      Specified by:
      connectionRemoved in interface ConnectionListener
      connectionRemoved in class AbstractHostedService