org.apache.vinci.transport
Class BaseClient

java.lang.Object
  extended by org.apache.vinci.transport.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 Summary
static int DEFAULT_CONNECT_TIMEOUT
           
static int DEFAULT_SOCKET_TIMEOUT
           
 
Constructor Summary
BaseClient()
          Create a base client without establishing a connection.
BaseClient(java.lang.String h, int p)
          Open up the service at the specified host and port, using a VinciFrame factory.
BaseClient(java.lang.String h, int p, int connect_timeout)
          Open up the service at the specified host and port, using a VinciFrame factory.
BaseClient(java.lang.String h, int p, TransportableFactory f)
          Open up the service at the specified host and port.
BaseClient(java.lang.String h, int p, TransportableFactory f, int timeout)
          Open up the service at the specified host and port, using the specified connect timeout.
BaseClient(TransportableFactory f)
          Create a base client without establishing a connection.
BaseClient(TransportableFactory f, int timeout)
          Create a base client without establishing a connection.
 
Method Summary
 void close()
          Close the connection.
 KeyValuePair getHeader()
          Fetch the header of the last Transportable received.
 java.lang.String getHost()
          Get the hostname/ip address to which this client is connected.
 int getPort()
           
protected  java.net.Socket getSocket()
          Get the socket used for the connection.
 int getSocketTimeout()
          Get the default timeout value for the socket (0 indicates never timeout, which is the default, but generally NOT a good setting).
 boolean isOpen()
           
protected  boolean isSocketKeepAliveEnabled()
          Gets whether keepAlive should be turned on for client sockets.
 void open()
          (Re)connects the client to a previously specified host and port.
 void open(java.net.Socket use_me)
          Make this client use an already established socket connection.
protected  void open(java.lang.String h, int p)
          Connects the client to the specified host and port.
 Transportable receive()
          The other 1/2 of the split RPC.
protected  void reopen(java.lang.Exception e)
           
 VinciFrame rpc(Transportable query)
          Same as sendAndReceive(Transportable) except for return type.
 VinciFrame rpc(Transportable query, int timeout)
          Same as sendAndReceive(Transportable, timeout) except for return type.
static VinciFrame rpc(Transportable in, java.lang.String host_name, int p)
          Convenience method for "one-shot" or "single-query" connections.
static VinciFrame rpc(Transportable in, java.lang.String host_name, int p, int socket_timeout)
          Convenience method for "one-shot" or "single-query" connections.
static VinciFrame rpc(Transportable in, java.lang.String host_name, int p, int socket_timeout, int connect_timeout)
          Convenience method for "one-shot" or "single-query" connections.
 void send(Transportable in)
          Support for 1/2 transaction RPC.
 Transportable sendAndReceive(Transportable in)
          Takes a transportable object, sends it across the connection, then retrieves the response and returns it.
 Transportable sendAndReceive(Transportable in, int timeout)
          Takes a transportable object, sends it across the connection, then retrieves the response and returns it.
static Transportable sendAndReceive(Transportable in, java.lang.String host_name, int p, TransportableFactory f)
          Convenience method for "one-shot" or "single-query" connections.
static Transportable sendAndReceive(Transportable in, java.lang.String host_name, int p, TransportableFactory f, int socket_timeout)
          Convenience method for "one-shot" or "single-query" connections with socket timeout support.
static Transportable sendAndReceive(Transportable in, java.lang.String host_name, int p, TransportableFactory f, int socket_timeout, int connect_timeout)
          Convenience method for "one-shot" or "single-query" connections with socket timeout support & connect timeout support.
 Transportable sendAndReceive(Transportable in, TransportableFactory f)
          Same as sendAndReceive(Transportable) except the provided factory is used to create the return document in place of the default factory.
 Transportable sendAndReceive(Transportable in, TransportableFactory f, int timeout)
          Same as sendAndReceive(Transportable, timeout) except the provided factory is used to create the return document in place of the default factory.
protected  Transportable sendAndReceiveWork(Transportable in, TransportableFactory f)
           
protected  Transportable sendAndReceiveWork(Transportable in, TransportableFactory f, int timeout)
           
 void setConnectTimeout(int timeout)
          Set the timeout value used for connect timeouts.
 void setRetry(boolean to)
          Set connection restablishment on IOException to on/off, default is ON.
 void setSocketTimeout(int millis)
          Set the timeout value used by the underlying socket.
 void setTransportableFactory(TransportableFactory f)
          Set the transportable factory used by this client.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SOCKET_TIMEOUT

public static final int DEFAULT_SOCKET_TIMEOUT
See Also:
Constant Field Values

DEFAULT_CONNECT_TIMEOUT

public static final int DEFAULT_CONNECT_TIMEOUT
See Also:
Constant Field Values
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:
factory - 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:
factory - 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.


getPort

public int getPort()

getSocket

protected java.net.Socket getSocket()
Get the socket used for the connection.


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).


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.


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:
f - The factory to used to create the return document.
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:
factory - The factory to used to create the return document.
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).

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).

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.


getHeader

public KeyValuePair getHeader()
Fetch the header of the last Transportable received.


sendAndReceive

public static Transportable sendAndReceive(Transportable in,
                                           java.lang.String host_name,
                                           int p,
                                           TransportableFactory f)
                                    throws java.io.IOException,
                                           ServiceException
Convenience method for "one-shot" or "single-query" connections.

Throws:
java.io.IOException
ServiceException

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.

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.

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.

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.

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.

Throws:
java.io.IOException
ServiceException

sendAndReceiveWork

protected Transportable sendAndReceiveWork(Transportable in,
                                           TransportableFactory f)
                                    throws java.io.IOException,
                                           ServiceException
Throws:
java.io.IOException
ServiceException

sendAndReceiveWork

protected Transportable sendAndReceiveWork(Transportable in,
                                           TransportableFactory f,
                                           int timeout)
                                    throws java.io.IOException,
                                           ServiceException
Throws:
java.io.IOException
ServiceException

reopen

protected void reopen(java.lang.Exception e)
               throws java.io.IOException
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.

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


Copyright © 2012. All Rights Reserved.