Class: Universa::Service

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

Overview

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.

Constant Summary collapse

@@log_umi =
false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeService

Setup service initial parameters



23
24
25
26
27
28
29
30
31
32
# 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

Class Method Details

.configure(&block) ⇒ Object

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



66
67
68
# File 'lib/universa/service.rb', line 66

def configure &block
  instance.configure &block
end

.log_umiObject

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



16
17
18
# File 'lib/universa/service.rb', line 16

def self.log_umi
  @@log_umi = true
end

.umiUMI

Get the global UMI interface, creating it if need.

Returns:

  • (UMI)

    ready interface



73
74
75
76
77
# File 'lib/universa/service.rb', line 73

def umi
  @@instance_lock.synchronize {
    instance.umi
  }
end

Instance Method Details

#configure(&block) ⇒ Object

Implementation of configure

Raises:



35
36
37
38
# 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) ⇒ RemoteAdapter | Ref

Create object proxy for known types

Parameters:

  • ref (Ref)

    to transform

Returns:



56
57
58
59
60
# 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) ⇒ Object

push string to service log



49
50
51
# File 'lib/universa/service.rb', line 49

def log msg
  puts "U:Service: #{msg}"
end

#register_proxy(klass) ⇒ Object

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.

Parameters:

  • klass (Class)

    that will be

Raises:



89
90
91
92
93
94
# 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

#umiObject

Implementation of umi



41
42
43
44
45
# 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