Bill Kelly
2008-May-16 11:17 UTC
[Eventmachine-talk] Accessing variables from a server after
> From: Ceasless One > > I was thinking that if I could supply a block instead of a > module in my call to start_server, then I could just read > the @input variable directly. Is this at all possible? Another > thing is that the rest of the code is not running in EM, so > the normal hooks just aren''t viable.I usually use the block form of start_server to pass in a pointer to its ''owner''. class DCC_Chat < EventMachine::Connection def owner_init(owner) @owner = owner end def receive_data(dat) @inbuf << dat if some_interesting_state_reached @owner.notify(interesting_thing) end end end # i don''t know what ''self'' is where you''re doing EM::run... # so for the sake of example, i''ll just define a singleton # method... def self.notify(interesting_thing) @result = interesting_thing end EM::run { EventMachine::start_server("127.0.0.1",1667,DCC_Chat) do |conn| conn.owner_init(self) end } # if notify was called, @result should be set I''ve never used this owner_init technique for the purpose of passing back values from EM::run, but it should work... If all one needs is to return a value and has no other need to call methods on the ''owner'', then I suppose we could simplify it with a lambda: result_proc = lambda {|some_value| @result = some_value} EM::run { EventMachine::start_server("127.0.0.1",1667,DCC_Chat) do |conn| conn.set_result_proc(result_proc) end } # now @result will be set as long as the connection called back # the result_proc set by set_result_proc Another option might be to just ask the connection(s) after we''re finished. Like: conns = [] EM::run { EventMachine::start_server("127.0.0.1",1667,DCC_Chat) do |conn| conns << conn end } conns.each {|conn| p conn.result} Hope this helps, Bill
Roger Pack
2008-May-16 13:52 UTC
[Eventmachine-talk] Accessing variables from a server after
I don''t think it''s "defined" what EM::run returns.
You can set some
other variable and use that after it closes, though.
This does bring up an interesting topic.
What should happen if you do
EM::run {
EM::run {
# block 2 stuff
}
}
Should it do block 2 stuff, or raise?
I''d say raise, to keep things standard :) [and since you can just
easily define something like start_em_or_just_run { # block }
-R
On Fri, May 16, 2008 at 12:17 PM, Bill Kelly <billk at cts.com>
wrote:>
>
>> From: Ceasless One
>>
>> I was thinking that if I could supply a block instead of a
>> module in my call to start_server, then I could just read
>> the @input variable directly. Is this at all possible? Another
>> thing is that the rest of the code is not running in EM, so
>> the normal hooks just aren''t viable.
>
> I usually use the block form of start_server to pass in a pointer
> to its ''owner''.
>
> class DCC_Chat < EventMachine::Connection
>
> def owner_init(owner)
> @owner = owner
> end
>
> def receive_data(dat)
> @inbuf << dat
> if some_interesting_state_reached
> @owner.notify(interesting_thing)
> end
> end
>
> end
>
>
> # i don''t know what ''self'' is where
you''re doing EM::run... # so for the
> sake of example, i''ll just define a singleton # method...
>
> def self.notify(interesting_thing)
> @result = interesting_thing
> end
>
> EM::run {
> EventMachine::start_server("127.0.0.1",1667,DCC_Chat) do |conn|
> conn.owner_init(self)
> end
> }
>
> # if notify was called, @result should be set
>
> I''ve never used this owner_init technique for the purpose of
passing back
> values from EM::run, but it should work...
>
> If all one needs is to return a value and has no other need
> to call methods on the ''owner'', then I suppose we could
simplify it with a
> lambda:
>
>
> result_proc = lambda {|some_value| @result = some_value}
>
> EM::run {
> EventMachine::start_server("127.0.0.1",1667,DCC_Chat) do |conn|
> conn.set_result_proc(result_proc)
> end
> }
>
> # now @result will be set as long as the connection called back
> # the result_proc set by set_result_proc
>
>
> Another option might be to just ask the connection(s) after we''re
> finished. Like:
>
> conns = []
>
> EM::run {
> EventMachine::start_server("127.0.0.1",1667,DCC_Chat) do |conn|
> conns << conn
> end
> }
>
> conns.each {|conn| p conn.result}
>
>
>
> Hope this helps,
>
> Bill
>
>
> _______________________________________________
> Eventmachine-talk mailing list
> Eventmachine-talk at rubyforge.org
> http://rubyforge.org/mailman/listinfo/eventmachine-talk
>