pipe_rpc 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0358adf8660892a8dcd7f7fa2cdc00fa7aec1e7c
4
- data.tar.gz: 260d9f45bd8c159d9a27c5a0f8630da1edc1ad15
3
+ metadata.gz: 404b904e9d7fdd27b2fd405391e5436fef8209ff
4
+ data.tar.gz: ffff7dbe2b497f70071cb1cb1af302f59cc50f2c
5
5
  SHA512:
6
- metadata.gz: eeb67540050b3e85b54f21e0c33e2cc0880ef9b77bafe2aaa0c056b4691dc083087a212e015fe44237d4ba5b709c8f493c013c8e676196a3c606a848b3f4b8ae
7
- data.tar.gz: 7ee01e84a9ae90ac0c4f86820345a9a7c1ea2aab5a5dd1dcb3426649f5a9283c811b4f9b2cdf79098d20ce18e5fac6c376169c0425cf95a57c4df5a3fb1e6779
6
+ metadata.gz: 862dd2c6bb48569342693dfeec30f0844698bb4440cb0c11d29b5740de180b8c7c6ede268976a7f1d6ac5d2d2e27d82d1dfcbea4918e1802dd357e198eb5ada1
7
+ data.tar.gz: 937143f71bca5bfc7c50676e0d68ad6c3bae864915273c0fbf7c8dc00e6393d7b6bdb1ae0b963909f44616a8c7ef1f8e2bfcd201337ff53d374a5fc1ccd7a662
@@ -0,0 +1,18 @@
1
+ Module.class_eval do
2
+ def dup_including(*methods)
3
+ dup.class_eval do
4
+ aliased = (methods.last.is_a? Hash) ? methods.last : {}
5
+ aliased.each{ |from, to| alias_method to, from }
6
+
7
+ to_be_kept_methods = methods + aliased.values
8
+ (all_instance_methods - to_be_kept_methods).each{ |m| remove_method m }
9
+
10
+ self
11
+ end
12
+ end
13
+
14
+ def all_instance_methods
15
+ private_methods = (respond_to? :private_instance_methods) ? private_instance_methods : []
16
+ instance_methods + private_methods
17
+ end
18
+ end
@@ -1,26 +1,20 @@
1
1
  module PipeRpc
2
2
  class BasicInterface < BasicObject
3
+ def self.const_missing(name)
4
+ ::Object.const_get(name)
5
+ end
6
+
3
7
  wanted_methods = [:initialize, :__id__, :object_id, :__send__, :respond_to?, :method_missing]
8
+ wanted_methods += (Object.const_defined? :MRUBY_VERSION) ? [:!] : [:!, :==, :!=, :equal?]
4
9
 
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
10
+ existing_methods = BasicObject.all_instance_methods
11
11
 
12
12
  # Remove unwanted methods
13
13
  unwanted_methods = existing_methods - wanted_methods
14
14
  unwanted_methods.each { |m| undef_method m }
15
15
 
16
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
17
+ include Kernel.dup_including *(wanted_methods - existing_methods)
24
18
 
25
19
  alias send __send__
26
20
  end
@@ -5,9 +5,12 @@ module PipeRpc
5
5
  def initialize(server_id = :default, hub)
6
6
  Kernel.raise ClientInitializationError.new("server id #{server_id.inspect} no symbol") unless server_id.is_a? Symbol
7
7
  @__rpc_server_id__ = server_id
8
+ @__rpc_server_class_name__ = server_id.to_s.split('#').first
8
9
  @hub = hub
9
10
  end
10
11
 
12
+ attr_reader :__rpc_server_class_name__, :__rpc_server_id__
13
+
11
14
  def respond_to?(method)
12
15
  super or method_missing(:respond_to?, method)
13
16
  end
@@ -21,18 +24,4 @@ module PipeRpc
21
24
  "#{TRANSPORT_PREFIX}#{@__rpc_server_id__}"
22
25
  end
23
26
  end
24
-
25
- class << Client
26
- def const_missing(name)
27
- ::Object.const_get(name)
28
- end
29
-
30
- def from_rpc_transport?(transport)
31
- (transport.is_a? String) and (transport.start_with? Hub::Server::TRANSPORT_PREFIX)
32
- end
33
-
34
- def from_rpc_transport(hub, transport)
35
- hub.clients[transport.sub(Hub::Server::TRANSPORT_PREFIX, '').to_sym]
36
- end
37
- end
38
27
  end
@@ -4,7 +4,7 @@ module PipeRpc
4
4
  class Client::Request < Request
