Class: Microsandbox::AgentStream

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

Overview

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.



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

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:



45
46
47
# File 'lib/microsandbox/agent.rb', line 45

def id
  @id
end

Instance Method Details

#closenil

Close the stream and release its handle. Idempotent.

Returns:

  • (nil)


73
74
75
76
# File 'lib/microsandbox/agent.rb', line 73

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

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

Yield Parameters:

Returns:

  • (self, Enumerator)


62
63
64
65
66
67
68
69
# File 'lib/microsandbox/agent.rb', line 62

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:



55
56
57
58
# File 'lib/microsandbox/agent.rb', line 55

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