Module: MultiBinder

Defined in:
lib/multibinder.rb,
lib/multibinder/version.rb

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.bind(address, port, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/multibinder.rb', line 5

def self.bind(address, port, options={})
  abort 'MULTIBINDER_SOCK environment variable must be set' if !ENV['MULTIBINDER_SOCK']

  binder = UNIXSocket.open(ENV['MULTIBINDER_SOCK'])

  # make the request
  binder.sendmsg JSON.dump({
    :jsonrpc => '2.0',
    :method => 'bind',
    :params => [{
      :address => address,
      :port => port,
    }.merge(options)]
  }, 0, nil)

  # get the response
  msg, _, _, ctl = binder.recvmsg(:scm_rights=>true)
  response = JSON.parse(msg)
  if response['error']
    raise response['error']['message']
  end

  binder.close

  socket = ctl.unix_rights[0]
  socket.fcntl(Fcntl::F_SETFD, socket.fcntl(Fcntl::F_GETFD) & (-Fcntl::FD_CLOEXEC-1))
  socket
end