Skip to content

Commit

Permalink
Add Akephalos::ClientManager for DRb Server
Browse files Browse the repository at this point in the history
This is a refactoring of the internal communication structure between
the MRI ruby process and the JRuby DRb server. This sets the path for
allowing per-instance configurations and eventually supporting a single
long-running akephalos instance.
  • Loading branch information
bernerdschaefer committed Feb 3, 2011
1 parent 044ae84 commit 4ed46ea
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
7 changes: 1 addition & 6 deletions lib/akephalos/capybara.rb
Expand Up @@ -162,11 +162,6 @@ def select_node

attr_reader :app, :rack_server

# @return [Client] an instance of Akephalos::Client
def self.driver
@driver ||= Akephalos::Client.new
end

def initialize(app)
@app = app
@rack_server = Capybara::Server.new(@app)
Expand Down Expand Up @@ -271,7 +266,7 @@ def page

# @return the browser
def browser
self.class.driver
@browser ||= Akephalos::Client.new
end

# @return the session cookies
Expand Down
10 changes: 0 additions & 10 deletions lib/akephalos/client.rb
Expand Up @@ -35,16 +35,6 @@ def initialize
Thread.new { @_client.run }
end

# Set the global configuration settings for Akephalos.
#
# @note This is only used when communicating over DRb, since just a
# single client instance is exposed.
# @param [Hash] config the configuration settings
# @return [Hash] the configuration
def configuration=(config)
Akephalos.configuration = config
end

# Visit the requested URL and return the page.
#
# @param [String] url the URL to load
Expand Down
24 changes: 16 additions & 8 deletions lib/akephalos/remote_client.rb
Expand Up @@ -15,23 +15,31 @@ module Akephalos
# client.visit "http://www.oinopa.com"
# client.page.source # => "<!DOCTYPE html PUBLIC..."
class RemoteClient
# Start a remote akephalos server and return the remote Akephalos::Client
# instance.
#
# @return [DRbObject] the remote client instance
# @return [DRbObject] a new instance of Akephalos::Client from the DRb
# server
def self.new
manager.new_client
end

# Starts a remove JRuby DRb server unless already running and returns an
# instance of Akephalos::ClientManager.
#
# @returns [DRbObject] an instance of Akephalos::ClientManager
def self.manager
return @manager if defined?(@manager)

server_port = start!

DRb.start_service("druby://127.0.0.1:#{find_available_port}")
client = DRbObject.new_with_uri("druby://127.0.0.1:#{server_port}")
DRb.start_service
manager = DRbObject.new_with_uri("druby://127.0.0.1:#{server_port}")

# 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)
manager.configuration = Akephalos.configuration.extend(DRbUndumped)

client
@manager = manager
end

# Start a remote server process and return when it is available for use.
Expand Down
31 changes: 27 additions & 4 deletions lib/akephalos/server.rb
Expand Up @@ -23,16 +23,39 @@ def _dump

module Akephalos

# The ClientManager is shared over DRb with the remote process, and
# facilitates communication between the processes.
#
# @api private
class ClientManager
include DRbUndumped

# @return [Akephalos::Client] a new client instance
def self.new_client
# Store the client to ensure it isn't prematurely garbage collected.
@client = Client.new
end

# Set the global configuration settings for Akephalos.
#
# @param [Hash] config the configuration settings
# @return [Hash] the configuration
def self.configuration=(config)
Akephalos.configuration = config
end

end

# Akephalos::Server is used by `akephalos --server` to start a DRb server
# serving an instance of Akephalos::Client.
# serving Akephalos::ClientManager.
class Server
# Start DRb service for an Akephalos::Client.

# Start DRb service for Akephalos::ClientManager.
#
# @param [String] port attach server to
def self.start!(port)
abort_on_parent_exit!
client = Client.new
DRb.start_service("druby://127.0.0.1:#{port}", client)
DRb.start_service("druby://127.0.0.1:#{port}", ClientManager)
DRb.thread.join
end

Expand Down

0 comments on commit 4ed46ea

Please sign in to comment.