Class SpincastWebsocketEndpoint

java.lang.Object
org.spincast.plugins.undertow.SpincastWebsocketEndpoint
All Implemented Interfaces:
WebsocketEndpointManager, WebsocketEndpointWriter, WebsocketEndpoint

public class SpincastWebsocketEndpoint extends Object implements WebsocketEndpoint
  • Field Details

    • logger

      protected static final org.slf4j.Logger logger
    • EXCHANGE_VARIABLE_PEER_ID

      public static final String EXCHANGE_VARIABLE_PEER_ID
  • Constructor Details

  • Method Details

    • init

      @Inject protected void init()
    • getWebSocketChannelByPeerId

      protected Map<String,io.undertow.websockets.core.WebSocketChannel> getWebSocketChannelByPeerId()
    • getEventsHandler

      protected WebsocketEndpointHandler getEventsHandler()
    • getUndertowWebsocketEndpointWriterFactory

      protected UndertowWebsocketEndpointWriterFactory getUndertowWebsocketEndpointWriterFactory()
    • getSpincastUndertowConfig

      protected SpincastUndertowConfig getSpincastUndertowConfig()
    • getSpincastUndertowUtils

      protected SpincastUndertowUtils getSpincastUndertowUtils()
    • getUndertowWebsocketWriter

      protected UndertowWebsocketEndpointWriter getUndertowWebsocketWriter()
    • getNewPeerIdLock

      protected Object getNewPeerIdLock(String peerId)
      Gets the creation lock for a peer id.
    • getEndpointId

      public String getEndpointId()
      Description copied from interface: WebsocketEndpointManager
      The endpoint id
      Specified by:
      getEndpointId in interface WebsocketEndpointManager
    • getPeersIds

      public Set<String> getPeersIds()
      Description copied from interface: WebsocketEndpointManager
      The connected peers' ids.
      Specified by:
      getPeersIds in interface WebsocketEndpointManager
    • closePeer

      public void closePeer(String peerId)
      Description copied from interface: WebsocketEndpointManager
      Closes a specific peer connection on the endpoint.
      Specified by:
      closePeer in interface WebsocketEndpointManager
    • closePeer

      public void closePeer(String peerId, int closingCode, String closingReason)
      Description copied from interface: WebsocketEndpointManager
      Closes a specific peer connection on the endpoint.
      Specified by:
      closePeer in interface WebsocketEndpointManager
    • removePeerChannelAndSendPeerClosedAppEvent

      protected void removePeerChannelAndSendPeerClosedAppEvent(String peerId)
    • removePeerChannel

      protected void removePeerChannel(String peerId)
    • managePeersWriteConnectionClosed

      protected void managePeersWriteConnectionClosed(Set<String> peerIds)
      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 interface WebsocketEndpointManager
    • isClosed

      public boolean isClosed()
      Description copied from interface: WebsocketEndpointManager
      Is this endpoint closed?
      Specified by:
      isClosed in interface WebsocketEndpointManager
    • 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 interface WebsocketEndpointManager
    • 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 interface WebsocketEndpointManager
      Parameters:
      sendClosingMessageToPeers - if true, Spincast will try to send a "closing" message to the peers before closing their connections.
    • closeEndpoint

      public void closeEndpoint(int closingCode, String closingReason)
      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 interface WebsocketEndpointManager
    • 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

      public void sendMessage(String message)
      Description copied from interface: WebsocketEndpointWriter
      Sends a String message to all peers of the endpoint.
      Specified by:
      sendMessage in interface WebsocketEndpointWriter
    • sendMessage

      public void sendMessage(String peerId, String message)
      Description copied from interface: WebsocketEndpointWriter
      Sends a String message to a specific peer.
      Specified by:
      sendMessage in interface WebsocketEndpointWriter
    • sendMessageExcept

      public void sendMessageExcept(String peerId, String message)
      Description copied from interface: WebsocketEndpointWriter
      Sends a String message to all peers except the specified one.
      Specified by:
      sendMessageExcept in interface WebsocketEndpointWriter
    • sendMessageExcept

      public void sendMessageExcept(Set<String> peerIdsToRemove, String message)
      Description copied from interface: WebsocketEndpointWriter
      Sends a String message to all peers except the specified ones.
      Specified by:
      sendMessageExcept in interface WebsocketEndpointWriter
    • sendMessage

      public void sendMessage(Set<String> peerIds, String message)
      Description copied from interface: WebsocketEndpointWriter
      Sends a String message to specific peers.
      Specified by:
      sendMessage in interface WebsocketEndpointWriter
    • 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 interface WebsocketEndpointWriter
    • sendMessage

      public void sendMessage(String peerId, byte[] message)
      Description copied from interface: WebsocketEndpointWriter
      Sends a byte array message to a specific peer.
      Specified by:
      sendMessage in interface WebsocketEndpointWriter
    • sendMessageExcept

      public void sendMessageExcept(String peerId, byte[] message)
      Description copied from interface: WebsocketEndpointWriter
      Sends a byte array message to all peers except the specified one.
      Specified by:
      sendMessageExcept in interface WebsocketEndpointWriter
    • sendMessageExcept

      public void sendMessageExcept(Set<String> peerIdsToRemove, byte[] message)
      Description copied from interface: WebsocketEndpointWriter
      Sends a byte array message to all peers except the specified ones.
      Specified by:
      sendMessageExcept in interface WebsocketEndpointWriter
    • sendMessage

      public void sendMessage(Set<String> peerIds, byte[] message)
      Description copied from interface: WebsocketEndpointWriter
      Sends a byte array message to specific peers.
      Specified by:
      sendMessage in interface WebsocketEndpointWriter
    • handleConnectionRequest

      public void handleConnectionRequest(io.undertow.server.HttpServerExchange exchange, String peerId)
      Specified by:
      handleConnectionRequest in interface WebsocketEndpoint
    • getWebSocketProtocolHandshakeHandler

      protected io.undertow.websockets.WebSocketProtocolHandshakeHandler getWebSocketProtocolHandshakeHandler()
      The handler to use for the Websocket connection.
    • sendOnPeerConnectedAppEvent

      protected void sendOnPeerConnectedAppEvent(String peerId)
      Sends a "Peer connected " event to the app.
    • sendOnStringMessageAppEvent

      protected void sendOnStringMessageAppEvent(String peerId, String message)
      Sends a "String message" event to the app.
    • sendOnBytesMessageAppEvent

      protected void sendOnBytesMessageAppEvent(String peerId, byte[] message)
      Sends a "byte[] message" event to the app.
    • sendPeerClosedAppEvent

      protected void sendPeerClosedAppEvent(String peerId)
      Sends a "peer closed" event to the app.
    • sendAppEventInNewThread

      protected void sendAppEventInNewThread(Runnable runnable)
      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

      protected TimeUnit getThreadExecutorForAppEventsTimeoutTimeUnit()
      The timeout unit before cancelling a task when sending events to the application.
    • getThreadExecutorForAppEvents

      protected ExecutorService 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

      protected ThreadFactory 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.