class Universa::Service

The service is a singleton to provide process-wide objects and methods. For example, the {UMI} interface and reference class factory are unique per-process for Universa library. It uses exactly one lazy created {UMI} connection which is shared among all threads. As UMI server is multithreaded by nature, is will not block ruby threads waiting for remote invocation.

Public Class Methods

configure(&block) click to toggle source

Call it before everything to update UMI interface parameters before is is created. Calling it when UMI is already constructed raises Error.

# File lib/universa/service.rb, line 66
def configure &block
  instance.configure &block
end
log_umi() click to toggle source

set log mode for UMI commands. Works only when called before any Service usage, e.g. before the UMI client has been constructed.

# File lib/universa/service.rb, line 16
def self.log_umi
  @@log_umi = true
end
new() click to toggle source

Setup service initial parameters

# File lib/universa/service.rb, line 23
def initialize
  @config = SmartHash.new path: nil
  @@log_umi && @config['log'] = 'umi.log'
  @known_proxies = {}
  [Contract, PrivateKey, PublicKey, KeyAddress, HashId, Binder,
   Role, SimpleRole, RoleLink, ListRole, Parcel, UnsContract,
   ChangeOwnerPermission, ChangeRolePermission, RevokePermission,
   ModifyDataPermission,  SplitJoinPermission, QuorumVoteRole,
   UmiClient, Duration, Compound, KeyInfo, PBKDF2].each {|klass| register_proxy klass}
end
umi() click to toggle source

Get the global UMI interface, creating it if need. @return [UMI] ready interface

# File lib/universa/service.rb, line 73
def umi
  @@instance_lock.synchronize {
    instance.umi
  }
end

Public Instance Methods

configure(&block) click to toggle source

Implementation of {Service.configure}

# File lib/universa/service.rb, line 35
def configure &block
  raise Error, "config call must happen before interface creation" if @umi
  block.call @config
end
create_proxy(ref) click to toggle source

Create object proxy for known types @param [Ref] ref to transform @return [RemoteAdapter | Ref] transformed or source reference

# File lib/universa/service.rb, line 56
def create_proxy ref
  proxy_class = @known_proxies[ref._remote_class_name]
  return ref unless proxy_class
  proxy_class.new(ReferenceCreationData.new(ref))
end
log(msg) click to toggle source

push string to service log

# File lib/universa/service.rb, line 49
def log msg
  puts "U:Service: #{msg}"
end
register_proxy(klass) click to toggle source

Register a class that will work as a proxy for UMI remote class. Such adapter class mist extend RemoteAdapter class. Once the class is registered, serive will automatically instantiate it when UMI will pass the instance of the corresponding remote class. @param [Class] klass that will be

# File lib/universa/service.rb, line 89
def register_proxy(klass)
  klass < RemoteAdapter or raise ArgumentError, "#{klass.name} must be based on RemoteAdapter"
  remote_class_name = klass.remote_class_name
  raise Error, "#{remote_class_name} is already registered in Service" if @known_proxies.include?(remote_class_name)
  @known_proxies[remote_class_name] = klass
end
umi() click to toggle source

Implementation of {Service.umi}

# File lib/universa/service.rb, line 41
def umi
  c = @config.to_h.transform_keys(&:to_sym).update(convert_case: true)
  c[:factory] = -> (ref) {create_proxy(ref)}
  @umi ||= UMI.new(c.delete(:path), **c)
end