Module: DRb
- Defined in:
- lib/drb/drb.rb,
lib/drb/eq.rb,
lib/drb/gw.rb,
lib/drb/ssl.rb,
lib/drb/unix.rb,
lib/drb/extserv.rb,
lib/drb/version.rb,
lib/drb/extservm.rb,
lib/drb/observer.rb,
lib/drb/weakidconv.rb,
lib/drb/timeridconv.rb
Overview
Overview
dRuby is a distributed object system for Ruby. It is written in pure Ruby and uses its own protocol. No add-in services are needed beyond those provided by the Ruby runtime, such as TCP sockets. It does not rely on or interoperate with other distributed object systems such as CORBA, RMI, or .NET.
dRuby allows methods to be called in one Ruby process upon a Ruby object located in another Ruby process, even on another machine. References to objects can be passed between processes. Method arguments and return values are dumped and loaded in marshalled format. All of this is done transparently to both the caller of the remote method and the object that it is called upon.
An object in a remote process is locally represented by a DRb::DRbObject instance. This acts as a sort of proxy for the remote object. Methods called upon this DRbObject instance are forwarded to its remote object. This is arranged dynamically at run time. There are no statically declared interfaces for remote objects, such as CORBA’s IDL.
dRuby calls made into a process are handled by a DRb::DRbServer instance within that process. This reconstitutes the method call, invokes it upon the specified local object, and returns the value to the remote caller. Any object can receive calls over dRuby. There is no need to implement a special interface, or mixin special functionality. Nor, in the general case, does an object need to explicitly register itself with a DRbServer in order to receive dRuby calls.
One process wishing to make dRuby calls upon another process must somehow obtain an initial reference to an object in the remote process by some means other than as the return value of a remote method call, as there is initially no remote object reference it can invoke a method upon. This is done by attaching to the server by URI. Each DRbServer binds itself to a URI such as ‘druby://example.com:8787’. A DRbServer can have an object attached to it that acts as the server’s front object. A DRbObject can be explicitly created from the server’s URI. This DRbObject’s remote object will be the server’s front object. This front object can then return references to other Ruby objects in the DRbServer’s process.
Method calls made over dRuby behave largely the same as normal Ruby method calls made within a process. Method calls with blocks are supported, as are raising exceptions. In addition to a method’s standard errors, a dRuby call may also raise one of the dRuby-specific errors, all of which are subclasses of DRb::DRbError.
Any type of object can be passed as an argument to a dRuby call or returned as its return value. By default, such objects are dumped or marshalled at the local end, then loaded or unmarshalled at the remote end. The remote end therefore receives a copy of the local object, not a distributed reference to it; methods invoked upon this copy are executed entirely in the remote process, not passed on to the local original. This has semantics similar to pass-by-value.
However, if an object cannot be marshalled, a dRuby reference to it is passed or returned instead. This will turn up at the remote end as a DRbObject instance. All methods invoked upon this remote proxy are forwarded to the local object, as described in the discussion of DRbObjects. This has semantics similar to the normal Ruby pass-by-reference.
The easiest way to signal that we want an otherwise marshallable object to be passed or returned as a DRbObject reference, rather than marshalled and sent as a copy, is to include the DRb::DRbUndumped mixin module.
dRuby supports calling remote methods with blocks. As blocks (or rather the Proc objects that represent them) are not marshallable, the block executes in the local, not the remote, context. Each value yielded to the block is passed from the remote object to the local block, then the value returned by each block invocation is passed back to the remote execution context to be collected, before the collected values are finally returned to the local context as the return value of the method invocation.
Examples of usage
For more dRuby samples, see the samples directory in the full dRuby distribution.
dRuby in client/server mode
This illustrates setting up a simple client-server drb system. Run the server and client code in different terminals, starting the server code first.
Server code
require 'drb/drb'
# The URI for the server to connect to
URI="druby://localhost:8787"
class TimeServer
def get_current_time
return Time.now
end
end
# The object that handles requests on the server
FRONT_OBJECT=TimeServer.new
DRb.start_service(URI, FRONT_OBJECT)
# Wait for the drb server thread to finish before exiting.
DRb.thread.join
Client code
require 'drb/drb'
# The URI to connect to
SERVER_URI="druby://localhost:8787"
# Start a local DRbServer to handle callbacks.
#
# Not necessary for this small example, but will be required
# as soon as we pass a non-marshallable object as an argument
# to a dRuby call.
#
# Note: this must be called at least once per process to take any effect.
# This is particularly important if your application forks.
DRb.start_service
timeserver = DRbObject.new_with_uri(SERVER_URI)
puts timeserver.get_current_time
Remote objects under dRuby
This example illustrates returning a reference to an object from a dRuby call. The Logger instances live in the server process. References to them are returned to the client process, where methods can be invoked upon them. These methods are executed in the server process.
Server code
require 'drb/drb'
URI="druby://localhost:8787"
class Logger
# Make dRuby send Logger instances as dRuby references,
# not copies.
include DRb::DRbUndumped
def initialize(n, fname)
@name = n
@filename = fname
end
def log()
File.open(@filename, "a") do |f|
f.puts("#{Time.now}: #{@name}: #{message}")
end
end
end
# We have a central object for creating and retrieving loggers.
# This retains a local reference to all loggers created. This
# is so an existing logger can be looked up by name, but also
# to prevent loggers from being garbage collected. A dRuby
# reference to an object is not sufficient to prevent it being
# garbage collected!
class LoggerFactory
def initialize(bdir)
@basedir = bdir
@loggers = {}
end
def get_logger(name)
if !@loggers.has_key? name
# make the filename safe, then declare it to be so
fname = name.gsub(/[.\/\\\:]/, "_")
@loggers[name] = Logger.new(name, @basedir + "/" + fname)
end
return @loggers[name]
end
end
FRONT_OBJECT=LoggerFactory.new("/tmp/dlog")
DRb.start_service(URI, FRONT_OBJECT)
DRb.thread.join
Client code
require 'drb/drb'
SERVER_URI="druby://localhost:8787"
DRb.start_service
log_service=DRbObject.new_with_uri(SERVER_URI)
["loga", "logb", "logc"].each do |logname|
logger=log_service.get_logger(logname)
logger.log("Hello, world!")
logger.log("Goodbye, world!")
logger.log("=== EOT ===")
end
Security
As with all network services, security needs to be considered when using dRuby. By allowing external access to a Ruby object, you are not only allowing outside clients to call the methods you have defined for that object, but by default to execute arbitrary Ruby code on your server. Consider the following:
# !!! UNSAFE CODE !!!
ro = DRbObject::new_with_uri("druby://your.server.com:8989")
class << ro
undef :instance_eval # force call to be passed to remote object
end
ro.instance_eval("`rm -rf *`")
The dangers posed by instance_eval and friends are such that a DRbServer should only be used when clients are trusted.
A DRbServer can be configured with an access control list to selectively allow or deny access from specified IP addresses. The main druby distribution provides the ACL class for this purpose. In general, this mechanism should only be used alongside, rather than as a replacement for, a good firewall.
dRuby internals
dRuby is implemented using three main components: a remote method call marshaller/unmarshaller; a transport protocol; and an ID-to-object mapper. The latter two can be directly, and the first indirectly, replaced, in order to provide different behaviour and capabilities.
Marshalling and unmarshalling of remote method calls is performed by a DRb::DRbMessage instance. This uses the Marshal module to dump the method call before sending it over the transport layer, then reconstitute it at the other end. There is normally no need to replace this component, and no direct way is provided to do so. However, it is possible to implement an alternative marshalling scheme as part of an implementation of the transport layer.
The transport layer is responsible for opening client and server network connections and forwarding dRuby request across them. Normally, it uses DRb::DRbMessage internally to manage marshalling and unmarshalling. The transport layer is managed by DRb::DRbProtocol. Multiple protocols can be installed in DRbProtocol at the one time; selection between them is determined by the scheme of a dRuby URI. The default transport protocol is selected by the scheme ‘druby:’, and implemented by DRb::DRbTCPSocket. This uses plain TCP/IP sockets for communication. An alternative protocol, using UNIX domain sockets, is implemented by DRb::DRbUNIXSocket in the file drb/unix.rb, and selected by the scheme ‘drbunix:’. A sample implementation over HTTP can be found in the samples accompanying the main dRuby distribution.
The ID-to-object mapping component maps dRuby object ids to the objects they refer to, and vice versa. The implementation to use can be specified as part of a DRb::DRbServer’s configuration. The default implementation is provided by DRb::DRbIdConv. It uses an object’s ObjectSpace id as its dRuby id. This means that the dRuby reference to that object only remains meaningful for the lifetime of the object’s process and the lifetime of the object within that process. A modified implementation is provided by DRb::TimerIdConv in the file drb/timeridconv.rb. This implementation retains a local reference to all objects exported over dRuby for a configurable period of time (defaulting to ten minutes), to prevent them being garbage-collected within this time. Another sample implementation is provided in sample/name.rb in the main dRuby distribution. This allows objects to specify their own id or “name”. A dRuby reference can be made persistent across processes by having each process register an object using the same dRuby name.
Defined Under Namespace
Modules: DRbObservable, DRbProtocol, DRbUndumped Classes: DRbArray, DRbBadScheme, DRbBadURI, DRbConn, DRbConnError, DRbError, DRbIdConv, DRbMessage, DRbObject, DRbObjectSpace, DRbRemoteError, DRbSSLSocket, DRbServer, DRbServerNotFound, DRbTCPSocket, DRbUNIXSocket, DRbURIOption, DRbUnknown, DRbUnknownError, ExtServ, ExtServManager, GW, GWIdConv, ThreadObject, TimerIdConv
Constant Summary collapse
- DRB_OBJECT_SPACE =
:nodoc:
This is an internal singleton instance. This must not be used by users.
DRbObjectSpace.new
- VERSION =
"2.2.3"
Class Method Summary collapse
-
.config ⇒ Object
Get the configuration of the current server.
-
.const_missing(name) ⇒ Object
DRb::WeakIdConv is deprecated since 2.2.1.
-
.current_server ⇒ Object
Get the ‘current’ server.
-
.fetch_server(uri) ⇒ Object
Retrieves the server with the given
uri. -
.front ⇒ Object
Get the front object of the current server.
-
.here?(uri) ⇒ Boolean
Is
urithe URI for the current local server?. -
.install_acl(acl) ⇒ Object
Set the default ACL to
acl. -
.install_id_conv(idconv) ⇒ Object
Set the default id conversion object.
-
.mutex ⇒ Object
:nodoc:.
-
.primary_server ⇒ Object
The primary local dRuby server.
-
.primary_server=(value) ⇒ Object
The primary local dRuby server.
-
.regist_server(server) ⇒ Object
Registers
serverwith DRb. -
.remove_server(server) ⇒ Object
Removes
serverfrom the list of registered servers. -
.start_service(uri = nil, front = nil, config = nil) ⇒ Object
Start a dRuby server locally.
-
.stop_service ⇒ Object
Stop the local dRuby server.
-
.thread ⇒ Object
Get the thread of the primary server.
-
.to_id(obj) ⇒ Object
Get a reference id for an object using the current server.
-
.to_obj(ref) ⇒ Object
Convert a reference into an object using the current server.
-
.uri ⇒ Object
Get the URI defining the local dRuby space.
Class Method Details
.config ⇒ Object
Get the configuration of the current server.
If there is no current server, this returns the default configuration. See #current_server and DRbServer::make_config.
1882 1883 1884 1885 1886 |
# File 'lib/drb/drb.rb', line 1882 def config current_server.config rescue DRbServer.make_config end |
.const_missing(name) ⇒ Object
DRb::WeakIdConv is deprecated since 2.2.1. You don’t need to use DRb::WeakIdConv instead of DRb::DRbIdConv. It’s the same class.
This file still exists for backward compatibility.
To use WeakIdConv:
DRb.start_service(nil, nil, {:idconv => DRb::WeakIdConv.new})
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/drb/weakidconv.rb', line 15 def self.const_missing(name) # :nodoc: case name when :WeakIdConv warn("DRb::WeakIdConv is deprecated. " + "You can use the DRb::DRbIdConv. " + "You don't need to use this.", uplevel: 1) const_set(:WeakIdConv, DRbIdConv) singleton_class.remove_method(:const_missing) DRbIdConv else super end end |
.current_server ⇒ Object
Get the ‘current’ server.
In the context of execution taking place within the main thread of a dRuby server (typically, as a result of a remote call on the server or one of its objects), the current server is that server. Otherwise, the current server is the primary server.
If the above rule fails to find a server, a DRbServerNotFound error is raised.
1839 1840 1841 1842 1843 1844 |
# File 'lib/drb/drb.rb', line 1839 def current_server drb = Thread.current['DRb'] server = (drb && drb['server']) ? drb['server'] : @primary_server raise DRbServerNotFound unless server return server end |
.fetch_server(uri) ⇒ Object
Retrieves the server with the given uri.
See also regist_server and remove_server.
1984 1985 1986 |
# File 'lib/drb/drb.rb', line 1984 def fetch_server(uri) @server[uri] end |
.front ⇒ Object
Get the front object of the current server.
This raises a DRbServerNotFound error if there is no current server. See #current_server.
1893 1894 1895 |
# File 'lib/drb/drb.rb', line 1893 def front current_server.front end |
.here?(uri) ⇒ Boolean
Is uri the URI for the current local server?
1872 1873 1874 1875 |
# File 'lib/drb/drb.rb', line 1872 def here?(uri) current_server.here?(uri) rescue false # (current_server.uri rescue nil) == uri end |
.install_acl(acl) ⇒ Object
Set the default ACL to acl.
See DRb::DRbServer.default_acl.
1938 1939 1940 |
# File 'lib/drb/drb.rb', line 1938 def install_acl(acl) DRbServer.default_acl(acl) end |
.install_id_conv(idconv) ⇒ Object
Set the default id conversion object.
This is expected to be an instance such as DRb::DRbIdConv that responds to #to_id and #to_obj that can convert objects to and from DRb references.
See DRbServer#default_id_conv.
1930 1931 1932 |
# File 'lib/drb/drb.rb', line 1930 def install_id_conv(idconv) DRbServer.default_id_conv(idconv) end |
.mutex ⇒ Object
:nodoc:
1944 1945 1946 |
# File 'lib/drb/drb.rb', line 1944 def mutex # :nodoc: @mutex end |
.primary_server ⇒ Object
The primary local dRuby server.
This is the server created by the #start_service call.
1826 1827 1828 |
# File 'lib/drb/drb.rb', line 1826 def primary_server @primary_server end |
.primary_server=(value) ⇒ Object
The primary local dRuby server.
This is the server created by the #start_service call.
1826 1827 1828 |
# File 'lib/drb/drb.rb', line 1826 def primary_server=(value) @primary_server = value end |
.regist_server(server) ⇒ Object
Registers server with DRb.
This is called when a new DRb::DRbServer is created.
If there is no primary server then server becomes the primary server.
Example:
require 'drb'
s = DRb::DRbServer.new # automatically calls regist_server
DRb.fetch_server s.uri #=> #<DRb::DRbServer:0x...>
1962 1963 1964 1965 1966 1967 |
# File 'lib/drb/drb.rb', line 1962 def regist_server(server) @server[server.uri] = server mutex.synchronize do @primary_server = server unless @primary_server end end |
.remove_server(server) ⇒ Object
Removes server from the list of registered servers.
1971 1972 1973 1974 1975 1976 1977 1978 |
# File 'lib/drb/drb.rb', line 1971 def remove_server(server) @server.delete(server.uri) mutex.synchronize do if @primary_server == server @primary_server = nil end end end |
.start_service(uri = nil, front = nil, config = nil) ⇒ Object
Start a dRuby server locally.
The new dRuby server will become the primary server, even if another server is currently the primary server.
uri is the URI for the server to bind to. If nil, the server will bind to random port on the default local host name and use the default dRuby protocol.
front is the server’s front object. This may be nil.
config is the configuration for the new server. This may be nil.
See DRbServer::new.
1818 1819 1820 |
# File 'lib/drb/drb.rb', line 1818 def start_service(uri=nil, front=nil, config=nil) @primary_server = DRbServer.new(uri, front, config) end |
.stop_service ⇒ Object
Stop the local dRuby server.
This operates on the primary server. If there is no primary server currently running, it is a noop.
1851 1852 1853 1854 |
# File 'lib/drb/drb.rb', line 1851 def stop_service @primary_server.stop_service if @primary_server @primary_server = nil end |
.thread ⇒ Object
Get the thread of the primary server.
This returns nil if there is no primary server. See #primary_server.
1919 1920 1921 |
# File 'lib/drb/drb.rb', line 1919 def thread @primary_server ? @primary_server.thread : nil end |
.to_id(obj) ⇒ Object
Get a reference id for an object using the current server.
This raises a DRbServerNotFound error if there is no current server. See #current_server.
1910 1911 1912 |
# File 'lib/drb/drb.rb', line 1910 def to_id(obj) current_server.to_id(obj) end |
.to_obj(ref) ⇒ Object
Convert a reference into an object using the current server.
This raises a DRbServerNotFound error if there is no current server. See #current_server.
1902 1903 1904 |
# File 'lib/drb/drb.rb', line 1902 def to_obj(ref) current_server.to_obj(ref) end |
.uri ⇒ Object
Get the URI defining the local dRuby space.
This is the URI of the current server. See #current_server.
1860 1861 1862 1863 1864 1865 1866 1867 1868 |
# File 'lib/drb/drb.rb', line 1860 def uri drb = Thread.current['DRb'] client = (drb && drb['client']) if client uri = client.uri return uri if uri end current_server.uri end |