Class WebsocketRouter
- All Implemented Interfaces:
WebsocketConnectionListener
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 Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(String routerId, RouterListener listener) Add a listener for when the specified routerId is added/removed from the forwarding table.void
addMessageHandler
(String type, RouterMessageHandler handler) Add a message handler to the router.Calls createOutboundRouterWebsocket(alias) without an alias.Create an outbound router to router websocket client.boolean
isRouterIdConnected
(String routerId) Return true if the specified routerId is in the routing tablevoid
onConnect
(WebsocketConnection conn) Process connections from various sources including the http server which allows clients to connect, as well as outbound websockets.void
Called when a client disconnects from the server.void
removeListener
(String routerId, RouterListener listener) Remove a previously registered router listener.void
removeMessageHandler
(String type) Remove the message handler for the specified type.Resolve the specified address if it is an alias.void
sendMsg
(OutboundMessage msg) Send a message via the router.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.tccc.kos.commons.web.websocket.WebsocketConnectionListener
onError
-
Field Details
-
TYPE_OUTBOUND_ROUTER
- See Also:
-
MSG_TYPE_GET_ADDR
- See Also:
-
MSG_TYPE_SET_ALIAS
- See Also:
-
MSG_TYPE_START_SNIFFER
- See Also:
-
MSG_TYPE_STOP_SNIFFER
- See Also:
-
-
Method Details
-
addMessageHandler
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 forwardhandler
- the handler
-
removeMessageHandler
Remove the message handler for the specified type.- Parameters:
type
- the message type to remove the handler for
-
createOutboundRouterWebsocket
Calls createOutboundRouterWebsocket(alias) without an alias. Just a convenience method as aliases are pretty rare. -
createOutboundRouterWebsocket
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
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
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 interfaceWebsocketConnectionListener
- Parameters:
conn
- the new connection- Throws:
Exception
-
onDisconnect
Description copied from interface:WebsocketConnectionListener
Called when a client disconnects from the server.- Specified by:
onDisconnect
in interfaceWebsocketConnectionListener
- Parameters:
conn
- the connection that closed- Throws:
Exception
-
resolve
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
Return true if the specified routerId is in the routing table- Parameters:
routerId
- the routerId to check
-
addListener
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 interestlistener
- the listener to call
-
removeListener
Remove a previously registered router listener.- Parameters:
routerId
- the router of interestlistener
- the listener to remove
-
getEngine
-