Sha256: c7282acc6a360cb2c4a9ab844dc6bc2277a6e27213a8c2ee4a53a158c6a5b04d

Contents?: true

Size: 1.88 KB

Versions: 9

Compression:

Stored size: 1.88 KB

Contents

require 'drb/drb'

# We need to define our own NativeException class for the cases when a native
# exception is raised by the JRuby DRb server.
class NativeException < StandardError; end # :nodoc:

module Akephalos
  ##
  # The +RemoteClient+ class provides an interface to an +Akephalos::Client+
  # isntance on a remote DRb server.
  #
  # == Usage
  #     client = Akephalos::RemoteClient.new
  #     client.visit "http://www.oinopa.com"
  #     client.page.source # => "<!DOCTYPE html PUBLIC..."
  class RemoteClient
    @socket_file = "/tmp/akephalos.#{Process.pid}.sock"

    ##
    # Starts a remote akephalos server and returns the remote Akephalos::Client
    # instance.
    def self.new
      start!
      DRb.start_service
      client = DRbObject.new_with_uri("drbunix://#{@socket_file}")
      # We want to share our local configuration with the remote server
      # process, so we share an undumped version of our configuration. This
      # lets us continue to make changes locally and have them reflected in the
      # remote process.
      client.configuration = Akephalos.configuration.extend(DRbUndumped)
      client
    end

    ##
    # Start a remote server process, returning when it is available for use.
    def self.start!
      remote_client = fork do
        exec("#{Akephalos::BIN_DIR + 'akephalos'} #{@socket_file}")
      end

      # Set up a monitor thread to detect if the forked server exits
      # prematurely.
      server_monitor = Thread.new { Thread.current[:exited] = Process.wait }

      # Wait for the server to be accessible on the socket we specified.
      until File.exists?(@socket_file)
        exit!(1) if server_monitor[:exited]
        sleep 1
      end
      server_monitor.kill

      # Ensure that the remote server shuts down gracefully when we are
      # finished.
      at_exit { Process.kill(:INT, remote_client); File.unlink(@socket_file) }
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
akephalos-0.2.3-java lib/akephalos/remote_client.rb
akephalos-0.2.3 lib/akephalos/remote_client.rb
akephalos-0.2.2-java lib/akephalos/remote_client.rb
akephalos-0.2.2 lib/akephalos/remote_client.rb
akephalos-0.2.1 lib/akephalos/remote_client.rb
akephalos-0.2.0 lib/akephalos/remote_client.rb
akephalos-0.2.0-java lib/akephalos/remote_client.rb
akephalos-0.1.0-java lib/akephalos/remote_client.rb
akephalos-0.1.0 lib/akephalos/remote_client.rb