# EventMachine: dynamic "client" connections

**URL:** <https://www.ruby-forum.com/t/eventmachine-dynamic-client-connections/179119>\
**Category:** Ruby\
**Created:** [November 18, 2009, 1:06am UTC](https://www.ruby-forum.com/t/eventmachine-dynamic-client-connections/179119 "2009-11-18T01:06:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [November 18, 2009, 1:06am UTC](https://www.ruby-forum.com/t/eventmachine-dynamic-client-connections/179119/1 "2009-11-18T01:06:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [November 18, 2009, 1:15am UTC](https://www.ruby-forum.com/t/eventmachine-dynamic-client-connections/179119/2 "2009-11-18T01:15:54Z")

</div>

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

[http://allmydata.org/trac/tahoe](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! 🙂

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

[etc for as many args as you want]

These arguments will get passed along to initialize

---

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [November 18, 2009, 2:49am UTC](https://www.ruby-forum.com/t/eventmachine-dynamic-client-connections/179119/3 "2009-11-18T02:49:32Z")

</div>

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