Class: Microsandbox::AgentStream

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/microsandbox/agent.rb

Overview

Note:

Single-pass, forward-only, single-consumer. each drains a one-shot native channel — not rewindable, iterate once from a single thread; a second pass or a post-drain combinator yields nothing.

An open raw agent stream, from Microsandbox::AgentClient#stream. Enumerable: iterate to consume AgentFrames until the stream ends (the terminal frame is delivered, then iteration stops). Mirrors the official SDKs' AgentStream.

Examples:

stream = client.stream(0, request_body)
stream.each { |frame| handle(frame) }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(native, id, handle) ⇒ AgentStream

Returns a new instance of AgentStream.



51
52
53
54
55
# File 'lib/microsandbox/agent.rb', line 51

def initialize(native, id, handle)
  @native = native
  @id = id
  @handle = handle
end

Instance Attribute Details

#idInteger (readonly)

Returns the protocol correlation id (pass to Microsandbox::AgentClient#send_frame).

Returns:



49
50
51
# File 'lib/microsandbox/agent.rb', line 49

def id
  @id
end

Instance Method Details

#closenil

Close the stream and release its handle. Idempotent.

Returns:

  • (nil)


77
78
79
80
# File 'lib/microsandbox/agent.rb', line 77

def close
  @native.stream_close(@handle)
  nil
end

#each {|frame| ... } ⇒ self, Enumerator

Yield Parameters:

Returns:

  • (self, Enumerator)


66
67
68
69
70
71
72
73
# File 'lib/microsandbox/agent.rb', line 66

def each
  return enum_for(:each) unless block_given?

  while (frame = recv)
    yield frame
  end
  self
end

#recvAgentFrame?

Pull the next frame, or nil at end-of-stream.

Returns:



59
60
61
62
# File 'lib/microsandbox/agent.rb', line 59

def recv
  frame = @native.stream_next(@handle)
  frame && AgentFrame.new(frame)
end