onedrb 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/lib/onedrb.rb +209 -0
  4. data.tar.gz.sig +0 -0
  5. metadata +22 -2
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1715575559f96238261a54678e475a056223491a3ff7c610a5df823001fe454c
4
- data.tar.gz: 66acb7de842732715d02c287c753d1221d2963631b6bc2edd3ca842eecd88665
3
+ metadata.gz: e484349df963bdb3ab1dafb85fa5697dc9460c330d1f863e8ed723ca941e8fcc
4
+ data.tar.gz: c58a17ce4eddc48d611115ae13d5dbd97fddd983c3e2ca537b478f4029f663d3
5
5
  SHA512:
6
- metadata.gz: befc1ab68c1a9ede94f24415faeeb3fbbfcfc044edd3e73cdda361f3fa8cf77d309158804aecc5c2b1f995ee81b7bd382dbe359f798c24bfa222f9422479cff3
7
- data.tar.gz: a83d5eb21b2270e5e51c1c8918fffe1b510c43237076f137dbe8d788a5e6394f964bbd10db08d3be4abde3f8e0551766b280c3ba27e6cb94b03fcb579cb03723
6
+ metadata.gz: 96d256270dbc280f69f0e9873787c608132f9ebf45cf20bd06be1a69a2845080e2bd2177d8b35fdce8ca344d6e0edb50c5bf5e3893c6941600c6ec1a3de1fe09
7
+ data.tar.gz: 4c119540720a72be369dec14c3156fa9f96c849b93df6c49229c14d838cb93846058496d53621a64242b72176ecf8ce800f657339f11c8221affa4f3fc0e373d
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/onedrb.rb CHANGED
@@ -6,6 +6,215 @@
6
6
 
7
7
  require 'drb'
8
8
  require 'c32'
