Class: DRb::DRbObject

Inherits:
Object
  • Object
show all
Defined in:
lib/opal/drb/drb_object.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, uri = nil) ⇒ DRbObject

Returns a new instance of DRbObject.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/opal/drb/drb_object.rb', line 25

def initialize(obj, uri=nil)
  @uri = nil
  @ref = nil
  if obj.nil?
    return if uri.nil?
    @uri, option = DRbProtocol.uri_option(uri, DRb::default_config)
    @ref = DRbURIOption.new(option) unless option.nil?
  else
    @uri = uri ? uri : DRb.current_server.uri
    @ref = obj ? DRb.to_id(obj) : nil
    DRbObject.id2ref[@ref] = obj
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msg_id, *a, &b) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/opal/drb/drb_object.rb', line 66

def method_missing(msg_id, *a, &b)
  promise = Promise.new
  DRbConn.open(@uri) do |conn|
    conn.send_message(self, msg_id, a, b) do |succ, result|
      if succ
        promise.resolve result
      elsif DRbUnknown === result
        promise.resolve result
      else
        bt = self.class.prepare_backtrace(@uri, result)
        result.set_backtrace(bt + caller)
        promise.resolve result
      end
    end
  end
  promise
end

Class Method Details

._load(s) ⇒ Object



3
4
5
6
7
8
# File 'lib/opal/drb/drb_object.rb', line 3

def self._load(s)
  uri, ref = Marshal.load(s)
  self.new_with(uri, ref)
rescue Exception => e
  `console.log(e)`
end

.id2refObject



47
48
49
# File 'lib/opal/drb/drb_object.rb', line 47

def self.id2ref
  @id2ref ||= {}
end

.new_with(uri, ref) ⇒ Object



10
11
12
13
14
15
# File 'lib/opal/drb/drb_object.rb', line 10

def self.new_with(uri, ref)
  it = self.allocate
  it.instance_variable_set(:@uri, uri)
  it.instance_variable_set(:@ref, ref)
  it
end

.new_with_uri(uri) ⇒ Object



17
18
19
# File 'lib/opal/drb/drb_object.rb', line 17

def self.new_with_uri(uri)
  self.new(nil, uri)
end

.prepare_backtrace(uri, result) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/opal/drb/drb_object.rb', line 84

def self.prepare_backtrace(uri, result)
  prefix = "(#{uri}) "
  bt = []
  result.backtrace.each do |x|
    break if /`__send__'$/ =~ x
    if /^\(druby:\/\// =~ x
      bt.push(x)
    else
      bt.push(prefix + x)
    end
  end
  bt
end

Instance Method Details

#__drbrefObject



43
44
45
# File 'lib/opal/drb/drb_object.rb', line 43

def __drbref
  @ref
end

#__drburiObject



39
40
41
# File 'lib/opal/drb/drb_object.rb', line 39

def __drburi
  @uri
end

#_dump(lv) ⇒ Object



21
22
23
# File 'lib/opal/drb/drb_object.rb', line 21

def _dump(lv)
  Marshal.dump([@uri, @ref])
end

#inspectObject



51
52
53
# File 'lib/opal/drb/drb_object.rb', line 51

def inspect
  @ref && @ref.inspect
end

#pretty_print(q) ⇒ Object



98
99
100
# File 'lib/opal/drb/drb_object.rb', line 98

def pretty_print(q)
  q.pp_object(self)
end

#pretty_print_cycle(q) ⇒ Object



102
103
104
105
106
107
# File 'lib/opal/drb/drb_object.rb', line 102

def pretty_print_cycle(q)
  q.object_address_group(self) {
    q.breakable
    q.text '...'
  }
end

#respond_to?(msg_id, priv = false) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
# File 'lib/opal/drb/drb_object.rb', line 55

def respond_to?(msg_id, priv=false)
  case msg_id
  when :_dump
    true
  when :marshal_dump
    false
  else
    false
  end
end