class DRb::DRbServer

started by calling DRb.start_service.
Unless multiple servers are being used, the local DRbServer is normally
passing marshalled parameters.
in the local process if you are only making outgoing dRuby calls
never actually called remotely. You do not need to start a DRbServer
dRuby references to remote processes, even if those local objects are
dRuby calls can be accepted, or any local objects can be passed as
A DRbServer must be running in the local process before any incoming
Class representing a drb server instance.

def self.default_acl(acl)

See also DRb::ACL and #new()

Set the default access control list to +acl+. The default ACL is +nil+.
def self.default_acl(acl)
  @@acl = acl
end

def self.default_argc_limit(argc)

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

Set the default value for the :argc_limit option.
def self.default_argc_limit(argc)
  @@argc_limit = argc
end

def self.default_id_conv(idconv)

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

Set the default value for the :id_conv option.
def self.default_id_conv(idconv)
  @@idconv = idconv
end

def self.default_load_limit(sz)

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

Set the default value for the :load_limit option.
def self.default_load_limit(sz)
  @@load_limit = sz
end

def self.make_config(hash={}) # :nodoc:

:nodoc:
def self.make_config(hash={})  # :nodoc:
  default_config = {
    :idconv => @@idconv,
    :verbose => @@verbose,
    :tcp_acl => @@acl,
    :load_limit => @@load_limit,
    :argc_limit => @@argc_limit,
  }
  default_config.update(hash)
end

def self.verbose

Get the default value of the :verbose option.
def self.verbose
  @@verbose
end

def self.verbose=(on)

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

Set the default value of the :verbose option.
def self.verbose=(on)
  @@verbose = on
end

def alive?

Is this server alive?
def alive?
  @thread.alive?
end

def any_to_s(obj)

to_s is not defined for the object.
Coerce an object to a string, providing our own representation if
def any_to_s(obj)
  "#{obj}:#{obj.class}"
rescue
  Kernel.instance_method(:to_s).bind_call(obj)
end

def check_insecure_method(obj, msg_id)

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

method name, as a Symbol.
+obj+ is the object we want to invoke the method on. +msg_id+ is the

Check that a method is callable via dRuby.
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)
  case obj
  when Object
    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
  else
    if Kernel.instance_method(:private_methods).bind(obj).call.include?(msg_id)
      desc = any_to_s(obj)
      raise NoMethodError, "private method '#{msg_id}' called for #{desc}"
    elsif Kernel.instance_method(:protected_methods).bind(obj).call.include?(msg_id)
      desc = any_to_s(obj)
      raise NoMethodError, "protected method '#{msg_id}' called for #{desc}"
    else
      true
    end
  end
end

def error_print(exception)

def error_print(exception)
  exception.backtrace.inject(true) do |first, x|
    if first
      $stderr.puts "#{x}: #{exception} (#{exception.class})"
    else
      $stderr.puts "\tfrom #{x}"
    end
    false
  end
end

def here?(uri)

Is +uri+ the URI for this server?
def here?(uri)
  @exported_uri.include?(uri)
end

def initialize(uri=nil, front=nil, config_or_acl=nil)

The server will immediately start running in its own thread.

this will become the primary server.
If no other server is currently set as the primary server,

See the :tcp_acl option for more details.
assumed to be the access control list for this server.
If +config_or_acl+ is not a hash, but is not nil, it is

and #verbose=
#default_load_limit, #default_acl, #default_id_conv,
a class-wide basis by the class methods #default_argc_limit,
The default values of these options can be modified on
256.
method accepted by the server. Defaults to
:argc_limit :: the maximum number of arguments to a remote
the server. Defaults to 25 MB (26214400).
:load_limit :: the maximum message size in bytes accepted by
the ACL class from the main dRuby distribution.
:tcp_acl :: the access control list for this server. See
by default.
in the server will be logged to $stdout. false
:verbose :: if true, all unsuccessful remote calls on objects
to an instance of the class DRb::DRbIdConv.
:idconv :: an id-to-object conversion object. This defaults

use for this server. The following options are recognised:
If +config_or_acl+ is a hash, it is the configuration to

nil, then the server will not accept remote method calls.
to which remote method calls on the server will be passed. If
+front+ is the front object for the server, that is, the object

as 'drbunix:', can be specified instead.
the default dRuby transport protocol: another protocol, such
can be retrieved from the +uri+ attribute. 'druby:' specifies
will be bound to, on a port selected by the system; these value
the local machine. If nil, then the system's default hostname
'druby://:' where is a hostname of
+uri+ is the URI to bind to. This is normally of the form

Create a new DRbServer instance.
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]
  @grp = ThreadGroup.new
  @thread = run
  DRb.regist_server(self)
end

def insecure_method?(msg_id)

Has a method been included in the list of insecure methods?
def insecure_method?(msg_id)
  INSECURE_METHOD.include?(msg_id)
end

def main_loop

or a local method call fails.
returning responses, until the client closes the connection
from the client, invoking them on a local object, and
thread to handle it. This thread loops, receiving requests
Accepts a connection from a client, and starts up its own

The main loop performed by a DRbServer's internal thread.
def main_loop
  client0 = @protocol.accept
  return nil if !client0
  Thread.start(client0) 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
    _last_invoke_method = nil
    loop do
      begin
        succ = false
        invoke_method = InvokeMethod.new(self, client)
        succ, result = invoke_method.perform
        error_print(result) if !succ && verbose
        unless DRbConnError === result && result.message == 'connection closed'
          client.send_reply(succ, result)
        end
      rescue Exception => e
        error_print(e) if verbose
      ensure
        _last_invoke_method = invoke_method
        client.close unless succ
        if Thread.current['DRb']['stop_service']
          shutdown
          break
        end
        break unless succ
      end
    end
  end
end

def run

def run
  Thread.start do
    begin
      while main_loop
      end
    ensure
      @protocol.close if @protocol
    end
  end
end

def shutdown

def shutdown
  current = Thread.current
  if @protocol.respond_to? :shutdown
    @protocol.shutdown
  else
    [@thread, *@grp.list].each { |thread|
      thread.kill unless thread == current # xxx: Thread#kill
    }
  end
  @thread.join unless @thread == current
end

def stop_service

Stop this server.
def stop_service
  DRb.remove_server(self)
  if  Thread.current['DRb'] && Thread.current['DRb']['server'] == self
    Thread.current['DRb']['stop_service'] = true
  else
    shutdown
  end
end

def to_id(obj)

Convert a local object to a dRuby reference.
def to_id(obj)
  return nil if obj.__id__ == front.__id__
  @idconv.to_id(obj)
end

def to_obj(ref)

Convert a dRuby reference to the local object it refers to.
def to_obj(ref)
  return front if ref.nil?
  return front[ref.to_s] if DRbURIOption === ref
  @idconv.to_obj(ref)
end

def verbose; @config[:verbose]; end

In verbose mode, failed calls are logged to stdout.

Get whether the server is in verbose mode.
def verbose; @config[:verbose]; end

def verbose=(v); @config[:verbose]=v; end

In verbose mode, failed calls are logged to stdout.

Set whether to operate in verbose mode.
def verbose=(v); @config[:verbose]=v; end