pipe_rpc 0.2.0 → 0.2.2

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: 2ad289824f7ad565af6f419ddf007c4bd1745e37
4
- data.tar.gz: d6511143979b1c2a998eb3072042a596d264307e
3
+ metadata.gz: 3a46e3217d0a8f86c3f3c550958abff130034dfd
4
+ data.tar.gz: dc2d57fff58b882edec090d2351508f02bc55705
5
5
  SHA512:
6
- metadata.gz: a13574ab492318a87acb1c82b5f74eb6a7a4e28e4022106725ce3c405de8a56528d08d54493386b0e5d4fb5e5a025d4d77577c3065aa0cea4081529076380542
7
- data.tar.gz: eeeb48e3f988525d562d66a5cd64d3f88d7df8edef572f10d4df391b00a53616e7b08ed9935ccd05923b7321ff60fac816f091f15c0d0dfc1c59cd22335077a6
6
+ metadata.gz: ccd3933db763f066f5ea5db393030a7acc5c347e3b2382a636a3901415b4619bcc88184ea40e05e9c6c5a0008e78f60fd07896214aefc812c26bc49ad70731d9
7
+ data.tar.gz: 3a83c2c879e4a7704fae148372aaf732abc31a38ee658fb1fc9ca601f6c80f2760731be6c9f7c55a2805abd578c0f7e655a96660f2eef34d637baaa43ea6b7f6
@@ -63,7 +63,7 @@ module PipeRpc
63
63
  end
64
64
 
65
65
  def internal_error
66
- InternalError.new("#{@data[:message]}").tap do |error|
66
+ InternalError.new(@data[:message]).tap do |error|
67
67
  error.set_backtrace(@data[:backtrace]) if error.respond_to? :set_backtrace
68
68
  end
69
69
  end
@@ -25,7 +25,7 @@ module PipeRpc
25
25
  def handler
26
26
  @handler ||= type.new(@message)
27
27
  rescue => e
28
- @hub.send_error(id: nil, code: -32600, data: { message: e.message })
28
+ @hub.send_response ErrorResponse.new(id: nil, error: { code: -32600, data: { message: e.message } })
29
29
  nil
30
30
  end
31
31
 
@@ -0,0 +1,44 @@
1
+ module PipeRpc
2
+ class Hub::Responder::Request
3
+ class Error
4
+ def initialize(error)
5
+ @error = error
6
+ end
7
+
8
+ attr_reader :error
9
+
10
+ def code
11
+ case @error
12
+ when NoServerError then -32604
13
+ when method_call_error? && NoMethodError then -32601
14
+ when method_call_error? && ArgumentError then -32602
15
+ when InternalError then -32605
16
+ when StandardError then -32603
17
+ else nil
18
+ end
19
+ end
20
+
21
+ def message
22
+ @error.message
23
+ end
24
+
25
+ def backtrace
26
+ @error.backtrace.to_a.dup.tap do |backtrace|
27
+ # In ruby, backtraces of argument errors have the location of the
28
+ # method definition and not from where it is called as first location
29
+ # in the backtrace. To normalize argument and no method errors,
30
+ # remove the first location for argument errors.
31
+ backtrace.shift if @error.is_a?(ArgumentError) and not Object.const_defined?(:MRUBY_VERSION)
32
+ end
33
+ end
34
+
35
+ def method_call_error?
36
+ if Object.const_defined?(:MRUBY_VERSION)
37
+ backtrace.first.nil?
38
+ else
39
+ backtrace.first.split(':').first(2) == [CALL_FILE, CALL_LINENO.to_s]
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,22 @@
1
+ module PipeRpc
2
+ class Hub::Responder::Request
3
+ class ErrorResult
4
+ def initialize(request, error)
5
+ @request, @error = request, Error.new(error)
6
+ end
7
+
8
+ def value
9
+ @error.error
10
+ end
11
+
12
+ def asynchronous?
13
+ false
14
+ end
15
+
16
+ def to_response
17
+ ErrorResponse.new(id: @request.id, error: { code: @error.code, data: { message: @error.message,
18
+ backtrace: @error.backtrace, method: @request.method, args: @request.arguments } })
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module PipeRpc
2
+ class Hub::Responder::Request::Result
3
+ def initialize(request, result)
4
+ @request, @result = request, result
5
+ end
6
+
7
+ def value
8
+ @result
9
+ end
10
+
11
+ def asynchronous?
12
+ @result == @request.evaluate_result_proc
13
+ end
14
+
15
+ def to_response
16
+ ResultResponse.new(id: @request.id, result: @result)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,35 @@
1
+ module PipeRpc
2
+ class Hub::Responder
3
+ class Request
4
+ def initialize(responder, request)
5
+ @responder, @request = responder, request
6
+ end
7
+
8
+ def id
9
+ @request.id
10
+ end
11
+
12
+ CALL_FILE, CALL_LINENO = __FILE__, __LINE__+3
13
+ def handle
14
+ evaluate_result do
15
+ @responder.servers[@request.server].__send__(@request.method, *@request.arguments, &evaluate_result_proc)
16
+ end
17
+ end
18
+
19
+ def evaluate_result(result = nil, &block)
20
+ result = Result.new(self, block_given? ? yield : result)
21
+ rescue Exception => e
22
+ result = ErrorResult.new(@request, e)
23
+ ensure
24
+ @responder.send_response(result.to_response) unless result.asynchronous?
25
+ return result.value
26
+ end
27
+
28
+ def evaluate_result_proc
29
+ @evaluate_result_proc ||= proc do |result = nil, &block|
30
+ evaluate_result(result, &block)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -3,7 +3,11 @@ module PipeRpc
3
3
  class Responder
