Class: BrB::EventMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/brb/event_machine.rb

Direct Known Subclasses

Protocol

Class Method Summary collapse

Class Method Details

.open(uri, klass, opts = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/brb/event_machine.rb', line 29

def open(uri, klass, opts = {})
  host, port = parse_uri(uri)
  begin
    ensure_em_is_started!

    q = Queue.new
    EM.schedule do
      q << EM::connect(host, port, klass, opts.merge(:uri => "brb://#{host}:#{port}"))
    end
    
    # Wait for socket connection with the q.pop
    return q.pop

  rescue Exception => e
    BrB.logger.error e.backtrace.join("\n")
    raise "#{e} - #{uri}"
  end
end

.open_server(uri, klass, opts = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/brb/event_machine.rb', line 48

def open_server(uri, klass, opts = {})
  host, port = parse_uri(uri)
  max = 80 # Nb try before giving up
  begin
    uri = "brb://#{host}:#{port}"
    ensure_em_is_started!

    # Schedule server creation for thread safety
    q = Queue.new
    EM.schedule do
      q << EM::start_server(host, port, klass, opts.merge(:uri => uri))
    end

    # Wait for server creation with the q.pop
    return uri, q.pop

  rescue Exception => e
    max -= 1
    port += 1
    retry if max > 0
    BrB.logger.error e.backtrace.join("\n")
    raise "#{e} - BrB Tcp Event machine Can not bind on #{host}:#{port}"
  end

end