9
+ require 'app-mgr'
10
+
11
+
12
+ # Note: The Server object can also use a default ServiceMgr object.
13
+ # Which allows multiple services (user-defined objects) to be
14
+ # hosted dynamically.
15
+ # OneDrb recommended port: 57844 (as this port is recognised by the RSC gem)
16
+ #
17
+ class ServiceMgr < AppMgr
18
+
19
+ def initialize(rsf: nil, debug: false)
20
+ super(rsf: rsf, type: 'service', debug: debug)
21
+ @services = @app
22
+ end
23
+
24
+
25
+ def call(service, methodname, *a)
26
+
27
+ return @services[service.to_sym][:running] unless methodname
28
+ proc1 = @services[service.to_sym][:running].method(methodname.to_sym)
29
+ a.last.is_a?(Hash) ? proc1.call(*a[0..-2], **a.last) : proc1.call(*a)
30
+
31
+ end
32
+
33
+
34
+ # OneDRb version 2; Allows multiple user-defined objects to be
35
+ # served from 1 DRb service
36
+ #
37
+ # not used by any client, but may be in future
38
+ #
39
+ #def odrb2?()
40
+ # true
41
+ #end
42
+
43
+ def services()
44
+ @services.map do |key, object|
45
+ next unless object[:running]
46
+ [key, object[:running].public_methods - Object.public_methods]
47
+ end.compact
48
+ end
49
+
50
+ def stop(service)
51
+ super(service).sub('app', 'service')
52
+ end
53
+
54
+ def unload(service)
55
+ super(service).sub('app', 'service')
56
+ end
57
+
58
+ def method_missing(sym, *args)
59
+ #puts 'servicemgr sym: ' + sym.inspect
60
+ #puts 'args: ' + args.inspect
61
+ end
62
+ end
63
+
64
+
65
+ class OneDrbError < Exception
66
+ end
67
+
68
+
69
+ module OneDrb
70
+
71
+ class Server
72
+ using ColouredText
73
+
74
+ def initialize(host: '127.0.0.1', port: (49152..65535).to_a.sample.to_s,
75
+ obj: nil, log: nil)
76
+
77
+ log.info self.class.to_s + '/initialize: active' if log
78
+
79
+ @host, @port, @log = host, port, log
80
+
81
+ if obj then
82
+ @obj = obj
83
+ else
84
+ msg = "No object supplied!".error
85
+ puts msg
86
+ raise OneDrbError, msg
87
+ end
88
+
89
+ log.info self.class.to_s +'/initialize: completed' if log
90
+
91
+ end
92
+
93
+ def start()
94
+
95
+ @log.info self.class.to_s +'/start: active' if @log
96
+ puts (self.class.to_s + " running on port " + @port).info
97
+ DRb.start_service "druby://#{@host}:#{@port}", @obj
98
+ DRb.thread.join
99
+
100
+ end
101
+
102
+ end
103
+
104
+
105
+ class Client
106
+ using ColouredText
107
+
108
+ def initialize(host: '127.0.0.1', port: nil)
109
+
110
+ DRb.start_service
111
+
112
+ puts 'no port supplied'.error unless port
113
+
114
+ puts ('client connecting to port ' + port).info
115
+ @obj = DRbObject.new_with_uri("druby://#{host}:#{port}")
116
+ parent = self
117
+ @obj&.services.each do |name, methods|
118
+
119
+ make_class(name, methods)
120
+
121
+ end
122
+
123
+ end
124
+
125
+ # Makes a remote call in 1 line of code using a URI
126
+ # Only available when using the ServiceMgr as the server object;
127
+ #
128
+ # e.g. OneDrb::Client.call 'odrb://clara.home/fun/go?arg=James&age=49'
129
+ #
130
+ def self.call(s, port: nil)
131
+
132
+ r = s.match(/^odrb:\/\/([^\/]+)\/([^\/]+)\/([^\?]+)\??(.*)/).captures
133
+
134
+ rawhostname, service, methodname, rawargs = r
135
+ hostname, rawport = rawhostname.split(':',2)
136
+ port ||= rawport
137
+
138
+ rawargs2 = rawargs&.split('&').map {|x| x.split('=')}
139
+
140
+ a1, a2 = rawargs2.partition do |field, value|
141
+ field.downcase.to_sym == :arg
142
+ end
143
+
144
+ h = a2.map {|k,v| [k.to_sym, v]}.to_h
145
+ a = a1.map(&:last)
146
+
147
+ client = OneDrb::Client.new host: hostname, port: port
148
+
149
+ if h.any? then
150
+
151
+ if a.any? then
152
+ client.call service.to_sym, methodname.to_sym, *a, **h
153
+ else
154
+ client.call service.to_sym, methodname.to_sym, **h
155
+ end
156
+
157
+ elsif a.any?
158
+
159
+ client.call service.to_sym, methodname.to_sym, *a
160
+
161
+ else
162
+
163
+ client.call service.to_sym, methodname.to_sym
164
+
165
+ end
166
+
167
+ end
168
+
169
+ def remote()
170
+ @obj
171
+ end
172
+
173
+ def restart(service)
174
+
175
+ return unless @obj.respond_to? :services
176
+
177
+ @obj.restart(service)
178
+ found = @obj.services.assoc(service)
179
+ make_class(*found) if found.any?
180
+ end
181
+
182
+ def method_missing(sym, *args)
183
+
184
+ if args.last.is_a?(Hash) then
185
+ @obj.send(sym, *args[0..-2], **args.last)
186
+ else
187
+ @obj.send(sym, *args)
188
+ end
189
+
190
+ end
191
+
192
+ private
193
+
194
+ def make_class(name, methods)
195
+
196
+ class_name = name.capitalize
197
+ klass = Object.const_set(class_name,Class.new)
198
+
199
+ klass.class_eval do
200
+ methods.each do |method_name|
201
+ define_method method_name do |*args|
202
+ parent.call name, method_name, *args
203
+ end
204
+ end
205
+ end
206
+
207
+ define_singleton_method name do
208
+ klass.new
209
+ end
210
+
211
+ end
212
+
213
+ end
214
+
215
+ end
216
+
217
+
9
218
 
10
219
  # Note: The Server object can also use a default ServiceMgr object.
11
220
  # Which allows multiple services (user-defined objects) to be
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onedrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  lK7OtCGPauDHVuIWDW0EnHb+MMBt6F5SqC+PSy9iOzs4zAWkJMFp86MOEK2yrfnO
36
36
  /ghrVtneK08DqmYOaive8I6P
37
37
  -----END CERTIFICATE-----
38
- date: 2022-04-23 00:00:00.000000000 Z
38
+ date: 2022-04-24 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: c32
@@ -57,6 +57,26 @@ dependencies:
57
57
  - - ">="
58
58
  - !ruby/object:Gem::Version
59
59
  version: 0.3.0
60
+ - !ruby/object:Gem::Dependency
61
+ name: app-mgr
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '0.4'
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 0.4.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.4'
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 0.4.0
60
80
  description:
61
81
  email: digital.robertson@gmail.com
62
82
  executables: []
metadata.gz.sig CHANGED
Binary file