Class: Docker::API::Stream::Demultiplexer

Inherits:
Object
  • Object
show all
Defined in:
lib/docker/api/stream.rb

Overview

Splits Docker's multiplexed stdout/stderr framing back into named streams.

Used whenever a container was created without a TTY. With a TTY the daemon sends unframed bytes instead, which is what Raw is for.

Examples:

demux = Stream::Demultiplexer.new { |name, chunk| $stdout << chunk if name == :stdout }
demux << first_chunk << second_chunk

Instance Method Summary collapse

Constructor Details

#initialize {|name, chunk| ... } ⇒ Demultiplexer

Returns a new instance of Demultiplexer.

Yield Parameters:

  • name (Symbol)

    one of :stdin, :stdout, :stderr

  • chunk (String)

    the payload, in UTF-8

Raises:

  • (ArgumentError)


36
37
38
39
40
41
# File 'lib/docker/api/stream.rb', line 36

def initialize(&block)
  raise ArgumentError, "Demultiplexer needs a block to yield frames to" unless block

  @block = block
  @buffer = +"".b
end

Instance Method Details

#<<(chunk) ⇒ self

Feed bytes in. Complete frames are yielded; a partial frame is held until the rest of it arrives.

Parameters:

  • chunk (String)

    any number of bytes, at any boundary

Returns:

  • (self)


48
49
50
51
52
# File 'lib/docker/api/stream.rb', line 48

def <<(chunk)
  @buffer << chunk.to_s.b
  emit_frames
  self
end

#pending?Boolean

Returns whether bytes are being held back for a partial frame.

Returns:

  • (Boolean)

    whether bytes are being held back for a partial frame



55
56
57
# File 'lib/docker/api/stream.rb', line 55

def pending?
  !@buffer.empty?
end