4
4
  def initialize(hub)
5
5
  @hub = hub
6
- @servers = {}
6
+ @servers = Hash.new{ |hash, key| ::Kernel.raise NoServerError.new("no server #{key}") }
7
+ end
8
+
9
+ def servers
10
+ @servers.dup
7
11
  end
8
12
 
9
13
  def add_server(servers)
@@ -19,71 +23,16 @@ module PipeRpc
19
23
  end
20
24
 
21
25
  def handle_notification(notification)
22
- handle_request(notification) {}
23
- end
24
-
25
- def handle_request(request, &on_result)
26
- id, server, method, args = request.id, @servers[request.server], request.method, request.arguments
27
-
28
- unless server
29
- @hub.send_error(id: id, code: -32604)
30
- return
31
- end
32
-
33
- async = proc do |result|
34
- on_result.call ResultResponse.new(id: id, result: result)
35
- end
36
-
37
- @on_request.call(server, method, args) if @on_request
38
-
39
- # methods returning the async block as result state that they are asynchronous!
40
- method_lineno = __LINE__+1
41
- result = server.__send__(method, *args, &async)
42
-
43
- # directly return result for synchronous calls
44
- async.call(result) unless result == async
45
-
46
- rescue NoMethodError => e
47
- backtrace = e.backtrace.to_a
48
- code = itself_caused_no_method_error?(backtrace, method_lineno) ? -32601 : -32603
49
- @hub.send_error(id: id, code: code, data: { message: e.message, method: e.name,
50
- args: e.args, backtrace: backtrace })
51
-
52
- rescue ArgumentError => e
53
- backtrace = e.backtrace.to_a
54
- code = itself_caused_argument_error?(backtrace, method_lineno) ? -32602 : -32603
55
- @hub.send_error(id: id, code: code, data: { message: e.message, backtrace: backtrace })
56
-
57
- rescue InternalError => e # raised on the other side
58
- backtrace = e.backtrace.to_a
59
- @hub.send_error(id: id, code: -32605, data: { message: e.message, backtrace: backtrace })
60
-
61
- rescue => e
62
- backtrace = e.backtrace.to_a
63
- @hub.send_error(id: id, code: -32603, data: { message: e.message, backtrace: backtrace })
64
- end
65
-
66
- private
67
-
68
- def itself_caused_no_method_error?(backtrace, ref_lineno)
69
- if Object.const_defined?(:MRUBY_VERSION)
70
- !backtrace[0]
71
- else
72
- itself_caused_error?(backtrace[0], ref_lineno)
73
- end
26
+ handle_request(notification)
74
27
  end
