Class: RubyAMI::Stream

Inherits:
Object show all
Includes:
Celluloid::IO
Defined in:
lib/ruby_ami/stream.rb

Defined Under Namespace

Classes: ConnectionStatus

Constant Summary collapse

Connected =
Class.new ConnectionStatus
Disconnected =
Class.new ConnectionStatus

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, event_callback, logger = Logger, timeout = 0) ⇒ Stream

Returns a new instance of Stream.



21
22
23
24
25
26
# File 'lib/ruby_ami/stream.rb', line 21

def initialize(host, port, event_callback, logger = Logger, timeout = 0)
  super()
  @host, @port, @event_callback, @logger, @timeout = host, port, event_callback, logger, timeout
  logger.debug "Starting up..."
  @lexer = Lexer.new self
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



17
18
19
# File 'lib/ruby_ami/stream.rb', line 17

def logger
  @logger
end

Instance Method Details

#message_received(message) ⇒ Object Also known as: error_received



68
69
70
71
# File 'lib/ruby_ami/stream.rb', line 68

def message_received(message)
  logger.trace "[RECV] #{message.inspect}"
  @event_callback.call message
end

#post_initObject



49
50
51
52
# File 'lib/ruby_ami/stream.rb', line 49

def post_init
  @state = :started
  @event_callback.call Connected.new
end

#receive_data(data) ⇒ Object



63
64
65
66
# File 'lib/ruby_ami/stream.rb', line 63

def receive_data(data)
  logger.trace "[RECV] #{data}"
  @lexer << data
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_ami/stream.rb', line 32

def run
  Timeout::timeout(@timeout) do
    @socket = TCPSocket.from_ruby_socket ::TCPSocket.new(@host, @port)
  end
  post_init
  loop { receive_data @socket.readpartial(4096) }
rescue Errno::ECONNREFUSED, SocketError => e
  logger.error "Connection failed due to #{e.class}. Check your config and the server."
  current_actor.terminate!
rescue EOFError
  logger.info "Client socket closed!"
  current_actor.terminate!
rescue Timeout::Error
  logger.error "Timeout exceeded while trying to connect."
  current_actor.terminate!
end

#send_action(action) ⇒ Object



58
59
60
61
# File 'lib/ruby_ami/stream.rb', line 58

def send_action(action)
  logger.trace "[SEND] #{action.to_s}"
  send_data action.to_s
end

#send_data(data) ⇒ Object



54
55
56
# File 'lib/ruby_ami/stream.rb', line 54

def send_data(data)
  @socket.write data
end

#syntax_error_encountered(ignored_chunk) ⇒ Object



73
74
75
# File 'lib/ruby_ami/stream.rb', line 73

def syntax_error_encountered(ignored_chunk)
  logger.error "Encountered a syntax error. Ignoring chunk: #{ignored_chunk.inspect}"
end