Class: RubyAMI::Stream

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/ruby_ami/stream.rb

Defined Under Namespace

Classes: ConnectionStatus

Constant Summary

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Stream) initialize(event_callback)

Returns a new instance of Stream



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

def initialize(event_callback)
  super()
  @event_callback = event_callback
  @logger = Logger.new($stdout)
  @logger.level = Logger::FATAL
  @logger.debug "Starting up..."
  @lexer = Lexer.new self
end

Class Method Details

+ (Object) start(host, port, event_callback)



14
15
16
# File 'lib/ruby_ami/stream.rb', line 14

def self.start(host, port, event_callback)
  EM.connect host, port, self, event_callback
end

Instance Method Details

- (Object) message_received(message) Also known as: error_received



46
47
48
49
# File 'lib/ruby_ami/stream.rb', line 46

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

- (Object) post_init



31
32
33
34
# File 'lib/ruby_ami/stream.rb', line 31

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

- (Object) receive_data(data)



41
42
43
44
# File 'lib/ruby_ami/stream.rb', line 41

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

- (Object) send_action(action)



36
37
38
39
# File 'lib/ruby_ami/stream.rb', line 36

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

- (Object) unbind

Called by EM when the connection is closed



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

def unbind
  @state = :stopped
  @event_callback.call Disconnected.new
end