5
5
  def initialize(hub, request)
6
6
  @hub = hub
7
- request[:arguments] = TransportMapper.to_transport(request[:arguments], hub)
7
+ request[:arguments] = hub.transport_mapper.to_transport request[:arguments]
8
8
  super request
9
9
  # discard first three entries mentioning Client::Request#initialize,
10
10
  # Client::Request.new and Client#method_missing in stacktrace
@@ -18,7 +18,7 @@ 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
- TransportMapper.from_transport(@value, @request.hub)
21
+ @request.hub.transport_mapper.from_transport @value
22
22
  end
23
23
  end
24
24
  end
data/lib/pipe_rpc/hub.rb CHANGED
@@ -6,9 +6,10 @@ module PipeRpc
6
6
  @requests = Requests.new(self)
7
7
  @servers = Servers.new
8
8
  @clients = Clients.new(self)
9
+ @transport_mapper = TransportMapper.new(self)
9
10
  end
10
11
 
11
- attr_reader :channel, :servers, :clients, :requests, :socket
12
+ attr_reader :channel, :servers, :clients, :requests, :socket, :transport_mapper
12
13
 
13
14
  def handle_message
14
15
  Message.new(self, @socket.read).handle
@@ -12,7 +12,7 @@ module PipeRpc
12
12
  def handle
13
13
  evaluate_result do
14
14
  # the block is passed for asynchronous evaluation
15
- mapped_arguments = TransportMapper.from_transport(arguments, @hub)
15
+ mapped_arguments = @hub.transport_mapper.from_transport arguments
16
16
  @hub.servers[server].__send__(method, *mapped_arguments, &evaluate_result_proc)
17
17
  end
18
18
  end
@@ -5,7 +5,7 @@ module PipeRpc
5
5
  end
6
6
 
7
7
  def value
8
- TransportMapper.to_transport(@result, @request.hub)
8
+ @request.hub.transport_mapper.to_transport @result
9
9
  end
10
10
 
11
11
  def to_response
@@ -5,8 +5,8 @@ module PipeRpc
5
5
  end
6
6
 
7
7
  def add(server)
8
- id, server = if server.respond_to? :to_rpc_server_id
9
- [server.to_rpc_server_id, server]
8
+ id, server = if server.respond_to? :__rpc_server_id__
9
+ [server.__rpc_server_id__, server]
10
10
  else
11
11
  server.first
12
12
  end
@@ -0,0 +1,30 @@
1
+ module PipeRpc
2
+ class Mapper; end
3
+ class Hub::TransportMapper < Mapper
4
+ def initialize(hub)
5
+ @hub = hub
6
+ end
7
+
8
+ def to_transport(object)
9
+ map(object) do |value|
10
+ if value.respond_to? :to_rpc_transport
11
+ value.to_rpc_transport(@hub)
12
+ else
13
+ value
14
+ end
15
+ end
16
+ end
17
+
18
+ def from_transport(object)
19
+ map(object) do |value|
20
+ if (value.is_a? String) and (value.start_with? Client::TRANSPORT_PREFIX)
21
+ @hub.servers[value.sub(Client::TRANSPORT_PREFIX, '').to_sym]
22
+ elsif (value.is_a? String) and (value.start_with? Server::TRANSPORT_PREFIX)
23
+ @hub.clients[value.sub(Server::TRANSPORT_PREFIX, '').to_sym]
24
+ else
25
+ value
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ module PipeRpc
2
+ class Mapper
3
+ def map(object, &mapper)
4
+ if Client === object
5
+ # first filter out clients so for them subsequent checks are not sent
6
+ # through the pipe
7
+ yield object
8
+ elsif Hash === object
9
+ object.map{ |k, v| [k, map(v, &mapper)] }.to_h
10
+ elsif object.respond_to? :map
11
+ object.map{ |item| map(item, &mapper) }
12
+ else
13
+ yield object
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ module PipeRpc
2
+ class Server < BasicInterface
3
+ TRANSPORT_PREFIX = "__rpc_server__"
4
+
5
+ include Kernel.dup_including :object_id, class: :__class__
6
+ private :__class__
7
+
8
+ def __rpc_server_class_name__
9
+ __class__.to_s
10
+ end
11
+
12
+ def __rpc_server_id__
13
+ "#{__rpc_server_class_name__}##{__id__}".to_sym
14
+ end
15
+
16
+ def to_rpc_transport(hub)
17
+ hub.servers.add(self) unless hub.servers.registered? __rpc_server_id__
18
+ "#{TRANSPORT_PREFIX}#{__rpc_server_id__}"
19
+ end
20
+ end
21
+
22
+ class << Server
23
+ def instance_eval_for(server, *args, &block)
24
+ Object.instance_method(:instance_eval).bind(server).call(*args, &block)
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module PipeRpc
2
- VERSION = "2.3.0"
2
+ VERSION = "2.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pipe_rpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Aue
@@ -98,6 +98,7 @@ files:
98
98
  - Rakefile
