pipe_rpc 2.4.0 → 2.5.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: 404b904e9d7fdd27b2fd405391e5436fef8209ff
4
- data.tar.gz: ffff7dbe2b497f70071cb1cb1af302f59cc50f2c
3
+ metadata.gz: b0b220bb6af80445f30658013667fa408749b8c4
4
+ data.tar.gz: 3df1bab18503474475c842ae78db2e9c6bcec98a
5
5
  SHA512:
6
- metadata.gz: 862dd2c6bb48569342693dfeec30f0844698bb4440cb0c11d29b5740de180b8c7c6ede268976a7f1d6ac5d2d2e27d82d1dfcbea4918e1802dd357e198eb5ada1
7
- data.tar.gz: 937143f71bca5bfc7c50676e0d68ad6c3bae864915273c0fbf7c8dc00e6393d7b6bdb1ae0b963909f44616a8c7ef1f8e2bfcd201337ff53d374a5fc1ccd7a662
6
+ metadata.gz: c114586ee4dd7873b17ec13a083cc57cdc1d00cc496bed4a857d2f82b60969646bbafdaad3e12095f4e5537054118970db2ec4944a0dd1821ee06d48af25464b
7
+ data.tar.gz: 83e3feddd8530083ff24eeb73fc05cdd6cf1c594bb4faf5ad7d216141145fe662b7b4b91da61d3de4c87d9a2f404d32b0a1eba06b5de419b6b28bd9b4284b154
@@ -15,4 +15,18 @@ Module.class_eval do
15
15
  private_methods = (respond_to? :private_instance_methods) ? private_instance_methods : []
16
16
  instance_methods + private_methods
17
17
  end
18
+
19
+ if const_defined? :MRUBY_VERSION
20
+ alias_method :simple_const_get, :const_get
21
+
22
+ def const_get(constant_path, *args)
23
+ if constant_path.is_a? String
24
+ constant_path.split('::').inject(self) do |mod, constant|
25
+ mod.simple_const_get(constant.to_sym, *args)
26
+ end
27
+ else
28
+ simple_const_get(constant_path, *args)
29
+ end
30
+ end
31
+ end
18
32
  end
@@ -1,7 +1,29 @@
1
1
  module PipeRpc
2
2
  class BasicInterface < BasicObject
3
- def self.const_missing(name)
4
- ::Object.const_get(name)
3
+ class << self
4
+ def const_missing(name)
5
+ ::Object.const_get(name)
6
+ end
7
+
8
+ def instance_eval_for(object, *args, &block)
9
+ BasicInterface.add_instance_eval
10
+ object.instance_eval(*args, &block)
11
+ ensure
12
+ BasicInterface.remove_instance_eval
13
+ end
14
+
15
+ def remove_instance_eval
16
+ class_eval do
17
+ def instance_eval(*args)
18
+ caller = (Kernel.respond_to? :caller) ? [Kernel.caller(1)] : nil
19
+ Kernel.raise ::NoMethodError, "undefined method `instance_eval'", *caller
20
+ end
21
+ end
22
+ end
23
+
24
+ def add_instance_eval
25
+ __send__(:remove_method, :instance_eval)
26
+ end
5
27
  end
6
28
 
7
29
  wanted_methods = [:initialize, :__id__, :object_id, :__send__, :respond_to?, :method_missing]
@@ -9,9 +31,11 @@ module PipeRpc
9
31
 
10
32
  existing_methods = BasicObject.all_instance_methods
11
33
 
12
- # Remove unwanted methods
34
+ # Remove unwanted methods (while treating #instance_eval specially so we
35
+ # can selectively use it)
13
36
  unwanted_methods = existing_methods - wanted_methods
14
- unwanted_methods.each { |m| undef_method m }
37
+ (unwanted_methods - [:instance_eval]).each { |m| undef_method m }
38
+ remove_instance_eval
15
39
 
16
40
  # Add non-existing methods by including sorted out Kernel module
17
41
  include Kernel.dup_including *(wanted_methods - existing_methods)
@@ -2,6 +2,8 @@ module PipeRpc
2
2
  class Client < BasicInterface
3
3
  TRANSPORT_PREFIX = "__rpc_client__"
4
4
 
5
+ undef_method :instance_eval
6
+
5
7
  def initialize(server_id = :default, hub)
6
8
  Kernel.raise ClientInitializationError.new("server id #{server_id.inspect} no symbol") unless server_id.is_a? Symbol
7
9
  @__rpc_server_id__ = server_id
@@ -11,8 +13,8 @@ module PipeRpc
11
13
 
12
14
  attr_reader :__rpc_server_class_name__, :__rpc_server_id__
13
15
 
14
- def respond_to?(method)
15
- super or method_missing(:respond_to?, method)
16
+ def respond_to?(*args)
17
+ super or method_missing(:respond_to?, *args)
16
18
  end
17
19
 
18
20
  def method_missing(method, *args)
@@ -20,8 +22,15 @@ module PipeRpc
20
22
  @hub.requests.evaluate(request)
21
23
  end
22
24
 
