Permalink
Browse files

playing with drb

  • Loading branch information...
1 parent eba93a5 commit d291fb99082f9c7841b3742d2f61b3cf1393934d Sven Fuchs committed Feb 19, 2010
@@ -1,24 +0,0 @@
-module Steam
- class Forker
- attr_reader :pid
-
- def initialize(&block)
- @pid = Kernel.fork { block.call }
- at_exit { kill }
- end
-
- def kill(signal = 'TERM')
- Process.kill(Signal.list[signal], @pid) && true if running?
- end
-
- def interrupt
- kill('INT')
- end
-
- def running?
- @pid ? !!Process.getpgid(@pid) : false
- rescue Errno::ESRCH
- false
- end
- end
-end
@@ -0,0 +1,23 @@
+module Process
+ def self.daemon(nochdir = nil, noclose = nil)
+ exit if fork # Parent exits, child continues.
+ Process.setsid # Become session leader.
+ exit if fork # Zap session leader. See [1].
+
+ unless nochdir
+ Dir.chdir "/" # Release old working directory.
+ end
+
+ File.umask 0000 # Ensure sensible umask. Adjust as needed.
+
+ unless noclose
+ STDIN.reopen "/dev/null" # Free file descriptors and
+ STDOUT.reopen "/dev/null", "a" # point them somewhere sensible.
+ STDERR.reopen '/dev/null', 'a'
+ end
+
+ trap("TERM") { exit }
+
+ return 0
+ end unless respond_to?(:daemon)
+end
View
@@ -4,7 +4,6 @@ module Steam
autoload :Browser, 'steam/browser'
autoload :Connection, 'steam/connection'
autoload :Java, 'steam/java'
- autoload :Reloader, 'steam/reloader'
autoload :Request, 'steam/request'
autoload :Response, 'steam/response'
autoload :Session, 'steam/session'
@@ -18,6 +17,7 @@ def config
:url_scheme => 'http',
:charset => 'utf-8',
:java_load_params => '-Xmx1024M',
+ :drb_uri => 'druby://127.0.0.1:9000',
:html_unit => {
:java_path => File.expand_path("../htmlunit-2.6/", __FILE__),
:browser_version => :FIREFOX_3,
@@ -10,6 +10,8 @@ module Browser
class HtmlUnit
autoload :Actions, 'steam/browser/html_unit/actions'
autoload :Client, 'steam/browser/html_unit/client'
+ autoload :Drb, 'steam/browser/html_unit/drb'
+ autoload :Forker, 'steam/browser/html_unit/forker'
autoload :Connection, 'steam/browser/html_unit/connection'
autoload :Matchers, 'steam/browser/html_unit/matchers'
autoload :Page, 'steam/browser/html_unit/page'
@@ -0,0 +1,33 @@
+require 'drb'
+
+module Steam
+ module Browser
+ class HtmlUnit
+ class Drb
+ autoload :Client, 'steam/browser/html_unit/drb/client'
+ autoload :Service, 'steam/browser/html_unit/drb/service'
+
+ def initialize
+ DRb.start_service
+ @browser = DRbObject.new(nil, Steam.config[:drb_uri])
+ end
+
+ def daemonize(connection = nil, options = {})
+ Forker.new { start(connection, options) }
+ sleep(1) # FIXME
+ end
+
+ def start(connection = nil, options = {})
+ uri = Steam.config[:drb_uri]
+ DRb.start_service(uri, HtmlUnit.new(connection, options))
+ puts "Browser ready and listening at #{uri}"
+ DRb.thread.join
+ end
+
+ def method_missing(method, *args, &block)
+ @browser.send(method, *args, &block)
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,20 @@
+require 'drb'
+
+module Steam
+ module Browser
+ class HtmlUnit
+ module Drb
+ class Client
+ def initialize
+ DRb.start_service
+ @browser = DRbObject.new(nil, Service.uri)
+ end
+
+ def method_missing(method, *args, &block)
+ @browser.send(method, *args, &block) # if @browser.respond_to?(method)
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,28 @@
+require 'drb'
+require 'core_ext/ruby/process/daemon'
+
+module Steam
+ module Browser
+ class HtmlUnit
+ module Drb
+ module Service
+ class << self
+ def uri
+ Steam.config[:drb_uri]
+ end
+
+ def daemonize(connection = nil, options = {})
+ Forker.new { start(connection, options) }
+ end
+
+ def start(connection = nil, options = {})
+ DRb.start_service(uri, HtmlUnit.new(connection, options))
+ puts "Browser ready and listening at #{uri}"
+ DRb.thread.join
+ end
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,51 @@
+module Steam
+ class Forker
+ attr_reader :options
+
+ def initialize(options = {}, &block)
+ options = {
+ :pid => '/tmp/steam.pid',
+ :stdin => $stdin,
+ :stdout => $stdout,
+ :stderr => $stderr
+ }.merge(options)
+
+ @pid = Kernel.fork do
+ STDIN.reopen options[:stdin]
+ STDOUT.reopen options[:stdout]
+ STDERR.reopen options[:stderr]
+ block.call
+ end
+ # write_pid
+ at_exit { kill }
+ end
+
+ def interrupt
+ kill('INT')
+ end
+
+ def kill(signal = 'TERM')
+ if running?
+ Process.kill(Signal.list[signal], @pid)
+ delete_pid
+ true
+ end
+ end
+
+ def running?
+ @pid ? !!Process.getpgid(@pid) : false
+ rescue Errno::ESRCH
+ false
+ end
+
+ protected
+
+ def write_pid
+ ::File.open('/tmp/steam.pid', 'w'){ |f| f.write("#{@pid}") }
+ end
+
+ def delete_pid
+ ::File.delete('/tmp/steam.pid') rescue Errno::ENOENT
+ end
+ end
+end
View
@@ -0,0 +1,19 @@
+$: << File.expand_path("../../../lib", __FILE__)
+
+require 'steam'
+
+include Steam
+
+@app = Connection::Mock.new
+root = File.expand_path("../../fixtures/public", __FILE__)
+static = Connection::Static.new(:root => root, :urls => %w(/ /javascripts /stylesheets))
+connection = Rack::Cascade.new([static, @app])
+
+browser = Browser::HtmlUnit::Drb.new
+
+browser.daemonize(connection)
+
+browser.request('/index.html')
+p browser.response.status
+
+# @browser = Browser::HtmlUnit.new(connection)

0 comments on commit d291fb9

Please sign in to comment.