Created
December 2, 2011 23:55
-
-
Save filterfish/1425413 to your computer and use it in GitHub Desktop.
EventMachine Pipe Clinet/Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'pathname' | |
uri = ARGV[0] || (puts "usage: #{$0} <uri>"; exit 1) | |
Pathname.new('pipe').open("w") do |fd| | |
fd.puts(uri) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'digest' | |
require 'fcntl' | |
require 'em-http' | |
require 'pathname' | |
require 'addressable/uri' | |
class PipeServer < EventMachine::FileWatch | |
def notify_readable | |
data = @io.readlines | |
data.each do |line| | |
uri = Addressable::URI.parse(line.strip) | |
if uri.scheme && uri.host | |
puts "Fetching: #{uri.to_s}" | |
http = EventMachine::HttpRequest.new(uri.to_s).get(:redirects => 5) | |
http.callback do | |
if http.response_header.status == 200 | |
path = Pathname.new("#{uri.scheme}.#{uri.host}.#{Digest::SHA1.hexdigest(http.response)}") | |
path.open("w") { |fd| fd.write(http.response) } | |
puts "Written output to: #{path.to_s}" | |
else | |
puts "Something went wrong: #{http.response_header.status}" | |
end | |
end | |
http.errback do | |
puts "Problem:", http.inspect | |
end | |
else | |
puts "That's not a proper url." | |
end | |
end | |
end | |
def unbind | |
puts "#{path} monitoring ceased" | |
EM.stop | |
end | |
end | |
EM.run { | |
fd = IO.sysopen('pipe', Fcntl::O_RDONLY|Fcntl::O_NONBLOCK) | |
pipe = IO.new(fd, Fcntl::O_RDONLY|Fcntl::O_NONBLOCK) | |
fdmon = EM.watch(pipe, PipeServer) | |
fdmon.notify_readable = true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create a named pipe:
mknod pipe p
. Start the pipe server:./pipe-server
, and then run./pipe-client <uri>
. The uri will be downloaded and saved in the current directory. The client is just there for show; you could just use echo:echo <uri> > pipe
Note lines 45--51 are from this gist: https://gist.github.com/1141924