Package org.apache.vinci.transport
Class BaseClient
- java.lang.Object
-
- org.apache.vinci.transport.BaseClient
-
- Direct Known Subclasses:
VinciClient
public class BaseClient extends java.lang.ObjectClass 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
Fields Modifier and Type Field Description private intconnectTimeoutstatic intDEFAULT_CONNECT_TIMEOUTstatic intDEFAULT_SOCKET_TIMEOUTprivate TransportableFactoryfactoryprivate KeyValuePairheaderprivate java.lang.Stringhostprivate java.io.InputStreamisprivate java.io.OutputStreamosprivate intportprivate booleanretryprivate java.net.Socketsocketprivate intsocketTimeout
-
Constructor Summary
Constructors Constructor Description 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Close the connection.KeyValuePairgetHeader()Fetch the header of the last Transportable received.java.lang.StringgetHost()Get the hostname/ip address to which this client is connected.intgetPort()protected java.net.SocketgetSocket()Get the socket used for the connection.intgetSocketTimeout()Get the default timeout value for the socket (0 indicates never timeout, which is the default, but generally NOT a good setting).booleanisOpen()protected booleanisSocketKeepAliveEnabled()Gets whether keepAlive should be turned on for client sockets.voidopen()(Re)connects the client to a previously specified host and port.protected voidopen(java.lang.String h, int p)Connects the client to the specified host and port.voidopen(java.net.Socket use_me)Make this client use an already established socket connection.Transportablereceive()The other 1/2 of the split RPC.protected voidreopen(java.lang.Exception e)VinciFramerpc(Transportable query)Same as sendAndReceive(Transportable) except for return type.VinciFramerpc(Transportable query, int timeout)Same as sendAndReceive(Transportable, timeout) except for return type.static VinciFramerpc(Transportable in, java.lang.String host_name, int p)Convenience method for "one-shot" or "single-query" connections.static VinciFramerpc(Transportable in, java.lang.String host_name, int p, int socket_timeout)Convenience method for "one-shot" or "single-query" connections.static VinciFramerpc(Transportable in, java.lang.String host_name, int p, int socket_timeout, int connect_timeout)Convenience method for "one-shot" or "single-query" connections.voidsend(Transportable in)Support for 1/2 transaction RPC.TransportablesendAndReceive(Transportable in)Takes a transportable object, sends it across the connection, then retrieves the response and returns it.TransportablesendAndReceive(Transportable in, int timeout)Takes a transportable object, sends it across the connection, then retrieves the response and returns it.static TransportablesendAndReceive(Transportable in, java.lang.String host_name, int p, TransportableFactory f)Convenience method for "one-shot" or "single-query" connections.static TransportablesendAndReceive(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 TransportablesendAndReceive(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.TransportablesendAndReceive(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.TransportablesendAndReceive(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 TransportablesendAndReceiveWork(Transportable in, TransportableFactory f)protected TransportablesendAndReceiveWork(Transportable in, TransportableFactory f, int timeout)voidsetConnectTimeout(int timeout)Set the timeout value used for connect timeouts.voidsetRetry(boolean to)Set connection restablishment on IOException to on/off, default is ON.voidsetSocketTimeout(int millis)Set the timeout value used by the underlying socket.voidsetTransportableFactory(TransportableFactory f)Set the transportable factory used by this client.
-
-
-
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
-
host
private java.lang.String host
-
port
private int port
-
factory
private TransportableFactory factory
-
socket
private java.net.Socket socket
-
is
private java.io.InputStream is
-
os
private java.io.OutputStream os
-
header
private KeyValuePair header
-
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.IOExceptionOpen 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.IOExceptionOpen 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.IOExceptionOpen 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.IOExceptionOpen 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) throws java.io.IOException, ServiceException
Convenience method for "one-shot" or "single-query" connections.- Parameters:
in- -host_name- -p- -f- -- 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) 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- -
-
sendAndReceiveWork
protected Transportable sendAndReceiveWork(Transportable in, TransportableFactory f) throws java.io.IOException, ServiceException
- Parameters:
in- -f- -- Returns:
- -
- Throws:
java.io.IOException- -ServiceException- -
-
sendAndReceiveWork
protected Transportable sendAndReceiveWork(Transportable in, TransportableFactory f, int timeout) throws java.io.IOException, ServiceException
- Parameters:
in- -f- -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.IOExceptionConnects 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.IOExceptionMake 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.IOExceptionSet 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
-
-