Class DRb::DRbServer
In: lib/drb/drb.rb
lib/drb/invokemethod.rb
Parent: Object

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.

Methods

Classes and Modules

Module DRb::DRbServer::InvokeMethod18Mixin

Constants

INSECURE_METHOD = [ :__send__   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] 
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

Set the default value for the :acl option.

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

[Source]

      # File lib/drb/drb.rb, line 1252
1252:     def self.default_acl(acl)
1253:       @@acl = acl
1254:     end

Set the default value for the :argc_limit option.

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

[Source]

      # File lib/drb/drb.rb, line 1238
1238:     def self.default_argc_limit(argc)
1239:       @@argc_limit = argc
1240:     end

Set the default value for the :id_conv option.

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

[Source]

      # File lib/drb/drb.rb, line 1259
1259:     def self.default_id_conv(idconv)
1260:       @@idconv = idconv
1261:     end

Set the default value for the :load_limit option.

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

[Source]

      # File lib/drb/drb.rb, line 1245
1245:     def self.default_load_limit(sz)
1246:       @@load_limit = sz
1247:     end

[Source]

      # File lib/drb/drb.rb, line 1263
1263:     def self.default_safe_level(level)
1264:       @@safe_level = level
1265:     end

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.

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.

[Source]

      # File lib/drb/drb.rb, line 1334
1334:     def initialize(uri=nil, front=nil, config_or_acl=nil)
1335:       if Hash === config_or_acl
1336:         config = config_or_acl.dup
1337:       else
1338:         acl = config_or_acl || @@acl
1339:         config = {
1340:           :tcp_acl => acl
1341:         }
1342:       end
1343: 
1344:       @config = self.class.make_config(config)
1345: 
1346:       @protocol = DRbProtocol.open_server(uri, @config)
1347:       @uri = @protocol.uri
1348: 
1349:       @front = front
1350:       @idconv = @config[:idconv]
1351:       @safe_level = @config[:safe_level]
1352: 
1353:       @grp = ThreadGroup.new
1354:       @thread = run
1355: 
1356:       DRb.regist_server(self)
1357:     end

Get the default value of the :verbose option.

[Source]

      # File lib/drb/drb.rb, line 1275
1275:     def self.verbose
1276:       @@verbose
1277:     end

Set the default value of the :verbose option.

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

[Source]

      # File lib/drb/drb.rb, line 1270
1270:     def self.verbose=(on)
1271:       @@verbose = on
1272:     end

Public Instance methods

Is this server alive?

[Source]

      # File lib/drb/drb.rb, line 1391
1391:     def alive?
1392:       @thread.alive?
1393:     end

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.

[Source]

      # File lib/drb/drb.rb, line 1474
1474:     def check_insecure_method(obj, msg_id)
1475:       return true if Proc === obj && msg_id == :__drb_yield
1476:       raise(ArgumentError, "#{any_to_s(msg_id)} is not a symbol") unless Symbol == msg_id.class
1477:       raise(SecurityError, "insecure method `#{msg_id}'") if insecure_method?(msg_id)
1478:       
1479:       if obj.private_methods.include?(msg_id.to_s)
1480:         desc = any_to_s(obj)
1481:         raise NoMethodError, "private method `#{msg_id}' called for #{desc}"
1482:       elsif obj.protected_methods.include?(msg_id.to_s)
1483:         desc = any_to_s(obj)
1484:         raise NoMethodError, "protected method `#{msg_id}' called for #{desc}"
1485:       else
1486:         true
1487:       end
1488:     end

Stop this server.

[Source]

      # File lib/drb/drb.rb, line 1396
1396:     def stop_service
1397:       DRb.remove_server(self)
1398:       if  Thread.current['DRb'] && Thread.current['DRb']['server'] == self
1399:         Thread.current['DRb']['stop_service'] = true
1400:       else
1401:         @thread.kill
1402:       end
1403:     end

Convert a local object to a dRuby reference.

[Source]

      # File lib/drb/drb.rb, line 1413
1413:     def to_id(obj)
1414:       return nil if obj.__id__ == front.__id__
1415:       @idconv.to_id(obj)
1416:     end

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

[Source]

      # File lib/drb/drb.rb, line 1406
1406:     def to_obj(ref)
1407:       return front if ref.nil?
1408:       return front[ref.to_s] if DRbURIOption === ref
1409:       @idconv.to_obj(ref)
1410:     end

Get whether the server is in verbose mode.

In verbose mode, failed calls are logged to stdout.

[Source]

      # File lib/drb/drb.rb, line 1388
1388:     def verbose; @config[:verbose]; end

Set whether to operate in verbose mode.

In verbose mode, failed calls are logged to stdout.

[Source]

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

Private Instance methods

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

[Source]

      # File lib/drb/drb.rb, line 1460
1460:     def any_to_s(obj)
1461:       obj.to_s + ":#{obj.class}"
1462:     rescue
1463:       sprintf("#<%s:0x%lx>", obj.class, obj.__id__)      
1464:     end

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

[Source]

      # File lib/drb/drb.rb, line 1454
1454:     def insecure_method?(msg_id)
1455:       INSECURE_METHOD.include?(msg_id)
1456:     end

[Source]

      # File lib/drb/drb.rb, line 1419
1419:     def kill_sub_thread
1420:       Thread.new do
1421:         grp = ThreadGroup.new
1422:         grp.add(Thread.current)
1423:         list = @grp.list
1424:         while list.size > 0
1425:           list.each do |th|
1426:             th.kill if th.alive?
1427:           end
1428:           list = @grp.list
1429:         end
1430:       end
1431:     end

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.

[Source]

      # File lib/drb/drb.rb, line 1587
1587:     def main_loop
1588:       Thread.start(@protocol.accept) do |client|
1589:         @grp.add Thread.current
1590:         Thread.current['DRb'] = { 'client' => client ,
1591:                                   'server' => self }
1592:         loop do
1593:           begin
1594:             succ = false
1595:             invoke_method = InvokeMethod.new(self, client)
1596:             succ, result = invoke_method.perform
1597:             if !succ && verbose
1598:               p result
1599:               result.backtrace.each do |x|
1600:                 puts x
1601:               end
1602:             end
1603:             client.send_reply(succ, result) rescue nil
1604:           ensure
1605:             client.close unless succ
1606:             if Thread.current['DRb']['stop_service']
1607:               Thread.new { stop_service }
1608:             end
1609:             break unless succ
1610:           end
1611:         end
1612:       end
1613:     end

[Source]

      # File lib/drb/drb.rb, line 1433
1433:     def run
1434:       Thread.start do
1435:         begin
1436:           while true
1437:             main_loop
1438:           end
1439:         ensure
1440:           @protocol.close if @protocol
1441:           kill_sub_thread
1442:         end
1443:       end
1444:     end

[Validate]