Class: Dizby::Spawned

Inherits:
Object
  • Object
show all
Defined in:
lib/dizby/tunnel/spawned.rb

Class Method Summary collapse

Class Method Details

.dynamic(uri, config, &block)


14
15
16
17
18
# File 'lib/dizby/tunnel/spawned.rb', line 14

def self.dynamic(uri, config, &block)
  handle_spawned(uri, config, block) do |service|
    $stdout.puts "Running on port #{service.server.port}."
  end
end

.handle_spawned(uri, config, origin)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dizby/tunnel/spawned.rb', line 20

def self.handle_spawned(uri, config, origin)
  service = nil

  obj = obtain_object(&origin)

  obj.define_singleton_method :__dizby_exit__ do
    service.close if service
  end

  service = Service.new(uri, obj, Marshal.load(config))
  yield service if block_given?
ensure
  service.wait if service
end

.obtain_object(&origin)


35
36
37
38
39
40
41
42
43
# File 'lib/dizby/tunnel/spawned.rb', line 35

def self.obtain_object(&origin)
  barriers = [$stdout, $stdin, $stderr].map { |io| IOBarrier.new(io) }

  barriers.each(&:block)

  origin.call
ensure
  barriers.each(&:allow)
end

.static(uri, config, &block)


10
11
12
# File 'lib/dizby/tunnel/spawned.rb', line 10

def self.static(uri, config, &block)
  handle_spawned(uri, config, block)
end