Class AgentMessageRouter

java.lang.Object
com.ultikits.ultitools.utils.AgentMessageRouter

@Internal public final class AgentMessageRouter extends Object
Validates envelopes and dispatches framework handlers; internal, not for module use. Handlers validate their own payloads. Synchronous validation and runtime failures become correlated errors; diagnostics never include raw payload or exception text.

The capability predicate receives contract capability names such as commands and files:read, plus the literal fromPayload:module for module.request. Integration must return true for that sentinel so the module handler can validate the payload's module identifier and answer module_not_found when no responder is owned by that module. The module handler does not check the negotiated modules:<id>@<version> capabilities; whether a request for a module the gateway did not accept is filtered is decided in 22-21. The sentinel is never declared in hello or welcome. Likewise, accept power here; its handler applies the operator switch and answers power_disabled itself.

Since:
6.3.1
  • Field Details

  • Constructor Details

    • AgentMessageRouter

      public AgentMessageRouter(Predicate<String> capabilityEnabled, Function<String,String> refusalForCapability)
      Creates a router using the system clock for warning throttling.
      Parameters:
      capabilityEnabled - the per-request capability gate
      refusalForCapability - supplies safe refusal text naming the configuration key
    • AgentMessageRouter

      public AgentMessageRouter(Predicate<String> capabilityEnabled, Function<String,String> refusalForCapability, LongSupplier clock)
      Creates a router with an injectable clock. Warnings are limited to one per error code per minute; rejected requests still receive their own error reply.
      Parameters:
      capabilityEnabled - the per-request capability gate
      refusalForCapability - supplies safe refusal text, never payload or exception text
      clock - supplies epoch milliseconds for warning throttling
  • Method Details

    • onSwitchRefusal

      void onSwitchRefusal(AgentMessageRouter.SwitchRefusalListener listener)
      Sets the listener told of each refusal by the capability gate; null removes it. A listener that throws never costs the request its reply, which was sent before the listener runs.
      Parameters:
      listener - the listener, or null
    • onShutdown

      void onShutdown(ShutdownGate gate)
      Sets the shutdown gate (ShutdownGate): from the moment it refuses, every state-changing type it covers is answered with its shutting-down failure before the switch gate or a handler sees it; a request it lets in keeps the gate's permit for the whole switch check and handler call. null removes it.
      Parameters:
      gate - the gate, or null
    • register

      public void register(String type, CloudRequestHandler handler)
      Registers or replaces the handler for one frozen-contract message type.
      Parameters:
      type - the exact registered protocol type
      handler - the non-null handler, invoked on the dispatching thread
      Throws:
      IllegalArgumentException - if the type is not in the contract
    • dispatch

      public void dispatch(String text, CloudChannel channel)
      Parses and dispatches one frame, applying the capability gate before the handler. Invalid frames without a usable id are discarded at FINE; otherwise failures receive a correlated protocol error. Unhandled error, ack and welcome are ignored.
      Parameters:
      text - the incoming JSON text, never included in diagnostics
      channel - supplies current limits and receives replies
    • dispatch

      private void dispatch(Envelope envelope, CloudChannel channel)
    • dispatchAdmitted

      private void dispatchAdmitted(Envelope envelope, CloudChannel channel)
    • refuse

      private void refuse(CloudChannel channel, String id, String capability, String message)
    • error

      private void error(CloudChannel channel, String id, String code, String message, com.google.gson.JsonObject details)
    • error

      private void error(CloudChannel channel, String id, String code, String message, com.google.gson.JsonObject details, String diagnostic)
    • warn

      private void warn(String code, String diagnostic)