Sha256: 84c5309b55f7e947e29fbb8dccd70564d6ef20cb32e5c6eed344f5d72ed30ff6

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

require 'whois'
require 'em-synchrony'

module Whois
  class Server
    module Adapters

      class Base

        private

          #
          # Overwrite Whois::Server::Adapters::Base#ask_the_socket to 
          # be EventMachine-aware, and send calls offs asynchronously
          # if the EM reactor is running, otherwise fallback to the
          # synchronous connection.
          #
          
          alias :orig_ask_the_socket :ask_the_socket

          def ask_the_socket(*args)
            defined?(EM) && EM.reactor_running? ? em_ask_the_socket(*args) : orig_ask_the_socket(*args)
          end # ask_the_socket

          def em_ask_the_socket(query, *args)
            fiber = Fiber.current
            EM::connect args[0], args[1], AsyncClient, query, fiber
            Fiber.yield
          end # em_ask_the_socket

      end # Base

      class AsyncClient < EventMachine::Connection

        def initialize *args
          @query, @fiber = args[0..1]
          @data = ""
          super
        end

        def post_init
          send_data "#{@query}\r\n"
        end

        def receive_data(data)
          @data << data
        end

        def unbind
          @fiber.resume @data
        end

      end # AsyncClient

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
em-whois-0.3.0 lib/em-whois.rb
em-whois-0.2.0 lib/em-whois.rb