StreamingClient
public class HTTPStreamClient extends java.lang.Object implements StreamingClient
HTTPStreamClient is designed to work with larger HTTPStreams of data. This can mean sending them or receiving them. The basic concept is that once the connection is established and the HTTP headers are send and/or received everything pretty much works like a normal socket connection in litesockets.
The HttpStreamClient can also do many requests/responses in a row w/o closing the connection
Its important to note that if chunked encoding is set in a header anything sent and/or received will automatically remove the chunk headers. If the HTTPResponse is chunked you will get a call to onRead for each chunk but the chunk header will be removed. If you are sending and have turned on chunked encoding every call to .write will add a chunked header to it, and calling .write with a ByteBuffer of 0 will end the stream
Modifier and Type | Class | Description |
---|---|---|
static interface |
HTTPStreamClient.HTTPStreamReader |
Simple HTTPStreamReader callback interface.
|
Constructor | Description |
---|---|
HTTPStreamClient(org.threadly.litesockets.SocketExecuter se,
java.lang.String host,
int port) |
When constructing the HTTPStreamClient we connect to a specific server.
|
HTTPStreamClient(org.threadly.litesockets.TCPClient client) |
Creates an HTTPStreaming client from an already existing TCPClient.
|
HTTPStreamClient(org.threadly.litesockets.TCPClient client,
java.lang.String host) |
Creates an HTTPStreaming client from an already existing TCPClient.
|
Modifier and Type | Method | Description |
---|---|---|
void |
addCloseListener(java.lang.Runnable cl) |
Sets a runnable to be ran once this connection is closed.
|
void |
close() |
This will close the client.
|
org.threadly.concurrent.future.ListenableFuture<java.lang.Boolean> |
connect() |
This is called to connect this client to the server.
|
void |
enableSSL() |
This is called to enable ssl on this connection.
|
void |
enableSSL(javax.net.ssl.SSLEngine ssle) |
This is called to enable ssl on this connection.
|
java.util.concurrent.Executor |
getClientsThreadExecutor() |
Gets the
Executor for this client. |
java.lang.String |
getHost() |
|
org.threadly.concurrent.future.ListenableFuture<?> |
getLastWriteFuture() |
|
int |
getPort() |
|
boolean |
isConnected() |
|
void |
setConnectionTimeout(int timeout) |
This sets the timeout on the connection to the remote host.
|
void |
setHTTPStreamReader(HTTPStreamClient.HTTPStreamReader hsr) |
Sets the HTTPStreamReader for this client.
|
void |
setRequestResponseHeaders(org.threadly.litesockets.protocols.http.request.HTTPRequest httpRequest,
org.threadly.litesockets.protocols.http.response.HTTPResponse httpResponse,
boolean writeResponse) |
Sets the HTTPRequest and HTTPResponse allowing the streaming client to start streaming
w/o sending these in itself.
|
org.threadly.concurrent.future.ListenableFuture<?> |
write(java.nio.ByteBuffer bb) |
This performs a write to the connection.
|
org.threadly.concurrent.future.ListenableFuture<org.threadly.litesockets.protocols.http.response.HTTPResponse> |
writeRequest(org.threadly.litesockets.protocols.http.request.HTTPRequest request) |
Tell the client to write an HTTPRequest to the server.
|
public HTTPStreamClient(org.threadly.litesockets.TCPClient client)
client
- the TCPClient
to use for this connection.public HTTPStreamClient(org.threadly.litesockets.TCPClient client, java.lang.String host)
client
- the TCPClient
to use for this connection.host
- the hostname or ip address to connect to.public HTTPStreamClient(org.threadly.litesockets.SocketExecuter se, java.lang.String host, int port) throws java.io.IOException
When constructing the HTTPStreamClient we connect to a specific server. If you want to use the HTTPRequest to set the server/port you can use the .getHost() and .getPort() methods on it.
se
- the SocketExecuter to use for this client.host
- the hostname or ip address to connect to.port
- the tcp port to connect to.java.io.IOException
- this will happen if we have problems connecting for some reason.public void enableSSL()
StreamingClient
enableSSL
in interface StreamingClient
public void enableSSL(javax.net.ssl.SSLEngine ssle)
StreamingClient
SSLEngine
to use for this connection allow you to validate the certificates if needed.enableSSL
in interface StreamingClient
ssle
- the SSLEngine
to use for this connection.public void setConnectionTimeout(int timeout)
StreamingClient
StreamingClient.connect()
is called.setConnectionTimeout
in interface StreamingClient
timeout
- the timeout in milliseconds to use for this connection.public java.lang.String getHost()
public int getPort()
public void setRequestResponseHeaders(org.threadly.litesockets.protocols.http.request.HTTPRequest httpRequest, org.threadly.litesockets.protocols.http.response.HTTPResponse httpResponse, boolean writeResponse)
StreamingClient
setRequestResponseHeaders
in interface StreamingClient
public org.threadly.concurrent.future.ListenableFuture<org.threadly.litesockets.protocols.http.response.HTTPResponse> writeRequest(org.threadly.litesockets.protocols.http.request.HTTPRequest request)
Tell the client to write an HTTPRequest to the server. This can technically be done whenever you want to but obviously use only when you know you can sent a request, right after opening a connection for example.
request
- the request to send. This is generally done with HTTPRequestBuilders buildHeadersOnly() method.
You can send a full request if you expect the response (not the sending) to be a stream.public org.threadly.concurrent.future.ListenableFuture<?> write(java.nio.ByteBuffer bb)
StreamingClient
write
in interface StreamingClient
bb
- the ByteBuffer
to write to socket.ListenableFuture
that will be completed once the frame has been fully written to the socket.public org.threadly.concurrent.future.ListenableFuture<?> getLastWriteFuture()
getLastWriteFuture
in interface StreamingClient
public void setHTTPStreamReader(HTTPStreamClient.HTTPStreamReader hsr)
hsr
- the HTTPStreamClient.HTTPStreamReader
public java.util.concurrent.Executor getClientsThreadExecutor()
StreamingClient
Executor
for this client.getClientsThreadExecutor
in interface StreamingClient
Executor
for this client.public org.threadly.concurrent.future.ListenableFuture<java.lang.Boolean> connect()
StreamingClient
connect
in interface StreamingClient
ListenableFuture
that will be completed when the upgrade is complete, if the upgrade fails the a ListenableFuture
be failed.public void addCloseListener(java.lang.Runnable cl)
StreamingClient
addCloseListener
in interface StreamingClient
cl
- the runnable to run on the connection close.public boolean isConnected()
isConnected
in interface StreamingClient
public void close()
StreamingClient
close
in interface StreamingClient