Rafael Sevilla
2008-Jan-14 19:27 UTC
[Eventmachine-talk] Using EventMachine to listen from non-network
I''ve actually found a way to make EventMachine listen to events from a serial port, but only by using the pure Ruby version, using Guilliame Pierronnet''s Ruby/SerialPort library. It turned out to be simpler than I thought it would be: require ''serialport'' $eventmachine_library = :pure_ruby require ''eventmachine'' module EventMachine class << self def connect_serial(dev, baud, databits, stopbits, parity) EvmaSerialPort.open(dev, baud, databits, stopbits, parity).uuid end end def EventMachine::open_serial(dev, baud, databits, stopbits, parity, handler=nil) klass = if (handler and handler.is_a?(Class)) handler else Class.new( Connection ) {handler and include handler} end s = connect_serial(dev, baud, databits, stopbits, parity) c = klass.new s @conns[s] = c block_given? and yield c c end end module EventMachine class EvmaSerialPort < StreamObject def self.open(dev, baud, databits, stopbits, parity) io = SerialPort.new(dev, baud, databits, stopbits, parity) EvmaSerialPort.new(io) end def initialize(io) super end end end EventMachine::open_serial is used the way you would use EventMachine::connect when writing a TCP client. I wonder if it is actually possible to do something like this using the C++ extension, without needing to write additional C++ code. -- As long as you''re doing nothing wrong, you don''t need privacy. (What the Government considers wrong subject to change without notice. Civil rights void where journalists prohibited. Contents may settle during extraordinary rendition.) http://stormwyrm.blogspot.com -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 489 bytes Desc: not available Url : http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080115/5e1e7fff/attachment.bin
Francis Cianfrocca
2008-Jan-15 02:53 UTC
[Eventmachine-talk] Using EventMachine to listen from
On Jan 14, 2008 10:27 PM, Rafael Sevilla <dido at imperium.ph> wrote:> I''ve actually found a way to make EventMachine listen to events from a > serial port, but only by using the pure Ruby version, using Guilliame > Pierronnet''s Ruby/SerialPort library. It turned out to be simpler > than I thought it would be: >..snip..> module EventMachine > class EvmaSerialPort < StreamObject > def self.open(dev, baud, databits, stopbits, parity) > io = SerialPort.new(dev, baud, databits, stopbits, parity) > EvmaSerialPort.new(io) > end >Bravo! It looks to me like the only piece of "magic" here is in the line "io SerialPort.new...." where you call into Guillaume''s library. Is it true that SerialPort is a subclass of Ruby''s IO class? I can''t tell from this code whether it''s possible to successfully set a SerialPort object nonblocking. Is it? By the way, another broad class of non-network events that is of interest is events from GUI frameworks like Tcl/Tk. Twisted makes an attempt to integrate these events but they did so much violence that I''m not convinced we should make the attempt, unless someone can think of an elegant way to do it. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080115/32fb211b/attachment.html
Rafael Sevilla
2008-Jan-15 03:24 UTC
[Eventmachine-talk] Using EventMachine to listen from
On Tue, 15 Jan 2008 05:53:27 -0500 "Francis Cianfrocca" <garbagecat10 at gmail.com> wrote:> It looks to me like the only piece of "magic" here is in the line "io > = SerialPort.new...." where you call into Guillaume''s library. Is it > true that SerialPort is a subclass of Ruby''s IO class? >It seems that way, as far as I can tell.> I can''t tell from this code whether it''s possible to successfully set > a SerialPort object nonblocking. Is it? >I tested it: a SerialPort instance actually responds to IO#fcntl methods and it appears to do the Right Thing when an Fcntl::O_NONBLOCK is added. There''s a SerialPort#readtimeout= method, which when set to a negative value, results in nonblocking reads.> By the way, another broad class of non-network events that is of > interest is events from GUI frameworks like Tcl/Tk. Twisted makes an > attempt to integrate these events but they did so much violence that > I''m not convinced we should make the attempt, unless someone can > think of an elegant way to do it.Now this is an interesting idea. I was actually considering doing this sort of thing, but with XUL. -- As long as you''re doing nothing wrong, you don''t need privacy. (What the Government considers wrong subject to change without notice. Civil rights void where journalists prohibited. Contents may settle during extraordinary rendition.) http://stormwyrm.blogspot.com -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 489 bytes Desc: not available Url : http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080115/38357d47/attachment.bin
Michael S. Fischer
2008-Jan-15 08:47 UTC
[Eventmachine-talk] Using EventMachine to listen from
On Jan 15, 2008 2:53 AM, Francis Cianfrocca <garbagecat10 at gmail.com> wrote:> By the way, another broad class of non-network events that is of interest is > events from GUI frameworks like Tcl/Tk. Twisted makes an attempt to > integrate these events but they did so much violence that I''m not convinced > we should make the attempt, unless someone can think of an elegant way to do > it.On my wishlist is a ''tail -f'' implementation in EventMachine. What would that entail? Best regards, --Michael
Francis Cianfrocca
2008-Jan-15 15:29 UTC
[Eventmachine-talk] Using EventMachine to listen from
On Jan 15, 2008 11:47 AM, Michael S. Fischer <michael at dynamine.net> wrote:> On Jan 15, 2008 2:53 AM, Francis Cianfrocca <garbagecat10 at gmail.com> > wrote: > > > By the way, another broad class of non-network events that is of > interest is > > events from GUI frameworks like Tcl/Tk. Twisted makes an attempt to > > integrate these events but they did so much violence that I''m not > convinced > > we should make the attempt, unless someone can think of an elegant way > to do > > it. > > On my wishlist is a ''tail -f'' implementation in EventMachine. What > would that entail? >require ''rubygems'' require ''eventmachine'' module Tailer def receive_data data puts data end end EM.run { EM.popen "tail -f some_file", Tailer } Got any hard questions? :-) -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080115/f8542886/attachment.html
Michael S. Fischer
2008-Jan-15 17:07 UTC
[Eventmachine-talk] Using EventMachine to listen from
On Jan 15, 2008 3:29 PM, Francis Cianfrocca <garbagecat10 at gmail.com> wrote:> > On my wishlist is a ''tail -f'' implementation in EventMachine. What > > would that entail?> EM.popen "tail -f some_file", TailerVery funny :-) (For an example of an event-driven ''tail'', see http://distanz.ch/inotail/ - requires Linux + inotify) --Michael
On 15 Jan 2008, at 03:27, Rafael Sevilla wrote:> I''ve actually found a way to make EventMachine listen to events from a > serial port, but only by using the pure Ruby version, using Guilliame > Pierronnet''s Ruby/SerialPort library.Awesome. We''ve got a couple of apps that deal with Serial devices and I''ve been looking at this and discussed something briefly with Francis a while back. I managed to get the code working on OS X, and presumably will work fine on Linux too (after some patches to ruby-serialport). On Win32 I ran into a couple of issues. For our purposes, being GPL may be an issue, but a simple Serial to Socket proxy built out of eventmachine would do nicely, so there may be some scope for building that as a standalone app. Anyway, enough on license stuff.. On what platforms did you try this? (Worked on OS X, and I am relatively sure it will work fine on BSD + Linux, Windows had EM related issues). Did you need to patch ruby-serialport? (I had to for OS X Leopard and for Windows) Good stuff, Thanks :-)
Francis Cianfrocca
2008-Jan-15 18:54 UTC
[Eventmachine-talk] Using EventMachine to listen from
On Jan 15, 2008 8:07 PM, Michael S. Fischer <michael at dynamine.net> wrote:> On Jan 15, 2008 3:29 PM, Francis Cianfrocca <garbagecat10 at gmail.com> > wrote: > > > > On my wishlist is a ''tail -f'' implementation in EventMachine. What > > > would that entail? > > > EM.popen "tail -f some_file", Tailer > > Very funny :-) > > (For an example of an event-driven ''tail'', see > http://distanz.ch/inotail/ - requires Linux + inotify) > > >Let me try something different on you: require ''eventmachine'' class Tailer def on_data data puts data end end EM.reactor { add_io( File.open( some_filename, "r" ), Tailer ) } Is that closer to what you had in mind? This is actual working code written in EM Version 1 (currently unreleased). It does handle the tail correctly. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080115/11a0b7af/attachment.html
Rafael Sevilla
2008-Jan-15 20:25 UTC
[Eventmachine-talk] Using EventMachine to listen from
On Wed, 16 Jan 2008 01:58:10 +0000 James Tucker <jftucker at gmail.com> wrote:> On what platforms did you try this? (Worked on OS X, and I am > relatively sure it will work fine on BSD + Linux, Windows had EM > related issues). > > Did you need to patch ruby-serialport? (I had to for OS X Leopard > and for Windows)Linux only. Our people were already using ruby-serialport (AFAIK they didn''t need to patch it) and they were looking for a framework that would hep make programming the serial port simpler. I was already using EventMachine for network programming and figured that this would be right on the money--if I could get it to receive data from the serial port. -- As long as you''re doing nothing wrong, you don''t need privacy. (What the Government considers wrong subject to change without notice. Civil rights void where journalists prohibited. Contents may settle during extraordinary rendition.) http://stormwyrm.blogspot.com -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 489 bytes Desc: not available Url : http://rubyforge.org/pipermail/eventmachine-talk/attachments/20080116/8f686d62/attachment.bin
Michael S. Fischer
2008-Jan-15 20:41 UTC
[Eventmachine-talk] Using EventMachine to listen from
On Jan 15, 2008 6:54 PM, Francis Cianfrocca <garbagecat10 at gmail.com> wrote:> class Tailer > def on_data data > puts data > end > end > > EM.reactor { > add_io( File.open( some_filename, "r" ), Tailer ) > } > > > Is that closer to what you had in mind?NIce! --Michael
On Jan 15, 2008 6:24 AM, Rafael Sevilla <dido at imperium.ph>> Now this is an interesting idea. I was actually considering doing > this sort of thing, but with XUL.Is ruby working in xulrunner finally? Last I checked xpcom/ruby was dead...