Class DefaultServer

java.lang.Object
com.jme3.network.base.DefaultServer
All Implemented Interfaces:
Server

public class DefaultServer extends Object implements Server
A default implementation of the Server interface that delegates its network connectivity to kernel.Kernel.
Author:
Paul Speed
  • Constructor Details

    • DefaultServer

      public DefaultServer(String gameName, int version, Kernel reliable, Kernel fast)
      Creates a server with the specified game name, version, and primary kernels.
      Parameters:
      gameName - the expected game name
      version - the expected protocol version
      reliable - the reliable kernel
      fast - the optional fast kernel
  • Method Details

    • addStandardServices

      protected void addStandardServices()
      Adds the default hosted services.
    • getGameName

      public String getGameName()
      Description copied from interface: Server
      Returns the 'game name' for this server. This should match the 'game name' set on connecting clients or they will be turned away.
      Specified by:
      getGameName in interface Server
      Returns:
      the server game name
    • getVersion

      public int getVersion()
      Description copied from interface: Server
      Returns the game-specific version of this server used for detecting mismatched clients.
      Specified by:
      getVersion in interface Server
      Returns:
      the server protocol version
    • getServices

      public HostedServiceManager getServices()
      Description copied from interface: Server
      Returns the manager for hosted services. Hosted services extend the functionality of the server.
      Specified by:
      getServices in interface Server
      Returns:
      the hosted service manager
    • addChannel

      public int addChannel(int port)
      Description copied from interface: Server
      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.
      Specified by:
      addChannel in interface Server
      Parameters:
      port - the port to bind for the new channel
      Returns:
      the newly assigned channel id
    • checkChannel

      protected void checkChannel(int channel)
      Validates an alternate server channel index.
      Parameters:
      channel - the channel index to validate
    • start

      public void start()
      Description copied from interface: Server
      Start the server so that it will begin accepting new connections and processing messages.
      Specified by:
      start in interface Server
    • isRunning

      public boolean isRunning()
      Description copied from interface: Server
      Returns true if the server has been started.
      Specified by:
      isRunning in interface Server
      Returns:
      true if the server is running
    • close

      public void close()
      Description copied from interface: Server
      Closes all client connections, stops and running processing threads, and closes the host connection.
      Specified by:
      close in interface Server
    • broadcast

      public void broadcast(Message message)
      Description copied from interface: Server
      Sends the specified message to all connected clients.
      Specified by:
      broadcast in interface Server
      Parameters:
      message - the message to broadcast
    • broadcast

      public void broadcast(Filter<? super HostedConnection> filter, Message message)
      Description copied from interface: Server
      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 );
      
      Specified by:
      broadcast in interface Server
      Parameters:
      filter - the connection filter, or null to target all connections
      message - the message to broadcast
    • broadcast

      public void broadcast(int channel, Filter<? super HostedConnection> filter, Message message)
      Description copied from interface: Server
      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 );
      
      Specified by:
      broadcast in interface Server
      Parameters:
      channel - the alternate channel to use
      filter - the connection filter, or null to target all connections
      message - the message to broadcast
    • getConnection

      public HostedConnection getConnection(int id)
      Description copied from interface: Server
      Retrieves a hosted connection by ID.
      Specified by:
      getConnection in interface Server
      Parameters:
      id - the server-assigned connection id
      Returns:
      the matching connection, or null if none exists
    • hasConnections

      public boolean hasConnections()
      Description copied from interface: Server
      Returns true if the server has active connections at the time of this call.
      Specified by:
      hasConnections in interface Server
      Returns:
      true if at least one connection is active
    • getConnections

      public Collection<HostedConnection> getConnections()
      Description copied from interface: Server
      Retrieves a read-only collection of all currently connected connections.
      Specified by:
      getConnections in interface Server
      Returns:
      the current hosted connections
    • addConnectionListener

      public void addConnectionListener(ConnectionListener listener)
      Description copied from interface: Server
      Adds a listener that will be notified when new hosted connections arrive.
      Specified by:
      addConnectionListener in interface Server
      Parameters:
      listener - the listener to add
    • removeConnectionListener

      public void removeConnectionListener(ConnectionListener listener)
      Description copied from interface: Server
      Removes a previously registered connection listener.
      Specified by:
      removeConnectionListener in interface Server
      Parameters:
      listener - the listener to remove
    • addMessageListener

      public void addMessageListener(MessageListener<? super HostedConnection> listener)
      Description copied from interface: Server
      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.

      Specified by:
      addMessageListener in interface Server
      Parameters:
      listener - the listener to add
    • addMessageListener

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

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

      public void removeMessageListener(MessageListener<? super HostedConnection> listener, Class... classes)
      Description copied from interface: Server
      Removes a previously registered type-specific listener from the specified types.
      Specified by:
      removeMessageListener in interface Server
      Parameters:
      listener - the listener to remove
      classes - the message classes to unregister
    • dispatch

      protected void dispatch(HostedConnection source, Message m)
      Dispatches a received message to registered listeners.
      Parameters:
      source - the hosted connection that sent the message, or null
      m - the received message
    • fireConnectionAdded

      protected void fireConnectionAdded(HostedConnection conn)
      Notifies registered listeners that a connection was added.
      Parameters:
      conn - the added connection
    • fireConnectionRemoved

      protected void fireConnectionRemoved(HostedConnection conn)
      Notifies registered listeners that a connection was removed.
      Parameters:
      conn - the removed connection
    • getChannel

      protected int getChannel(KernelAdapter ka)
      Returns the internal channel index for the specified adapter or -1 if not found
      Parameters:
      ka - the kernel adapter
      Returns:
      the adapter index
    • registerClient

      protected void registerClient(KernelAdapter ka, Endpoint p, ClientRegistrationMessage m)
      Registers or completes registration for a connecting client.
      Parameters:
      ka - the adapter that received the registration
      p - the endpoint that sent the registration
      m - the registration message
    • getConnection

      protected HostedConnection getConnection(Endpoint endpoint)
      Resolves a hosted connection from an endpoint.
      Parameters:
      endpoint - the endpoint to resolve
      Returns:
      the hosted connection, or null if none is mapped
    • removeConnecting

      protected void removeConnecting(Endpoint p)
      Removes partially connected state associated with the specified endpoint.
      Parameters:
      p - the endpoint to remove
    • connectionClosed

      protected void connectionClosed(Endpoint p)
      Handles closure of an endpoint and removes any associated connection state.
      Parameters:
      p - the closed endpoint