Class BaseClient

  • Direct Known Subclasses:
    VinciClient

    public class BaseClient
    extends java.lang.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 Detail

      • DEFAULT_CONNECT_TIMEOUT

        public static final int DEFAULT_CONNECT_TIMEOUT
        See Also:
        Constant Field Values
      • host

        private java.lang.String host
      • port

        private int port
      • socket

        private java.net.Socket socket
      • is

        private java.io.InputStream is
      • os

        private java.io.OutputStream os
      • socketTimeout

        private int socketTimeout
      • connectTimeout

        private int connectTimeout
      • retry

        private boolean retry
    • Constructor Detail

      • BaseClient

        public BaseClient​(java.lang.String h,
                          int p)
                   throws java.io.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:
        java.io.IOException - if the underlying socket fails to connect
      • BaseClient

        public BaseClient​(java.lang.String h,
                          int p,
                          int connect_timeout)
                   throws java.io.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:
        java.io.IOException - if the underlying socket fails to connect
      • BaseClient

        public BaseClient​(java.lang.String h,
                          int p,
                          TransportableFactory f)
                   throws java.io.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:
        java.io.IOException - if the underlying socket fails to connect.
      • BaseClient

        public BaseClient​(java.lang.String h,
                          int p,
                          TransportableFactory f,
                          int timeout)
                   throws java.io.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:
        java.io.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 Detail

      • getHost

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

        public int getPort()
      • getSocket

        protected java.net.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 java.io.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:
        java.io.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 java.io.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:
        java.io.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,
                                            TransportableFactory f)
                                     throws java.io.IOException,
                                            ServiceException
        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:
        java.io.IOException - -
        ServiceException - -
      • sendAndReceive

        public Transportable sendAndReceive​(Transportable in,
                                            TransportableFactory f,
                                            int timeout)
                                     throws java.io.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:
        java.io.IOException - -
        ServiceException - -
      • rpc

        public VinciFrame rpc​(Transportable query)
                       throws java.io.IOException,
                              ServiceException
        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:
        java.io.IOException - -
        ServiceException - -
      • rpc

        public VinciFrame rpc​(Transportable query,
                              int timeout)
                       throws java.io.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:
        java.io.IOException - -
        ServiceException - -
      • send

        public void send​(Transportable in)
                  throws java.io.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:
        java.io.IOException - Thrown by the underlying transport layer.
      • receive

        public Transportable receive()
                              throws java.io.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:
        java.io.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,
                                                   java.lang.String host_name,
                                                   int p,
                                                   TransportableFactory f,
                                                   int socket_timeout)
                                            throws java.io.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:
        java.io.IOException - -
        ServiceException - -
      • sendAndReceive

        public static Transportable sendAndReceive​(Transportable in,
                                                   java.lang.String host_name,
                                                   int p,
                                                   TransportableFactory f,
                                                   int socket_timeout,
                                                   int connect_timeout)
                                            throws java.io.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:
        java.io.IOException - -
        ServiceException - -
      • rpc

        public static VinciFrame rpc​(Transportable in,
                                     java.lang.String host_name,
                                     int p)
                              throws java.io.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:
        java.io.IOException - -
        ServiceException - -
      • rpc

        public static VinciFrame rpc​(Transportable in,
                                     java.lang.String host_name,
                                     int p,
                                     int socket_timeout)
                              throws java.io.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:
        java.io.IOException - -
        ServiceException - -
      • rpc

        public static VinciFrame rpc​(Transportable in,
                                     java.lang.String host_name,
                                     int p,
                                     int socket_timeout,
                                     int connect_timeout)
                              throws java.io.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:
        java.io.IOException - -
        ServiceException - -
      • reopen

        protected void reopen​(java.lang.Exception e)
                       throws java.io.IOException
        Parameters:
        e - -
        Throws:
        java.io.IOException - -
      • open

        protected final void open​(java.lang.String h,
                                  int p)
                           throws java.io.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:
        java.io.IOException - Thrown by underlying Socket open() call.
      • open

        public final void open()
                        throws java.io.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:
        java.io.IOException - Thrown by underlying Socket open() call.
      • open

        public void open​(java.net.Socket use_me)
                  throws java.io.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:
        java.io.IOException - Thrown by underlying Socket open() call.
      • setSocketTimeout

        public void setSocketTimeout​(int millis)
                              throws java.io.IOException
        Set the timeout value used by the underlying socket. Default is 2 minutes.
        Parameters:
        millis - -
        Throws:
        java.io.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