Class: Universa::Service
- Inherits:
-
Object
- Object
- Universa::Service
- 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
-
.configure(&block) ⇒ Object
Call it before everything to update UMI interface parameters before is is created.
-
.log_umi ⇒ Object
set log mode for UMI commands.
-
.umi ⇒ UMI
Get the global UMI interface, creating it if need.
Instance Method Summary collapse
-
#configure(&block) ⇒ Object
Implementation of Service.configure.
-
#create_proxy(ref) ⇒ RemoteAdapter | Ref
Create object proxy for known types.
-
#initialize ⇒ Service
constructor
Setup service initial parameters.
-
#log(msg) ⇒ Object
push string to service log.
-
#register_proxy(klass) ⇒ Object
Register a class that will work as a proxy for UMI remote class.
-
#umi ⇒ Object
Implementation of Service.umi.
Constructor Details
#initialize ⇒ Service
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_umi ⇒ Object
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 |
.umi ⇒ UMI
Get the global UMI interface, creating it if need.
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
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
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.
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 |