23
- def to_rpc_transport(hub = nil)
25
+ def to_rpc_transport(gateway)
24
26
  "#{TRANSPORT_PREFIX}#{@__rpc_server_id__}"
25
27
  end
26
28
  end
29
+
30
+ class << Client
31
+ def from_rpc_transport(gateway, transport)
32
+ client = gateway.clients[transport.sub(Server::TRANSPORT_PREFIX, '').to_sym]
33
+ ClientWrapper.from_rpc_transport(gateway, client)
34
+ end
35
+ end
27
36
  end
@@ -21,7 +21,7 @@ module PipeRpc
21
21
  private
22
22
 
23
23
  def no_method_error
24
- NoMethodError.new("undefined method `#{@data.fetch(:method)}' for <Client:#{@request.server}>")
24
+ NoMethodError.new("undefined method `#{@data.fetch(:method)}' for #{@request.server}")
25
25
  end
26
26
 
27
27
  def argument_error
@@ -0,0 +1,29 @@
1
+ module PipeRpc
2
+ module ClientWrappers; end
3
+
4
+ class ClientWrapper < BasicInterface
5
+ def initialize(gateway, client)
6
+ @client = client
7
+ @gateway = gateway
8
+ end
9
+
10
+ def inspect
11
+ __rpc_server_id__
12
+ end
13
+ alias to_s inspect
14
+
15
+ def method_missing(method, *args)
16
+ @client.__send__(method, *args)
17
+ end
18
+ end
19
+
20
+ class << ClientWrapper
21
+ def from_rpc_transport(gateway, client)
22
+ if gateway.client_wrapper_class_namespace.const_defined?(client.__rpc_server_class_name__, false)
23
+ gateway.client_wrapper_class_namespace.const_get(client.__rpc_server_class_name__, false).new(gateway, client)
24
+ else
25
+ client
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,9 +1,13 @@
1
1
  module PipeRpc
2
2
  class Gateway
3
- def initialize(args)
4
- @hub = Hub.new(args)
3
+ def initialize(channels)
4
+ @hub = Hub.new(self, channels)
5
+ @client_wrapper_class_namespace = ClientWrappers
6
+ @subject_server_class_namespace = SubjectServers
5
7
  end
6
8
 
9
+ attr_accessor :client_wrapper_class_namespace, :subject_server_class_namespace
10
+
7
11
  def channel
8
12
  @hub.channel
9
13
  end
@@ -20,6 +24,10 @@ module PipeRpc
20
24
  @hub.requests.wait_for_results_through(iteration)
21
25
  end
22
26
 
27
+ def handle_message
28
+ @hub.handle_message
29
+ end
30
+
23
31
  def on_sent(&on_sent)
24
32
  @hub.socket.on_sent(&on_sent)
25
33
  end
@@ -39,9 +47,5 @@ module PipeRpc
39
47
  def close(reason = 'manually closed')
40
48
  @hub.socket.close(reason)
41
49
  end
42
-
43
- def handle_message
44
- @hub.handle_message
45
- end
46
50
  end
47
51
  end
data/lib/pipe_rpc/hub.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  module PipeRpc
2
2
  class Hub
3
- def initialize(args)
4
- @channel = args[:input]
5
- @socket = Socket.new(self, args)
3
+ def initialize(gateway, channels)
4
+ @channel = channels[:input]
5
+ @socket = Socket.new(self, channels)
6
6
  @requests = Requests.new(self)
7
7
  @servers = Servers.new
8
8
  @clients = Clients.new(self)
9
- @transport_mapper = TransportMapper.new(self)
9
+ @transport_mapper = TransportMapper.new(gateway)
10
10
  end
11
11
 
12
12
  attr_reader :channel, :servers, :clients, :requests, :socket, :transport_mapper
@@ -5,7 +5,7 @@ module PipeRpc
5
5
  end
6
6
 
7
7
  def add(server)
8
- id, server = if server.respond_to? :__rpc_server_id__
8
+ id, server = if Server === server
9
9
  [server.__rpc_server_id__, server]
10
10
  else
11
11
  server.first
@@ -1,14 +1,15 @@
1
1
  module PipeRpc
2
2
  class Mapper; end
3
+
3
4
  class Hub::TransportMapper < Mapper
4
- def initialize(hub)
5
- @hub = hub
5
+ def initialize(gateway)
6
+ @gateway = gateway
6
7
  end
7
8
 
8
9
  def to_transport(object)
9
10
  map(object) do |value|
10
11
  if value.respond_to? :to_rpc_transport
11
- value.to_rpc_transport(@hub)
12
+ value.to_rpc_transport(@gateway)
12
13
  else
13
14
  value
14
15
  end
@@ -18,9 +19,9 @@ module PipeRpc
18
19
  def from_transport(object)
19
20
  map(object) do |value|
20
21
  if (value.is_a? String) and (value.start_with? Client::TRANSPORT_PREFIX)
21
- @hub.servers[value.sub(Client::TRANSPORT_PREFIX, '').to_sym]
22
+ Server.from_rpc_transport(@gateway, value)
22
23
  elsif (value.is_a? String) and (value.start_with? Server::TRANSPORT_PREFIX)
