Package org.spincast.plugins.undertow
Class SpincastWebsocketEndpoint
java.lang.Object
org.spincast.plugins.undertow.SpincastWebsocketEndpoint
- All Implemented Interfaces:
WebsocketEndpointManager
,WebsocketEndpointWriter
,WebsocketEndpoint
-
Field Summary
-
Constructor Summary
ConstructorDescriptionSpincastWebsocketEndpoint
(String endpointId, WebsocketEndpointHandler eventsHandler, UndertowWebsocketEndpointWriterFactory undertowWebsocketEndpointWriterFactory, SpincastUndertowConfig spincastUndertowConfig, SpincastUndertowUtils spincastUndertowUtils) Constructor -
Method Summary
Modifier and TypeMethodDescriptionvoid
Closes the entire WebSocket endpoint.void
closeEndpoint
(boolean sendClosingMessageToPeers) Closes the entire WebSocket endpoint.void
closeEndpoint
(int closingCode, String closingReason) Closes the entire WebSocket endpoint.protected void
closeEndpoint
(int closingCode, String closingReason, boolean sendClosingMessageToPeers) void
Closes a specific peer connection on the endpoint.void
Closes a specific peer connection on the endpoint.The endpoint idprotected WebsocketEndpointHandler
protected Object
getNewPeerIdLock
(String peerId) Gets the creation lock for a peer id.The connected peers' ids.protected SpincastUndertowConfig
protected SpincastUndertowUtils
protected ExecutorService
The ExecutorService to use to send events to the application.protected int
The maximum number of concurrent threads used when sending events to the application.protected ThreadFactory
The ThreadFactory to use for the Executor that sends events to the application.protected int
The timeout amount before cancelling a task when sending events to the application.protected TimeUnit
The timeout unit before cancelling a task when sending events to the application.protected UndertowWebsocketEndpointWriterFactory
protected UndertowWebsocketEndpointWriter
protected io.undertow.websockets.WebSocketProtocolHandshakeHandler
The handler to use for the Websocket connection.void
handleConnectionRequest
(io.undertow.server.HttpServerExchange exchange, String peerId) protected void
init()
boolean
isClosed()
Is this endpoint closed?boolean
Is this endpoint closing?protected void
managePeersWriteConnectionClosed
(Set<String> peerIds) Called when a write operation detected a closed connection on some peers.protected void
removePeerChannel
(String peerId) protected void
protected void
sendAppEventInNewThread
(Runnable runnable) Sends an event to the application in a separated thread.void
sendMessage
(byte[] message) Sends a byte array message to all peers of the endpoint.void
sendMessage
(String message) Sends a String message to all peers of the endpoint.void
sendMessage
(String peerId, byte[] message) Sends a byte array message to a specific peer.void
sendMessage
(String peerId, String message) Sends a String message to a specific peer.void
sendMessage
(Set<String> peerIds, byte[] message) Sends a byte array message to specific peers.void
sendMessage
(Set<String> peerIds, String message) Sends a String message to specific peers.void
sendMessageExcept
(String peerId, byte[] message) Sends a byte array message to all peers except the specified one.void
sendMessageExcept
(String peerId, String message) Sends a String message to all peers except the specified one.void
sendMessageExcept
(Set<String> peerIdsToRemove, byte[] message) Sends a byte array message to all peers except the specified ones.void
sendMessageExcept
(Set<String> peerIdsToRemove, String message) Sends a String message to all peers except the specified ones.protected void
sendOnBytesMessageAppEvent
(String peerId, byte[] message) Sends a "byte[] message" event to the app.protected void
sendOnPeerConnectedAppEvent
(String peerId) Sends a "Peer connected " event to the app.protected void
sendOnStringMessageAppEvent
(String peerId, String message) Sends a "String message" event to the app.protected void
sendPeerClosedAppEvent
(String peerId) Sends a "peer closed" event to the app.protected void
Starts sending automatic pings to the peers.protected void
protected void
validateWebsocketClosingCode
(int closingCode) Is the Websocket closing code valid?
-
Field Details
-
logger
protected static final org.slf4j.Logger logger -
EXCHANGE_VARIABLE_PEER_ID
-
-
Constructor Details
-
SpincastWebsocketEndpoint
public SpincastWebsocketEndpoint(String endpointId, WebsocketEndpointHandler eventsHandler, UndertowWebsocketEndpointWriterFactory undertowWebsocketEndpointWriterFactory, SpincastUndertowConfig spincastUndertowConfig, SpincastUndertowUtils spincastUndertowUtils) Constructor
-
-
Method Details
-
init
@Inject protected void init() -
getWebSocketChannelByPeerId
-
getEventsHandler
-
getUndertowWebsocketEndpointWriterFactory
-
getSpincastUndertowConfig
-
getSpincastUndertowUtils
-
getUndertowWebsocketWriter
-
getNewPeerIdLock
Gets the creation lock for a peer id. -
getEndpointId
Description copied from interface:WebsocketEndpointManager
The endpoint id- Specified by:
getEndpointId
in interfaceWebsocketEndpointManager
-
getPeersIds
Description copied from interface:WebsocketEndpointManager
The connected peers' ids.- Specified by:
getPeersIds
in interfaceWebsocketEndpointManager
-
closePeer
Description copied from interface:WebsocketEndpointManager
Closes a specific peer connection on the endpoint.- Specified by:
closePeer
in interfaceWebsocketEndpointManager
-
closePeer
Description copied from interface:WebsocketEndpointManager
Closes a specific peer connection on the endpoint.- Specified by:
closePeer
in interfaceWebsocketEndpointManager
-
removePeerChannelAndSendPeerClosedAppEvent
-
removePeerChannel
-
managePeersWriteConnectionClosed
Called when a write operation detected a closed connection on some peers. -
isClosing
public boolean isClosing()Description copied from interface:WebsocketEndpointManager
Is this endpoint closing?- Specified by:
isClosing
in interfaceWebsocketEndpointManager
-
isClosed
public boolean isClosed()Description copied from interface:WebsocketEndpointManager
Is this endpoint closed?- Specified by:
isClosed
in interfaceWebsocketEndpointManager
-
closeEndpoint
public void closeEndpoint()Description copied from interface:WebsocketEndpointManager
Closes the entire WebSocket endpoint. All peer connections of this endpoint will be closed and the endpoint will be removed.Will try to send a "closing" message to the peers before closing their connection.
- Specified by:
closeEndpoint
in interfaceWebsocketEndpointManager
-
closeEndpoint
public void closeEndpoint(boolean sendClosingMessageToPeers) Description copied from interface:WebsocketEndpointManager
Closes the entire WebSocket endpoint. All peer connections of this endpoint will be closed and the endpoint will be removed.- Specified by:
closeEndpoint
in interfaceWebsocketEndpointManager
- Parameters:
sendClosingMessageToPeers
- iftrue
, Spincast will try to send a "closing" message to the peers before closing their connections.
-
closeEndpoint
Description copied from interface:WebsocketEndpointManager
Closes the entire WebSocket endpoint. All peer connections of this endpoint will be closed and the endpoint will be removed.Will try to send a "closing" message to the peers before closing their connection.
- Specified by:
closeEndpoint
in interfaceWebsocketEndpointManager
-
closeEndpoint
protected void closeEndpoint(int closingCode, String closingReason, boolean sendClosingMessageToPeers) -
validateWebsocketClosingCode
protected void validateWebsocketClosingCode(int closingCode) Is the Websocket closing code valid?- Throws:
an
- expcetion is the code is not valid.
-
startSendingPings
protected void startSendingPings()Starts sending automatic pings to the peers. -
stopSendingPings
protected void stopSendingPings() -
sendMessage
Description copied from interface:WebsocketEndpointWriter
Sends a String message to all peers of the endpoint.- Specified by:
sendMessage
in interfaceWebsocketEndpointWriter
-
sendMessage
Description copied from interface:WebsocketEndpointWriter
Sends a String message to a specific peer.- Specified by:
sendMessage
in interfaceWebsocketEndpointWriter
-
sendMessageExcept
Description copied from interface:WebsocketEndpointWriter
Sends a String message to all peers except the specified one.- Specified by:
sendMessageExcept
in interfaceWebsocketEndpointWriter
-
sendMessageExcept
Description copied from interface:WebsocketEndpointWriter
Sends a String message to all peers except the specified ones.- Specified by:
sendMessageExcept
in interfaceWebsocketEndpointWriter
-
sendMessage
Description copied from interface:WebsocketEndpointWriter
Sends a String message to specific peers.- Specified by:
sendMessage
in interfaceWebsocketEndpointWriter
-
sendMessage
public void sendMessage(byte[] message) Description copied from interface:WebsocketEndpointWriter
Sends a byte array message to all peers of the endpoint.- Specified by:
sendMessage
in interfaceWebsocketEndpointWriter
-
sendMessage
Description copied from interface:WebsocketEndpointWriter
Sends a byte array message to a specific peer.- Specified by:
sendMessage
in interfaceWebsocketEndpointWriter
-
sendMessageExcept
Description copied from interface:WebsocketEndpointWriter
Sends a byte array message to all peers except the specified one.- Specified by:
sendMessageExcept
in interfaceWebsocketEndpointWriter
-
sendMessageExcept
Description copied from interface:WebsocketEndpointWriter
Sends a byte array message to all peers except the specified ones.- Specified by:
sendMessageExcept
in interfaceWebsocketEndpointWriter
-
sendMessage
Description copied from interface:WebsocketEndpointWriter
Sends a byte array message to specific peers.- Specified by:
sendMessage
in interfaceWebsocketEndpointWriter
-
handleConnectionRequest
- Specified by:
handleConnectionRequest
in interfaceWebsocketEndpoint
-
getWebSocketProtocolHandshakeHandler
protected io.undertow.websockets.WebSocketProtocolHandshakeHandler getWebSocketProtocolHandshakeHandler()The handler to use for the Websocket connection. -
sendOnPeerConnectedAppEvent
Sends a "Peer connected " event to the app. -
sendOnStringMessageAppEvent
Sends a "String message" event to the app. -
sendOnBytesMessageAppEvent
Sends a "byte[] message" event to the app. -
sendPeerClosedAppEvent
Sends a "peer closed" event to the app. -
sendAppEventInNewThread
Sends an event to the application in a separated thread. -
getThreadExecutorForAppEventsTimeoutAmount
protected int getThreadExecutorForAppEventsTimeoutAmount()The timeout amount before cancelling a task when sending events to the application. -
getThreadExecutorForAppEventsTimeoutTimeUnit
The timeout unit before cancelling a task when sending events to the application. -
getThreadExecutorForAppEvents
The ExecutorService to use to send events to the application. -
getThreadExecutorForAppEventsThreadNumber
protected int getThreadExecutorForAppEventsThreadNumber()The maximum number of concurrent threads used when sending events to the application. -
getThreadExecutorForAppEventsThreadThreadFactory
The ThreadFactory to use for the Executor that sends events to the application.- Returns:
- the ThreadFactory to use or
null
to use the default one.
-