Class: Servolux::Piper

Inherits:
Object show all
Defined in:
lib/emissary/servolux.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Piper

Returns a new instance of Piper.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/emissary/servolux.rb', line 39

def initialize( *args )
  opts = args.last.is_a?(Hash) ? args.pop : {}
  mode = args.first || 'r'

  unless %w[r w rw].include? mode
    raise ArgumentError, "Unsupported mode #{mode.inspect}"
  end

  @timeout = opts.key?(:timeout) ? opts[:timeout] : nil
  socket_pair = Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM, 0)
  @child_pid = Kernel.fork

  if child?
    @socket = socket_pair[1]
    socket_pair[0].close

    case mode
    when 'r'; @socket.close_read
    when 'w'; @socket.close_write
    end
  else
    # prevent zombie processes - register disinterest
    # in return status of child process
    Process.detach @child_pid

    @socket = socket_pair[0]
    socket_pair[1].close

    case mode
    when 'r'; @socket.close_write
    when 'w'; @socket.close_read
    end

  end
end