Package com.suse.salt.netapi.event
Class WebSocketEventStream
- java.lang.Object
-
- com.suse.salt.netapi.event.AbstractEventStream
-
- com.suse.salt.netapi.event.WebSocketEventStream
-
- All Implemented Interfaces:
EventStream,java.lang.AutoCloseable
public class WebSocketEventStream extends AbstractEventStream
Event stream implementation based on aClientEndpointWebSocket. It is used to connect the WebSocket to aServerEndpointand receive messages from it.
-
-
Field Summary
Fields Modifier and Type Field Description private intdefaultBufferSizeDefault message buffer size in characters.private intmaxMessageLengthMaximum message length in charactersprivate java.lang.StringBuildermessageBufferBuffer for partial messages.private jakarta.websocket.SessionsessionThe WebSocketSession.private jakarta.websocket.WebSocketContainerwebsocketContainerTheWebSocketContainerobject for a @ClientEndpoint implementation.
-
Constructor Summary
Constructors Constructor Description WebSocketEventStream(java.net.URI uri, Token token, long sessionIdleTimeout, long idleTimeout, int maxMsgSize, EventListener... listeners)Constructor used to create an event stream: open a websocket connection and start event processing.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this Stream; Events will not be posted to listeners after this call.private voidclose(jakarta.websocket.CloseReason closeReason)Close the WebSocketSessionwith a given close reason.private voidinitializeStream(java.net.URI uri, Token token, long sessionIdleTimeout, long idleTimeout)Connect the WebSocket to the server pointing to /ws/{token} to receive events.booleanisEventStreamClosed()Helper method to check if the stream is able to receive Events.voidonClose(jakarta.websocket.Session session, jakarta.websocket.CloseReason closeReason)On closing the websocket, refresh the session and notify all subscribed listeners.voidonError(java.lang.Throwable throwable)On error, convertThrowableintoCloseReasonand close the session.voidonMessage(java.lang.String partialMessage, boolean last)Notify listeners on each event received on the websocket and buffer partial messages.voidonOpen(jakarta.websocket.Session session, jakarta.websocket.EndpointConfig config)On handshake completed, get the WebSocket Session and send a message to ServerEndpoint that WebSocket is ready.-
Methods inherited from class com.suse.salt.netapi.event.AbstractEventStream
addEventListener, clearListeners, getListenerCount, notifyListeners, removeEventListener
-
-
-
-
Field Detail
-
defaultBufferSize
private final int defaultBufferSize
Default message buffer size in characters.- See Also:
- Constant Field Values
-
maxMessageLength
private final int maxMessageLength
Maximum message length in characters
-
messageBuffer
private final java.lang.StringBuilder messageBuffer
Buffer for partial messages.
-
websocketContainer
private final jakarta.websocket.WebSocketContainer websocketContainer
TheWebSocketContainerobject for a @ClientEndpoint implementation.
-
session
private jakarta.websocket.Session session
The WebSocketSession.
-
-
Constructor Detail
-
WebSocketEventStream
public WebSocketEventStream(java.net.URI uri, Token token, long sessionIdleTimeout, long idleTimeout, int maxMsgSize, EventListener... listeners) throws SaltExceptionConstructor used to create an event stream: open a websocket connection and start event processing.- Parameters:
uri- salt api urllisteners- event listeners to be added before stream initializationidleTimeout- idle timeout to pass to the http client configmaxMsgSize- maximum event data size to acceptsessionIdleTimeout- session idle timeout to pass to the http client configtoken- salt session token to use for authentication- Throws:
SaltException- in case of an error during stream initialization
-
-
Method Detail
-
initializeStream
private void initializeStream(java.net.URI uri, Token token, long sessionIdleTimeout, long idleTimeout) throws SaltExceptionConnect the WebSocket to the server pointing to /ws/{token} to receive events.- Throws:
SaltException- in case of an error during stream initialization
-
isEventStreamClosed
public boolean isEventStreamClosed()
Helper method to check if the stream is able to receive Events.- Returns:
- A flag indicating the Stream state.
-
close
public void close() throws java.io.IOExceptionCloses this Stream; Events will not be posted to listeners after this call.- Throws:
java.io.IOException- in case of an error when closing the session
-
close
private void close(jakarta.websocket.CloseReason closeReason) throws java.io.IOExceptionClose the WebSocketSessionwith a given close reason.- Parameters:
closeReason- the reason for the websocket closure- Throws:
java.io.IOException- in case of an error when closing the session
-
onOpen
public void onOpen(jakarta.websocket.Session session, jakarta.websocket.EndpointConfig config) throws java.io.IOExceptionOn handshake completed, get the WebSocket Session and send a message to ServerEndpoint that WebSocket is ready. http://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#ws- Parameters:
session- The just started WebSocketSession.config- TheEndpointConfigcontaining the handshake informations.- Throws:
java.io.IOException- if something goes wrong sending message to the remote peer
-
onMessage
public void onMessage(java.lang.String partialMessage, boolean last) throws MessageTooBigExceptionNotify listeners on each event received on the websocket and buffer partial messages.- Parameters:
partialMessage- partial message received on this websocketlast- indicate the last part of a message- Throws:
MessageTooBigException- in case the message is longer than maxMessageLength
-
onError
public void onError(java.lang.Throwable throwable) throws java.io.IOExceptionOn error, convertThrowableintoCloseReasonand close the session.- Parameters:
throwable- The Throwable object received on the current error.- Throws:
java.io.IOException- in case of an error when closing the session
-
onClose
public void onClose(jakarta.websocket.Session session, jakarta.websocket.CloseReason closeReason)On closing the websocket, refresh the session and notify all subscribed listeners. Upon exit from this method, all subscribed listeners will be removed.- Parameters:
session- the websocketSessioncloseReason- theCloseReasonfor the websocket closure
-
-