/
remote_client.rb
92 lines (75 loc) · 2.68 KB
/
remote_client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require 'socket'
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
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
# @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
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.
manager.configuration = Akephalos.configuration.extend(DRbUndumped)
@manager = manager
end
# Start a remote server process and return when it is available for use.
def self.start!
port = find_available_port
remote_client = IO.popen("#{Akephalos::BIN_DIR + 'akephalos'} #{port}")
# Set up a monitor thread to detect if the forked server exits
# prematurely.
server_monitor = Thread.new { Thread.current[:exited] = Process.wait(remote_client.pid) }
# Wait for the server to be accessible on the socket we specified.
until responsive?(port)
exit!(1) if server_monitor[:exited]
sleep 0.5
end
server_monitor.kill
# Ensure that the remote server shuts down gracefully when we are
# finished.
at_exit { Process.kill(:INT, remote_client.pid) }
port
end
private
# @api private
# @param [Integer] port the port to check for responsiveness
# @return [true, false] whether the port is responsive
def self.responsive?(port)
socket = TCPSocket.open('127.0.0.1', port)
true
rescue Errno::ECONNREFUSED
false
ensure
socket.close if socket
end
# @api private
# @return [Integer] the next available port
def self.find_available_port
server = TCPServer.new('127.0.0.1', 0)
server.addr[1]
ensure
server.close if server
end
end
end