> EM.add_periodic_timer(5) do > cnt +=1; > if cnt <= 3 > emc.send_data "Message: #{cnt}" > else > emc.send_data "quit" > end > end > }This timer just keeps going and going. Add EM::stop in there :)
Hi Roger - On Jan 13, 2008 11:19 AM, Roger Pack <rogerpack2005 at gmail.com> wrote:> > EM.add_periodic_timer(5) do > > cnt +=1; > > if cnt <= 3 > > emc.send_data "Message: #{cnt}" > > else > > emc.send_data "quit" > > end > > end > > } > > This timer just keeps going and going. Add EM::stop in there :)Yes that will work, but really I wanted to show: - The client tells the sever to shutdown. - In response, the server tells the client to shutdown, then the server shuts down. - The client shuts down when told to by the server. Placing EM.stop where you suggest will shut the client down, but that might/will happen before it receives any acknowledgment data from the server. Or have I misunderstood? Cheers Mark> _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >
I''m not sure what the question is, here. If you use EM::PeriodicTimer (which is newer than #add_periodic_timer), there is a cancel method, which you can invoke inside or outside of the timer block. t = EM::PeriodicTimer.new(5) { # Do this every five seconds } t.cancel On Jan 12, 2008 7:19 PM, Roger Pack <rogerpack2005 at gmail.com> wrote:> > EM.add_periodic_timer(5) do > > cnt +=1; > > if cnt <= 3 > > emc.send_data "Message: #{cnt}" > > else > > emc.send_data "quit" > > end > > end > > } > > This timer just keeps going and going. Add EM::stop in there :) > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080113/c9674373/attachment.html
Hi, On Jan 14, 2008 12:43 AM, Francis Cianfrocca <garbagecat10 at gmail.com> wrote:> I''m not sure what the question is, here. If you use EM::PeriodicTimer (whichI''ll try this suggestion when I''m back at my desk - thanks for the tip. The question was: Why doesn''t the client respond to the quit instruction issued by the server in the following example - I''m sure I''ve done something dumb :) 1) Start an instance of irb in a new shell/dos command prompt #### Start server code #### $stderr.sync = $stdout.sync = true require ''rubygems'' require ''eventmachine'' module EchoServer def receive_data data puts "Server received: #{data}" send_data ">>>you sent: #{data}" puts "Server sent: >>>you sent: #{data}" (puts "CLOSING"; send_data "quit"; close_connection(true); EM.stop) if data =~ /quit/i end end EM::run{ EM::start_server "127.0.0.1", 8100, EchoServer } #### End server code #### 2) Start another instance of irb in a new shell/dos command prompt #### Start client code #### $stderr.sync = $stdout.sync = true require ''rubygems'' require ''eventmachine'' class EchoClient < EM::Connection def receive_data data puts "Client Received: #{data}" (puts "CLOSING"; close_connection(false); EM.stop) if data =~ /quit/i end end cnt=0 EM::run { emc = EM::connect "127.0.0.1", 8100, EchoClient EM.add_periodic_timer(5) do cnt +=1; if cnt <= 3 emc.send_data "Message: #{cnt}" else emc.send_data "quit" end end } #### End client code #### In the server shell/command window you should see: Server received: Message: 1 Server sent: >>>you sent: Message: 1 Server received: Message: 2 Server sent: >>>you sent: Message: 2 Server received: Message: 3 Server sent: >>>you sent: Message: 3 Server received: quit Server sent: >>>you sent: quit CLOSING => nil In the client shell/command window you should see: Client Received: >>>you sent: Message: 1 Client Received: >>>you sent: Message: 2 Client Received: >>>you sent: Message: 3> is newer than #add_periodic_timer), there is a cancel method, which you can > invoke inside or outside of the timer block. > > t = EM::PeriodicTimer.new (5) { > # Do this every five seconds > } > t.cancel > > > > > > On Jan 12, 2008 7:19 PM, Roger Pack <rogerpack2005 at gmail.com> wrote: > > > EM.add_periodic_timer(5) do > > > cnt +=1; > > > if cnt <= 3 > > > emc.send_data "Message: #{cnt}" > > > else > > > emc.send_data "quit" > > > end > > > end > > > } > > > > This timer just keeps going and going. Add EM::stop in there :) > > _______________________________________________ > > Eventmachine-talk mailing list > > Eventmachine-talk at rubyforge.org > > http://rubyforge.org/mailman/listinfo/eventmachine-talk > > > > > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >
Mark, did you ever get this code to work? On Jan 13, 2008 6:15 PM, Mark Van De Vyver <mvyver at gmail.com> wrote:> Hi, > > On Jan 14, 2008 12:43 AM, Francis Cianfrocca <garbagecat10 at gmail.com> > wrote: > > I''m not sure what the question is, here. If you use EM::PeriodicTimer > (which > > I''ll try this suggestion when I''m back at my desk - thanks for the tip. > The question was: Why doesn''t the client respond to the quit > instruction issued by the server in the following example - I''m sure > I''ve done something dumb :) > > 1) Start an instance of irb in a new shell/dos command prompt > #### Start server code #### > $stderr.sync = $stdout.sync = true > require ''rubygems'' > require ''eventmachine'' > > module EchoServer > def receive_data data > puts "Server received: #{data}" > send_data ">>>you sent: #{data}" > puts "Server sent: >>>you sent: #{data}" > (puts "CLOSING"; send_data "quit"; close_connection(true); > EM.stop) if data =~ /quit/i > end > end > > EM::run{ > EM::start_server "127.0.0.1", 8100, EchoServer > } > #### End server code #### > > 2) Start another instance of irb in a new shell/dos command prompt > #### Start client code #### > $stderr.sync = $stdout.sync = true > require ''rubygems'' > require ''eventmachine'' > > class EchoClient < EM::Connection > def receive_data data > puts "Client Received: #{data}" > (puts "CLOSING"; close_connection(false); EM.stop) if data =~ /quit/i > end > end > cnt=0 > EM::run { > emc = EM::connect "127.0.0.1", 8100, EchoClient > EM.add_periodic_timer(5) do > cnt +=1; > if cnt <= 3 > emc.send_data "Message: #{cnt}" > else > emc.send_data "quit" > end > end > } > #### End client code #### > In the server shell/command window you should see: > Server received: Message: 1 > Server sent: >>>you sent: Message: 1 > Server received: Message: 2 > Server sent: >>>you sent: Message: 2 > Server received: Message: 3 > Server sent: >>>you sent: Message: 3 > Server received: quit > Server sent: >>>you sent: quit > CLOSING > => nil > > In the client shell/command window you should see: > Client Received: >>>you sent: Message: 1 > Client Received: >>>you sent: Message: 2 > Client Received: >>>you sent: Message: 3 > > > > is newer than #add_periodic_timer), there is a cancel method, which you > can > > invoke inside or outside of the timer block. > > > > t = EM::PeriodicTimer.new (5) { > > # Do this every five seconds > > } > > t.cancel > > > > > > > > > > > > On Jan 12, 2008 7:19 PM, Roger Pack <rogerpack2005 at gmail.com> wrote: > > > > EM.add_periodic_timer(5) do > > > > cnt +=1; > > > > if cnt <= 3 > > > > emc.send_data "Message: #{cnt}" > > > > else > > > > emc.send_data "quit" > > > > end > > > > end > > > > } > > > > > > This timer just keeps going and going. Add EM::stop in there :) > > > _______________________________________________ > > > Eventmachine-talk mailing list > > > Eventmachine-talk at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/eventmachine-talk > > > > > > > > > _______________________________________________ > > Eventmachine-talk mailing list > > Eventmachine-talk at rubyforge.org > > http://rubyforge.org/mailman/listinfo/eventmachine-talk > > > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080115/c034568a/attachment.html