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 1245
1245:     def self.default_acl(acl)
1246:       @@acl = acl
1247:     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 1231
1231:     def self.default_argc_limit(argc)
1232:       @@argc_limit = argc
1233:     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 1252
1252:     def self.default_id_conv(idconv)
1253:       @@idconv = idconv
1254:     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 1238
1238:     def self.default_load_limit(sz)
1239:       @@load_limit = sz
1240:     end

[Source]

      # File lib/drb/drb.rb, line 1256
1256:     def self.default_safe_level(level)
1257:       @@safe_level = level
1258:     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 1327
1327:     def initialize(uri=nil, front=nil, config_or_acl=nil)
1328:       if Hash === config_or_acl
1329:         config = config_or_acl.dup
1330:       else
1331:         acl = config_or_acl || @@acl
1332:         config = {
1333:           :tcp_acl => acl
1334:         }
1335:       end
1336: 
1337:       @config = self.class.make_config(config)
1338: 
1339:       @protocol = DRbProtocol.open_server(uri, @config)
1340:       @uri = @protocol.uri
1341: 
1342:       @front = front
1343:       @idconv = @config[:idconv]
1344:       @safe_level = @config[:safe_level]
1345: 
1346:       @grp = ThreadGroup.new
1347:       @thread = run
1348: 
1349:       DRb.regist_server(self)
1350:     end

Get the default value of the :verbose option.

[Source]

      # File lib/drb/drb.rb, line 1268
1268:     def self.verbose
1269:       @@verbose
1270:     end

Set the default value of the :verbose option.

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

[Source]

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

Public Instance methods

Is this server alive?

[Source]

      # File lib/drb/drb.rb, line 1384
1384:     def alive?
1385:       @thread.alive?
1386:     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 1467
1467:     def check_insecure_method(obj, msg_id)
1468:       return true if Proc === obj && msg_id == :__drb_yield
1469:       raise(ArgumentError, "#{any_to_s(msg_id)} is not a symbol") unless Symbol == msg_id.class
1470:       raise(SecurityError, "insecure method `#{msg_id}'") if insecure_method?(msg_id)
1471:       
1472:       if obj.private_methods.include?(msg_id.to_s)
1473:         desc = any_to_s(obj)
1474:         raise NoMethodError, "private method `#{msg_id}' called for #{desc}"
1475:       elsif obj.protected_methods.include?(msg_id.to_s)
1476:         desc = any_to_s(obj)
1477:         raise NoMethodError, "protected method `#{msg_id}' called for #{desc}"
1478:       else
1479:         true
1480:       end
1481:     end

Stop this server.

[Source]

      # File lib/drb/drb.rb, line 1389
1389:     def stop_service
1390:       DRb.remove_server(self)
1391:       if  Thread.current['DRb'] && Thread.current['DRb']['server'] == self
1392:         Thread.current['DRb']['stop_service'] = true
1393:       else
1394:         @thread.kill
1395:       end
1396:     end

Convert a local object to a dRuby reference.

[Source]

      # File lib/drb/drb.rb, line 1406
1406:     def to_id(obj)
1407:       return nil if obj.__id__ == front.__id__
1408:       @idconv.to_id(obj)
1409:     end

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

[Source]

      # File lib/drb/drb.rb, line 1399
1399:     def to_obj(ref)
1400:       return front if ref.nil?
1401:       return front[ref.to_s] if DRbURIOption === ref
1402:       @idconv.to_obj(ref)
1403:     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 1381
1381:     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 1376
1376:     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 1453
1453:     def any_to_s(obj)
1454:       obj.to_s + ":#{obj.class}"
1455:     rescue
1456:       sprintf("#<%s:0x%lx>", obj.class, obj.__id__)      
1457:     end

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

[Source]

      # File lib/drb/drb.rb, line 1447
1447:     def insecure_method?(msg_id)
1448:       INSECURE_METHOD.include?(msg_id)
1449:     end

[Source]

      # File lib/drb/drb.rb, line 1412
1412:     def kill_sub_thread
1413:       Thread.new do
1414:         grp = ThreadGroup.new
1415:         grp.add(Thread.current)
1416:         list = @grp.list
1417:         while list.size > 0
1418:           list.each do |th|
1419:             th.kill if th.alive?
1420:           end
1421:           list = @grp.list
1422:         end
1423:       end
1424:     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 1580
1580:     def main_loop
1581:       Thread.start(@protocol.accept) do |client|
1582:         @grp.add Thread.current
1583:         Thread.current['DRb'] = { 'client' => client ,
1584:                                   'server' => self }
1585:         loop do
1586:           begin
1587:             succ = false
1588:             invoke_method = InvokeMethod.new(self, client)
1589:             succ, result = invoke_method.perform
1590:             if !succ && verbose
1591:               p result
1592:               result.backtrace.each do |x|
1593:                 puts x
1594:               end
1595:             end
1596:             client.send_reply(succ, result) rescue nil
1597:           ensure
1598:             client.close unless succ
1599:             if Thread.current['DRb']['stop_service']
1600:               Thread.new { stop_service }
1601:             end
1602:             break unless succ
1603:           end
1604:         end
1605:       end
1606:     end

[Source]

      # File lib/drb/drb.rb, line 1426
1426:     def run
1427:       Thread.start do
1428:         begin
1429:           while true
1430:             main_loop
1431:           end
1432:         ensure
1433:           @protocol.close if @protocol
1434:           kill_sub_thread
1435:         end
1436:       end
1437:     end

[Validate]