Module: Rbuv::EM

Defined in:
lib/rbuv/em.rb,
lib/rbuv/em/version.rb,
lib/rbuv/em/connection.rb

Defined Under Namespace

Classes: Connection

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.add_periodic_timer(interval, blk = nil, &block) ⇒ Object



68
69
70
71
# File 'lib/rbuv/em.rb', line 68

def self.add_periodic_timer(interval, blk=nil, &block)
  blk ||= block
  EventMachine::PeriodicTimer.new(interval, blk)
end

.add_timer(interval, blk = nil, &block) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/rbuv/em.rb', line 60

def self.add_timer(interval, blk=nil, &block)
  if blk ||= block
    s = add_oneshot_timer((interval.to_f * 1000).to_i, &blk)
    @timers[s] = blk
    s
  end
end

.bind_connect(bind_addr, bind_port, server, port = nil, handler = nil, *args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rbuv/em.rb', line 107

def self.bind_connect bind_addr, bind_port, server, port=nil, handler=nil, *args
  begin
    port = Integer(port)
  rescue ArgumentError, TypeError
    # there was no port, so server must be a unix domain socket
    # the port argument is actually the handler, and the handler is one of the args
    args.unshift handler if handler
    handler = port
    port = nil
  end if port

  klass = klass_from_handler(Connection, handler, *args)

  s = if port
        if bind_addr
          bind_connect_server bind_addr, bind_port.to_i, server, port
        else
          connect_server server, port
        end
      else
        connect_unix_server server
      end

  c = klass.new s, *args
  @conns[s] = c
  block_given? and yield c
  c
end

.cancel_timer(timer_or_sig) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/rbuv/em.rb', line 73

def self.cancel_timer(timer_or_sig)
  if timer_or_sig.respond_to? :cancel
    timer_or_sig.cancel
  else
    @timers[timer_or_sig] = false if @timers.has_key?(timer_or_sig)
  end
end

.close_connection(tcp, _after_writing) ⇒ Object



141
142
143
# File 'lib/rbuv/em.rb', line 141

def self.close_connection(tcp, _after_writing)
  tcp.close
end

.connect(server, port = nil, handler = nil, *args, &blk) ⇒ Object



103
104
105
# File 'lib/rbuv/em.rb', line 103

def self.connect(server, port=nil, handler=nil, *args, &blk)
  bind_connect nil, nil, server, port, handler, *args, &blk
end

.reactor_running?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/rbuv/em.rb', line 56

def self.reactor_running?
  @reactor_running || false
end

.run(blk = nil, tail = nil, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbuv/em.rb', line 13

def self.run(blk=nil, tail=nil, &block)
  tail && @tails.unshift(tail)

  b = blk || block
  if reactor_running?
    b && b.call
  else
    @conns = {}
    @acceptors = {}
    @timers = {}
    @tails ||= []
    begin
      @reactor_running = true
      initialize_event_machine
      b && add_timer(0, b)
      run_machine
    ensure
      until @tails.empty?
        @tails.pop.call
      end

      release_machine

      @reactor_running = false
    end
  end
end

.run_blockObject



49
50
51
52
53
54
# File 'lib/rbuv/em.rb', line 49

def self.run_block
  run do
    yield
    Rbuv.stop
  end
end

.send_data(tcp, data, _size) ⇒ Object



136
137
138
139
# File 'lib/rbuv/em.rb', line 136

def self.send_data(tcp, data, _size)
  p [tcp, data, _size]
  tcp.write(data)
end

.start_server(server, port = nil, handler = nil, *args, &block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rbuv/em.rb', line 81

def self.start_server(server, port=nil, handler=nil, *args, &block)
  begin
    port = Integer(port)
  rescue ArgumentError, TypeError
    # there was no port, so server must be a unix domain socket
    # the port argument is actually the handler, and the handler is one of the args
    args.unshift handler if handler
    handler = port
    port = nil
  end if port

  klass = klass_from_handler(Connection, handler, *args)

  s = if port
        start_tcp_server server, port
      else
        start_unix_server server
      end
  @acceptors[s] = [klass, args, block]
  s
end

.stopObject



41
42
43
# File 'lib/rbuv/em.rb', line 41

def self.stop
  Rbuv.stop
end

.stop_event_loopObject



45
46
47
# File 'lib/rbuv/em.rb', line 45

def self.stop_event_loop
  self.stop
end