Sha256: 8cfcc636f819d0bd180b6aec778c38fc737acbde598d223eb755e3f3f9cf4468

Contents?: true

Size: 1.31 KB

Versions: 10

Compression:

Stored size: 1.31 KB

Contents

require 'rhcp/rhcp_exception'
require 'rhcp/broker'

module RHCP

  class DispatchingBroker < Broker
    def initialize
      @brokers = []
    end
    
    def add_broker(new_broker)
      @brokers << new_broker
      # TODO this is not a good way of handling duplicate detection since the broker has been added to the list already
      get_command_list()
    end

    def get_broker_for_command_name(command_name, context)
      result = nil
      @brokers.each do |broker|
        broker.get_command_list(context).each do |name, command|
          if name == command_name
            result = broker
          end
        end
      end
      raise RHCP::RhcpException.new("no broker found that offers command '#{command_name}'") if result == nil
      result
    end

    # returns a list of all known commands
    def get_command_list(context = RHCP::Context.new())
      result = {}
      @brokers.each do |broker|
        broker.get_command_list(context).each do |name, command|
          raise RHCP::RhcpException.new("duplicate command: '#{name}' has already been defined.") if result.has_key?(name)
          result[name] = command
        end
      end
      result
    end

    def execute(request)
      broker = get_broker_for_command_name(request.command.name, request.context)
      broker.execute(request)
    end

  end
  
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rhcp-0.2.23 lib/rhcp/dispatching_broker.rb
rhcp-0.2.22 lib/rhcp/dispatching_broker.rb
rhcp-0.2.21 lib/rhcp/dispatching_broker.rb
rhcp-0.2.20 lib/rhcp/dispatching_broker.rb
rhcp-0.2.19 lib/rhcp/dispatching_broker.rb
rhcp-0.2.18 lib/rhcp/dispatching_broker.rb
rhcp-0.2.17 lib/rhcp/dispatching_broker.rb
rhcp-0.2.16 lib/rhcp/dispatching_broker.rb
rhcp-0.2.15 lib/rhcp/dispatching_broker.rb
rhcp-0.2.14 lib/rhcp/dispatching_broker.rb