75
28
 
76
- def itself_caused_argument_error?(backtrace, ref_lineno)
77
- if Object.const_defined?(:MRUBY_VERSION)
78
- !backtrace[0]
79
- else
80
- itself_caused_error?(backtrace[1], ref_lineno)
81
- end
29
+ def handle_request(request)
30
+ @on_request.call(request.server, request.method, request.arguments) if @on_request
31
+ Request.new(self, request).handle
82
32
  end
83
33
 
84
- def itself_caused_error?(backtrace_loc, ref_lineno)
85
- file, lineno, = backtrace_loc.split(':')
86
- (file == __FILE__) and (lineno.to_i == ref_lineno)
34
+ def send_response(response)
35
+ @hub.send_response response
87
36
  end
88
37
  end
89
38
  end
data/lib/pipe_rpc/hub.rb CHANGED
@@ -70,8 +70,8 @@ module PipeRpc
70
70
  end
71
71
  end
72
72
 
73
- def send_error(error = {})
74
- @socket.write ErrorResponse.new(id: error.delete(:id), error: error)
73
+ def send_response(response)
74
+ @socket.write response
75
75
  end
76
76
 
77
77
  def handle_message
@@ -82,13 +82,14 @@ module PipeRpc
82
82
  if message.notification?
83
83
  @responder.handle_notification(message.handler)
84
84
  elsif message.request?
85
- @responder.handle_request(message.handler) { |response| @socket.write response }
85
+ @responder.handle_request(message.handler)
86
86
  elsif message.response?
87
87
  @requester.handle_response(message.handler)
88
88
  elsif message.error?
89
89
  raise message.handler.native_error
90
90
  else
91
- send_error(id: nil, code: -32600, data: { message: 'no request, result or error' })
91
+ send_response ErrorResponse.new(id: nil, error: { code: -32600,
92
+ data: { message: 'no request, result or error' } })
92
93
  end
93
94
  end
94
95
 
@@ -97,7 +98,7 @@ module PipeRpc
97
98
  def next_message
98
99
  Message.new(self, @socket.read) # blocks
99
100
  rescue => e
100
- send_error(id: nil, code: -32700, data: { message: e.message })
101
+ send_response ErrorResponse.new(id: nil, error: { code: -32700, data: { message: e.message } })
101
102
  nil
102
103
  end
103
104
  end
@@ -1,3 +1,3 @@
1
1
  module PipeRpc
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.2"
3
3
  end
data/lib/pipe_rpc.rb CHANGED
@@ -3,14 +3,6 @@ end
3
3
 
4
4
  require 'json'
5
5
  require_relative 'hash'
6
- require_relative "pipe_rpc/version"
7
- require_relative "pipe_rpc/error"
8
- require_relative "pipe_rpc/request"
9
- require_relative "pipe_rpc/result_response"
10
- require_relative "pipe_rpc/error_response"
11
- require_relative "pipe_rpc/socket"
12
- require_relative "pipe_rpc/client"
13
- require_relative "pipe_rpc/hub"
14
- require_relative "pipe_rpc/hub/message"
15
- require_relative "pipe_rpc/hub/requester"
16
- require_relative "pipe_rpc/hub/responder"
6
+
7
+ lib = File.dirname(__FILE__)
8
+ Dir["#{lib}/pipe_rpc/**/*.rb"].sort.each { |f| require f }
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: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Aue
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-25 00:00:00.000000000 Z
11
+ date: 2016-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -105,6 +105,10 @@ files:
105
105
  - lib/pipe_rpc/hub/message.rb
106
106
  - lib/pipe_rpc/hub/requester.rb
107
107
  - lib/pipe_rpc/hub/responder.rb
108
+ - lib/pipe_rpc/hub/responder/request.rb
109
+ - lib/pipe_rpc/hub/responder/request/error.rb
110
+ - lib/pipe_rpc/hub/responder/request/error_result.rb
111
+ - lib/pipe_rpc/hub/responder/request/result.rb
108
112
  - lib/pipe_rpc/request.rb
109
113
  - lib/pipe_rpc/result_response.rb
110
114
  - lib/pipe_rpc/socket.rb