Class: FnordMetric::TCPAcceptor

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/fnordmetric/acceptors/tcp_acceptor.rb

Constant Summary collapse

@@opts =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.options(opts) ⇒ Object



9
10
11
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 9

def self.options(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

#apiObject



54
55
56
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 54

def api
  @api ||= FnordMetric::API.new(FnordMetric.options)
end

#close_connection?Boolean

Returns:

  • (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_eventObject



18
19
20
21
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 18

def next_event
  read_next_event
  push_next_event
end

#post_initObject



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_eventObject



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_eventObject



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

#unbindObject



49
50
51
52
# File 'lib/fnordmetric/acceptors/tcp_acceptor.rb', line 49

def unbind
  @streaming = false
  close_connection?
end