pipe_rpc 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pipe_rpc/basic_interface.rb +27 -0
- data/lib/pipe_rpc/client.rb +11 -15
- data/lib/pipe_rpc/client_request.rb +26 -3
- data/lib/pipe_rpc/client_request_result.rb +3 -1
- data/lib/pipe_rpc/hub_server.rb +19 -0
- data/lib/pipe_rpc/hub_servers.rb +12 -8
- data/lib/pipe_rpc/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1991805b18e6be65590ca3b7467624b7c6fd804b
|
4
|
+
data.tar.gz: e61fcf89ca3cd3a8a8b0be4ecc195c2173cd8961
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e48baa8052601b1ab367a6425225bb1a2df1b941bd5ef6127d1bc4569a6ee57d02a72381f63f45ebd1fb3cf194fbb39b741eca8218ef1b99416a82e8df37ed04
|
7
|
+
data.tar.gz: 4395b41a3b820e9e79e7727a47527878dd8d169801aa28398ad59dbea25d8e2ce64975fb024c38495781cc766302c7e699293120c3e762ffce131b3af1f15d18
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module PipeRpc
|
2
|
+
class BasicInterface < BasicObject
|
3
|
+
wanted_methods = [:initialize, :__id__, :__send__, :respond_to?, :method_missing]
|
4
|
+
|
5
|
+
existing_methods = if ::Object.const_defined?(:MRUBY_VERSION)
|
6
|
+
[:initialize, :method_missing]
|
7
|
+
else
|
8
|
+
[:initialize, :__id__, :__send__, :instance_eval, :instance_exec, :method_missing,
|
9
|
+
:singleton_method_added, :singleton_method_removed, :singleton_method_undefined]
|
10
|
+
end
|
11
|
+
|
12
|
+
# Remove unwanted methods
|
13
|
+
unwanted_methods = existing_methods - wanted_methods
|
14
|
+
unwanted_methods.each { |m| undef_method m }
|
15
|
+
|
16
|
+
# Add non-existing methods by including sorted out Kernel module
|
17
|
+
to_be_added_methods = wanted_methods - existing_methods
|
18
|
+
kernel = ::Kernel.dup
|
19
|
+
kernel.class_eval do
|
20
|
+
(instance_methods - to_be_added_methods).each{ |m| remove_method m }
|
21
|
+
private_instance_methods.each{ |m| remove_method m } if respond_to? :private_instance_methods
|
22
|
+
end
|
23
|
+
include kernel
|
24
|
+
|
25
|
+
alias send __send__
|
26
|
+
end
|
27
|
+
end
|
data/lib/pipe_rpc/client.rb
CHANGED
@@ -1,27 +1,23 @@
|
|
1
1
|
module PipeRpc
|
2
|
-
class Client <
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
def initialize(server = :default, hub)
|
7
|
-
@server = server.to_sym
|
2
|
+
class Client < BasicInterface
|
3
|
+
def initialize(server_id = :default, hub)
|
4
|
+
@__rpc_server_id__ = server_id.to_sym
|
8
5
|
@hub = hub
|
9
6
|
end
|
10
7
|
|
8
|
+
attr_reader :__rpc_server_id__
|
9
|
+
|
10
|
+
def respond_to?(method)
|
11
|
+
super or method_missing(:respond_to?, method)
|
12
|
+
end
|
13
|
+
|
11
14
|
def method_missing(method, *args)
|
12
|
-
request = Client::Request.new(server: @
|
15
|
+
request = Client::Request.new(@hub, server: @__rpc_server_id__, method: method, arguments: args)
|
13
16
|
@hub.requests.evaluate(request)
|
14
17
|
end
|
15
|
-
alias __send__ method_missing
|
16
|
-
alias send method_missing
|
17
|
-
alias public_send method_missing
|
18
18
|
|
19
19
|
def to_s
|
20
|
-
"<Client:#{@
|
21
|
-
end
|
22
|
-
|
23
|
-
def is_a?(object)
|
24
|
-
object == Client
|
20
|
+
"<Client:#{@__rpc_server_id__}>"
|
25
21
|
end
|
26
22
|
end
|
27
23
|
end
|
@@ -2,15 +2,17 @@ class PipeRpc::Request; end
|
|
2
2
|
|
3
3
|
module PipeRpc
|
4
4
|
class Client::Request < Request
|
5
|
-
def initialize(
|
6
|
-
|
5
|
+
def initialize(hub, request)
|
6
|
+
@hub = hub
|
7
|
+
request[:arguments] = arguments_to_server_ids(request[:arguments])
|
8
|
+
super request
|
7
9
|
# discard first three entries mentioning Client::Request#initialize,
|
8
10
|
# Client::Request.new and Client#method_missing in stacktrace
|
9
11
|
@stacktrace = Kernel.respond_to?(:caller) ? Kernel.caller(3).to_a : []
|
10
12
|
@result = Result.new(self)
|
11
13
|
end
|
12
14
|
|
13
|
-
attr_reader :stacktrace, :result
|
15
|
+
attr_reader :hub, :stacktrace, :result
|
14
16
|
|
15
17
|
def id=(id)
|
16
18
|
raise 'id already set' if @id
|
@@ -20,5 +22,26 @@ module PipeRpc
|
|
20
22
|
def id
|
21
23
|
@id or raise 'id not set'
|
22
24
|
end
|
25
|
+
|
26
|
+
def arguments_to_server_ids(arguments)
|
27
|
+
deep_map(arguments) do |value|
|
28
|
+
if value.respond_to? :__rpc_server_id__ # is a client
|
29
|
+
value.__rpc_server_id__
|
30
|
+
elsif value.respond_to? :to_rpc_server_id # is a servable
|
31
|
+
@hub.servers.add(value)
|
32
|
+
value.to_rpc_server_id
|
33
|
+
else
|
34
|
+
value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def deep_map(object, &mapper)
|
40
|
+
case object
|
41
|
+
when Hash then object.map{ |k, v| [k, deep_map(v, &mapper)] }.to_h
|
42
|
+
when Array then object.map{ |item| deep_map(item, &mapper) }
|
43
|
+
else yield object
|
44
|
+
end
|
45
|
+
end
|
23
46
|
end
|
24
47
|
end
|
@@ -18,7 +18,9 @@ module PipeRpc
|
|
18
18
|
@value.set_backtrace(@value.backtrace.to_a + @request.stacktrace) if @value.respond_to? :set_backtrace
|
19
19
|
raise @value
|
20
20
|
else
|
21
|
-
@value
|
21
|
+
@request.deep_map(@value) do |value|
|
22
|
+
(@request.hub.servers.registered? value) ? @request.hub.servers[value] : value
|
23
|
+
end
|
22
24
|
end
|
23
25
|
end
|
24
26
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module PipeRpc
|
2
|
+
class Hub::Server < BasicInterface
|
3
|
+
kernel = ::Kernel.dup
|
4
|
+
kernel.class_eval do
|
5
|
+
alias_method :__rpc_server_class__, :class
|
6
|
+
(instance_methods - [:__rpc_server_class__]).each{ |m| remove_method m }
|
7
|
+
private_instance_methods.each{ |m| remove_method m } if respond_to? :private_instance_methods
|
8
|
+
end
|
9
|
+
include kernel
|
10
|
+
|
11
|
+
def self.const_missing(name)
|
12
|
+
::Object.const_get(name)
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_rpc_server_id
|
16
|
+
"#{self.__rpc_server_class__.to_s}##{__id__}".to_sym
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/pipe_rpc/hub_servers.rb
CHANGED
@@ -4,20 +4,24 @@ module PipeRpc
|
|
4
4
|
@servers = Hash.new{ |hash, key| raise NoServerError.new("no server #{key}") }
|
5
5
|
end
|
6
6
|
|
7
|
-
def add(servers)
|
8
|
-
|
7
|
+
def add(*servers)
|
8
|
+
if servers.first.respond_to? :to_rpc_server_id
|
9
|
+
servers.each{ |server| @servers[server.to_rpc_server_id] = server }
|
10
|
+
else
|
11
|
+
servers.first.each{ |id, server| @servers[id.to_sym] = server }
|
12
|
+
end
|
9
13
|
end
|
10
14
|
|
11
|
-
def [](
|
12
|
-
@servers[
|
15
|
+
def [](id)
|
16
|
+
@servers[id.to_sym]
|
13
17
|
end
|
14
18
|
|
15
|
-
def remove(
|
16
|
-
@servers.delete(
|
19
|
+
def remove(id)
|
20
|
+
@servers.delete(id.to_sym)
|
17
21
|
end
|
18
22
|
|
19
|
-
def registered?(
|
20
|
-
@servers.has_key?(
|
23
|
+
def registered?(id)
|
24
|
+
@servers.has_key?(id)
|
21
25
|
end
|
22
26
|
end
|
23
27
|
end
|
data/lib/pipe_rpc/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pipe_rpc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christopher Aue
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: msgpack
|
@@ -98,6 +98,7 @@ files:
|
|
98
98
|
- Rakefile
|
99
99
|
- lib/msgpack_config.rb
|
100
100
|
- lib/pipe_rpc.rb
|
101
|
+
- lib/pipe_rpc/basic_interface.rb
|
101
102
|
- lib/pipe_rpc/client.rb
|
102
103
|
- lib/pipe_rpc/client_request.rb
|
103
104
|
- lib/pipe_rpc/client_request_error_response.rb
|
@@ -116,6 +117,7 @@ files:
|
|
116
117
|
- lib/pipe_rpc/hub_request_error_result.rb
|
117
118
|
- lib/pipe_rpc/hub_request_result.rb
|
118
119
|
- lib/pipe_rpc/hub_requests.rb
|
120
|
+
- lib/pipe_rpc/hub_server.rb
|
119
121
|
- lib/pipe_rpc/hub_servers.rb
|
120
122
|
- lib/pipe_rpc/hub_socket.rb
|
121
123
|
- lib/pipe_rpc/request.rb
|