23
- @hub.clients[value.sub(Server::TRANSPORT_PREFIX, '').to_sym]
24
+ Client.from_rpc_transport(@gateway, value)
24
25
  else
25
26
  value
26
27
  end
@@ -0,0 +1,32 @@
1
+ module PipeRpc
2
+ module Servable
3
+ def self.included(klass)
4
+ klass.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def rpc_server_class_name
9
+ to_s
10
+ end
11
+
12
+ def __rpc_server_class_name__
13
+ "#{rpc_server_class_name}".tap do |class_name|
14
+ class_name << 'Class' if is_a? Class
15
+ end
16
+ end
17
+ end
18
+
19
+ def __rpc_server_class_name__
20
+ self.class.rpc_server_class_name
21
+ end
22
+
23
+ def rpc_server_instance_id
24
+ __id__
25
+ end
26
+
27
+ def to_rpc_transport(gateway)
28
+ server_class = gateway.subject_server_class_namespace.const_get(__rpc_server_class_name__, false)
29
+ server_class.new(self).to_rpc_transport(gateway)
30
+ end
31
+ end
32
+ end
@@ -9,19 +9,29 @@ module PipeRpc
9
9
  __class__.to_s
10
10
  end
11
11
 
12
+ def __rpc_server_instance_id__
13
+ __id__.to_s
14
+ end
15
+
12
16
  def __rpc_server_id__
13
- "#{__rpc_server_class_name__}##{__id__}".to_sym
17
+ @__rpc_server_id__ ||= [__rpc_server_class_name__, *__rpc_server_instance_id__].join('#').to_sym
18
+ end
19
+
20
+ def inspect
21
+ __rpc_server_id__
14
22
  end
23
+ alias to_s inspect
15
24
 
16
- def to_rpc_transport(hub)
17
- hub.servers.add(self) unless hub.servers.registered? __rpc_server_id__
25
+ def to_rpc_transport(gateway)
26
+ gateway.servers.add(self) unless gateway.servers.registered? __rpc_server_id__
18
27
  "#{TRANSPORT_PREFIX}#{__rpc_server_id__}"
19
28
  end
20
29
  end
21
30
 
22
31
  class << Server
23
- def instance_eval_for(server, *args, &block)
24
- Object.instance_method(:instance_eval).bind(server).call(*args, &block)
32
+ def from_rpc_transport(gateway, transport)
33
+ server = gateway.servers[transport.sub(Client::TRANSPORT_PREFIX, '').to_sym]
34
+ SubjectServer.from_rpc_transport(gateway, server)
25
35
  end
26
36
  end
27
37
  end
@@ -0,0 +1,39 @@
1
+ module PipeRpc
2
+ module SubjectServers; end
3
+
4
+ class SubjectServer < Server
5
+ def initialize(server_subject)
6
+ @server_subject = server_subject
7
+ end
8
+
9
+ def __rpc_server_class_name__
10
+ if @server_subject.respond_to? :__rpc_server_class_name__
11
+ @server_subject.__rpc_server_class_name__
12
+ elsif @server_subject.is_a? Module
13
+ @server_subject.to_s
14
+ else
15
+ @server_subject.class.to_s
16
+ end
17
+ end
18
+
19
+ def __rpc_server_instance_id__
20
+ if @server_subject.respond_to? :rpc_server_instance_id
21
+ @server_subject.rpc_server_instance_id
22
+ elsif @server_subject.is_a? Module
23
+ nil
24
+ else
25
+ @server_subject.__id__.to_s
26
+ end
27
+ end
28
+ end
29
+
30
+ class << SubjectServer
31
+ def from_rpc_transport(gateway, server)
32
+ if self === server
33
+ instance_eval_for(server, '@server_subject')
34
+ else
35
+ server
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module PipeRpc
2
- VERSION = "2.4.0"
2
+ VERSION = "2.5.0"
3
3
  end
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.0
4
+ version: 2.5.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-09-25 00:00:00.000000000 Z
11
+ date: 2016-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack
@@ -106,6 +106,7 @@ files:
106
106
  - lib/pipe_rpc/client_request_response.rb
107
107
  - lib/pipe_rpc/client_request_result.rb
108
108
  - lib/pipe_rpc/client_request_result_response.rb
109
+ - lib/pipe_rpc/client_wrapper.rb
109
110
  - lib/pipe_rpc/error.rb
110
111
  - lib/pipe_rpc/error_response.rb
111
112
  - lib/pipe_rpc/gateway.rb
@@ -124,7 +125,9 @@ files:
124
125
  - lib/pipe_rpc/mapper.rb
125
126
  - lib/pipe_rpc/request.rb
126
127
  - lib/pipe_rpc/result_response.rb
128
+ - lib/pipe_rpc/servable.rb
127
129
  - lib/pipe_rpc/server.rb
130
+ - lib/pipe_rpc/subject_server.rb
128
131
  - lib/pipe_rpc/version.rb
129
132
  - pipe_rpc.gemspec
130
133
  homepage: https://github.com/christopheraue/ruby-pipe_rpc