Class: IRuby::OutStream

Inherits:
Object
  • Object
show all
Defined in:
lib/iruby/out_stream.rb

Instance Method Summary collapse

Constructor Details

#initialize(session, pub_socket, name, max_buffer = 200) ⇒ OutStream

A file like object that publishes the stream to a 0MQ PUB socket.



5
6
7
8
9
10
11
12
13
# File 'lib/iruby/out_stream.rb', line 5

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_header = {}
end

Instance Method Details

#_maybe_sendObject



76
77
78
79
80
81
82
83
# File 'lib/iruby/out_stream.rb', line 76

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

#closeObject



20
21
22
# File 'lib/iruby/out_stream.rb', line 20

def close
  @pub_socket = nil
end

#flushObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/iruby/out_stream.rb', line 24

def flush
  # STDERR.puts("flushing, parent to follow")
  # STDERR.puts @parent_header.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_header) if @session
      # FIXME: Wha?
      # STDERR.puts msg.to_json
      #@pub_socket.send(msg.to_json)
      @session.send(@pub_socket, 'stream', content, @parent_header)
      @_buffer_len = 0
      @_buffer = []
      nil
    end
  end
end

#isattrObject



48
49
50
# File 'lib/iruby/out_stream.rb', line 48

def isattr
  return false
end

#nextObject



52
53
54
# File 'lib/iruby/out_stream.rb', line 52

def next
  raise 'Read not supported on a write only stream.'
end

#puts(str) ⇒ Object



72
73
74
# File 'lib/iruby/out_stream.rb', line 72

def puts str
  write "#{str}\n"
end

#read(size = 0) ⇒ Object Also known as: readline



56
57
58
# File 'lib/iruby/out_stream.rb', line 56

def read size=0
  raise 'Read not supported on a write only stream.'
end

#set_parent(parent) ⇒ Object



15
16
17
18
# File 'lib/iruby/out_stream.rb', line 15

def set_parent parent
  header = Message.extract_header(parent)
  @parent_header = header
end

#write(s) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/iruby/out_stream.rb', line 61

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

#writelines(sequence) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/iruby/out_stream.rb', line 85

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