# class GxG::Communications::Bridge

### Public Class Methods

new(process=nil, the_url=nil, options=nil) click to toggle source

    # File lib/gxg/gxg_communications.rb, line 823
    def initialize(process=nil, the_url=nil, options=nil)
        # Note: process (required) is a Service or Application instance only
        unless process.is_a?(::GxG::Services::Service)
            raise ArgumentError, "You MUST provide a Service or Application instance to bind to."
        end
        # Ensure protocol supported
        if the_url.is_a?(::String)
            the_url = ::URI::parse(the_url)
        end
        unless the_url.is_a?(::URI::Generic)
            raise ArgumentError, "You MUST provide a valid URL as a String or URI."
        end
        the_type = ::GxG::BRIDGES_AVAILABLE[(the_url.scheme.to_sym)]
        unless the_type
            raise ArgumentError, "Sorry, #{the_url.scheme.to_sym.inspect} is not a supported protocol."
        end
        # Variables
        @process = process
        @url = the_url
        @adapter = the_type.new(self, @url, options)
        @uuid = ::GxG::uuid_generate.to_sym
        @interface = {}
        self.on(:interface, "Available Commands") do
            self.interface()
        end
        self
    end

### Public Instance Methods

call_event(operation_envelope=nil) click to toggle source

    # File lib/gxg/gxg_communications.rb, line 869
    def call_event(operation_envelope=nil)
      result = nil
      if operation_envelope.is_a?(::Hash)
        the_event = operation_envelope.keys[0]
        if the_event
          data = operation_envelope[(the_event)]
          if @interface[(the_event)]
            begin
              result = {:result => @interface[(the_event)][:procedure].call(@process, self, data)}
            rescue Exception => the_error
              log_error({:error => the_error, :parameters => {:data => data}})
              result = {:result => nil, :error => the_error.to_s}
            end
          else
            result = {:result => nil, :error => "Command #{the_event.inspect} Not Found"}
          end
        end
      end
      result
    end

interface() click to toggle source

    # File lib/gxg/gxg_communications.rb, line 890
    def interface()
      result = {}
      @interface.each_pair do |the_event, the_record|
        result[(the_event)] = the_record[:description]
      end
      result
    end

on(the_event, description=nil, &block) click to toggle source

Command Interface

    # File lib/gxg/gxg_communications.rb, line 855
    def on(the_event, description=nil, &block)
      unless the_event.is_a?(::Symbol)
        raise ArgumentError, "You must specify an event listener with a unique Symbol."
      end
      unless block.respond_to?(:call)
        raise ArgumentError, "You must provide an event code block to execute."
      end
      unless description
        description = "{ '#{the_event.to_s}': '(your_data_payload)' }"
      end
      @interface[(the_event)] = {:description => description, :procedure => block}
      true
    end

reply(the_channel_uuid=nil, reply_body=nil, address=nil) click to toggle source

    # File lib/gxg/gxg_communications.rb, line 947
    def reply(the_channel_uuid=nil, reply_body=nil, address=nil)
        result = {:result => false}
        if ::GxG::valid_uuid?(the_channel_uuid) && reply_body.is_a?(::GxG::Events::Message) && address
            @adapter.send_message(the_channel_uuid, reply_body, address)
            result[:result] = true
        end
        result
    end

request(the_channel_uuid=nil, request_body=nil, address=nil, options={}) click to toggle source

Request/Reply `Support`

    # File lib/gxg/gxg_communications.rb, line 908
    def request(the_channel_uuid=nil, request_body=nil, address=nil, options={})
        result = nil
        #
        if ::GxG::valid_uuid?(the_channel_uuid) && request_body.is_a?(::GxG::Events::Message) && address
            payload = nil
            context = ::GxG::uuid_generate
            if address.to_s.valid_jid?
                # Point-to-Point Request
                payload = new_message({:sender => @adapter.jid().to_s, :body => request_body})
                payload[:context] = context.to_s
                if payload
                    @adapter.send_message(the_channel_uuid, payload, address)
                    the_reply_list = @adapter.get_messages_by_context(the_channel_uuid, context)
                    if options.is_a?(::Hash)
                        if options[:timeout].is_a?(::Numeric)
                            timeout = Time.now.to_f + options[:timeout].to_f
                        else
                            timeout = Time.now.to_f + 30.0
                        end
                    else
                        timeout = Time.now.to_f + 30.0
                    end
                    until the_reply_list.size > 0 do
                        the_reply_list = @adapter.get_messages_by_context(the_channel_uuid, context)
                        sleep 0.5
                        if Time.now.to_f >= timeout
                            break
                        end
                    end
                    if the_reply_list.size > 0
                        result = the_reply_list[0]
                    end
                end
            end
        end
        #
        result
    end

respond_to_event?(the_event=nil) click to toggle source

    # File lib/gxg/gxg_communications.rb, line 898
    def respond_to_event?(the_event=nil)
      result = false
      if the_event.is_a?(::Symbol)
        if @interface[(the_event)]
          result = true
        end
      end
      result
    end

uuid() click to toggle source

    # File lib/gxg/gxg_communications.rb, line 851
    def uuid()
        @uuid
    end