Sha256: cdf6964d1bb89d80d556e6192041430518b7f572473c5d6215b6f415fb105c3d

Contents?: true

Size: 995 Bytes

Versions: 1

Compression:

Stored size: 995 Bytes

Contents

require 'whois'
require 'em-synchrony'

module Whois
  class Server
    class SocketHandler

      #
      # Overwrite Whois::Server::SocketHandler#call to
      # be EventMachine-aware, and send calls offs asynchronously
      # if the EM reactor is running, otherwise fallback to the
      # synchronous connection.
      #

      alias :orig_call :call

      def call(*args)
        defined?(EM) && EM.reactor_running? ? em_call(*args) : orig_call(*args)
      end

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

    end

    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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
em-whois-0.4.0 lib/em-whois.rb