Class: Hoosegow::Protocol::Proxy
- Inherits:
-
Object
- Object
- Hoosegow::Protocol::Proxy
- 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
-
#return_value ⇒ Object
readonly
The return value.
Instance Method Summary collapse
-
#encode_send(method_name, args) ⇒ Object
Encodes a “send” method call for an inmate.
-
#initialize(options) ⇒ Proxy
constructor
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.
- #raise_args(remote_error) ⇒ Object
-
#receive(type, msg) ⇒ Object
Decodes a message from an inmate via docker.
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() @yield_block = .fetch(:yield, nil) @stdout = .fetch(:stdout, $stdout) @stderr = .fetch(:stderr, $stderr) end |
Instance Attribute Details
#return_value ⇒ Object (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 = "#{remote_error['class']}: #{remote_error['message']}" if remote_backtrace = remote_error['backtrace'] << ("\n" + Array(remote_backtrace).join("\n")) end [exception_class, ] 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 |