Class: Hoosegow::Protocol::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/hoosegow/protocol.rb

Overview

Hoosegow, on the app side of the proxy.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Proxy

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



15
16
17
18
19
# 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

Instance Attribute Details

#return_valueObject (readonly)

The return value



27
28
29
# File 'lib/hoosegow/protocol.rb', line 27

def return_value
  @return_value
end

Instance Method Details

#encode_send(method_name, args) ⇒ Object

Encodes a “send” method call for an inmate.



22
23
24
# File 'lib/hoosegow/protocol.rb', line 22

def encode_send(method_name, args)
  MessagePack.pack([method_name, args])
end

#raise_args(remote_error) ⇒ Object



51
52
53
54
55
56
57
58
# 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) ⇒ Object

Decodes a message from an inmate via docker.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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