2003-09-11 22:29:38 -04:00
|
|
|
#
|
2004-05-16 11:09:52 -04:00
|
|
|
# = drb/drb.rb
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
2004-05-16 11:09:52 -04:00
|
|
|
# Distributed Ruby: _dRuby_ version 2.0.4
|
|
|
|
#
|
|
|
|
# Copyright (c) 1999-2003 Masatoshi SEKI. You can redistribute it and/or
|
|
|
|
# modify it under the same terms as Ruby.
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
2007-11-19 13:30:18 -05:00
|
|
|
# Author:: Masatoshi SEKI
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
2007-11-19 13:30:18 -05:00
|
|
|
# Documentation:: William Webber (william@williamwebber.com)
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
|
|
|
# == Overview
|
|
|
|
#
|
|
|
|
# dRuby is a distributed object system for Ruby. It allows an object in one
|
|
|
|
# Ruby process to invoke methods on an object in another Ruby process on the
|
|
|
|
# same or a different machine.
|
|
|
|
#
|
|
|
|
# The Ruby standard library contains the core classes of the dRuby package.
|
|
|
|
# However, the full package also includes access control lists and the
|
2009-03-05 22:56:38 -05:00
|
|
|
# Rinda tuple-space distributed task management system, as well as a
|
2003-09-11 22:29:38 -04:00
|
|
|
# large number of samples. The full dRuby package can be downloaded from
|
|
|
|
# the dRuby home page (see *References*).
|
|
|
|
#
|
|
|
|
# For an introduction and examples of usage see the documentation to the
|
|
|
|
# DRb module.
|
|
|
|
#
|
|
|
|
# == References
|
|
|
|
#
|
|
|
|
# [http://www2a.biglobe.ne.jp/~seki/ruby/druby.html]
|
|
|
|
# The dRuby home page, in Japanese. Contains the full dRuby package
|
|
|
|
# and links to other Japanese-language sources.
|
|
|
|
#
|
|
|
|
# [http://www2a.biglobe.ne.jp/~seki/ruby/druby.en.html]
|
|
|
|
# The English version of the dRuby home page.
|
|
|
|
#
|
2012-06-13 17:21:11 -04:00
|
|
|
# [http://pragprog.com/book/sidruby/the-druby-book]
|
|
|
|
# The dRuby Book: Distributed and Parallel Computing with Ruby
|
|
|
|
# by Masatoshi Seki and Makoto Inoue
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
2012-06-13 17:18:29 -04:00
|
|
|
# [http://www.ruby-doc.org/docs/ProgrammingRuby/html/ospace.html]
|
2003-09-11 22:29:38 -04:00
|
|
|
# The chapter from *Programming* *Ruby* by Dave Thomas and Andy Hunt
|
|
|
|
# which discusses dRuby.
|
|
|
|
#
|
|
|
|
# [http://www.clio.ne.jp/home/web-i31s/Flotuard/Ruby/PRC2K_seki/dRuby.en.html]
|
|
|
|
# Translation of presentation on Ruby by Masatoshi Seki.
|
2003-06-18 11:45:12 -04:00
|
|
|
|
|
|
|
require 'socket'
|
|
|
|
require 'thread'
|
|
|
|
require 'fcntl'
|
2004-02-26 10:56:40 -05:00
|
|
|
require 'drb/eq'
|
2003-06-18 11:45:12 -04:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
|
|
|
# == 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.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# 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.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-16 11:09:52 -04:00
|
|
|
# == Examples of usage
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
|
|
|
# For more dRuby samples, see the +samples+ directory in the full
|
|
|
|
# dRuby distribution.
|
|
|
|
#
|
2004-05-16 11:09:52 -04:00
|
|
|
# === dRuby in client/server mode
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
|
|
|
# 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
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# require 'drb/drb'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# # The URI for the server to connect to
|
2009-03-05 22:56:38 -05:00
|
|
|
# URI="druby://localhost:8787"
|
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# class TimeServer
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# def get_current_time
|
|
|
|
# return Time.now
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# # The object that handles requests on the server
|
|
|
|
# FRONT_OBJECT=TimeServer.new
|
|
|
|
#
|
|
|
|
# $SAFE = 1 # disable eval() and friends
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# DRb.start_service(URI, FRONT_OBJECT)
|
|
|
|
# # Wait for the drb server thread to finish before exiting.
|
|
|
|
# DRb.thread.join
|
|
|
|
#
|
|
|
|
# ==== Client code
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# require 'drb/drb'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# # 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.
|
2014-01-31 16:40:18 -05:00
|
|
|
# #
|
|
|
|
# # Note: this must be called at least once per process to take any effect.
|
|
|
|
# # This is particularly important if your application forks.
|
2003-09-11 22:29:38 -04:00
|
|
|
# DRb.start_service
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# timeserver = DRbObject.new_with_uri(SERVER_URI)
|
2009-03-05 22:56:38 -05:00
|
|
|
# puts timeserver.get_current_time
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
|
|
|
# === 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,
|
2009-03-05 22:56:38 -05:00
|
|
|
# where methods can be invoked upon them. These methods are
|
2003-09-11 22:29:38 -04:00
|
|
|
# executed in the server process.
|
|
|
|
#
|
|
|
|
# ==== Server code
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# require 'drb/drb'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# URI="druby://localhost:8787"
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# class Logger
|
|
|
|
#
|
|
|
|
# # Make dRuby send Logger instances as dRuby references,
|
|
|
|
# # not copies.
|
|
|
|
# include DRb::DRbUndumped
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# def initialize(n, fname)
|
|
|
|
# @name = n
|
|
|
|
# @filename = fname
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# def log(message)
|
|
|
|
# File.open(@filename, "a") do |f|
|
|
|
|
# f.puts("#{Time.now}: #{@name}: #{message}")
|
|
|
|
# end
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# # 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
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# def initialize(bdir)
|
|
|
|
# @basedir = bdir
|
|
|
|
# @loggers = {}
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# def get_logger(name)
|
|
|
|
# if !@loggers.has_key? name
|
|
|
|
# # make the filename safe, then declare it to be so
|
2013-11-09 18:13:35 -05:00
|
|
|
# fname = name.gsub(/[.\/\\\:]/, "_").untaint
|
2003-09-11 22:29:38 -04:00
|
|
|
# @loggers[name] = Logger.new(name, @basedir + "/" + fname)
|
|
|
|
# end
|
|
|
|
# return @loggers[name]
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# FRONT_OBJECT=LoggerFactory.new("/tmp/dlog")
|
|
|
|
#
|
|
|
|
# $SAFE = 1 # disable eval() and friends
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# DRb.start_service(URI, FRONT_OBJECT)
|
|
|
|
# DRb.thread.join
|
|
|
|
#
|
|
|
|
# ==== Client code
|
|
|
|
#
|
|
|
|
# require 'drb/drb'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# SERVER_URI="druby://localhost:8787"
|
|
|
|
#
|
|
|
|
# DRb.start_service
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# log_service=DRbObject.new_with_uri(SERVER_URI)
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# ["loga", "logb", "logc"].each do |logname|
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# logger=log_service.get_logger(logname)
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# logger.log("Hello, world!")
|
|
|
|
# logger.log("Goodbye, world!")
|
|
|
|
# logger.log("=== EOT ===")
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# 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
|
2009-03-05 22:56:38 -05:00
|
|
|
# DRbServer should generally be run with $SAFE set to at least
|
|
|
|
# level 1. This will disable eval() and related calls on strings
|
|
|
|
# passed across the wire. The sample usage code given above follows
|
2003-09-11 22:29:38 -04:00
|
|
|
# this practice.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2003-06-18 11:45:12 -04:00
|
|
|
module DRb
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# Superclass of all errors raised in the DRb module.
|
2003-06-18 11:45:12 -04:00
|
|
|
class DRbError < RuntimeError; end
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# Error raised when an error occurs on the underlying communication
|
|
|
|
# protocol.
|
2003-11-28 03:30:01 -05:00
|
|
|
class DRbConnError < DRbError; end
|
2003-06-18 11:45:12 -04:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Class responsible for converting between an object and its id.
|
|
|
|
#
|
|
|
|
# This, the default implementation, uses an object's local ObjectSpace
|
|
|
|
# __id__ as its id. This means that an object's identification over
|
2009-03-05 22:56:38 -05:00
|
|
|
# drb remains valid only while that object instance remains alive
|
2003-09-11 22:29:38 -04:00
|
|
|
# within the server runtime.
|
|
|
|
#
|
|
|
|
# For alternative mechanisms, see DRb::TimerIdConv in rdb/timeridconv.rb
|
|
|
|
# and DRbNameIdConv in sample/name.rb in the full drb distribution.
|
2003-06-18 11:45:12 -04:00
|
|
|
class DRbIdConv
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# Convert an object reference id to an object.
|
|
|
|
#
|
|
|
|
# This implementation looks up the reference id in the local object
|
|
|
|
# space and returns the object it refers to.
|
2003-06-18 11:45:12 -04:00
|
|
|
def to_obj(ref)
|
|
|
|
ObjectSpace._id2ref(ref)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Convert an object into a reference id.
|
|
|
|
#
|
|
|
|
# This implementation returns the object's __id__ in the local
|
|
|
|
# object space.
|
2003-06-18 11:45:12 -04:00
|
|
|
def to_id(obj)
|
|
|
|
obj.nil? ? nil : obj.__id__
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Mixin module making an object undumpable or unmarshallable.
|
|
|
|
#
|
|
|
|
# If an object which includes this module is returned by method
|
|
|
|
# called over drb, then the object remains in the server space
|
|
|
|
# and a reference to the object is returned, rather than the
|
|
|
|
# object being marshalled and moved into the client space.
|
2009-03-05 22:56:38 -05:00
|
|
|
module DRbUndumped
|
2003-09-11 22:29:38 -04:00
|
|
|
def _dump(dummy) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
raise TypeError, 'can\'t dump'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Error raised by the DRb module when an attempt is made to refer to
|
|
|
|
# the context's current drb server but the context does not have one.
|
|
|
|
# See #current_server.
|
2003-06-18 11:45:12 -04:00
|
|
|
class DRbServerNotFound < DRbError; end
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# Error raised by the DRbProtocol module when it cannot find any
|
|
|
|
# protocol implementation support the scheme specified in a URI.
|
2003-06-18 11:45:12 -04:00
|
|
|
class DRbBadURI < DRbError; end
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# Error raised by a dRuby protocol when it doesn't support the
|
|
|
|
# scheme specified in a URI. See DRb::DRbProtocol.
|
2003-06-18 11:45:12 -04:00
|
|
|
class DRbBadScheme < DRbError; end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# An exception wrapping a DRb::DRbUnknown object
|
2003-06-18 11:45:12 -04:00
|
|
|
class DRbUnknownError < DRbError
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# Create a new DRbUnknownError for the DRb::DRbUnknown object +unknown+
|
2003-06-18 11:45:12 -04:00
|
|
|
def initialize(unknown)
|
|
|
|
@unknown = unknown
|
|
|
|
super(unknown.name)
|
|
|
|
end
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# Get the wrapped DRb::DRbUnknown object.
|
2003-06-18 11:45:12 -04:00
|
|
|
attr_reader :unknown
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def self._load(s) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
Marshal::load(s)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def _dump(lv) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
Marshal::dump(@unknown)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-19 18:07:42 -04:00
|
|
|
# An exception wrapping an error object
|
2004-12-11 07:12:17 -05:00
|
|
|
class DRbRemoteError < DRbError
|
2013-01-24 22:25:39 -05:00
|
|
|
|
|
|
|
# Creates a new remote error that wraps the Exception +error+
|
2004-12-11 07:12:17 -05:00
|
|
|
def initialize(error)
|
|
|
|
@reason = error.class.to_s
|
|
|
|
super("#{error.message} (#{error.class})")
|
|
|
|
set_backtrace(error.backtrace)
|
|
|
|
end
|
2006-06-19 18:07:42 -04:00
|
|
|
|
|
|
|
# the class of the error, as a string.
|
2004-12-11 07:12:17 -05:00
|
|
|
attr_reader :reason
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Class wrapping a marshalled object whose type is unknown locally.
|
|
|
|
#
|
|
|
|
# If an object is returned by a method invoked over drb, but the
|
|
|
|
# class of the object is unknown in the client namespace, or
|
|
|
|
# the object is a constant unknown in the client namespace, then
|
|
|
|
# the still-marshalled object is returned wrapped in a DRbUnknown instance.
|
|
|
|
#
|
|
|
|
# If this object is passed as an argument to a method invoked over
|
|
|
|
# drb, then the wrapped object is passed instead.
|
|
|
|
#
|
|
|
|
# The class or constant name of the object can be read from the
|
|
|
|
# +name+ attribute. The marshalled object is held in the +buf+
|
|
|
|
# attribute.
|
2003-06-18 11:45:12 -04:00
|
|
|
class DRbUnknown
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Create a new DRbUnknown object.
|
|
|
|
#
|
|
|
|
# +buf+ is a string containing a marshalled object that could not
|
2009-03-05 22:56:38 -05:00
|
|
|
# be unmarshalled. +err+ is the error message that was raised
|
2003-09-11 22:29:38 -04:00
|
|
|
# when the unmarshalling failed. It is used to determine the
|
|
|
|
# name of the unmarshalled object.
|
2003-06-18 11:45:12 -04:00
|
|
|
def initialize(err, buf)
|
2004-04-15 23:38:53 -04:00
|
|
|
case err.to_s
|
2003-06-18 11:45:12 -04:00
|
|
|
when /uninitialized constant (\S+)/
|
2011-05-18 17:19:18 -04:00
|
|
|
@name = $1
|
2003-06-18 11:45:12 -04:00
|
|
|
when /undefined class\/module (\S+)/
|
2011-05-18 17:19:18 -04:00
|
|
|
@name = $1
|
2003-06-18 11:45:12 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
@name = nil
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
@buf = buf
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# The name of the unknown thing.
|
|
|
|
#
|
|
|
|
# Class name for unknown objects; variable name for unknown
|
|
|
|
# constants.
|
|
|
|
attr_reader :name
|
|
|
|
|
|
|
|
# Buffer contained the marshalled, unknown object.
|
|
|
|
attr_reader :buf
|
|
|
|
|
|
|
|
def self._load(s) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
begin
|
2011-05-18 17:19:18 -04:00
|
|
|
Marshal::load(s)
|
2003-06-18 11:45:12 -04:00
|
|
|
rescue NameError, ArgumentError
|
2011-05-18 17:19:18 -04:00
|
|
|
DRbUnknown.new($!, s)
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def _dump(lv) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
@buf
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Attempt to load the wrapped marshalled object again.
|
|
|
|
#
|
|
|
|
# If the class of the object is now known locally, the object
|
2009-03-05 22:56:38 -05:00
|
|
|
# will be unmarshalled and returned. Otherwise, a new
|
2003-09-11 22:29:38 -04:00
|
|
|
# but identical DRbUnknown object will be returned.
|
2003-06-18 11:45:12 -04:00
|
|
|
def reload
|
|
|
|
self.class._load(@buf)
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Create a DRbUnknownError exception containing this object.
|
2003-06-18 11:45:12 -04:00
|
|
|
def exception
|
|
|
|
DRbUnknownError.new(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-24 22:25:39 -05:00
|
|
|
# An Array wrapper that can be sent to another server via DRb.
|
|
|
|
#
|
|
|
|
# All entries in the array will be dumped or be references that point to
|
|
|
|
# the local server.
|
|
|
|
|
2003-10-30 09:43:03 -05:00
|
|
|
class DRbArray
|
2013-01-24 22:25:39 -05:00
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Creates a new DRbArray that either dumps or wraps all the items in the
|
|
|
|
# Array +ary+ so they can be loaded by a remote DRb server.
|
2013-01-24 22:25:39 -05:00
|
|
|
|
2003-10-30 09:43:03 -05:00
|
|
|
def initialize(ary)
|
2009-03-05 22:56:38 -05:00
|
|
|
@ary = ary.collect { |obj|
|
2011-05-18 17:19:18 -04:00
|
|
|
if obj.kind_of? DRbUndumped
|
|
|
|
DRbObject.new(obj)
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Marshal.dump(obj)
|
|
|
|
obj
|
|
|
|
rescue
|
|
|
|
DRbObject.new(obj)
|
|
|
|
end
|
|
|
|
end
|
2003-10-30 09:43:03 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-01-24 22:25:39 -05:00
|
|
|
def self._load(s) # :nodoc:
|
2003-10-30 09:43:03 -05:00
|
|
|
Marshal::load(s)
|
|
|
|
end
|
|
|
|
|
2013-01-24 22:25:39 -05:00
|
|
|
def _dump(lv) # :nodoc:
|
2003-10-30 09:43:03 -05:00
|
|
|
Marshal.dump(@ary)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Handler for sending and receiving drb messages.
|
|
|
|
#
|
|
|
|
# This takes care of the low-level marshalling and unmarshalling
|
|
|
|
# of drb requests and responses sent over the wire between server
|
|
|
|
# and client. This relieves the implementor of a new drb
|
|
|
|
# protocol layer with having to deal with these details.
|
|
|
|
#
|
|
|
|
# The user does not have to directly deal with this object in
|
|
|
|
# normal use.
|
2003-06-18 11:45:12 -04:00
|
|
|
class DRbMessage
|
2003-09-11 22:29:38 -04:00
|
|
|
def initialize(config) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
@load_limit = config[:load_limit]
|
|
|
|
@argc_limit = config[:argc_limit]
|
|
|
|
end
|
|
|
|
|
2004-12-11 07:12:17 -05:00
|
|
|
def dump(obj, error=false) # :nodoc:
|
|
|
|
obj = make_proxy(obj, error) if obj.kind_of? DRbUndumped
|
2003-06-18 11:45:12 -04:00
|
|
|
begin
|
2011-05-18 17:19:18 -04:00
|
|
|
str = Marshal::dump(obj)
|
2003-06-18 11:45:12 -04:00
|
|
|
rescue
|
2011-05-18 17:19:18 -04:00
|
|
|
str = Marshal::dump(make_proxy(obj, error))
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
[str.size].pack('N') + str
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def load(soc) # :nodoc:
|
2003-11-28 02:45:27 -05:00
|
|
|
begin
|
2011-05-18 20:07:25 -04:00
|
|
|
sz = soc.read(4) # sizeof (N)
|
2003-11-28 02:45:27 -05:00
|
|
|
rescue
|
2003-11-28 03:30:01 -05:00
|
|
|
raise(DRbConnError, $!.message, $!.backtrace)
|
2003-11-28 02:45:27 -05:00
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
raise(DRbConnError, 'connection closed') if sz.nil?
|
|
|
|
raise(DRbConnError, 'premature header') if sz.size < 4
|
|
|
|
sz = sz.unpack('N')[0]
|
|
|
|
raise(DRbConnError, "too large packet #{sz}") if @load_limit < sz
|
2003-11-28 02:45:27 -05:00
|
|
|
begin
|
|
|
|
str = soc.read(sz)
|
|
|
|
rescue
|
2003-11-28 03:30:01 -05:00
|
|
|
raise(DRbConnError, $!.message, $!.backtrace)
|
2003-11-28 02:45:27 -05:00
|
|
|
end
|
2004-07-28 05:05:08 -04:00
|
|
|
raise(DRbConnError, 'connection closed') if str.nil?
|
2003-06-18 11:45:12 -04:00
|
|
|
raise(DRbConnError, 'premature marshal format(can\'t read)') if str.size < sz
|
2007-11-19 13:30:18 -05:00
|
|
|
DRb.mutex.synchronize do
|
2005-04-11 10:48:50 -04:00
|
|
|
begin
|
|
|
|
save = Thread.current[:drb_untaint]
|
|
|
|
Thread.current[:drb_untaint] = []
|
|
|
|
Marshal::load(str)
|
|
|
|
rescue NameError, ArgumentError
|
|
|
|
DRbUnknown.new($!, str)
|
|
|
|
ensure
|
|
|
|
Thread.current[:drb_untaint].each do |x|
|
|
|
|
x.untaint
|
|
|
|
end
|
|
|
|
Thread.current[:drb_untaint] = save
|
|
|
|
end
|
2007-11-19 13:30:18 -05:00
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def send_request(stream, ref, msg_id, arg, b) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
ary = []
|
|
|
|
ary.push(dump(ref.__drbref))
|
|
|
|
ary.push(dump(msg_id.id2name))
|
|
|
|
ary.push(dump(arg.length))
|
|
|
|
arg.each do |e|
|
2011-05-18 17:19:18 -04:00
|
|
|
ary.push(dump(e))
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
ary.push(dump(b))
|
|
|
|
stream.write(ary.join(''))
|
2003-12-04 07:50:13 -05:00
|
|
|
rescue
|
|
|
|
raise(DRbConnError, $!.message, $!.backtrace)
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def recv_request(stream) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
ref = load(stream)
|
|
|
|
ro = DRb.to_obj(ref)
|
|
|
|
msg = load(stream)
|
|
|
|
argc = load(stream)
|
2010-06-14 04:52:30 -04:00
|
|
|
raise(DRbConnError, "too many arguments") if @argc_limit < argc
|
2006-06-23 10:39:01 -04:00
|
|
|
argv = Array.new(argc, nil)
|
2003-06-18 11:45:12 -04:00
|
|
|
argc.times do |n|
|
2011-05-18 17:19:18 -04:00
|
|
|
argv[n] = load(stream)
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
block = load(stream)
|
|
|
|
return ro, msg, argv, block
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def send_reply(stream, succ, result) # :nodoc:
|
2004-12-11 07:12:17 -05:00
|
|
|
stream.write(dump(succ) + dump(result, !succ))
|
2003-12-04 07:50:13 -05:00
|
|
|
rescue
|
|
|
|
raise(DRbConnError, $!.message, $!.backtrace)
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def recv_reply(stream) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
succ = load(stream)
|
|
|
|
result = load(stream)
|
|
|
|
[succ, result]
|
|
|
|
end
|
2004-12-11 07:12:17 -05:00
|
|
|
|
|
|
|
private
|
2013-01-24 22:25:39 -05:00
|
|
|
def make_proxy(obj, error=false) # :nodoc:
|
2004-12-11 07:12:17 -05:00
|
|
|
if error
|
|
|
|
DRbRemoteError.new(obj)
|
|
|
|
else
|
|
|
|
DRbObject.new(obj)
|
|
|
|
end
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Module managing the underlying network protocol(s) used by drb.
|
|
|
|
#
|
|
|
|
# By default, drb uses the DRbTCPSocket protocol. Other protocols
|
|
|
|
# can be defined. A protocol must define the following class methods:
|
|
|
|
#
|
|
|
|
# [open(uri, config)] Open a client connection to the server at +uri+,
|
|
|
|
# using configuration +config+. Return a protocol
|
|
|
|
# instance for this connection.
|
|
|
|
# [open_server(uri, config)] Open a server listening at +uri+,
|
|
|
|
# using configuration +config+. Return a
|
|
|
|
# protocol instance for this listener.
|
|
|
|
# [uri_option(uri, config)] Take a URI, possibly containing an option
|
2009-03-05 22:56:38 -05:00
|
|
|
# component (e.g. a trailing '?param=val'),
|
2003-09-11 22:29:38 -04:00
|
|
|
# and return a [uri, option] tuple.
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# All of these methods should raise a DRbBadScheme error if the URI
|
2003-09-11 22:29:38 -04:00
|
|
|
# does not identify the protocol they support (e.g. "druby:" for
|
|
|
|
# the standard Ruby protocol). This is how the DRbProtocol module,
|
|
|
|
# given a URI, determines which protocol implementation serves that
|
|
|
|
# protocol.
|
|
|
|
#
|
|
|
|
# The protocol instance returned by #open_server must have the
|
|
|
|
# following methods:
|
|
|
|
#
|
|
|
|
# [accept] Accept a new connection to the server. Returns a protocol
|
|
|
|
# instance capable of communicating with the client.
|
|
|
|
# [close] Close the server connection.
|
|
|
|
# [uri] Get the URI for this server.
|
|
|
|
#
|
|
|
|
# The protocol instance returned by #open must have the following methods:
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# [send_request (ref, msg_id, arg, b)]
|
2003-09-11 22:29:38 -04:00
|
|
|
# Send a request to +ref+ with the given message id and arguments.
|
|
|
|
# This is most easily implemented by calling DRbMessage.send_request,
|
|
|
|
# providing a stream that sits on top of the current protocol.
|
|
|
|
# [recv_reply]
|
|
|
|
# Receive a reply from the server and return it as a [success-boolean,
|
|
|
|
# reply-value] pair. This is most easily implemented by calling
|
2009-03-05 22:56:38 -05:00
|
|
|
# DRb.recv_reply, providing a stream that sits on top of the
|
2003-09-11 22:29:38 -04:00
|
|
|
# current protocol.
|
|
|
|
# [alive?]
|
|
|
|
# Is this connection still alive?
|
|
|
|
# [close]
|
|
|
|
# Close this connection.
|
|
|
|
#
|
|
|
|
# The protocol instance returned by #open_server().accept() must have
|
|
|
|
# the following methods:
|
|
|
|
#
|
|
|
|
# [recv_request]
|
|
|
|
# Receive a request from the client and return a [object, message,
|
|
|
|
# args, block] tuple. This is most easily implemented by calling
|
|
|
|
# DRbMessage.recv_request, providing a stream that sits on top of
|
|
|
|
# the current protocol.
|
|
|
|
# [send_reply(succ, result)]
|
|
|
|
# Send a reply to the client. This is most easily implemented
|
|
|
|
# by calling DRbMessage.send_reply, providing a stream that sits
|
|
|
|
# on top of the current protocol.
|
|
|
|
# [close]
|
|
|
|
# Close this connection.
|
|
|
|
#
|
|
|
|
# A new protocol is registered with the DRbProtocol module using
|
|
|
|
# the add_protocol method.
|
|
|
|
#
|
|
|
|
# For examples of other protocols, see DRbUNIXSocket in drb/unix.rb,
|
|
|
|
# and HTTP0 in sample/http0.rb and sample/http0serv.rb in the full
|
|
|
|
# drb distribution.
|
2003-06-18 11:45:12 -04:00
|
|
|
module DRbProtocol
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# Add a new protocol to the DRbProtocol module.
|
2003-06-18 11:45:12 -04:00
|
|
|
def add_protocol(prot)
|
|
|
|
@protocol.push(prot)
|
|
|
|
end
|
2003-09-11 22:29:38 -04:00
|
|
|
module_function :add_protocol
|
2003-06-18 11:45:12 -04:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Open a client connection to +uri+ with the configuration +config+.
|
|
|
|
#
|
|
|
|
# The DRbProtocol module asks each registered protocol in turn to
|
|
|
|
# try to open the URI. Each protocol signals that it does not handle that
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-18 19:19:47 -04:00
|
|
|
# URI by raising a DRbBadScheme error. If no protocol recognises the
|
2003-09-11 22:29:38 -04:00
|
|
|
# URI, then a DRbBadURI error is raised. If a protocol accepts the
|
|
|
|
# URI, but an error occurs in opening it, a DRbConnError is raised.
|
2009-03-05 22:56:38 -05:00
|
|
|
def open(uri, config, first=true)
|
2003-06-18 11:45:12 -04:00
|
|
|
@protocol.each do |prot|
|
2011-05-18 17:19:18 -04:00
|
|
|
begin
|
|
|
|
return prot.open(uri, config)
|
|
|
|
rescue DRbBadScheme
|
|
|
|
rescue DRbConnError
|
|
|
|
raise($!)
|
|
|
|
rescue
|
|
|
|
raise(DRbConnError, "#{uri} - #{$!.inspect}")
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
if first && (config[:auto_load] != false)
|
2011-05-18 17:19:18 -04:00
|
|
|
auto_load(uri, config)
|
|
|
|
return open(uri, config, false)
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
raise DRbBadURI, 'can\'t parse uri:' + uri
|
|
|
|
end
|
2003-09-11 22:29:38 -04:00
|
|
|
module_function :open
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
# Open a server listening for connections at +uri+ with
|
2003-09-11 22:29:38 -04:00
|
|
|
# configuration +config+.
|
|
|
|
#
|
|
|
|
# The DRbProtocol module asks each registered protocol in turn to
|
2009-03-05 22:56:38 -05:00
|
|
|
# try to open a server at the URI. Each protocol signals that it does
|
|
|
|
# not handle that URI by raising a DRbBadScheme error. If no protocol
|
|
|
|
# recognises the URI, then a DRbBadURI error is raised. If a protocol
|
|
|
|
# accepts the URI, but an error occurs in opening it, the underlying
|
2003-09-11 22:29:38 -04:00
|
|
|
# error is passed on to the caller.
|
2003-06-18 11:45:12 -04:00
|
|
|
def open_server(uri, config, first=true)
|
|
|
|
@protocol.each do |prot|
|
2011-05-18 17:19:18 -04:00
|
|
|
begin
|
|
|
|
return prot.open_server(uri, config)
|
|
|
|
rescue DRbBadScheme
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
if first && (config[:auto_load] != false)
|
2011-05-18 17:19:18 -04:00
|
|
|
auto_load(uri, config)
|
|
|
|
return open_server(uri, config, false)
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
raise DRbBadURI, 'can\'t parse uri:' + uri
|
|
|
|
end
|
2003-09-11 22:29:38 -04:00
|
|
|
module_function :open_server
|
2003-06-18 11:45:12 -04:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Parse +uri+ into a [uri, option] pair.
|
|
|
|
#
|
|
|
|
# The DRbProtocol module asks each registered protocol in turn to
|
|
|
|
# try to parse the URI. Each protocol signals that it does not handle that
|
|
|
|
# URI by raising a DRbBadScheme error. If no protocol recognises the
|
2009-03-05 22:56:38 -05:00
|
|
|
# URI, then a DRbBadURI error is raised.
|
2003-06-18 11:45:12 -04:00
|
|
|
def uri_option(uri, config, first=true)
|
|
|
|
@protocol.each do |prot|
|
2011-05-18 17:19:18 -04:00
|
|
|
begin
|
|
|
|
uri, opt = prot.uri_option(uri, config)
|
|
|
|
# opt = nil if opt == ''
|
|
|
|
return uri, opt
|
|
|
|
rescue DRbBadScheme
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
if first && (config[:auto_load] != false)
|
2011-05-18 17:19:18 -04:00
|
|
|
auto_load(uri, config)
|
2003-06-18 11:45:12 -04:00
|
|
|
return uri_option(uri, config, false)
|
|
|
|
end
|
|
|
|
raise DRbBadURI, 'can\'t parse uri:' + uri
|
|
|
|
end
|
2003-09-11 22:29:38 -04:00
|
|
|
module_function :uri_option
|
2003-06-18 11:45:12 -04:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def auto_load(uri, config) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
if uri =~ /^drb([a-z0-9]+):/
|
2011-05-18 17:19:18 -04:00
|
|
|
require("drb/#{$1}") rescue nil
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
2003-09-11 22:29:38 -04:00
|
|
|
module_function :auto_load
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
2013-01-24 22:25:39 -05:00
|
|
|
# The default drb protocol which communicates over a TCP socket.
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
2013-01-24 22:25:39 -05:00
|
|
|
# The DRb TCP protocol URI looks like:
|
|
|
|
# <code>druby://<host>:<port>?<option></code>. The option is optional.
|
|
|
|
|
2003-06-18 11:45:12 -04:00
|
|
|
class DRbTCPSocket
|
2013-01-24 22:25:39 -05:00
|
|
|
# :stopdoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
private
|
|
|
|
def self.parse_uri(uri)
|
|
|
|
if uri =~ /^druby:\/\/(.*?):(\d+)(\?(.*))?$/
|
2011-05-18 17:19:18 -04:00
|
|
|
host = $1
|
|
|
|
port = $2.to_i
|
|
|
|
option = $4
|
|
|
|
[host, port, option]
|
2003-06-18 11:45:12 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
raise(DRbBadScheme, uri) unless uri =~ /^druby:/
|
2011-05-18 20:07:25 -04:00
|
|
|
raise(DRbBadURI, 'can\'t parse uri:' + uri)
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
public
|
2003-09-11 22:29:38 -04:00
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Open a client connection to +uri+ (DRb URI string) using configuration
|
|
|
|
# +config+.
|
|
|
|
#
|
|
|
|
# This can raise DRb::DRbBadScheme or DRb::DRbBadURI if +uri+ is not for a
|
|
|
|
# recognized protocol. See DRb::DRbServer.new for information on built-in
|
|
|
|
# URI protocols.
|
2003-06-18 11:45:12 -04:00
|
|
|
def self.open(uri, config)
|
2010-07-11 12:59:10 -04:00
|
|
|
host, port, = parse_uri(uri)
|
2003-06-18 11:45:12 -04:00
|
|
|
host.untaint
|
|
|
|
port.untaint
|
|
|
|
soc = TCPSocket.open(host, port)
|
|
|
|
self.new(uri, soc, config)
|
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Returns the hostname of this server
|
2004-12-15 10:46:53 -05:00
|
|
|
def self.getservername
|
|
|
|
host = Socket::gethostname
|
|
|
|
begin
|
|
|
|
Socket::gethostbyname(host)[0]
|
|
|
|
rescue
|
2007-11-19 13:30:18 -05:00
|
|
|
'localhost'
|
2004-12-15 10:46:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# For the families available for +host+, returns a TCPServer on +port+.
|
|
|
|
# If +port+ is 0 the first available port is used. IPv4 servers are
|
|
|
|
# preferred over IPv6 servers.
|
2004-12-15 10:46:53 -05:00
|
|
|
def self.open_server_inaddr_any(host, port)
|
2009-03-05 22:56:38 -05:00
|
|
|
infos = Socket::getaddrinfo(host, nil,
|
2004-12-15 10:46:53 -05:00
|
|
|
Socket::AF_UNSPEC,
|
2009-03-05 22:56:38 -05:00
|
|
|
Socket::SOCK_STREAM,
|
2004-12-15 10:46:53 -05:00
|
|
|
0,
|
|
|
|
Socket::AI_PASSIVE)
|
2009-03-17 13:43:48 -04:00
|
|
|
families = Hash[*infos.collect { |af, *_| af }.uniq.zip([]).flatten]
|
|
|
|
return TCPServer.open('0.0.0.0', port) if families.has_key?('AF_INET')
|
|
|
|
return TCPServer.open('::', port) if families.has_key?('AF_INET6')
|
|
|
|
return TCPServer.open(port)
|
2013-01-24 22:25:39 -05:00
|
|
|
# :stopdoc:
|
2004-12-15 10:46:53 -05:00
|
|
|
end
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
# Open a server listening for connections at +uri+ using
|
2003-09-11 22:29:38 -04:00
|
|
|
# configuration +config+.
|
2003-06-18 11:45:12 -04:00
|
|
|
def self.open_server(uri, config)
|
|
|
|
uri = 'druby://:0' unless uri
|
2010-07-11 12:59:10 -04:00
|
|
|
host, port, _ = parse_uri(uri)
|
2007-11-19 13:30:18 -05:00
|
|
|
config = {:tcp_original_host => host}.update(config)
|
2003-06-18 11:45:12 -04:00
|
|
|
if host.size == 0
|
2004-12-15 10:46:53 -05:00
|
|
|
host = getservername
|
|
|
|
soc = open_server_inaddr_any(host, port)
|
2003-06-18 11:45:12 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
soc = TCPServer.open(host, port)
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
port = soc.addr[1] if port == 0
|
2007-11-19 13:30:18 -05:00
|
|
|
config[:tcp_port] = port
|
2003-06-18 11:45:12 -04:00
|
|
|
uri = "druby://#{host}:#{port}"
|
|
|
|
self.new(uri, soc, config)
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Parse +uri+ into a [uri, option] pair.
|
2003-06-18 11:45:12 -04:00
|
|
|
def self.uri_option(uri, config)
|
|
|
|
host, port, option = parse_uri(uri)
|
|
|
|
return "druby://#{host}:#{port}", option
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Create a new DRbTCPSocket instance.
|
|
|
|
#
|
|
|
|
# +uri+ is the URI we are connected to.
|
|
|
|
# +soc+ is the tcp socket we are bound to. +config+ is our
|
|
|
|
# configuration.
|
2003-06-18 11:45:12 -04:00
|
|
|
def initialize(uri, soc, config={})
|
|
|
|
@uri = uri
|
|
|
|
@socket = soc
|
|
|
|
@config = config
|
|
|
|
@acl = config[:tcp_acl]
|
|
|
|
@msg = DRbMessage.new(config)
|
|
|
|
set_sockopt(@socket)
|
|
|
|
end
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# Get the URI that we are connected to.
|
2003-06-18 11:45:12 -04:00
|
|
|
attr_reader :uri
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Get the address of our TCP peer (the other end of the socket
|
|
|
|
# we are bound to.
|
2003-06-18 11:45:12 -04:00
|
|
|
def peeraddr
|
|
|
|
@socket.peeraddr
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Get the socket.
|
2003-06-18 11:45:12 -04:00
|
|
|
def stream; @socket; end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# On the client side, send a request to the server.
|
2003-06-18 11:45:12 -04:00
|
|
|
def send_request(ref, msg_id, arg, b)
|
|
|
|
@msg.send_request(stream, ref, msg_id, arg, b)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# On the server side, receive a request from the client.
|
2003-06-18 11:45:12 -04:00
|
|
|
def recv_request
|
|
|
|
@msg.recv_request(stream)
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# On the server side, send a reply to the client.
|
2003-06-18 11:45:12 -04:00
|
|
|
def send_reply(succ, result)
|
|
|
|
@msg.send_reply(stream, succ, result)
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# On the client side, receive a reply from the server.
|
2003-06-18 11:45:12 -04:00
|
|
|
def recv_reply
|
|
|
|
@msg.recv_reply(stream)
|
|
|
|
end
|
|
|
|
|
|
|
|
public
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# Close the connection.
|
|
|
|
#
|
|
|
|
# If this is an instance returned by #open_server, then this stops
|
|
|
|
# listening for new connections altogether. If this is an instance
|
|
|
|
# returned by #open or by #accept, then it closes this particular
|
|
|
|
# client-server session.
|
2003-06-18 11:45:12 -04:00
|
|
|
def close
|
|
|
|
if @socket
|
2011-05-18 17:19:18 -04:00
|
|
|
@socket.close
|
|
|
|
@socket = nil
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
|
|
# On the server side, for an instance returned by #open_server,
|
2003-09-11 22:29:38 -04:00
|
|
|
# accept a client connection and return a new instance to handle
|
|
|
|
# the server's side of this client-server session.
|
2003-06-18 11:45:12 -04:00
|
|
|
def accept
|
|
|
|
while true
|
2011-05-18 17:19:18 -04:00
|
|
|
s = @socket.accept
|
|
|
|
break if (@acl ? @acl.allow_socket?(s) : true)
|
|
|
|
s.close
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
2007-11-19 13:30:18 -05:00
|
|
|
if @config[:tcp_original_host].to_s.size == 0
|
|
|
|
uri = "druby://#{s.addr[3]}:#{@config[:tcp_port]}"
|
|
|
|
else
|
|
|
|
uri = @uri
|
|
|
|
end
|
|
|
|
self.class.new(uri, s, @config)
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Check to see if this connection is alive.
|
2003-06-18 11:45:12 -04:00
|
|
|
def alive?
|
|
|
|
return false unless @socket
|
|
|
|
if IO.select([@socket], nil, nil, 0)
|
2011-05-18 17:19:18 -04:00
|
|
|
close
|
|
|
|
return false
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def set_sockopt(soc) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
soc.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
2003-12-03 05:40:27 -05:00
|
|
|
soc.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::FD_CLOEXEC
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module DRbProtocol
|
|
|
|
@protocol = [DRbTCPSocket] # default
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
class DRbURIOption # :nodoc: I don't understand the purpose of this class...
|
2003-06-18 11:45:12 -04:00
|
|
|
def initialize(option)
|
|
|
|
@option = option.to_s
|
|
|
|
end
|
2007-11-19 13:30:18 -05:00
|
|
|
attr :option
|
2003-06-18 11:45:12 -04:00
|
|
|
def to_s; @option; end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-06-18 11:45:12 -04:00
|
|
|
def ==(other)
|
|
|
|
return false unless DRbURIOption === other
|
|
|
|
@option == other.option
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-06-18 11:45:12 -04:00
|
|
|
def hash
|
|
|
|
@option.hash
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-06-18 11:45:12 -04:00
|
|
|
alias eql? ==
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Object wrapping a reference to a remote drb object.
|
|
|
|
#
|
|
|
|
# Method calls on this object are relayed to the remote
|
|
|
|
# object that this object is a stub for.
|
2003-06-18 11:45:12 -04:00
|
|
|
class DRbObject
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# Unmarshall a marshalled DRbObject.
|
|
|
|
#
|
|
|
|
# If the referenced object is located within the local server, then
|
|
|
|
# the object itself is returned. Otherwise, a new DRbObject is
|
|
|
|
# created to act as a stub for the remote referenced object.
|
2003-06-18 11:45:12 -04:00
|
|
|
def self._load(s)
|
|
|
|
uri, ref = Marshal.load(s)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-06-18 11:45:12 -04:00
|
|
|
if DRb.here?(uri)
|
2011-05-18 17:19:18 -04:00
|
|
|
obj = DRb.to_obj(ref)
|
2005-04-11 10:48:50 -04:00
|
|
|
if ((! obj.tainted?) && Thread.current[:drb_untaint])
|
|
|
|
Thread.current[:drb_untaint].push(obj)
|
|
|
|
end
|
|
|
|
return obj
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
2005-03-28 10:06:10 -05:00
|
|
|
self.new_with(uri, ref)
|
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Creates a DRb::DRbObject given the reference information to the remote
|
|
|
|
# host +uri+ and object +ref+.
|
2013-01-24 22:25:39 -05:00
|
|
|
|
2005-03-28 10:06:10 -05:00
|
|
|
def self.new_with(uri, ref)
|
|
|
|
it = self.allocate
|
2012-11-16 11:55:29 -05:00
|
|
|
it.instance_variable_set(:@uri, uri)
|
|
|
|
it.instance_variable_set(:@ref, ref)
|
2003-06-18 11:45:12 -04:00
|
|
|
it
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Create a new DRbObject from a URI alone.
|
2003-06-18 11:45:12 -04:00
|
|
|
def self.new_with_uri(uri)
|
|
|
|
self.new(nil, uri)
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Marshall this object.
|
|
|
|
#
|
|
|
|
# The URI and ref of the object are marshalled.
|
2003-06-18 11:45:12 -04:00
|
|
|
def _dump(lv)
|
|
|
|
Marshal.dump([@uri, @ref])
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Create a new remote object stub.
|
|
|
|
#
|
|
|
|
# +obj+ is the (local) object we want to create a stub for. Normally
|
|
|
|
# this is +nil+. +uri+ is the URI of the remote object that this
|
|
|
|
# will be a stub for.
|
2003-06-18 11:45:12 -04:00
|
|
|
def initialize(obj, uri=nil)
|
|
|
|
@uri = nil
|
|
|
|
@ref = nil
|
|
|
|
if obj.nil?
|
2011-05-18 17:19:18 -04:00
|
|
|
return if uri.nil?
|
|
|
|
@uri, option = DRbProtocol.uri_option(uri, DRb.config)
|
|
|
|
@ref = DRbURIOption.new(option) unless option.nil?
|
2003-06-18 11:45:12 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
@uri = uri ? uri : (DRb.uri rescue nil)
|
|
|
|
@ref = obj ? DRb.to_id(obj) : nil
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Get the URI of the remote object.
|
2009-03-05 22:56:38 -05:00
|
|
|
def __drburi
|
2003-06-18 11:45:12 -04:00
|
|
|
@uri
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Get the reference of the object, if local.
|
2003-06-18 11:45:12 -04:00
|
|
|
def __drbref
|
|
|
|
@ref
|
|
|
|
end
|
|
|
|
|
|
|
|
undef :to_s
|
2004-03-18 05:09:43 -05:00
|
|
|
undef :to_a if respond_to?(:to_a)
|
2005-02-01 07:57:03 -05:00
|
|
|
|
2013-01-24 22:25:39 -05:00
|
|
|
# Routes respond_to? to the referenced remote object.
|
2005-02-19 05:00:33 -05:00
|
|
|
def respond_to?(msg_id, priv=false)
|
2005-02-01 07:57:03 -05:00
|
|
|
case msg_id
|
|
|
|
when :_dump
|
|
|
|
true
|
|
|
|
when :marshal_dump
|
|
|
|
false
|
|
|
|
else
|
2005-02-19 05:00:33 -05:00
|
|
|
method_missing(:respond_to?, msg_id, priv)
|
2005-02-01 07:57:03 -05:00
|
|
|
end
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
|
2013-01-24 22:25:39 -05:00
|
|
|
# Routes method calls to the referenced remote object.
|
2003-06-18 11:45:12 -04:00
|
|
|
def method_missing(msg_id, *a, &b)
|
|
|
|
if DRb.here?(@uri)
|
2011-05-18 17:19:18 -04:00
|
|
|
obj = DRb.to_obj(@ref)
|
|
|
|
DRb.current_server.check_insecure_method(obj, msg_id)
|
|
|
|
return obj.__send__(msg_id, *a, &b)
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
2005-03-28 10:06:10 -05:00
|
|
|
succ, result = self.class.with_friend(@uri) do
|
|
|
|
DRbConn.open(@uri) do |conn|
|
|
|
|
conn.send_message(self, msg_id, a, b)
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
2005-03-28 10:06:10 -05:00
|
|
|
|
|
|
|
if succ
|
|
|
|
return result
|
|
|
|
elsif DRbUnknown === result
|
|
|
|
raise result
|
2003-06-18 11:45:12 -04:00
|
|
|
else
|
2005-03-28 10:06:10 -05:00
|
|
|
bt = self.class.prepare_backtrace(@uri, result)
|
2011-05-18 17:19:18 -04:00
|
|
|
result.set_backtrace(bt + caller)
|
2005-05-15 09:28:32 -04:00
|
|
|
raise result
|
2005-03-28 10:06:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Given the +uri+ of another host executes the block provided.
|
2013-01-24 22:25:39 -05:00
|
|
|
def self.with_friend(uri) # :nodoc:
|
2005-03-28 10:06:10 -05:00
|
|
|
friend = DRb.fetch_server(uri)
|
|
|
|
return yield() unless friend
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-03-28 10:06:10 -05:00
|
|
|
save = Thread.current['DRb']
|
|
|
|
Thread.current['DRb'] = { 'server' => friend }
|
|
|
|
return yield
|
|
|
|
ensure
|
|
|
|
Thread.current['DRb'] = save if friend
|
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Returns a modified backtrace from +result+ with the +uri+ where each call
|
|
|
|
# in the backtrace came from.
|
2013-01-24 22:25:39 -05:00
|
|
|
def self.prepare_backtrace(uri, result) # :nodoc:
|
2005-03-28 10:06:10 -05:00
|
|
|
prefix = "(#{uri}) "
|
|
|
|
bt = []
|
|
|
|
result.backtrace.each do |x|
|
2009-03-05 22:56:38 -05:00
|
|
|
break if /`__send__'$/ =~ x
|
2005-03-28 10:06:10 -05:00
|
|
|
if /^\(druby:\/\// =~ x
|
2011-05-18 20:07:25 -04:00
|
|
|
bt.push(x)
|
2005-03-28 10:06:10 -05:00
|
|
|
else
|
|
|
|
bt.push(prefix + x)
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
2005-03-28 10:06:10 -05:00
|
|
|
bt
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
2004-02-13 21:16:05 -05:00
|
|
|
|
|
|
|
def pretty_print(q) # :nodoc:
|
|
|
|
q.pp_object(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pretty_print_cycle(q) # :nodoc:
|
|
|
|
q.object_address_group(self) {
|
|
|
|
q.breakable
|
|
|
|
q.text '...'
|
|
|
|
}
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Class handling the connection between a DRbObject and the
|
|
|
|
# server the real object lives on.
|
|
|
|
#
|
|
|
|
# This class maintains a pool of connections, to reduce the
|
|
|
|
# overhead of starting and closing down connections for each
|
|
|
|
# method call.
|
|
|
|
#
|
|
|
|
# This class is used internally by DRbObject. The user does
|
|
|
|
# not normally need to deal with it directly.
|
2003-06-18 11:45:12 -04:00
|
|
|
class DRbConn
|
2003-09-11 22:29:38 -04:00
|
|
|
POOL_SIZE = 16 # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
@mutex = Mutex.new
|
|
|
|
@pool = []
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def self.open(remote_uri) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
begin
|
2011-05-18 17:19:18 -04:00
|
|
|
conn = nil
|
|
|
|
|
|
|
|
@mutex.synchronize do
|
|
|
|
#FIXME
|
|
|
|
new_pool = []
|
|
|
|
@pool.each do |c|
|
|
|
|
if conn.nil? and c.uri == remote_uri
|
|
|
|
conn = c if c.alive?
|
|
|
|
else
|
|
|
|
new_pool.push c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@pool = new_pool
|
|
|
|
end
|
|
|
|
|
|
|
|
conn = self.new(remote_uri) unless conn
|
|
|
|
succ, result = yield(conn)
|
|
|
|
return succ, result
|
2003-06-18 11:45:12 -04:00
|
|
|
|
|
|
|
ensure
|
2011-05-18 17:19:18 -04:00
|
|
|
if conn
|
|
|
|
if succ
|
|
|
|
@mutex.synchronize do
|
|
|
|
@pool.unshift(conn)
|
|
|
|
@pool.pop.close while @pool.size > POOL_SIZE
|
|
|
|
end
|
|
|
|
else
|
|
|
|
conn.close
|
|
|
|
end
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def initialize(remote_uri) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
@uri = remote_uri
|
|
|
|
@protocol = DRbProtocol.open(remote_uri, DRb.config)
|
|
|
|
end
|
2003-09-11 22:29:38 -04:00
|
|
|
attr_reader :uri # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def send_message(ref, msg_id, arg, block) # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
@protocol.send_request(ref, msg_id, arg, block)
|
|
|
|
@protocol.recv_reply
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def close # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
@protocol.close
|
|
|
|
@protocol = nil
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def alive? # :nodoc:
|
2008-11-18 11:24:22 -05:00
|
|
|
return false unless @protocol
|
2003-06-18 11:45:12 -04:00
|
|
|
@protocol.alive?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Class representing a drb server instance.
|
|
|
|
#
|
|
|
|
# A DRbServer must be running in the local process before any incoming
|
|
|
|
# dRuby calls can be accepted, or any local objects can be passed as
|
|
|
|
# dRuby references to remote processes, even if those local objects are
|
|
|
|
# never actually called remotely. You do not need to start a DRbServer
|
|
|
|
# in the local process if you are only making outgoing dRuby calls
|
|
|
|
# passing marshalled parameters.
|
|
|
|
#
|
|
|
|
# Unless multiple servers are being used, the local DRbServer is normally
|
|
|
|
# started by calling DRb.start_service.
|
2003-06-18 11:45:12 -04:00
|
|
|
class DRbServer
|
|
|
|
@@acl = nil
|
|
|
|
@@idconv = DRbIdConv.new
|
|
|
|
@@secondary_server = nil
|
|
|
|
@@argc_limit = 256
|
|
|
|
@@load_limit = 256 * 102400
|
|
|
|
@@verbose = false
|
2005-02-13 10:13:54 -05:00
|
|
|
@@safe_level = 0
|
2003-06-18 11:45:12 -04:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Set the default value for the :argc_limit option.
|
|
|
|
#
|
|
|
|
# See #new(). The initial default value is 256.
|
2003-06-18 11:45:12 -04:00
|
|
|
def self.default_argc_limit(argc)
|
|
|
|
@@argc_limit = argc
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Set the default value for the :load_limit option.
|
|
|
|
#
|
|
|
|
# See #new(). The initial default value is 25 MB.
|
2003-06-18 11:45:12 -04:00
|
|
|
def self.default_load_limit(sz)
|
|
|
|
@@load_limit = sz
|
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Set the default access control list to +acl+. The default ACL is +nil+.
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
2013-01-24 23:02:46 -05:00
|
|
|
# See also DRb::ACL and #new()
|
2003-06-18 11:45:12 -04:00
|
|
|
def self.default_acl(acl)
|
|
|
|
@@acl = acl
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Set the default value for the :id_conv option.
|
|
|
|
#
|
|
|
|
# See #new(). The initial default value is a DRbIdConv instance.
|
2003-06-18 11:45:12 -04:00
|
|
|
def self.default_id_conv(idconv)
|
|
|
|
@@idconv = idconv
|
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Set the default safe level to +level+. The default safe level is 0
|
|
|
|
#
|
|
|
|
# See #new for more information.
|
2005-02-17 06:16:57 -05:00
|
|
|
def self.default_safe_level(level)
|
|
|
|
@@safe_level = level
|
2005-02-13 10:13:54 -05:00
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Set the default value of the :verbose option.
|
|
|
|
#
|
|
|
|
# See #new(). The initial default value is false.
|
2003-06-18 11:45:12 -04:00
|
|
|
def self.verbose=(on)
|
|
|
|
@@verbose = on
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Get the default value of the :verbose option.
|
2003-06-18 11:45:12 -04:00
|
|
|
def self.verbose
|
|
|
|
@@verbose
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
def self.make_config(hash={}) # :nodoc:
|
2009-03-05 22:56:38 -05:00
|
|
|
default_config = {
|
2011-05-18 17:19:18 -04:00
|
|
|
:idconv => @@idconv,
|
|
|
|
:verbose => @@verbose,
|
|
|
|
:tcp_acl => @@acl,
|
|
|
|
:load_limit => @@load_limit,
|
|
|
|
:argc_limit => @@argc_limit,
|
2005-02-13 10:13:54 -05:00
|
|
|
:safe_level => @@safe_level
|
2003-06-18 11:45:12 -04:00
|
|
|
}
|
|
|
|
default_config.update(hash)
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Create a new DRbServer instance.
|
|
|
|
#
|
|
|
|
# +uri+ is the URI to bind to. This is normally of the form
|
|
|
|
# 'druby://<hostname>:<port>' where <hostname> is a hostname of
|
|
|
|
# the local machine. If nil, then the system's default hostname
|
|
|
|
# will be bound to, on a port selected by the system; these value
|
|
|
|
# can be retrieved from the +uri+ attribute. 'druby:' specifies
|
|
|
|
# the default dRuby transport protocol: another protocol, such
|
|
|
|
# as 'drbunix:', can be specified instead.
|
|
|
|
#
|
|
|
|
# +front+ is the front object for the server, that is, the object
|
|
|
|
# to which remote method calls on the server will be passed. If
|
|
|
|
# nil, then the server will not accept remote method calls.
|
|
|
|
#
|
|
|
|
# If +config_or_acl+ is a hash, it is the configuration to
|
|
|
|
# use for this server. The following options are recognised:
|
|
|
|
#
|
|
|
|
# :idconv :: an id-to-object conversion object. This defaults
|
|
|
|
# to an instance of the class DRb::DRbIdConv.
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-18 19:19:47 -04:00
|
|
|
# :verbose :: if true, all unsuccessful remote calls on objects
|
2003-09-11 22:29:38 -04:00
|
|
|
# in the server will be logged to $stdout. false
|
|
|
|
# by default.
|
|
|
|
# :tcp_acl :: the access control list for this server. See
|
|
|
|
# the ACL class from the main dRuby distribution.
|
|
|
|
# :load_limit :: the maximum message size in bytes accepted by
|
|
|
|
# the server. Defaults to 25 MB (26214400).
|
|
|
|
# :argc_limit :: the maximum number of arguments to a remote
|
|
|
|
# method accepted by the server. Defaults to
|
|
|
|
# 256.
|
2013-01-24 23:02:46 -05:00
|
|
|
# :safe_level :: The safe level of the DRbServer. The attribute
|
|
|
|
# sets $SAFE for methods performed in the main_loop.
|
|
|
|
# Defaults to 0.
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
|
|
|
# The default values of these options can be modified on
|
|
|
|
# a class-wide basis by the class methods #default_argc_limit,
|
|
|
|
# #default_load_limit, #default_acl, #default_id_conv,
|
|
|
|
# and #verbose=
|
|
|
|
#
|
|
|
|
# If +config_or_acl+ is not a hash, but is not nil, it is
|
|
|
|
# assumed to be the access control list for this server.
|
|
|
|
# See the :tcp_acl option for more details.
|
|
|
|
#
|
|
|
|
# If no other server is currently set as the primary server,
|
|
|
|
# this will become the primary server.
|
|
|
|
#
|
|
|
|
# The server will immediately start running in its own thread.
|
2003-06-18 11:45:12 -04:00
|
|
|
def initialize(uri=nil, front=nil, config_or_acl=nil)
|
|
|
|
if Hash === config_or_acl
|
2011-05-18 17:19:18 -04:00
|
|
|
config = config_or_acl.dup
|
2003-06-18 11:45:12 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
acl = config_or_acl || @@acl
|
|
|
|
config = {
|
|
|
|
:tcp_acl => acl
|
|
|
|
}
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@config = self.class.make_config(config)
|
|
|
|
|
|
|
|
@protocol = DRbProtocol.open_server(uri, @config)
|
|
|
|
@uri = @protocol.uri
|
2011-06-27 11:18:22 -04:00
|
|
|
@exported_uri = [@uri]
|
2003-06-18 11:45:12 -04:00
|
|
|
|
|
|
|
@front = front
|
|
|
|
@idconv = @config[:idconv]
|
2005-02-13 10:13:54 -05:00
|
|
|
@safe_level = @config[:safe_level]
|
2003-06-18 11:45:12 -04:00
|
|
|
|
|
|
|
@grp = ThreadGroup.new
|
|
|
|
@thread = run
|
|
|
|
|
2005-03-28 10:06:10 -05:00
|
|
|
DRb.regist_server(self)
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# The URI of this DRbServer.
|
|
|
|
attr_reader :uri
|
|
|
|
|
|
|
|
# The main thread of this DRbServer.
|
|
|
|
#
|
|
|
|
# This is the thread that listens for and accepts connections
|
|
|
|
# from clients, not that handles each client's request-response
|
|
|
|
# session.
|
|
|
|
attr_reader :thread
|
|
|
|
|
|
|
|
# The front object of the DRbServer.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-09-11 22:29:38 -04:00
|
|
|
# This object receives remote method calls made on the server's
|
|
|
|
# URI alone, with an object id.
|
|
|
|
attr_reader :front
|
|
|
|
|
|
|
|
# The configuration of this DRbServer
|
2003-06-18 11:45:12 -04:00
|
|
|
attr_reader :config
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# The safe level for this server. This is a number corresponding to
|
|
|
|
# $SAFE.
|
|
|
|
#
|
|
|
|
# The default safe_level is 0
|
2005-02-13 10:13:54 -05:00
|
|
|
attr_reader :safe_level
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Set whether to operate in verbose mode.
|
|
|
|
#
|
|
|
|
# In verbose mode, failed calls are logged to stdout.
|
2003-06-18 11:45:12 -04:00
|
|
|
def verbose=(v); @config[:verbose]=v; end
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# Get whether the server is in verbose mode.
|
|
|
|
#
|
|
|
|
# In verbose mode, failed calls are logged to stdout.
|
2003-06-18 11:45:12 -04:00
|
|
|
def verbose; @config[:verbose]; end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Is this server alive?
|
2003-06-18 11:45:12 -04:00
|
|
|
def alive?
|
|
|
|
@thread.alive?
|
|
|
|
end
|
2012-08-21 00:50:18 -04:00
|
|
|
|
2013-01-24 22:25:39 -05:00
|
|
|
# Is +uri+ the URI for this server?
|
2011-06-27 11:18:22 -04:00
|
|
|
def here?(uri)
|
|
|
|
@exported_uri.include?(uri)
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Stop this server.
|
2003-06-18 11:45:12 -04:00
|
|
|
def stop_service
|
2005-03-28 10:06:10 -05:00
|
|
|
DRb.remove_server(self)
|
2005-01-02 01:46:37 -05:00
|
|
|
if Thread.current['DRb'] && Thread.current['DRb']['server'] == self
|
|
|
|
Thread.current['DRb']['stop_service'] = true
|
|
|
|
else
|
2010-02-18 10:06:06 -05:00
|
|
|
@thread.kill.join
|
2005-01-02 01:46:37 -05:00
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Convert a dRuby reference to the local object it refers to.
|
2003-06-18 11:45:12 -04:00
|
|
|
def to_obj(ref)
|
|
|
|
return front if ref.nil?
|
|
|
|
return front[ref.to_s] if DRbURIOption === ref
|
|
|
|
@idconv.to_obj(ref)
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Convert a local object to a dRuby reference.
|
2003-06-18 11:45:12 -04:00
|
|
|
def to_id(obj)
|
|
|
|
return nil if obj.__id__ == front.__id__
|
|
|
|
@idconv.to_id(obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2013-01-25 22:58:33 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# Starts the DRb main loop in a new thread.
|
|
|
|
|
2003-06-18 11:45:12 -04:00
|
|
|
def run
|
|
|
|
Thread.start do
|
2011-05-18 17:19:18 -04:00
|
|
|
begin
|
|
|
|
while true
|
|
|
|
main_loop
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@protocol.close if @protocol
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# List of insecure methods.
|
|
|
|
#
|
|
|
|
# These methods are not callable via dRuby.
|
2003-06-18 11:45:12 -04:00
|
|
|
INSECURE_METHOD = [
|
2005-02-13 10:13:54 -05:00
|
|
|
:__send__
|
2003-06-18 11:45:12 -04:00
|
|
|
]
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# Has a method been included in the list of insecure methods?
|
2003-06-18 11:45:12 -04:00
|
|
|
def insecure_method?(msg_id)
|
|
|
|
INSECURE_METHOD.include?(msg_id)
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Coerce an object to a string, providing our own representation if
|
|
|
|
# to_s is not defined for the object.
|
2003-06-18 11:45:12 -04:00
|
|
|
def any_to_s(obj)
|
2005-07-31 12:19:42 -04:00
|
|
|
obj.to_s + ":#{obj.class}"
|
|
|
|
rescue
|
2009-03-05 22:56:38 -05:00
|
|
|
sprintf("#<%s:0x%lx>", obj.class, obj.__id__)
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Check that a method is callable via dRuby.
|
|
|
|
#
|
|
|
|
# +obj+ is the object we want to invoke the method on. +msg_id+ is the
|
|
|
|
# method name, as a Symbol.
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# If the method is an insecure method (see #insecure_method?) a
|
2003-09-11 22:29:38 -04:00
|
|
|
# SecurityError is thrown. If the method is private or undefined,
|
|
|
|
# a NameError is thrown.
|
2003-06-18 11:45:12 -04:00
|
|
|
def check_insecure_method(obj, msg_id)
|
|
|
|
return true if Proc === obj && msg_id == :__drb_yield
|
|
|
|
raise(ArgumentError, "#{any_to_s(msg_id)} is not a symbol") unless Symbol == msg_id.class
|
|
|
|
raise(SecurityError, "insecure method `#{msg_id}'") if insecure_method?(msg_id)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-04-22 09:42:28 -04:00
|
|
|
if obj.private_methods.include?(msg_id)
|
2011-05-18 17:19:18 -04:00
|
|
|
desc = any_to_s(obj)
|
2005-07-31 12:19:42 -04:00
|
|
|
raise NoMethodError, "private method `#{msg_id}' called for #{desc}"
|
2008-04-22 09:42:28 -04:00
|
|
|
elsif obj.protected_methods.include?(msg_id)
|
2011-05-18 17:19:18 -04:00
|
|
|
desc = any_to_s(obj)
|
2005-07-31 12:19:42 -04:00
|
|
|
raise NoMethodError, "protected method `#{msg_id}' called for #{desc}"
|
|
|
|
else
|
|
|
|
true
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
public :check_insecure_method
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
class InvokeMethod # :nodoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
def initialize(drb_server, client)
|
2011-05-18 17:19:18 -04:00
|
|
|
@drb_server = drb_server
|
2005-02-13 10:13:54 -05:00
|
|
|
@safe_level = drb_server.safe_level
|
2011-05-18 17:19:18 -04:00
|
|
|
@client = client
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
2011-05-18 17:19:18 -04:00
|
|
|
@result = nil
|
|
|
|
@succ = false
|
|
|
|
setup_message
|
2005-02-13 10:13:54 -05:00
|
|
|
|
|
|
|
if $SAFE < @safe_level
|
|
|
|
info = Thread.current['DRb']
|
|
|
|
if @block
|
2007-11-19 13:30:18 -05:00
|
|
|
@result = Thread.new {
|
2005-02-13 10:13:54 -05:00
|
|
|
Thread.current['DRb'] = info
|
|
|
|
$SAFE = @safe_level
|
|
|
|
perform_with_block
|
|
|
|
}.value
|
|
|
|
else
|
2009-03-05 22:56:38 -05:00
|
|
|
@result = Thread.new {
|
2005-02-13 10:13:54 -05:00
|
|
|
Thread.current['DRb'] = info
|
|
|
|
$SAFE = @safe_level
|
|
|
|
perform_without_block
|
|
|
|
}.value
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
else
|
2005-02-13 10:13:54 -05:00
|
|
|
if @block
|
|
|
|
@result = perform_with_block
|
|
|
|
else
|
|
|
|
@result = perform_without_block
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
2011-05-18 17:19:18 -04:00
|
|
|
@succ = true
|
|
|
|
if @msg_id == :to_ary && @result.class == Array
|
|
|
|
@result = DRbArray.new(@result)
|
|
|
|
end
|
|
|
|
return @succ, @result
|
2003-06-18 11:45:12 -04:00
|
|
|
rescue StandardError, ScriptError, Interrupt
|
2011-05-18 17:19:18 -04:00
|
|
|
@result = $!
|
|
|
|
return @succ, @result
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def init_with_client
|
2011-05-18 17:19:18 -04:00
|
|
|
obj, msg, argv, block = @client.recv_request
|
2003-06-18 11:45:12 -04:00
|
|
|
@obj = obj
|
|
|
|
@msg_id = msg.intern
|
|
|
|
@argv = argv
|
|
|
|
@block = block
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-06-18 11:45:12 -04:00
|
|
|
def check_insecure_method
|
|
|
|
@drb_server.check_insecure_method(@obj, @msg_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_message
|
2011-05-18 17:19:18 -04:00
|
|
|
init_with_client
|
|
|
|
check_insecure_method
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-06-18 11:45:12 -04:00
|
|
|
def perform_without_block
|
2011-05-18 17:19:18 -04:00
|
|
|
if Proc === @obj && @msg_id == :__drb_yield
|
2003-06-18 11:45:12 -04:00
|
|
|
if @argv.size == 1
|
2011-05-18 17:19:18 -04:00
|
|
|
ary = @argv
|
|
|
|
else
|
|
|
|
ary = [@argv]
|
|
|
|
end
|
|
|
|
ary.collect(&@obj)[0]
|
|
|
|
else
|
|
|
|
@obj.__send__(@msg_id, *@argv)
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
if RUBY_VERSION >= '1.8'
|
|
|
|
require 'drb/invokemethod'
|
|
|
|
class InvokeMethod
|
|
|
|
include InvokeMethod18Mixin
|
|
|
|
end
|
|
|
|
else
|
2003-06-28 18:03:41 -04:00
|
|
|
require 'drb/invokemethod16'
|
2003-06-18 11:45:12 -04:00
|
|
|
class InvokeMethod
|
|
|
|
include InvokeMethod16Mixin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# The main loop performed by a DRbServer's internal thread.
|
|
|
|
#
|
|
|
|
# Accepts a connection from a client, and starts up its own
|
|
|
|
# thread to handle it. This thread loops, receiving requests
|
|
|
|
# from the client, invoking them on a local object, and
|
|
|
|
# returning responses, until the client closes the connection
|
|
|
|
# or a local method call fails.
|
2003-06-18 11:45:12 -04:00
|
|
|
def main_loop
|
|
|
|
Thread.start(@protocol.accept) do |client|
|
2011-05-18 17:19:18 -04:00
|
|
|
@grp.add Thread.current
|
|
|
|
Thread.current['DRb'] = { 'client' => client ,
|
2011-05-18 20:07:25 -04:00
|
|
|
'server' => self }
|
2011-06-27 11:18:22 -04:00
|
|
|
DRb.mutex.synchronize do
|
|
|
|
client_uri = client.uri
|
|
|
|
@exported_uri << client_uri unless @exported_uri.include?(client_uri)
|
|
|
|
end
|
2011-05-18 17:19:18 -04:00
|
|
|
loop do
|
|
|
|
begin
|
|
|
|
succ = false
|
|
|
|
invoke_method = InvokeMethod.new(self, client)
|
|
|
|
succ, result = invoke_method.perform
|
|
|
|
if !succ && verbose
|
|
|
|
p result
|
|
|
|
result.backtrace.each do |x|
|
|
|
|
puts x
|
|
|
|
end
|
|
|
|
end
|
|
|
|
client.send_reply(succ, result) rescue nil
|
|
|
|
ensure
|
2005-01-02 01:46:37 -05:00
|
|
|
client.close unless succ
|
|
|
|
if Thread.current['DRb']['stop_service']
|
|
|
|
Thread.new { stop_service }
|
|
|
|
end
|
|
|
|
break unless succ
|
2011-05-18 17:19:18 -04:00
|
|
|
end
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@primary_server = nil
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# 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.
|
2003-06-18 11:45:12 -04:00
|
|
|
def start_service(uri=nil, front=nil, config=nil)
|
|
|
|
@primary_server = DRbServer.new(uri, front, config)
|
|
|
|
end
|
|
|
|
module_function :start_service
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# The primary local dRuby server.
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# This is the server created by the #start_service call.
|
2003-06-18 11:45:12 -04:00
|
|
|
attr_accessor :primary_server
|
|
|
|
module_function :primary_server=, :primary_server
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# 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.
|
2003-06-18 11:45:12 -04:00
|
|
|
def current_server
|
2009-03-05 22:56:38 -05:00
|
|
|
drb = Thread.current['DRb']
|
|
|
|
server = (drb && drb['server']) ? drb['server'] : @primary_server
|
2003-06-18 11:45:12 -04:00
|
|
|
raise DRbServerNotFound unless server
|
|
|
|
return server
|
|
|
|
end
|
|
|
|
module_function :current_server
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Stop the local dRuby server.
|
|
|
|
#
|
|
|
|
# This operates on the primary server. If there is no primary
|
|
|
|
# server currently running, it is a noop.
|
2003-06-18 11:45:12 -04:00
|
|
|
def stop_service
|
|
|
|
@primary_server.stop_service if @primary_server
|
|
|
|
@primary_server = nil
|
|
|
|
end
|
|
|
|
module_function :stop_service
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Get the URI defining the local dRuby space.
|
|
|
|
#
|
|
|
|
# This is the URI of the current server. See #current_server.
|
2003-06-18 11:45:12 -04:00
|
|
|
def uri
|
2007-11-19 13:30:18 -05:00
|
|
|
drb = Thread.current['DRb']
|
|
|
|
client = (drb && drb['client'])
|
|
|
|
if client
|
|
|
|
uri = client.uri
|
|
|
|
return uri if uri
|
|
|
|
end
|
2003-06-18 11:45:12 -04:00
|
|
|
current_server.uri
|
|
|
|
end
|
|
|
|
module_function :uri
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Is +uri+ the URI for the current local server?
|
2003-06-18 11:45:12 -04:00
|
|
|
def here?(uri)
|
2011-06-27 11:18:22 -04:00
|
|
|
current_server.here?(uri) rescue false
|
|
|
|
# (current_server.uri rescue nil) == uri
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
module_function :here?
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# 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.
|
2003-06-18 11:45:12 -04:00
|
|
|
def config
|
|
|
|
current_server.config
|
|
|
|
rescue
|
|
|
|
DRbServer.make_config
|
|
|
|
end
|
|
|
|
module_function :config
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Get the front object of the current server.
|
|
|
|
#
|
|
|
|
# This raises a DRbServerNotFound error if there is no current server.
|
|
|
|
# See #current_server.
|
2003-06-18 11:45:12 -04:00
|
|
|
def front
|
|
|
|
current_server.front
|
|
|
|
end
|
|
|
|
module_function :front
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Convert a reference into an object using the current server.
|
|
|
|
#
|
|
|
|
# This raises a DRbServerNotFound error if there is no current server.
|
|
|
|
# See #current_server.
|
2003-06-18 11:45:12 -04:00
|
|
|
def to_obj(ref)
|
|
|
|
current_server.to_obj(ref)
|
|
|
|
end
|
2003-09-11 22:29:38 -04:00
|
|
|
|
|
|
|
# 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.
|
2003-06-18 11:45:12 -04:00
|
|
|
def to_id(obj)
|
|
|
|
current_server.to_id(obj)
|
|
|
|
end
|
|
|
|
module_function :to_id
|
|
|
|
module_function :to_obj
|
|
|
|
|
2003-09-11 22:29:38 -04:00
|
|
|
# Get the thread of the primary server.
|
|
|
|
#
|
|
|
|
# This returns nil if there is no primary server. See #primary_server.
|
2003-06-18 11:45:12 -04:00
|
|
|
def thread
|
|
|
|
@primary_server ? @primary_server.thread : nil
|
|
|
|
end
|
|
|
|
module_function :thread
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# 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.
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
|
|
|
# See DRbServer#default_id_conv.
|
2003-06-18 11:45:12 -04:00
|
|
|
def install_id_conv(idconv)
|
|
|
|
DRbServer.default_id_conv(idconv)
|
|
|
|
end
|
|
|
|
module_function :install_id_conv
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Set the default ACL to +acl+.
|
2003-09-11 22:29:38 -04:00
|
|
|
#
|
|
|
|
# See DRb::DRbServer.default_acl.
|
2003-06-18 11:45:12 -04:00
|
|
|
def install_acl(acl)
|
|
|
|
DRbServer.default_acl(acl)
|
|
|
|
end
|
|
|
|
module_function :install_acl
|
2005-03-28 10:06:10 -05:00
|
|
|
|
2007-11-19 13:30:18 -05:00
|
|
|
@mutex = Mutex.new
|
2013-01-24 22:25:39 -05:00
|
|
|
def mutex # :nodoc:
|
2007-11-19 13:30:18 -05:00
|
|
|
@mutex
|
|
|
|
end
|
|
|
|
module_function :mutex
|
|
|
|
|
2005-03-28 10:06:10 -05:00
|
|
|
@server = {}
|
2013-01-24 22:25:39 -05:00
|
|
|
# Registers +server+ with DRb.
|
|
|
|
#
|
2013-01-24 23:02:46 -05:00
|
|
|
# This is called when a new DRb::DRbServer is created.
|
|
|
|
#
|
2013-01-24 22:25:39 -05:00
|
|
|
# If there is no primary server then +server+ becomes the primary server.
|
2013-01-24 23:02:46 -05:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# require 'drb'
|
|
|
|
#
|
|
|
|
# s = DRb::DRbServer.new # automatically calls regist_server
|
|
|
|
# DRb.fetch_server s.uri #=> #<DRb::DRbServer:0x...>
|
2005-03-28 10:06:10 -05:00
|
|
|
def regist_server(server)
|
|
|
|
@server[server.uri] = server
|
2007-11-19 13:30:18 -05:00
|
|
|
mutex.synchronize do
|
2005-03-28 10:06:10 -05:00
|
|
|
@primary_server = server unless @primary_server
|
2007-11-19 13:30:18 -05:00
|
|
|
end
|
2005-03-28 10:06:10 -05:00
|
|
|
end
|
|
|
|
module_function :regist_server
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Removes +server+ from the list of registered servers.
|
2005-03-28 10:06:10 -05:00
|
|
|
def remove_server(server)
|
|
|
|
@server.delete(server.uri)
|
|
|
|
end
|
|
|
|
module_function :remove_server
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2013-01-24 22:25:39 -05:00
|
|
|
# Retrieves the server with the given +uri+.
|
2013-01-24 23:02:46 -05:00
|
|
|
#
|
|
|
|
# See also regist_server and remove_server.
|
2005-03-28 10:06:10 -05:00
|
|
|
def fetch_server(uri)
|
|
|
|
@server[uri]
|
|
|
|
end
|
|
|
|
module_function :fetch_server
|
2003-06-18 11:45:12 -04:00
|
|
|
end
|
|
|
|
|
2011-06-29 17:33:36 -04:00
|
|
|
# :stopdoc:
|
2003-06-18 11:45:12 -04:00
|
|
|
DRbObject = DRb::DRbObject
|
|
|
|
DRbUndumped = DRb::DRbUndumped
|
|
|
|
DRbIdConv = DRb::DRbIdConv
|