Sha256: e998ca819a7cf6af9e18d0a25d29e91ced8545b8d443241b018654c1e5b7ac28

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

=begin

    This file is part of the Arachni-RPC EM project and may be subject to
    redistribution and commercial restrictions. Please see the Arachni-RPC EM
    web site for more information on licensing and terms of use.

=end

module Arachni
module RPC

#
# Provides some convenient methods for EventMachine's Reactor.
#
# @author: Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
#
module EM

    module Synchrony
        def run( &block )
            Fiber.new{ block.call }.resume
        end

        extend self
    end

    #
    # Schedules a block to be run in the EM reactor.
    #
    # @param    [Proc]    &block
    #
    def schedule( &block )
        ensure_em_running
        ::EM.schedule( &block )
    end

    #
    # Blocks until the Reactor stops running
    #
    def block
        # beware of deadlocks, we can't join our own thread
        ::EM.reactor_thread.join if ::EM.reactor_thread && !::EM::reactor_thread?
    end

    #
    # Puts the Reactor in its own thread and runs it.
    #
    def ensure_em_running
        if !::EM::reactor_running?

            Thread.new do
                ::EM.run do
                    ::EM.error_handler do |e|
                        $stderr.puts "Exception raised during event loop: " +
                        "#{e.message} (#{e.class})\n#{(e.backtrace ||
                            [])[0..5].join("\n")}"
                    end
                end
            end

            sleep 0.1 while !::EM.reactor_running?
        end
    end

    extend self
end
end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
arachni-rpc-em-0.1.3 lib/arachni/rpc/em/em.rb
arachni-rpc-em-0.1.3.pre2 lib/arachni/rpc/em/em.rb
arachni-rpc-em-0.1.3.pre1 lib/arachni/rpc/em/em.rb
arachni-rpc-em-0.1.3dev1 lib/arachni/rpc/em/em.rb