Class: A2A::Server::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/server/dispatcher.rb

Overview

Routes incoming A2A operations to registered handler objects.

Each handler declares the operations it handles via ‘#operations`. When an operation arrives, the dispatcher finds all matching handlers and calls them. Errors in one handler do not prevent others from running.

The Dispatcher is a Rack app (terminal, not middleware). It reads env set by Triage and fans out to matching handlers.

Instance Method Summary collapse

Constructor Details

#initializeDispatcher

Returns a new instance of Dispatcher.



19
20
21
# File 'lib/a2a/server/dispatcher.rb', line 19

def initialize
  @handlers = Hash.new { |h, k| h[k] = [] }
end

Instance Method Details

#call(env) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/a2a/server/dispatcher.rb', line 36

def call(env)
  operation = env["a2a.operation"]

  if operation
    dispatch(operation, env)
  end

  [200, {}, []]
end

#handler_countObject



46
47
48
# File 'lib/a2a/server/dispatcher.rb', line 46

def handler_count
  @handlers.values.flatten.size
end

#register(handler) ⇒ Object

Register a handler object.

The handler must respond to:

#operations -> Array<String>  (e.g. ["SendMessage", "GetTask"])
#call(env)  -> void           (sets env["a2a.result"])


29
30
31
32
33
34
# File 'lib/a2a/server/dispatcher.rb', line 29

def register(handler)
  handler.operations.each do |op|
    @handlers[op] << handler
    Console.info(self) { "Registered #{handler.class.name} for #{op}" }
  end
end