Package co.arago.hiro.client.websocket
Class AuthenticatedWebSocketHandler
- java.lang.Object
-
- co.arago.hiro.client.websocket.AuthenticatedWebSocketHandler
-
- All Implemented Interfaces:
java.lang.AutoCloseable
- Direct Known Subclasses:
ActionWebSocket,EventWebSocket
public abstract class AuthenticatedWebSocketHandler extends java.lang.Object implements java.lang.AutoCloseableHandles websockets. Tries to renew any aborted connection until the websocket gets closed from this side.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAuthenticatedWebSocketHandler.Conf<T extends AuthenticatedWebSocketHandler.Conf<T>>Configuration interface for all the parameters of an AuthenticatedAPIHandler.protected classAuthenticatedWebSocketHandler.InternalListenerHiroWebSocketListener (thread) for incoming websocket messages.static classAuthenticatedWebSocketHandler.Status
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.StringapiNameprotected java.lang.Stringendpointprotected java.lang.Stringfragmentprotected HttpHeaderMapheadersprotected AuthenticatedWebSocketHandler.InternalListenerinternalListenerprotected intmaxRetriesprotected java.lang.Stringnameprotected java.lang.Stringprotocolprotected URIEncodedDataqueryprotected booleanreconnectOnFailedSendprotected java.util.concurrent.atomic.AtomicReference<AuthenticatedWebSocketHandler.Status>statusprotected TokenAPIHandlertokenAPIHandlerprotected longwebSocketRequestTimeout
-
Constructor Summary
Constructors Modifier Constructor Description protectedAuthenticatedWebSocketHandler(AuthenticatedWebSocketHandler.Conf<?> builder)Protected Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected intbackoff(int reconnectDelay)Backoff operations that fail.voidclose()protected voidcloseWebSocket(int status, java.lang.String reason)Close the websocket and setwebSocketto null.protected voidcreateWebSocket()Create new WebSocket.protected voidrestartWebSocket(boolean refreshToken)Restarts the websocket by closing the old and creating a new one.voidsend(java.lang.String message)Send a message across the websocket.voidstart()
-
-
-
Field Detail
-
status
protected final java.util.concurrent.atomic.AtomicReference<AuthenticatedWebSocketHandler.Status> status
-
name
protected final java.lang.String name
-
apiName
protected final java.lang.String apiName
-
endpoint
protected final java.lang.String endpoint
-
protocol
protected final java.lang.String protocol
-
maxRetries
protected final int maxRetries
-
tokenAPIHandler
protected final TokenAPIHandler tokenAPIHandler
-
query
protected final URIEncodedData query
-
headers
protected final HttpHeaderMap headers
-
fragment
protected final java.lang.String fragment
-
reconnectOnFailedSend
protected final boolean reconnectOnFailedSend
-
webSocketRequestTimeout
protected final long webSocketRequestTimeout
-
internalListener
protected AuthenticatedWebSocketHandler.InternalListener internalListener
-
-
Constructor Detail
-
AuthenticatedWebSocketHandler
protected AuthenticatedWebSocketHandler(AuthenticatedWebSocketHandler.Conf<?> builder)
Protected Constructor- Parameters:
builder- The builder for this Handler.
-
-
Method Detail
-
createWebSocket
protected void createWebSocket() throws HiroException, java.io.IOException, java.lang.InterruptedExceptionCreate new WebSocket. Closes any old websocket.- Throws:
HiroException- When creation of the webSocket fails.java.io.IOException- On IO problems.java.lang.InterruptedException- When the call gets interrupted.
-
closeWebSocket
protected void closeWebSocket(int status, java.lang.String reason)Close the websocket and setwebSocketto null.- Parameters:
status- The status for the close messagereason- The reason string for the close message.
-
restartWebSocket
protected void restartWebSocket(boolean refreshToken) throws HiroException, java.io.IOException, java.lang.InterruptedExceptionRestarts the websocket by closing the old and creating a new one. Uses the sameinternalListener. Delays the restart according tobackoff(int reconnectDelay).- Parameters:
refreshToken- Refresh the current token.- Throws:
HiroException- When creation of the webSocket fails.java.io.IOException- On IO problems.java.lang.InterruptedException- When the call gets interrupted.
-
backoff
protected int backoff(int reconnectDelay) throws java.lang.InterruptedExceptionBackoff operations that fail. The delay increases with every try up to 10 minutes.- Parameters:
reconnectDelay- The old delay value (seconds).- Returns:
- The new delay value (seconds).
- Throws:
java.lang.InterruptedException- WhenThread.sleep(long)gets interrupted.- See Also:
send(String),restartWebSocket(boolean)
-
send
public void send(java.lang.String message) throws HiroException, java.lang.InterruptedException, java.io.IOExceptionSend a message across the websocket. TriesmaxRetriestimes to send a message before resetting the websocket.- Parameters:
message- The message to send.- Throws:
HiroException- When sending finally fails.java.io.IOException- On IO problems.java.lang.InterruptedException- When the call gets interrupted.
-
start
public void start() throws HiroException, java.io.IOException, java.lang.InterruptedException- Throws:
HiroExceptionjava.io.IOExceptionjava.lang.InterruptedException
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
-