class Hoosegow::Protocol::Proxy

Hoosegow, on the app side of the proxy.

Sends data to and from an inmate, via a Docker container running `bin/hoosegow`.

Attributes

return_value[R]

The return value

Public Class Methods

new(options) click to toggle source

Options:

  • (optional) :yield - a block to call when the inmate yields

  • (optional) :stdout - an IO for writing STDOUT from the inmate

  • (optional) :stderr - an IO for writing STDERR from the inmate

# File lib/hoosegow/protocol.rb, line 15
def initialize(options)
  @yield_block = options.fetch(:yield, nil)
  @stdout      = options.fetch(:stdout, $stdout)
  @stderr      = options.fetch(:stderr, $stderr)
end

Public Instance Methods

encode_send(method_name, args) click to toggle source

Encodes a “send” method call for an inmate.

# File lib/hoosegow/protocol.rb, line 22
def encode_send(method_name, args)
  MessagePack.pack([method_name, args])
end
raise_args(remote_error) click to toggle source
# File lib/hoosegow/protocol.rb, line 51
def raise_args(remote_error)
  exception_class = Hoosegow::InmateRuntimeError
  exception_message = "#{remote_error['class']}: #{remote_error['message']}"
  if remote_backtrace = remote_error['backtrace']
    exception_message << ("\n" + Array(remote_backtrace).join("\n"))
  end
  [exception_class, exception_message]
end
receive(type, msg) click to toggle source

Decodes a message from an inmate via docker.

# File lib/hoosegow/protocol.rb, line 30
def receive(type, msg)
  if type == :stdout
    @unpacker ||= MessagePack::Unpacker.new
    @unpacker.feed_each(msg) do |decoded|
      inmate_type, inmate_value = decoded
      case inmate_type.to_s
      when 'yield'
        @yield_block.call(*inmate_value) if @yield_block
      when 'return'
        @return_value = inmate_value
      when 'raise'
        raise(*raise_args(inmate_value))
      when 'stdout'
        @stdout.write(inmate_value)
      end
    end
  elsif type == :stderr
    @stderr.write(msg)
  end
end