99
99
  - lib/msgpack_config.rb
100
100
  - lib/pipe_rpc.rb
101
+ - lib/pipe_rpc/_pipe_rpc.rb
101
102
  - lib/pipe_rpc/basic_interface.rb
102
103
  - lib/pipe_rpc/client.rb
103
104
  - lib/pipe_rpc/client_request.rb
@@ -117,12 +118,13 @@ files:
117
118
  - lib/pipe_rpc/hub_request_error_result.rb
118
119
  - lib/pipe_rpc/hub_request_result.rb
119
120
  - lib/pipe_rpc/hub_requests.rb
120
- - lib/pipe_rpc/hub_server.rb
121
121
  - lib/pipe_rpc/hub_servers.rb
122
122
  - lib/pipe_rpc/hub_socket.rb
123
+ - lib/pipe_rpc/hub_transport_mapper.rb
124
+ - lib/pipe_rpc/mapper.rb
123
125
  - lib/pipe_rpc/request.rb
124
126
  - lib/pipe_rpc/result_response.rb
125
- - lib/pipe_rpc/transport_mapper.rb
127
+ - lib/pipe_rpc/server.rb
126
128
  - lib/pipe_rpc/version.rb
127
129
  - pipe_rpc.gemspec
128
130
  homepage: https://github.com/christopheraue/ruby-pipe_rpc
@@ -1,40 +0,0 @@
1
- module PipeRpc
2
- class Hub::Server < BasicInterface
3
- TRANSPORT_PREFIX = "__rpc_server__"
4
-
5
- kernel = ::Kernel.dup
6
- kernel.class_eval do
7
- alias_method :__rpc_server_class__, :class
8
- (instance_methods - [:__rpc_server_class__, :object_id]).each{ |m| remove_method m }
9
- private_instance_methods.each{ |m| remove_method m } if respond_to? :private_instance_methods
10
- end
11
- include kernel
12
-
13
- def to_rpc_server_id
14
- "#{__rpc_server_class__}##{__id__}".to_sym
15
- end
16
-
17
- def to_rpc_transport(hub)
18
- hub.servers.add(self) unless hub.servers.registered? to_rpc_server_id
19
- "#{TRANSPORT_PREFIX}#{to_rpc_server_id}"
20
- end
21
- end
22
-
23
- class << Hub::Server
24
- def const_missing(name)
25
- ::Object.const_get(name)
26
- end
27
-
28
- def instance_eval_for(server, *args, &block)
29
- Object.instance_method(:instance_eval).bind(server).call(*args, &block)
30
- end
31
-
32
- def from_rpc_transport?(transport)
33
- (transport.is_a? String) and (transport.start_with? Client::TRANSPORT_PREFIX)
34
- end
35
-
36
- def from_rpc_transport(hub, transport)
37
- hub.servers[transport.sub(Client::TRANSPORT_PREFIX, '').to_sym]
38
- end
39
- end
40
- end
@@ -1,39 +0,0 @@
1
- module PipeRpc
2
- class TransportMapper
3
- def self.map(object, &mapper)
4
- if Client === object
5
- # first filter out clients so for them subsequent checks are not sent
6
- # through the pipe
7
- yield object
8
- elsif Hash === object
9
- object.map{ |k, v| [k, map(v, &mapper)] }.to_h
10
- elsif object.respond_to? :map
11
- object.map{ |item| map(item, &mapper) }
12
- else
13
- yield object
14
- end
15
- end
16
-
17
- def self.to_transport(object, hub)
18
- map(object) do |value|
19
- if value.respond_to? :to_rpc_transport
20
- value.to_rpc_transport(hub)
21
- else
22
- value
23
- end
24
- end
25
- end
26
-
27
- def self.from_transport(object, hub)
28
- map(object) do |value|
29
- if Hub::Server.from_rpc_transport? value
30
- Hub::Server.from_rpc_transport(hub, value)
31
- elsif Client.from_rpc_transport? value
32
- Client.from_rpc_transport(hub, value)
33
- else
34
- value
35
- end
36
- end
37
- end
38
- end
39
- end