Class: Universa::Service

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

Overview

The service is a singleton to provide porcess-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
# 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, RevokePermission, ModifyDataPermission,  SplitJoinPermission,
   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.



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

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



71
72
73
# File 'lib/universa/service.rb', line 71

def umi
  instance.umi
end

Instance Method Details

#configure(&block) ⇒ Object

Implementation of configure

Raises:



34
35
36
37
# File 'lib/universa/service.rb', line 34

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:



55
56
57
58
59
# File 'lib/universa/service.rb', line 55

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



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

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:



80
81
82
83
84
85
# File 'lib/universa/service.rb', line 80

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



40
41
42
43
44
# File 'lib/universa/service.rb', line 40

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