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
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
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
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 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
push string to service log
# File lib/universa/service.rb, line 49 def log msg puts "U:Service: #{msg}" end
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
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