Class WebsocketRouter

java.lang.Object
com.tccc.kos.commons.web.websocket.router.WebsocketRouter
All Implemented Interfaces:
WebsocketConnectionListener

public class WebsocketRouter extends Object implements WebsocketConnectionListener
This is the front-end to the websocket router. The actual routing logic is split out into the engine class in order to keep the code easier to understand.

Within the router logic there are two types of connections:

- Client / router : This is a connection with an external entity and our router. The external entity doesn't need to understand anything about how routing works and simply needs to know the destination address it wants to talk to. The client doesn't understand that it has an address but the router will assign this end of the websocket a local address relative to this node so other nodes can return traffic to the client. Any inbound messages will be tagged with the connection address as the source address so return messages can get back to this connection. This means that clients need to know nothing about how things work other than use some form of application level discovery to locate the nodes it wants to talk to (see aliases as well)

- Router / router : This is a connection between two routers. Routers discover each other using a special routing protocol which allows them to identify other routers and forward traffic. Any inbound traffic to a router is processed by the following general policy:

- no address : This is assumed to come from a locally attached client so it will be processed locally as if the router doesn't exist.

- router address : The router will process the message locally but use the src address in the message to send the result back to the correct caller.

- router protocol messages : These are forwarded to the router for processing.

- non-router address : The message will be forwarded. If the address is for an attached client, the router will forward to the client. If not for a local client, it will try to route the message towards the destination using the routing tables.

Since:
1.0
Version:
2022-09-28
  • Field Details

  • Method Details

    • addMessageHandler

      public void addMessageHandler(String type, RouterMessageHandler handler)
      Add a message handler to the router. The router will forward messages of this type destined for this node to the handler.
      Parameters:
      type - the message type to forward
      handler - the handler
    • removeMessageHandler

      public void removeMessageHandler(String type)
      Remove the message handler for the specified type.
      Parameters:
      type - the message type to remove the handler for
    • createOutboundRouterWebsocket

      public WebsocketClient createOutboundRouterWebsocket()
      Calls createOutboundRouterWebsocket(alias) without an alias. Just a convenience method as aliases are pretty rare.
    • createOutboundRouterWebsocket

      public WebsocketClient createOutboundRouterWebsocket(String alias)
      Create an outbound router to router websocket client. This returns a WebsocketClient which can be managed as needed (start/stop/endpoint) but the WebsocketListener is set to the router. The connections that originate from this client will be tagged as router to router which will immediately trigger the router discovery protocol and allow for message routing. If the client is no longer needed, simply stop it let it get garbage collected.
      Parameters:
      alias - optional alias for the remote router
    • sendMsg

      public void sendMsg(OutboundMessage msg)
      Send a message via the router. This is called for messages that originate on this node. This will stamp the message with the src address of this node before sending to the router engine.
      Parameters:
      msg - the message to send
    • onConnect

      public void onConnect(WebsocketConnection conn) throws Exception
      Process connections from various sources including the http server which allows clients to connect, as well as outbound websockets. The connection can have attributes that determine how the connection is used.
      Specified by:
      onConnect in interface WebsocketConnectionListener
      Parameters:
      conn - the new connection
      Throws:
      Exception
    • onDisconnect

      public void onDisconnect(WebsocketConnection conn) throws Exception
      Description copied from interface: WebsocketConnectionListener
      Called when a client disconnects from the server.
      Specified by:
      onDisconnect in interface WebsocketConnectionListener
      Parameters:
      conn - the connection that closed
      Throws:
      Exception
    • resolve

      public String resolve(String addr)
      Resolve the specified address if it is an alias. If the alias is unknown then this will return null. If the argument is already an address it will simply be returned.
    • isRouterIdConnected

      public boolean isRouterIdConnected(String routerId)
      Return true if the specified routerId is in the routing table
      Parameters:
      routerId - the routerId to check
    • addListener

      public void addListener(String routerId, RouterListener listener)
      Add a listener for when the specified routerId is added/removed from the forwarding table. This will match routers in the current zone as well as outside the zone. The callback will indicate whether the router is inside / outside the zone without having to manually check the address in the callback.
      Parameters:
      routerId - the router of interest
      listener - the listener to call
    • removeListener

      public void removeListener(String routerId, RouterListener listener)
      Remove a previously registered router listener.
      Parameters:
      routerId - the router of interest
      listener - the listener to remove
    • getEngine

      public RouterEngine getEngine()