I'd like to be able to do a select.
I'd also like to treat these as a single IO object.
So I thought: Well, I'll fork a TCPServer.
I'll let it read from the input stream and write to the client.
I'll let it read from the client and write to the output stream.
Then I'll open a socket to that server, et voila! There is my
select-able duplex IO object.
But in practice, I'm having trouble implementing this.
I won't even show you the code. ;)
Any assistance appreciated.
Hal
In message "IO issues: forking, select, and duplexing"
on 04/07/29, Hal Fulton <hal...@hypermetrics.com> writes:
|Suppose I have a source and a sink of data. These are IO-like,
|but you can't do a select on them.
|
|I'd like to be able to do a select.
|
|I'd also like to treat these as a single IO object.
If your IO-like object contains a reference to a real IO object that
can be a target of select, you can call select on the IO-like object
by just adding "to_io" method that returns real IO object.
|So I thought: Well, I'll fork a TCPServer.
If you really need full duplex IO object using fork, how about using
"open" instead of TCPServer:
open("|-", "w+") do
# the code to copy from STDIN to IO-like
# and from IO-like to STDOUT
end
matz.
> If your IO-like object contains a reference to a real IO object that can be
> a target of select, you can call select on the IO-like object by just adding
> "to_io" method that returns real IO object.
you mean this will (is) supported?
class Klass
def initialize path
@io = open path
end
def to_io
@io
end
end
cool.
can you give a quick brain dump of other to_XXX method we might want to know
about?
kind regards.
-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
That is very interesting. I don't think I can do this easily (as the
code is from a third party), but I'll look at it.
> |So I thought: Well, I'll fork a TCPServer.
>
> If you really need full duplex IO object using fork, how about using
> "open" instead of TCPServer:
>
> open("|-", "w+") do
> # the code to copy from STDIN to IO-like
> # and from IO-like to STDOUT
> end
This is also very interesting. I will try this soon.
Thanks,
Hal
May I suggest Socket.pair ?
sockets = Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM, 0)
sockets[0].puts "hello"
sockets[1].gets #=> "hello\n"
- Greg Millam
Cheers,
Kent.