Class: FnordMetric::TCPAcceptor
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- FnordMetric::TCPAcceptor
- Defined in:
- lib/fnordmetric/acceptors/tcp_acceptor.rb
Constant Summary collapse
- @@opts =
nil
Class Method Summary collapse
Instance Method Summary collapse
- #api ⇒ Object
- #close_connection? ⇒ Boolean
- #next_event ⇒ Object
- #post_init ⇒ Object
- #push_next_event ⇒ Object
- #read_next_event ⇒ Object
- #receive_data(chunk) ⇒ Object
- #unbind ⇒ Object
Class Method Details
.options(opts) ⇒ Object
9 10 11 |
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 9 def self.(opts) @@opts = opts end |
.start(opts) ⇒ Object
4 5 6 7 |
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 4 def self.start(opts) @@opts = opts EM.start_server(*(opts[:listen] + [self])) end |
Instance Method Details
#api ⇒ Object
54 55 56 |
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 54 def api @api ||= FnordMetric::API.new(FnordMetric.) end |
#close_connection? ⇒ Boolean
38 39 40 |
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 38 def close_connection? #@backend.hangup unless @streaming || (@events_buffered!=0) end |
#next_event ⇒ Object
18 19 20 21 |
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 18 def next_event read_next_event push_next_event end |
#post_init ⇒ Object
42 43 44 45 46 47 |
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 42 def post_init @events_buffered = 0 @streaming = true @buffer = "" @events = [] end |
#push_next_event ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 30 def push_next_event return true if @events.empty? @events_buffered -= 1 api.event(@events.pop) close_connection? EM.next_tick(&method(:push_next_event)) end |
#read_next_event ⇒ Object
23 24 25 26 27 28 |
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 23 def read_next_event while (event = @buffer.slice!(/^(.*)\n/)) @events_buffered += 1 @events << event end end |
#receive_data(chunk) ⇒ Object
13 14 15 16 |
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 13 def receive_data(chunk) @buffer << chunk next_event end |
#unbind ⇒ Object
49 50 51 52 |
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 49 def unbind @streaming = false close_connection? end |