Class RouterEngine

java.lang.Object
com.tccc.kos.commons.web.websocket.router.RouterEngine
All Implemented Interfaces:
WebsocketMsgHandler

public class RouterEngine extends Object implements WebsocketMsgHandler
This is the core of the websocket router.
Version:
Jul-18-22
  • Field Details

  • Constructor Details

    • RouterEngine

      public RouterEngine(WebsocketRouter router, String zone, String routerId, boolean primary)
      Create a new router engine.
      Parameters:
      router - the front end of the router
      zone - the zone the router is in (null to detect from master)
      routerId - the unique id of this router within the zone
      primary - true if this is a primary (used to alias)
  • Method Details

    • addClient

      public void addClient(WebsocketConnection conn)
      Add a client to the router. This may end up being a router connection eventually but we'll switch it once we get a hello message if that's the case.
      Parameters:
      conn - the connection to add
    • addRouter

      public void addRouter(WebsocketConnection conn)
      Add the connection as a router. This is only used for outbound connections where we know the connection is router / router so we don't accidentally send router protocol messages to clients.
      Parameters:
      conn - the connection to add
    • addAlias

      public void addAlias(WebsocketConnection conn, String alias)
      Add an alias to the specified connection.
    • disconnect

      public void disconnect(WebsocketConnection conn)
      Handle disconnect of a routed connection.
    • sendMsg

      public void sendMsg(WebsocketConnection conn, RoutedMessage msg)
      Convenience method to send a message to a connection
    • onReceiveFromRemote

      public void onReceiveFromRemote(WebsocketConnection conn, String txtMsg) throws Exception
      Route the message to the correct destination.
      Specified by:
      onReceiveFromRemote in interface WebsocketMsgHandler
      Parameters:
      conn - the connection the message was received on
      txtMsg - the message
      Throws:
      Exception - if there is any error
    • routeMsg

      public void routeMsg(OutboundMessage msg)
      Attempt to route the outbound message. This will serialize the outbound message to an inbound message suitable for routing. If the destination is this node then we're going to serialize to json and back again which isn't very efficient so we shouldn't be routing messages that don't need to be routed.
      Parameters:
      msg - the message to route
    • resolve

      public String resolve(String addr)
      Resolve the specified address using the aliases table. Returns the input if no there is no alias match. This does not validate that the input is a valid address.
    • routeMsg

      public void routeMsg(WebsocketConnection conn, InboundMessage msg)
      Attempt to route the specified message. This may result in the message being processed locally or forwarded to a client or another router. The source address should be set in the message before getting here in order to receive a response (unless the message originated on this node in which case no source address is required).
      Parameters:
      conn - the connection the message arrived on, may be null
      msg - the message to route
    • forwardMsg

      public void forwardMsg(InboundMessage msg, String dstAddr, String trace)
      Forward the message using the forwarding rules.
    • getTableRebuildDelay

      public int getTableRebuildDelay()
      Return the current table rebuild delay.
    • setTableRebuildDelay

      public void setTableRebuildDelay(int delay)
      Set the table rebuild delay.
      Parameters:
      delay - the new delay
    • getZone

      public String getZone()
    • getRouterId

      public String getRouterId()
    • getRouterAddr

      public String getRouterAddr()
    • getRouterTTL

      public int getRouterTTL()
    • setRouterTTL

      public void setRouterTTL(int routerTTL)