Sha256: b8858632bda3b0b097d64f245c9f222ee3b9f9b1061e8b0ee42f7f17e6cec039

Contents?: true

Size: 987 Bytes

Versions: 5

Compression:

Stored size: 987 Bytes

Contents

module Erlectricity
  class Port
    attr_reader :input, :output
    attr_reader :skipped
    attr_reader :queue
  
    def initialize(input=STDIN, output=STDOUT)
      @input = input
      @output = output
    
      input.sync = true
      output.sync = true
    
      @encoder = Erlectricity::Encoder.new(nil)
      @skipped = []
      @queue = []
    end
  
    def receive
      queue.empty? ? read_from_input : queue.shift
    end
  
    def send(term)
      @encoder.out = StringIO.new('', 'w')
      @encoder.write_any(term)
      data = @encoder.out.string
      output.write([data.length].pack("N"))
      output.write data
    end
  
    def restore_skipped
      @queue = self.skipped + self.queue
    end
  
    private
    def read_from_input
      raw = input.read(4)
      return nil unless raw
    
      packet_length = raw.unpack('N').first
      data = input.read(packet_length)
      result = Erlectricity::Decoder.read_any_from(data)
      result
    end
  end
end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
mojombo-erlectricity-0.2.1 lib/erlectricity/port.rb
tmm1-erlectricity-0.2.1 lib/erlectricity/port.rb
erlectricity-0.1.0 lib/erlectricity/port.rb
erlectricity-0.2.0 lib/erlectricity/port.rb
erlectricity-0.2.1 lib/erlectricity/port.rb