Hey all, I was wondering if anyone had any input on sharing connections in an app that is both a client and a server. This is what I came up with of the top of my head, but I don''t like it: class MyClient < EventMachine::Connection def post_init @clients = {} end def add_client(conn) @clients[peer()] = conn end def peer sockaddr = Socket.unpack_sockaddr_in(get_peername()) sockaddr[1] + ":" + sockaddr[0].to_s end end module MyServer attr_writer :client_conn def post_init @client_conn = nil end def receive_data(msg) @client_conn.send_data(msg) end end EventMachine::run do conn = nil EventMachine::connect(''127.0.0.1'', PORT, MyClient) { |c| conn = c } EventMachine::start_server(''127.0.0.1'', PORT, MyServer) do |s| s.client_conn = conn conn.add_client(s) end end Something to that effect anyway. Anybody have a better way? Thanks, Marc
On 1/17/07, Marc Soda <marcantoniosr at gmail.com> wrote:> > Hey all, > > I was wondering if anyone had any input on sharing connections in an > app that is both a client and a server. This is what I came up with > of the top of my head, but I don''t like it: > > class MyClient < EventMachine::Connection > def post_init > @clients = {} > end > > def add_client(conn) > @clients[peer()] = conn > end > > def peer > sockaddr = Socket.unpack_sockaddr_in(get_peername()) > sockaddr[1] + ":" + sockaddr[0].to_s > end > end > > module MyServer > attr_writer :client_conn > def post_init > @client_conn = nil > end > > def receive_data(msg) > @client_conn.send_data(msg) > end > end > > > EventMachine::run do > conn = nil > EventMachine::connect(''127.0.0.1'', PORT, MyClient) { |c| conn = c } > > EventMachine::start_server(''127.0.0.1'', PORT, MyServer) do |s| > s.client_conn = conn > conn.add_client(s) > end > end > > Something to that effect anyway. Anybody have a better way?Did this code actually do what you wanted it to? Your connect call (in the EM.run block) happens before you start the server running. I''d be surprised if it connects. Can you tell us a bit more about what you want to accomplish? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070118/85b6ab37/attachment.html
On 1/18/07, Francis Cianfrocca <garbagecat10 at gmail.com> wrote:> On 1/17/07, Marc Soda <marcantoniosr at gmail.com> wrote: > > > Hey all, > > > > I was wondering if anyone had any input on sharing connections in an > > app that is both a client and a server. This is what I came up with > > of the top of my head, but I don''t like it: > > > > class MyClient < EventMachine::Connection > > def post_init > > @clients = {} > > end > > > > def add_client(conn) > > @clients[peer()] = conn > > end > > > > def peer > > sockaddr = Socket.unpack_sockaddr_in(get_peername()) > > sockaddr[1] + ":" + sockaddr[0].to_s > > end > > end > > > > module MyServer > > attr_writer :client_conn > > def post_init > > @client_conn = nil > > end > > > > def receive_data(msg) > > @client_conn.send_data(msg) > > end > > end > > > > > > EventMachine::run do > > conn = nil > > EventMachine::connect(''127.0.0.1'', PORT, MyClient) { |c| conn = c } > > > > EventMachine::start_server(''127.0.0.1'', PORT, MyServer) do |s| > > s.client_conn = conn > > conn.add_client(s) > > end > > end > > > > Something to that effect anyway. Anybody have a better way? > > > > Did this code actually do what you wanted it to? Your connect call (in the > EM.run block) happens before you start the server running. I''d be surprised > if it connects. > > Can you tell us a bit more about what you want to accomplish? > > > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk > >Sorry I should have been more clear. The connect is to a separate server, I''m just testing it all on one box. It would be more like: EventMachine::run do conn = nil EventMachine::connect(''another.server.com'', 7777, MyClient) { |c| conn = c } EventMachine::start_server(''0.0.0.0'', 7777, MyServer) do |s| s.client_conn = conn conn.add_client(s) end end I''m not trying to connect to the same process, if that''s what it seemed. I want aggregate messages from multiple clients at this point and send them on to the another server. Marc
> I''m not trying to connect to the same process, if that''s what it > seemed. I want aggregate messages from multiple clients at this point > and send them on to the another server.I''m obviously missing something but I don''t see why you need all of the logic in the client class. Unless you have to parse the response from the remote server (that your client connects to) and send pieces of it to each of your clients. Take a look at the Deferrable class, by the way. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070118/d6884b63/attachment.html
> I''m obviously missing something but I don''t see why you need all of the > logic in the client class. Unless you have to parse the response from the > remote server (that your client connects to) and send pieces of it to each > of your clients.The way I envisioned setting this up is having multiple protocols handled by multiple processes. The protocol handlers will normalize the data sent to it from clients and send it back to a central server for processing. The server, in turn, will process the messages and then send the replies back through the protocol handlers who will pass them on to the clients. These processes will most likely be on separate boxes but could run on one. The part I''m working on now is the protocol handlers. I need them to receive messages from clients and connect to a server. I''ve tried to illustrate this in ASCII art below, hopefully the formating will hold up. PH are protocol handlers and A-E are clients. Server / | \ / | \ / | \ PH1 PH2 PH3 / | | | \ / | | | \ / | | | \ A B C D E Thanks!> > Take a look at the Deferrable class, by the way. > > > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk > >