EventMachine: dynamic "client" connections

Thanks for reading this and thanks in advance if you provide any help!

I’m new to Ruby. Trying to write a distributed file system consisting
of a Metadata Server (MS), Object Servers (OS), and clients. I’m able
to bring up a MS and an OS that registers with the MS. What I’m having
trouble with is sending a request from a client to the MS, which should
then send that request to the appropriate OS in order to serve the
client. The requests are simple download, upload, and delete commands.
This is what I would like to have happen (for example, an upload):

  1. client sends the upload command to the MS (?)
  2. MS finds a an OS with available space to upload to (I have this
    working)
  3. MS sends the client’s request and IP to the OS (?)
  4. OS contacts the client (?)
  5. client uploads file to OS (I figured out how to stream files)

So I need a way to dynamically create a connection to a variable IP and
send a variable message across it in order to do numbers 1, 3 & 4. I’ve
tried working with EventMachine in classes and modules, but cannot
figure out how to pass variables to it dynamically.

Here is an example of one of my many attempts at this:

######################################################

class SendCommand < EM::Connection
attr_accessor :cmd, :file

def post_init
puts “Sending: #{@cmd} #{@file}”
send_data("#{@cmd} #{@file}")
end
end

class ConnectToMS

def initialize(cmd, file)
@cmd = cmd
@file = file
end

def connect_to_ms
host = ‘127.0.0.1’
port = 6666
EM.run do
EM.connect(host, port, SendCommand) do |con|
con.cmd = @cmd
con.file = @file
end
puts “Sending command: #{@cmd} File: #{@file}”
end
end

end

######################################################

Then I would call it like this:

c = ConnectToMS.new(“UPLOAD”,“file_name”)
c.connect_to_ms

Neat project, sounds kinda like Tahoe (which is written in Python)

http://allmydata.org/trac/tahoe

If I’m reading your problem correctly, I actually ran into this myself
way
back in the day and contributed the patch to fix it! :slight_smile:

EM.connect(host, port, SendCommand, arg1, arg2)

[etc for as many args as you want]

These arguments will get passed along to initialize

Wow. I knew it was going to be a simple solution. Thank you so much,
Tony!