Class: Universa::RemoteAdapter

Inherits:
Delegator
  • Object
show all
Defined in:
lib/universa/service.rb

Overview

The basic class to write remote class adapters (extensions). Delegates contained Ref instance therefore behaves like remote interface with some extensions.

Key feature of RemoteAdapter class is the cross-call persistence. It means once created instances of the RemoteAdapter descendants are cached just like (in fact, instead of) Ref instances, so when the remote party returns the reference to the object once wrapped by this instance, the instance will be returned unless it is already garbage collected. instance will be returned, what means sort of cross-platform calls persistence.

Extending this class normally should not implement the constructor, By defaul the constructor is passed to the remote to create remote instance.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RemoteAdapter

Instantiate new proxy object passing arguments to the remote constructor. The UMI host will try to find overloaded constructor that matches the arguments.

Parameters:

  • args (*Any)

    any arguments that remote constructor may accept.



104
105
106
107
108
109
110
111
112
113
# File 'lib/universa/service.rb', line 104

def initialize(*args)
  if args.length == 1 && args[0].is_a?(ReferenceCreationData)
    @remote = args[0].ref
  else
    # User called constructor
    remote_class_name = self.class.remote_class_name
    remote_class_name&.length or raise Error, "provide remote_class_name"
    @remote = Service.umi.instantiate remote_class_name, *args, adapter: self
  end
end

Class Method Details

.invoke_static(method_name, *args) ⇒ Object



158
159
160
# File 'lib/universa/service.rb', line 158

def self.invoke_static(method_name, *args)
  Service.umi.invoke_static @remote_class_name, method_name, *args
end

.remote_class(name) ⇒ Object

Registers remote class name to be used with this adapted. Call it early in descendant class declaration.



143
144
145
# File 'lib/universa/service.rb', line 143

def self.remote_class name
  @remote_class_name = name
end

.remote_class_nameString

Returns remote class name. There is no need to override it, when inheriting it use remote_class helper:

class MyKeyAddress < ObjectProxy
   remote_class 'com.icodici.crypto.KeyAddress'

   #...
end

Notice: remote_class will do allnecessary work for you.

Returns:

  • (String)

    remote class name



137
138
139
# File 'lib/universa/service.rb', line 137

def self.remote_class_name
  @remote_class_name or raise Error, "provde remote class name"
end

.remote_field(*names) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/universa/service.rb', line 162

def self.remote_field *names
  names.each {|name|
    class_eval <<-End
      def #{name}
        Service.umi.get_field(self,"#{name}")
      end
      def #{name}=(value)
        Service.umi.set_field(self,"#{name}", value)
      end
      End
  }
end

.static_method(name) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/universa/service.rb', line 175

def self.static_method name
  class_eval <<-End
    def self.#{name} *args
      invoke_static "#{name.to_s}", *args
    end
  End
end

Instance Method Details

#__getobj__Ref

Delegated object

Returns:

  • (Ref)

    the wrapped instance whose methpds are delegated by this



117
118
119
# File 'lib/universa/service.rb', line 117

def __getobj__
  @remote
end

#__setobj__Object

Updating proxied object is not allowed. Raises error.



122
123
124
# File 'lib/universa/service.rb', line 122

def __setobj__
  raise "ObectProxy does not support changing referenced object"
end

#inspectObject

debugging label



148
149
150
# File 'lib/universa/service.rb', line 148

def inspect
  "<#{self.class.name}:#{__id__}:#{@remote._remote_class_name}:#{@remote._remote_id}>"
end

#to_sString

call the remote toString(). Does not cache it.

Returns:

  • (String)


154
155
156
# File 'lib/universa/service.rb', line 154

def to_s
  toString()
end