Interface Server

All Known Implementing Classes:
DefaultServer

public interface Server
Represents a host that can send and receive messages to a set of remote client connections.
Author:
Paul Speed
  • Method Details

    • getGameName

      String getGameName()
      Returns the 'game name' for this server. This should match the 'game name' set on connecting clients or they will be turned away.
      Returns:
      the server game name
    • getVersion

      int getVersion()
      Returns the game-specific version of this server used for detecting mismatched clients.
      Returns:
      the server protocol version
    • getServices

      HostedServiceManager getServices()
      Returns the manager for hosted services. Hosted services extend the functionality of the server.
      Returns:
      the hosted service manager
    • broadcast

      void broadcast(Message message)
      Sends the specified message to all connected clients.
      Parameters:
      message - the message to broadcast
    • broadcast

      void broadcast(Filter<? super HostedConnection> filter, Message message)
      Sends the specified message to all connected clients that match the filter. If no filter is specified then this is the same as calling broadcast(message) and the message will be delivered to all connections.

      Examples:

        // Broadcast to connections: conn1, conn2, and conn3
        server.broadcast( Filters.in( conn1, conn2, conn3 ), message );
      
        // Broadcast to all connections exception source
        server.broadcast( Filters.notEqualTo( source ), message );
      
      Parameters:
      filter - the connection filter, or null to target all connections
      message - the message to broadcast
    • broadcast

      void broadcast(int channel, Filter<? super HostedConnection> filter, Message message)
      Sends the specified message over the specified alternate channel to all connected clients that match the filter. If no filter is specified then this is the same as calling broadcast(message) and the message will be delivered to all connections.

      Examples:

        // Broadcast to connections: conn1, conn2, and conn3
        server.broadcast( Filters.in( conn1, conn2, conn3 ), message );
      
        // Broadcast to all connections exception source
        server.broadcast( Filters.notEqualTo( source ), message );
      
      Parameters:
      channel - the alternate channel to use
      filter - the connection filter, or null to target all connections
      message - the message to broadcast
    • start

      void start()
      Start the server so that it will begin accepting new connections and processing messages.
    • addChannel

      int addChannel(int port)
      Adds an alternate channel to the server, using the specified port. This is an entirely separate connection where messages are sent and received in parallel to the two primary default channels. All channels must be added before the connection is started. Returns the ID of the created channel for use when specifying the channel in send or broadcast calls. The ID is returned entirely out of convenience since the IDs are predictably incremented. The first channel is 0, second is 1, and so on.
      Parameters:
      port - the port to bind for the new channel
      Returns:
      the newly assigned channel id
    • isRunning

      boolean isRunning()
      Returns true if the server has been started.
      Returns:
      true if the server is running
    • close

      void close()
      Closes all client connections, stops and running processing threads, and closes the host connection.
    • getConnection

      HostedConnection getConnection(int id)
      Retrieves a hosted connection by ID.
      Parameters:
      id - the server-assigned connection id
      Returns:
      the matching connection, or null if none exists
    • getConnections

      Collection<HostedConnection> getConnections()
      Retrieves a read-only collection of all currently connected connections.
      Returns:
      the current hosted connections
    • hasConnections

      boolean hasConnections()
      Returns true if the server has active connections at the time of this call.
      Returns:
      true if at least one connection is active
    • addConnectionListener

      void addConnectionListener(ConnectionListener listener)
      Adds a listener that will be notified when new hosted connections arrive.
      Parameters:
      listener - the listener to add
    • removeConnectionListener

      void removeConnectionListener(ConnectionListener listener)
      Removes a previously registered connection listener.
      Parameters:
      listener - the listener to remove
    • addMessageListener

      void addMessageListener(MessageListener<? super HostedConnection> listener)
      Adds a listener that will be notified when any message or object is received from one of the clients.

      Note about MessageListener multithreading: on the server, message events may be delivered by more than one thread depending on the server implementation used. Listener implementations should treat their shared data structures accordingly and set them up for multithreaded access. The only threading guarantee is that for a single HostedConnection, there will only ever be one thread at a time and the messages will always be delivered to that connection in the order that they were delivered. This is the only restriction placed upon server message dispatch pool implementations.

      Parameters:
      listener - the listener to add
    • addMessageListener

      void addMessageListener(MessageListener<? super HostedConnection> listener, Class... classes)
      Adds a listener that will be notified when messages of the specified types are received from one of the clients.
      Parameters:
      listener - the listener to add
      classes - the message classes to match
    • removeMessageListener

      void removeMessageListener(MessageListener<? super HostedConnection> listener)
      Removes a previously registered wildcard listener. This does not remove this listener from any type-specific registrations.
      Parameters:
      listener - the listener to remove
    • removeMessageListener

      void removeMessageListener(MessageListener<? super HostedConnection> listener, Class... classes)
      Removes a previously registered type-specific listener from the specified types.
      Parameters:
      listener - the listener to remove
      classes - the message classes to unregister