# Example of connecting to Epmd to get the port number # of an Erlang node require 'rubygems' require 'eventmachine' class EpmdConnection < EM::Connection include EM::Deferrable attr_accessor :nodename def self.lookup_node(nodename) EM.connect("127.0.0.1",4369,EpmdConnection) do |conn| conn.nodename = nodename end end def connection_completed send_data lookup_port end def receive_data(data) parse_response(data) end def unbind puts "Done" end def lookup_port out = StringIO.new('', 'w') # Create the header with length: 2 out.write([@nodename.size + 1].pack('n')) # Next the request # tag. Length: 1 out.write([122].pack("C")) # nodename out.write(nodename) out.string end # If we get a good result we only return # the port (not reading all the information def parse_response(input) i = StringIO.new(input) code = i.read(1).unpack('C').first result = i.read(1).unpack('C').first if result == 0 # good response read the port port = i.read(2).unpack('n').first set_deferred_success "port for #{@nodename} is #{port}" else set_deferred_failure "bad result code" end end end EM.run do # Make sure you start an Erlang node: 'erl -sname hello' epmd = EpmdConnection.lookup_node("hello") epmd.callback do |resp| puts "Got: #{resp}" EM.stop end epmd.errback do |err| puts "Error: #{err}" EM.stop end end