Sha256: f33683d6cb42df95556ac09dbe6984c44bd0a0c6248df8232efe279989309f47

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

module IRuby
  class OutStream
    #A file like object that publishes the stream to a 0MQ PUB socket.

    attr_writer :parent

    def initialize session, pub_socket, name, max_buffer=200
      @session = session
      @pub_socket = pub_socket
      @name = name
      @buffer = []
      @buffer_len = 0
      @max_buffer = max_buffer
      @parent = {}
    end

    def close
      @pub_socket = nil
    end

    def flush
      # STDERR.puts("flushing, parent to follow")
      # STDERR.puts @parent.inspect
      if @pub_socket.nil?
        raise 'I/O operation on closed file'
      else
        if @buffer
          data = @buffer.join('')
          content = { name: @name, data: data }
          if ! @session
            return
          end
          # msg = @session.msg('stream', content, @parent) if @session
          # FIXME: Wha?
          # STDERR.puts msg.to_json
          #@pub_socket.send(msg.to_json)
          @session.send(@pub_socket, 'stream', content, @parent)
          @buffer_len = 0
          @buffer = []
          nil
        end
      end
    end

    def isatty
      false
    end
    alias tty? isatty

    def next
      raise IOError, 'not opened for reading'
    end

    def read(*args)
      raise IOError, 'not opened for reading'
    end
    alias readline read

    def write(s)
      if @pub_socket.nil?
        raise 'I/O operation on closed file'
      else
        s = s.to_s
        @buffer << s
        @buffer_len += s.length
        _maybe_send
      end
    end

    def puts(s)
      write "#{s}\n"
    end

    def _maybe_send
      #if self._buffer[-1].include?('\n')
        flush
      #end
      #if @bufferlen > @max_buffer
        #flush
      #end
    end

    def writelines sequence
      if @pub_socket.nil?
        raise 'I/O operation on closed file'
      else
        sequence.each do |s|
          write(s)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
iruby-0.0.1 lib/iruby/out_stream.rb