Sha256: 1ac37c430949302064e960d6df0246b93a9079c84375f26b0b4319765c386b0a

Contents?: true

Size: 1.32 KB

Versions: 7

Compression:

Stored size: 1.32 KB

Contents

module RubyAMI
  class Stream < EventMachine::Connection
    class ConnectionStatus
      def eql?(other)
        other.is_a? self.class
      end

      alias :== :eql?
    end

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

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

    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

    [:started, :stopped, :ready].each do |state|
      define_method("#{state}?") { @state == state }
    end

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

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

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

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

    alias :error_received :message_received

    # Called by EM when the connection is closed
    # @private
    def unbind
      @state = :stopped
      @event_callback.call Disconnected.new
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ruby_ami-1.0.1 lib/ruby_ami/stream.rb
ruby_ami-1.0.0 lib/ruby_ami/stream.rb
ruby_ami-0.1.5 lib/ruby_ami/stream.rb
ruby_ami-0.1.4 lib/ruby_ami/stream.rb
ruby_ami-0.1.3 lib/ruby_ami/stream.rb
ruby_ami-0.1.2 lib/ruby_ami/stream.rb
ruby_ami-0.1.1 lib/ruby_ami/stream.rb