Class BaseClient

java.lang.Object
org.apache.vinci.transport.BaseClient
Direct Known Subclasses:
VinciClient

public class BaseClient extends Object
Class for conjuring a Vinci service by host/port (that is, without interaction with the naming service). Usually you want to use VinciClient, which extends this class to invoke a service by (qualified) name. Provides generic "send/recieve/sendAndReceive" for communicating arbitrary (transportable) document models, and also specific "rpc" methods for more convenient support of the VinciFrame document model.
  • Field Details

    • DEFAULT_SOCKET_TIMEOUT

      public static final int DEFAULT_SOCKET_TIMEOUT
      See Also:
    • DEFAULT_CONNECT_TIMEOUT

      public static final int DEFAULT_CONNECT_TIMEOUT
      See Also:
  • Constructor Details

    • BaseClient

      public BaseClient(String h, int p) throws IOException
      Open up the service at the specified host and port, using a VinciFrame factory.
      Parameters:
      h - The hostname/ip address of the machine running the service.
      p - The port on which the service runs.
      Throws:
      IOException - if the underlying socket fails to connect
    • BaseClient

      public BaseClient(String h, int p, int connect_timeout) throws IOException
      Open up the service at the specified host and port, using a VinciFrame factory.
      Parameters:
      h - The hostname/ip address of the machine running the service.
      p - The port on which the service runs.
      connect_timeout - The number of milliseconds that will elapse before a connect attempt fails.
      Throws:
      IOException - if the underlying socket fails to connect
    • BaseClient

      public BaseClient(String h, int p, TransportableFactory f) throws IOException
      Open up the service at the specified host and port.
      Parameters:
      h - The hostname/ip address of the machine running the service.
      p - The port on which the service runs.
      f - A factory for creating documents of the desired type.
      Throws:
      IOException - if the underlying socket fails to connect.
    • BaseClient

      public BaseClient(String h, int p, TransportableFactory f, int timeout) throws IOException
      Open up the service at the specified host and port, using the specified connect timeout.
      Parameters:
      h - The hostname/ip address of the machine running the service.
      p - The port on which the service runs.
      f - A factory for creating documents of the desired type.
      timeout - The number of milliseconds that will elapse before a connect attempt fails.
      Throws:
      IOException - if the underlying socket fails to connect.
    • BaseClient

      public BaseClient()
      Create a base client without establishing a connection. This is useful for client classes which extend this class and which to perform their own connection establishment. Uses a VinciFrame factory.
    • BaseClient

      public BaseClient(TransportableFactory f)
      Create a base client without establishing a connection. This is useful for client classes which extend this class and which to perform their own connection establishment.
      Parameters:
      f - A factory for creating documents of the desired type.
    • BaseClient

      public BaseClient(TransportableFactory f, int timeout)
      Create a base client without establishing a connection. This is useful for client classes which extend this class and which to perform their own connection establishment.
      Parameters:
      f - A factory for creating documents of the desired type.
      timeout - The number of milliseconds that will elapse before a connect attempt fails.
  • Method Details

    • getHost

      public String getHost()
      Get the hostname/ip address to which this client is connected.
      Returns:
      -
    • getPort

      public int getPort()
    • getSocket

      protected Socket getSocket()
      Get the socket used for the connection.
      Returns:
      -
    • getSocketTimeout

      public int getSocketTimeout()
      Get the default timeout value for the socket (0 indicates never timeout, which is the default, but generally NOT a good setting).
      Returns:
      -
    • setConnectTimeout

      public void setConnectTimeout(int timeout)
      Set the timeout value used for connect timeouts. Note that if you use one of the connection-inducing constructors, then this method has no effect unless a subsequent connection attempt is made.
      Parameters:
      timeout - The number of milliseconds that will elapse before a connect attempt fails.
    • setTransportableFactory

      public void setTransportableFactory(TransportableFactory f)
      Set the transportable factory used by this client.
      Parameters:
      f - -
    • sendAndReceive

      public Transportable sendAndReceive(Transportable in) throws IOException, ServiceException
      Takes a transportable object, sends it across the connection, then retrieves the response and returns it.
      Parameters:
      in - The query frame.
      Returns:
      A transportable representing the result, its specific type determined by the factory provided through BaseClient's constructor.
      Throws:
      IOException - thrown by the underlying socket IO (e.g. due to connection timeout).
      ServiceException - thrown if the server threw ServiceException or returned an ErrorFrame.
    • sendAndReceive

      public Transportable sendAndReceive(Transportable in, int timeout) throws IOException, ServiceException
      Takes a transportable object, sends it across the connection, then retrieves the response and returns it.
      Parameters:
      in - The query frame.
      timeout - The timeout value to use in place of this client's default timeout setting.
      Returns:
      A transportable representing the result, its specific type determined by the factory provided through BaseClient's constructor.
      Throws:
      IOException - thrown by the underlying socket IO (e.g. due to connection timeout).
      ServiceException - thrown if the server threw ServiceException or returned an ErrorFrame.
    • sendAndReceive

      Same as sendAndReceive(Transportable) except the provided factory is used to create the return document in place of the default factory.
      Parameters:
      in - -
      f - The factory to used to create the return document.
      Returns:
      -
      Throws:
      IOException - -
      ServiceException - -
    • sendAndReceive

      public Transportable sendAndReceive(Transportable in, TransportableFactory f, int timeout) throws IOException, ServiceException
      Same as sendAndReceive(Transportable, timeout) except the provided factory is used to create the return document in place of the default factory.
      Parameters:
      in - -
      f - The factory to used to create the return document.
      timeout - -
      Returns:
      -
      Throws:
      IOException - -
      ServiceException - -
    • rpc

      Same as sendAndReceive(Transportable) except for return type. Syntactic sugar method for the case where return result is known to be VinciFrame (eliminates the need for casting in the typical usage case).
      Parameters:
      query - -
      Returns:
      A VinciFrame representing the service result.
      Throws:
      IOException - -
      ServiceException - -
    • rpc

      public VinciFrame rpc(Transportable query, int timeout) throws IOException, ServiceException
      Same as sendAndReceive(Transportable, timeout) except for return type. Syntactic sugar method for the case where return result is known to be VinciFrame (eliminates the need for casting in the typical usage case).
      Parameters:
      query - -
      timeout - -
      Returns:
      A VinciFrame representing the service result.
      Throws:
      IOException - -
      ServiceException - -
    • send

      public void send(Transportable in) throws IOException
      Support for 1/2 transaction RPC. This allows interaction with an asynchronous ("receive only") service, or for the sender to simply do something else before coming back and receiving the result (though at the risk of timeouts!).
      Parameters:
      in - The Transportable to send.
      Throws:
      IOException - Thrown by the underlying transport layer.
    • receive

      public Transportable receive() throws IOException, ServiceException
      The other 1/2 of the split RPC. This allows for interaction with an asynchronous "publish only" service, or simply picks up a result queried for earlier via send().
      Returns:
      The Transportable requested.
      Throws:
      IOException - Thrown by the underlying transport layer, or the socket is closed.
      ServiceException - Thrown if the remote server responded with an error frame.
    • close

      public void close()
      Close the connection. Using the Client object after this will throw an exception.
    • isOpen

      public boolean isOpen()
    • setRetry

      public void setRetry(boolean to)
      Set connection restablishment on IOException to on/off, default is ON. This way, by default, BaseClient attempts to reopen a connection at most once if it receives an IOException which can happen, for example, from the connection timing out.
      Parameters:
      to - -
    • getHeader

      public KeyValuePair getHeader()
      Fetch the header of the last Transportable received.
      Returns:
      -
    • sendAndReceive

      public static Transportable sendAndReceive(Transportable in, String host_name, int p, TransportableFactory f) throws IOException, ServiceException
      Convenience method for "one-shot" or "single-query" connections.
      Parameters:
      in - -
      host_name - -
      p - -
      f - -
      Returns:
      -
      Throws:
      IOException - -
      ServiceException - -
    • sendAndReceive

      public static Transportable sendAndReceive(Transportable in, String host_name, int p, TransportableFactory f, int socket_timeout) throws IOException, ServiceException
      Convenience method for "one-shot" or "single-query" connections with socket timeout support.
      Parameters:
      in - -
      host_name - -
      p - -
      f - -
      socket_timeout - -
      Returns:
      -
      Throws:
      IOException - -
      ServiceException - -
    • sendAndReceive

      public static Transportable sendAndReceive(Transportable in, String host_name, int p, TransportableFactory f, int socket_timeout, int connect_timeout) throws IOException, ServiceException
      Convenience method for "one-shot" or "single-query" connections with socket timeout support & connect timeout support.
      Parameters:
      in - -
      host_name - -
      p - -
      f - -
      socket_timeout - -
      connect_timeout - -
      Returns:
      -
      Throws:
      IOException - -
      ServiceException - -
    • rpc

      public static VinciFrame rpc(Transportable in, String host_name, int p) throws IOException, ServiceException
      Convenience method for "one-shot" or "single-query" connections. Same as sendAndReceive except uses VinciFrame factory so return type is known to be VinciFrame.
      Parameters:
      in - -
      host_name - -
      p - -
      Returns:
      -
      Throws:
      IOException - -
      ServiceException - -
    • rpc

      public static VinciFrame rpc(Transportable in, String host_name, int p, int socket_timeout) throws IOException, ServiceException
      Convenience method for "one-shot" or "single-query" connections. Same as sendAndReceive except uses VinciFrame factory so return type is known to be VinciFrame.
      Parameters:
      in - -
      host_name - -
      p - -
      socket_timeout - -
      Returns:
      -
      Throws:
      IOException - -
      ServiceException - -
    • rpc

      public static VinciFrame rpc(Transportable in, String host_name, int p, int socket_timeout, int connect_timeout) throws IOException, ServiceException
      Convenience method for "one-shot" or "single-query" connections. Same as sendAndReceive except uses VinciFrame factory so return type is known to be VinciFrame.
      Parameters:
      in - -
      host_name - -
      p - -
      socket_timeout - -
      connect_timeout - -
      Returns:
      -
      Throws:
      IOException - -
      ServiceException - -
    • sendAndReceiveWork

      protected Transportable sendAndReceiveWork(Transportable in, TransportableFactory f) throws IOException, ServiceException
      Parameters:
      in - -
      f - -
      Returns:
      -
      Throws:
      IOException - -
      ServiceException - -
    • sendAndReceiveWork

      protected Transportable sendAndReceiveWork(Transportable in, TransportableFactory f, int timeout) throws IOException, ServiceException
      Parameters:
      in - -
      f - -
      timeout - -
      Returns:
      -
      Throws:
      IOException - -
      ServiceException - -
    • reopen

      protected void reopen(Exception e) throws IOException
      Parameters:
      e - -
      Throws:
      IOException - -
    • open

      protected final void open(String h, int p) throws IOException
      Connects the client to the specified host and port.
      Parameters:
      h - The hostname/ip address of the server to connect to.
      p - The port to connect to.
      Throws:
      IOException - Thrown by underlying Socket open() call.
    • open

      public final void open() throws IOException
      (Re)connects the client to a previously specified host and port. Should only be called if this client has been previously closed via a call to "close".
      Throws:
      IOException - Thrown by underlying Socket open() call.
    • open

      public void open(Socket use_me) throws IOException
      Make this client use an already established socket connection. If you use this open method, then setRetry is set to false. Resetting it to true will cause problems since the client does not know how to reopen the connection.
      Parameters:
      use_me - The socket to use.
      Throws:
      IOException - Thrown by underlying Socket open() call.
    • setSocketTimeout

      public void setSocketTimeout(int millis) throws IOException
      Set the timeout value used by the underlying socket. Default is 2 minutes.
      Parameters:
      millis - -
      Throws:
      IOException - -
    • isSocketKeepAliveEnabled

      protected boolean isSocketKeepAliveEnabled()
      Gets whether keepAlive should be turned on for client sockets. Always returns true for BaseClient. Can be overridden in subclasses.
      Returns:
      whether socket keepAlive should be turned on