Event Machine - callbacks from different connections

Discussion in 'Ruby' started by Tim Conner, Mar 3, 2008.

  1. Tim Conner

    Tim Conner Guest

    In the RDoc for EventMachine, the following statement is made:
    "You can also call send_data to write to a connection other than the one
    whose callback you are calling send_data from. This is done by recording
    the value of the connection in any callback function (the value self),
    in any variable visible to other callback invocations on the same or
    different connection objects. (Need an example to make that clear.)"

    If I have the following code, how can I make it so that the 'send_data'
    method for ServerOne is called when data is received for ServerTwo?
    i.e. I want a packet of data to be sent out on port 1700 when a packet
    is received on port 1800 (but i need the listener on port 1700 to stay
    listening)

    require 'rubygems'
    require 'eventmachine'

    module ServerOne
    def receive_data data
    sender_info = get_peername[2,6].unpack "nC4"
    port = sender_info[0]
    ip = sender_info[1..4].join(".").to_s
    puts "IP = #{ip}"
    puts "Port = #{port}"
    end
    end

    module ServerTwo
    def receive_data data
    sender_info = get_peername[2,6].unpack "nC4"
    port = sender_info[0]
    ip = sender_info[1..4].join(".").to_s
    puts "IP = #{ip}"
    puts "Port = #{port}"
    end
    end

    EventMachine::run {
    EventMachine::eek:pen_datagram_socket "192.168.0.2", 1700, ServerOne
    EventMachine::eek:pen_datagram_socket "192.168.0.2", 1800, ServerTwo
    }


    Many thanks
    --
    Posted via http://www.ruby-forum.com/.
     
    Tim Conner, Mar 3, 2008
    #1
    1. Advertising

  2. The idea is that there's a collection of the connection objects held
    outside of the servers and used where necessary. In your case,
    something like this would work:

    $one_conns = []
    $two_conns = []

    module ServerOne
    def receive_data data
    sender_info = get_peername[2,6].unpack "nC4"
    port = sender_info[0]
    ip = sender_info[1..4].join(".").to_s
    puts "IP = #{ip}"
    puts "Port = #{port}"

    # find your $two_conn and
    $two_conns.send_data ...
    end
    end

    module ServerTwo
    def receive_data data
    sender_info = get_peername[2,6].unpack "nC4"
    port = sender_info[0]
    ip = sender_info[1..4].join(".").to_s
    puts "IP = #{ip}"
    puts "Port = #{port}"

    #Find your one conn and
    $one_conns.send_data ...
    end
    end

    EventMachine::run {
    EventMachine::eek:pen_datagram_socket "192.168.0.2", 1700, ServerOne do |conn|
    $one_conns << conn
    end

    EventMachine::eek:pen_datagram_socket "192.168.0.2", 1800, ServerTwo do |conn|
    $two_conns << conn
    end
    }


    Jason

    On Mon, Mar 3, 2008 at 6:11 AM, Tim Conner <> wrote:
    > In the RDoc for EventMachine, the following statement is made:
    > "You can also call send_data to write to a connection other than the one
    > whose callback you are calling send_data from. This is done by recording
    > the value of the connection in any callback function (the value self),
    > in any variable visible to other callback invocations on the same or
    > different connection objects. (Need an example to make that clear.)"
    >
    > If I have the following code, how can I make it so that the 'send_data'
    > method for ServerOne is called when data is received for ServerTwo?
    > i.e. I want a packet of data to be sent out on port 1700 when a packet
    > is received on port 1800 (but i need the listener on port 1700 to stay
    > listening)
    >
    > require 'rubygems'
    > require 'eventmachine'
    >
    > module ServerOne
    > def receive_data data
    > sender_info = get_peername[2,6].unpack "nC4"
    > port = sender_info[0]
    > ip = sender_info[1..4].join(".").to_s
    > puts "IP = #{ip}"
    > puts "Port = #{port}"
    > end
    > end
    >
    > module ServerTwo
    > def receive_data data
    > sender_info = get_peername[2,6].unpack "nC4"
    > port = sender_info[0]
    > ip = sender_info[1..4].join(".").to_s
    > puts "IP = #{ip}"
    > puts "Port = #{port}"
    > end
    > end
    >
    > EventMachine::run {
    > EventMachine::eek:pen_datagram_socket "192.168.0.2", 1700, ServerOne
    > EventMachine::eek:pen_datagram_socket "192.168.0.2", 1800, ServerTwo
    > }
    >
    >
    > Many thanks
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Jason Roelofs, Mar 3, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. UJ
    Replies:
    2
    Views:
    429
  2. Alfonso Morra

    Callbacks accross different threads

    Alfonso Morra, Sep 19, 2005, in forum: C++
    Replies:
    10
    Views:
    509
    red floyd
    Sep 20, 2005
  3. jobs
    Replies:
    2
    Views:
    903
  4. Frank H.

    Concurrent webservice connections to remote machine

    Frank H., Apr 26, 2009, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    738
    Frank H.
    Apr 26, 2009
  5. HmJ
    Replies:
    0
    Views:
    246
Loading...

Share This Page