class DRb::DRbServer

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.

Constants

INSECURE_METHOD

List of insecure methods.

These methods are not callable via dRuby.

Attributes

config[R]

The configuration of this DRbServer

front[R]

The front object of the DRbServer.

This object receives remote method calls made on the server's URI alone, with an object id.

safe_level[R]

The safe level for this server. This is a number corresponding to $SAFE.

The default #safe_level is 0

thread[R]

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.

uri[R]

The URI of this DRbServer.

Public Class Methods

default_acl(acl) click to toggle source

Set the default access control list to acl. The default ACL is nil.

See also DRb::ACL and new()

# File lib/drb/drb.rb, line 1273
def self.default_acl(acl)
  @@acl = acl
end
default_argc_limit(argc) click to toggle source

Set the default value for the :argc_limit option.

See new(). The initial default value is 256.

# File lib/drb/drb.rb, line 1259
def self.default_argc_limit(argc)
  @@argc_limit = argc
end
default_id_conv(idconv) click to toggle source

Set the default value for the :id_conv option.

See new(). The initial default value is a DRbIdConv instance.

# File lib/drb/drb.rb, line 1280
def self.default_id_conv(idconv)
  @@idconv = idconv
end
default_load_limit(sz) click to toggle source

Set the default value for the :load_limit option.

See new(). The initial default value is 25 MB.

# File lib/drb/drb.rb, line 1266
def self.default_load_limit(sz)
  @@load_limit = sz
end
default_safe_level(level) click to toggle source

Set the default safe level to level. The default safe level is 0

See new for more information.

# File lib/drb/drb.rb, line 1287
def self.default_safe_level(level)
  @@safe_level = level
end
new(uri=nil, front=nil, config_or_acl=nil) click to toggle source

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.

:verbose

if true, all unsuccessful remote calls on objects 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.

:safe_level

The safe level of the DRbServer. The attribute sets $SAFE for methods performed in the main_loop. Defaults to 0.

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.

# File lib/drb/drb.rb, line 1361
def initialize(uri=nil, front=nil, config_or_acl=nil)
  if Hash === config_or_acl
    config = config_or_acl.dup
  else
    acl = config_or_acl || @@acl
    config = {
      :tcp_acl => acl
    }
  end

  @config = self.class.make_config(config)

  @protocol = DRbProtocol.open_server(uri, @config)
  @uri = @protocol.uri
  @exported_uri = [@uri]

  @front = front
  @idconv = @config[:idconv]
  @safe_level = @config[:safe_level]

  @grp = ThreadGroup.new
  @thread = run

  DRb.regist_server(self)
end
verbose() click to toggle source

Get the default value of the :verbose option.

# File lib/drb/drb.rb, line 1299
def self.verbose
  @@verbose
end
verbose=(on) click to toggle source

Set the default value of the :verbose option.

See new(). The initial default value is false.

# File lib/drb/drb.rb, line 1294
def self.verbose=(on)
  @@verbose = on
end

Public Instance Methods

alive?() click to toggle source

Is this server alive?

# File lib/drb/drb.rb, line 1423
def alive?
  @thread.alive?
end
check_insecure_method(obj, msg_id) click to toggle source

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.

If the method is an insecure method (see insecure_method?) a SecurityError is thrown. If the method is private or undefined, a NameError is thrown.

# File lib/drb/drb.rb, line 1500
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)

  if obj.private_methods.include?(msg_id)
    desc = any_to_s(obj)
    raise NoMethodError, "private method `#{msg_id}' called for #{desc}"
  elsif obj.protected_methods.include?(msg_id)
    desc = any_to_s(obj)
    raise NoMethodError, "protected method `#{msg_id}' called for #{desc}"
  else
    true
  end
end
here?(uri) click to toggle source

Is uri the URI for this server?

# File lib/drb/drb.rb, line 1428
def here?(uri)
  @exported_uri.include?(uri)
end
stop_service() click to toggle source

Stop this server.

# File lib/drb/drb.rb, line 1433
def stop_service
  DRb.remove_server(self)
  if  Thread.current['DRb'] && Thread.current['DRb']['server'] == self
    Thread.current['DRb']['stop_service'] = true
  else
    @thread.kill.join
  end
end
to_id(obj) click to toggle source

Convert a local object to a dRuby reference.

# File lib/drb/drb.rb, line 1450
def to_id(obj)
  return nil if obj.__id__ == front.__id__
  @idconv.to_id(obj)
end
to_obj(ref) click to toggle source

Convert a dRuby reference to the local object it refers to.

# File lib/drb/drb.rb, line 1443
def to_obj(ref)
  return front if ref.nil?
  return front[ref.to_s] if DRbURIOption === ref
  @idconv.to_obj(ref)
end
verbose() click to toggle source

Get whether the server is in verbose mode.

In verbose mode, failed calls are logged to stdout.

# File lib/drb/drb.rb, line 1420
def verbose; @config[:verbose]; end
verbose=(v) click to toggle source

Set whether to operate in verbose mode.

In verbose mode, failed calls are logged to stdout.

# File lib/drb/drb.rb, line 1415
def verbose=(v); @config[:verbose]=v; end

Private Instance Methods

any_to_s(obj) click to toggle source

Coerce an object to a string, providing our own representation if to_s is not defined for the object.

# File lib/drb/drb.rb, line 1486
def any_to_s(obj)
  obj.to_s + ":#{obj.class}"
rescue
  sprintf("#<%s:0x%lx>", obj.class, obj.__id__)
end
insecure_method?(msg_id) click to toggle source

Has a method been included in the list of insecure methods?

# File lib/drb/drb.rb, line 1480
def insecure_method?(msg_id)
  INSECURE_METHOD.include?(msg_id)
end
main_loop() click to toggle source

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.

# File lib/drb/drb.rb, line 1613
def main_loop
  Thread.start(@protocol.accept) do |client|
    @grp.add Thread.current
    Thread.current['DRb'] = { 'client' => client ,
                              'server' => self }
    DRb.mutex.synchronize do
      client_uri = client.uri
      @exported_uri << client_uri unless @exported_uri.include?(client_uri)
    end
    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
        client.close unless succ
        if Thread.current['DRb']['stop_service']
          Thread.new { stop_service }
        end
        break unless succ
      end
    end
  end
end
run() click to toggle source

Starts the DRb main loop in a new thread.

# File lib/drb/drb.rb, line 1460
def run
  Thread.start do
    begin
      while true
        main_loop
      end
    ensure
      @protocol.close if @protocol
    end
  end
end