Skip to content

Instantly share code, notes, and snippets.

@filterfish
Created December 2, 2011 23:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filterfish/1425413 to your computer and use it in GitHub Desktop.
Save filterfish/1425413 to your computer and use it in GitHub Desktop.
EventMachine Pipe Clinet/Server
#!/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
#!/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
}
@filterfish
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment