Class DefaultClient

java.lang.Object
com.jme3.network.base.DefaultClient
All Implemented Interfaces:
Client, MessageConnection
Direct Known Subclasses:
Network.NetworkClientImpl

public class DefaultClient extends Object implements Client
A default implementation of the Client interface that delegates its network connectivity to a kernel.Connector.
Author:
Paul Speed
  • Constructor Details

    • DefaultClient

      public DefaultClient(String gameName, int version)
      Creates a client with the specified game name and protocol version.
      Parameters:
      gameName - the expected game name
      version - the expected protocol version
    • DefaultClient

      public DefaultClient(String gameName, int version, Connector reliable, Connector fast, ConnectorFactory connectorFactory)
      Creates a client with preconfigured reliable and optional fast connectors.
      Parameters:
      gameName - the expected game name
      version - the expected protocol version
      reliable - the reliable transport connector
      fast - the optional fast transport connector
      connectorFactory - the factory used for any additional channels
  • Method Details

    • addStandardServices

      protected void addStandardServices()
      Adds the default client services.
    • setPrimaryConnectors

      protected void setPrimaryConnectors(Connector reliable, Connector fast, ConnectorFactory connectorFactory)
      Sets the primary transport connectors used by this client.
      Parameters:
      reliable - the reliable connector
      fast - the optional fast connector
      connectorFactory - the factory used for additional channels
    • checkRunning

      protected void checkRunning()
      Ensures the client has been started.
    • start

      public void start()
      Description copied from interface: Client
      Starts the client allowing it to begin processing incoming messages and delivering them to listeners.
      Specified by:
      start in interface Client
    • isStarted

      public boolean isStarted()
      Description copied from interface: Client
      Returns true if this client has been started and is still running.
      Specified by:
      isStarted in interface Client
      Returns:
      true if the client has been started and not yet closed
    • waitForConnected

      protected void waitForConnected()
      Blocks until the client finishes connecting.
    • isConnected

      public boolean isConnected()
      Description copied from interface: Client
      Returns true if this client is fully connected to the host.
      Specified by:
      isConnected in interface Client
      Returns:
      true if the client is connected to the server
    • getId

      public int getId()
      Description copied from interface: Client
      Returns a unique ID for this client within the remote server or -1 if this client isn't fully connected to the server.
      Specified by:
      getId in interface Client
      Returns:
      the server-assigned client id, or -1 if not connected
    • getGameName

      public String getGameName()
      Description copied from interface: Client
      Returns the 'game name' for servers to which this client should be able to connect. This should match the 'game name' set on the server or this client will be turned away.
      Specified by:
      getGameName in interface Client
      Returns:
      the configured game name
    • getVersion

      public int getVersion()
      Description copied from interface: Client
      Returns the game-specific version of the server this client should be able to connect to.
      Specified by:
      getVersion in interface Client
      Returns:
      the expected game protocol version
    • getServices

      public ClientServiceManager getServices()
      Description copied from interface: Client
      Returns the manager for client services. Client services extend the functionality of the client.
      Specified by:
      getServices in interface Client
      Returns:
      the client service manager
    • send

      public void send(Message message)
      Description copied from interface: Client
      Sends a message to the server.
      Specified by:
      send in interface Client
      Specified by:
      send in interface MessageConnection
      Parameters:
      message - the message to send
    • send

      public void send(int channel, Message message)
      Description copied from interface: Client
      Sends a message to the other end of the connection using the specified alternate channel.
      Specified by:
      send in interface Client
      Specified by:
      send in interface MessageConnection
      Parameters:
      channel - the transport channel to use
      message - the message to send
    • send

      protected void send(int channel, Message message, boolean waitForConnected)
      Sends a message over a specific internal channel.
      Parameters:
      channel - the internal channel index
      message - the message to send
      waitForConnected - true to wait until the client is connected first
    • close

      public void close()
      Description copied from interface: Client
      Closes this connection to the server.
      Specified by:
      close in interface Client
    • closeConnections

      protected void closeConnections(ClientStateListener.DisconnectInfo info)
      Closes all connectors and terminates client services.
      Parameters:
      info - optional disconnect information to report to listeners
    • addClientStateListener

      public void addClientStateListener(ClientStateListener listener)
      Description copied from interface: Client
      Adds a listener that will be notified about connection state changes.
      Specified by:
      addClientStateListener in interface Client
      Parameters:
      listener - the listener to add
    • removeClientStateListener

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

      public void addMessageListener(MessageListener<? super Client> listener)
      Description copied from interface: Client
      Adds a listener that will be notified when any message or object is received from the server.
      Specified by:
      addMessageListener in interface Client
      Parameters:
      listener - the listener to add
    • addMessageListener

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

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

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

      public void addErrorListener(ErrorListener<? super Client> listener)
      Description copied from interface: Client
      Adds a listener that will be notified when any connection errors occur. If a client has no error listeners then the default behavior is to close the connection and provide an appropriate DisconnectInfo to any ClientStateListeners. If the application adds its own error listeners then it must take care of closing the connection itself.
      Specified by:
      addErrorListener in interface Client
      Parameters:
      listener - the listener to add
    • removeErrorListener

      public void removeErrorListener(ErrorListener<? super Client> listener)
      Description copied from interface: Client
      Removes a previously registered error listener.
      Specified by:
      removeErrorListener in interface Client
      Parameters:
      listener - the listener to remove
    • fireConnected

      protected void fireConnected()
      Notifies registered state listeners that the client connected.
    • startServices

      protected void startServices()
      Starts the client services after connection setup completes.
    • fireDisconnected

      protected void fireDisconnected(ClientStateListener.DisconnectInfo info)
      Notifies registered state listeners that the client disconnected.
      Parameters:
      info - optional disconnect information
    • handleError

      protected void handleError(Throwable t)
      Either calls the ErrorListener or closes the connection if there are no listeners.
      Parameters:
      t - the error to handle
    • configureChannels

      protected void configureChannels(long tempId, int[] ports)
      Configures any additional channels announced by the server.
      Parameters:
      tempId - the temporary client registration id
      ports - the alternate channel ports announced by the server
    • dispatch

      protected void dispatch(Message m)
      Dispatches a received message, handling connection-management messages internally.
      Parameters:
      m - the received message