Sha256: 1752c4a965127071c6df77bfd7d4bdbadde6c8a28c949ff0893f71990b8231e5

Contents?: true

Size: 938 Bytes

Versions: 8

Compression:

Stored size: 938 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)
      Erlectricity::Decoder.decode(data)
    end
  end
end

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
mojombo-erlectricity-1.0.0 lib/erlectricity/port.rb
mojombo-erlectricity-1.0.1 lib/erlectricity/port.rb
mojombo-erlectricity-1.0.2 lib/erlectricity/port.rb
erlectricity-funbox-1.1.2 lib/erlectricity/port.rb
erlectricity-1.1.1 lib/erlectricity/port.rb
erlectricity-1.1.0 lib/erlectricity/port.rb
erlectricity-1.0.1 lib/erlectricity/port.rb
erlectricity-1.0.0 lib/erlectricity/port.rb