Module: Arachni::RPC::EM

Extended by:
EM
Included in:
EM
Defined in:
lib/arachni/rpc/em/em.rb,
lib/arachni/rpc/em/client.rb,
lib/arachni/rpc/em/server.rb,
lib/arachni/rpc/em/version.rb,
lib/arachni/rpc/em/protocol.rb,
lib/arachni/rpc/em/connection_utilities.rb,
lib/arachni/rpc/em/ssl.rb

Overview

Provides some convenient methods for EventMachine’s Reactor.

@author: Tasos “Zapotek” Laskos

<tasos.laskos@gmail.com>
<zapotek@segfault.gr>

@version: 0.1

Defined Under Namespace

Modules: ConnectionUtilities, Protocol, SSL, Synchrony Classes: Client, Server

Constant Summary collapse

VERSION =
'0.1.1'

Instance Method Summary collapse

Instance Method Details

#add_to_reactor(&block) ⇒ Object

Adds a block in the Reactor.

Parameters:

  • &block (Proc)

    block to be included in the Reactor loop



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/arachni/rpc/em/em.rb', line 47

def add_to_reactor( &block )

    self.init

    # if we're already in the Reactor thread just run the block straight up.
    if ::EM::reactor_thread?
        block.call
    else
        @@reactor_tasks_mutex.lock
        @@reactor_tasks << block

        ensure_em_running!
        @@reactor_tasks_mutex.unlock
    end

end

#block!Object

Blocks until the Reactor stops running



67
68
69
70
# File 'lib/arachni/rpc/em/em.rb', line 67

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

#ensure_em_running!Object

Puts the Reactor in its own thread and runs it.

It also runs all blocks sent to #add_to_reactor.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/arachni/rpc/em/em.rb', line 77

def ensure_em_running!
    self.init

    if !::EM::reactor_running?
        q = Queue.new

        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

                @@reactor_tasks.each { |task| task.call }
                q << true
            end
        end
        q.pop
    end
end

#initObject

Inits method variables for the Reactor tasks and its Mutex.



37
38
39
40
# File 'lib/arachni/rpc/em/em.rb', line 37

def init
    @@reactor_tasks_mutex ||= Mutex.new
    @@reactor_tasks ||= []
end