Created
April 24, 2009 16:26
-
-
Save davebryson/101187 to your computer and use it in GitHub Desktop.
Example of talking to Erlang's epmd from Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment