How to get handle from Dispatch::Source::DATA type source?
Hi guys, So, now that GCD sources are supported (go Ernie!), I just thought I'd play with a simple echo server example in MacRuby: require 'socket' q = Dispatch::Queue.concurrent m = Dispatch::Queue.main hostname = "localhost" port = 25000 s = TCPServer.open(hostname, port) src = Dispatch::Source.new(Dispatch::Source::READ, s, 0, q) { |src| client = s.accept # XXX Would like to actually get this from src, rather than grabbing the server socket object directly client.puts(Time.now.ctime) client.puts "Closing down, Bunkie!" client.close } m.run The line with the XXX is the issue I'm trying to figure out. If I look at src.handle or src.data, I end up getting an integer which looks like a fd, not the original TCPSocket instance I passed in, which means I can't call the accept method on it. Is there some secret to getting at the original handle? Thanks! - Jordan
Hi Jordan, I actually thought about adding a "file" method on Dispatch::Source that (in cases where you passed in an IO object instead of an file descriptor), would return that object. Of course it would return nothing in other cases, but maybe that's OK. Still not sure what to call it -- any suggestions? Alternatively, we could only add that method to a subclass of Source, so it only existed when valid. If we did that, we should probably do the same for the "merge" method on custom sources - but having a subclass with only a single method seems lame. Maybe implement it in the superclass, but raise an exception if called for an invalid type? -- Ernie P. On Jan 20, 2010, at 12:37 AM, Jordan K. Hubbard wrote:
Hi guys,
So, now that GCD sources are supported (go Ernie!), I just thought I'd play with a simple echo server example in MacRuby:
require 'socket'
q = Dispatch::Queue.concurrent m = Dispatch::Queue.main
hostname = "localhost" port = 25000 s = TCPServer.open(hostname, port) src = Dispatch::Source.new(Dispatch::Source::READ, s, 0, q) { |src| client = s.accept # XXX Would like to actually get this from src, rather than grabbing the server socket object directly client.puts(Time.now.ctime) client.puts "Closing down, Bunkie!" client.close } m.run
The line with the XXX is the issue I'm trying to figure out. If I look at src.handle or src.data, I end up getting an integer which looks like a fd, not the original TCPSocket instance I passed in, which means I can't call the accept method on it. Is there some secret to getting at the original handle?
Thanks!
- Jordan
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
On Jan 20, 2010, at 7:04 AM, Ernest N. Prabhakar, Ph.D. wrote:
I actually thought about adding a "file" method on Dispatch::Source that (in cases where you passed in an IO object instead of an file descriptor), would return that object. Of course it would return nothing in other cases, but maybe that's OK. Still not sure what to call it -- any suggestions?
Well, frankly, I expected the handle method to return the original handle - that's sort of what the principle of least astonishment would suggest, no? I could see file as a method which returns the underlying fd, assuming you needed that for some reason, though you should also be able to extract that from handle if it's an IO Object, no? I'm trying to figure out why you would want anything but the original handle...
Alternatively, we could only add that method to a subclass of Source, so it only existed when valid. If we did that, we should probably do the same for the "merge" method on custom sources - but having a subclass with only a single method seems lame. Maybe implement it in the superclass, but raise an exception if called for an invalid type?
See above - do we really need to go through all that indirection? - Jordan
Hi Jordan, On Jan 20, 2010, at 9:35 AM, Jordan K. Hubbard wrote:
On Jan 20, 2010, at 7:04 AM, Ernest N. Prabhakar, Ph.D. wrote:
I actually thought about adding a "file" method on Dispatch::Source that (in cases where you passed in an IO object instead of an file descriptor), would return that object. Of course it would return nothing in other cases, but maybe that's OK. Still not sure what to call it -- any suggestions?
Well, frankly, I expected the handle method to return the original handle - that's sort of what the principle of least astonishment would suggest, no?
Unfortunately, there's two principles at work. One is fidelity with the underlying GCD code, where "handle" is always an integer -- which is what we do now, where we auto-convert the File object into an Integer to give to GCD. But you're right, a better application would be to simply store the passed handle in the Source object, and pass -that- back for the handle method (which would also save us from boxing/unboxing). Feel free to file a bug and assign it to me... -- Ernie P.
I could see file as a method which returns the underlying fd, assuming you needed that for some reason, though you should also be able to extract that from handle if it's an IO Object, no? I'm trying to figure out why you would want anything but the original handle...
Alternatively, we could only add that method to a subclass of Source, so it only existed when valid. If we did that, we should probably do the same for the "merge" method on custom sources - but having a subclass with only a single method seems lame. Maybe implement it in the superclass, but raise an exception if called for an invalid type?
See above - do we really need to go through all that indirection?
Well, right now
- Jordan
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
participants (2)
-
Ernest N. Prabhakar, Ph.D.
-
Jordan K. Hubbard