msgpack-rpc-over-http 0.1.0 → 0.2.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: 708df428ed2a1512a9705152873be65d6754f6d4
4
- data.tar.gz: d03376b801d75ad342504fdf834da6b7611344f0
3
+ metadata.gz: fcc624130da419cf6f942b65cd06712fae7fe467
4
+ data.tar.gz: 3d316dfe9368934799319bc296bac442fcc4fb1c
5
5
  SHA512:
6
- metadata.gz: 76f9bc631bf527bf7988c078dd97a3ee52f2cc20efbbe5809e24734d55bd3ed9bb5b697a97adb02557fd6b932af31f6a253afce8741900ea8bb9c277b533e3d1
7
- data.tar.gz: 1dfd76e81ce0db3827c567eef82b81b5268ea77b44a18f6c97b0d18454a6c54992b3ae33426a7076618c65629da6f3fa366ca5921b226f62c62a83308f5a8e0e
6
+ metadata.gz: 6339d58f07f8d36c58af4249af0d9d839c8672bf0b57b0168aeb2dccfd5bf5c31af20c5c83d4c048e2202f0120c0f2ba21d92da892923ddf0fc7c30bef75f2a4
7
+ data.tar.gz: 2819886c5da2363989f369595108b86cf398ecbf429bb8ded3a881a943e2563cdb9228b55b5fec6a7967ab79e1eb42baa69acdbc49c2ed52953592c77b8bc2ba
@@ -11,11 +11,12 @@ module MessagePack
11
11
 
12
12
  HEADER = {"Content-Type" => 'application/x-msgpack'}
13
13
 
14
- def initialize(url, options={})
14
+ def initialize(url, factory: nil)
15
15
  @url = url
16
16
  @client = HTTPClient.new
17
17
  @reqtable = {}
18
18
  @seqid = 0
19
+ @factory = factory || MessagePack::Factory.new
19
20
  end
20
21
 
21
22
  def_delegators(:@client,
@@ -98,11 +99,13 @@ module MessagePack
98
99
  msgid = @seqid
99
100
  @seqid += 1
100
101
  @seqid = 0 if @seqid >= (1 << 31)
101
- data = [REQUEST, msgid, method, param].to_msgpack
102
+ packer = @factory.packer
103
+ data = packer.write([REQUEST, msgid, method, param]).to_s
102
104
  end
103
105
 
104
106
  def get_result(body)
105
- type, msgid, err, res = MessagePack.unpack(body)
107
+ unpacker = @factory.unpacker
108
+ type, msgid, err, res = unpacker.feed(body).read
106
109
  raise "Unknown message type #{type}" if type != RESPONSE
107
110
 
108
111
  if err.nil?
@@ -11,11 +11,11 @@ module MessagePack
11
11
 
12
12
  # Retruns the application for MessagePack-RPC.
13
13
  # It's create with Rack::Builder
14
- def self.app(handler)
14
+ def self.app(handler, factory = nil)
15
15
  return Rack::Builder.app do
16
16
  use Rack::Chunked
17
- use RequestUnpacker
18
- use ResponsePacker
17
+ use RequestUnpacker, factory
18
+ use ResponsePacker, factory
19
19
  use Dispatcher
20
20
  run handler
21
21
  end
@@ -5,8 +5,9 @@ module MessagePack
5
5
  # Rack Middleware that unpacks a serialized string in a HTTP
6
6
  # request.
7
7
  class RequestUnpacker
8
- def initialize(app)
8
+ def initialize(app, factory = nil)
9
9
  @app = app
10
+ @factory = factory || MessagePack::Factory.new
10
11
  end
11
12
 
12
13
  def call(env)
@@ -49,7 +50,8 @@ module MessagePack
49
50
  end
50
51
 
51
52
  def unpack(env, body)
52
- msg = MessagePack.unpack(body)
53
+ unpacker = @factory.unpacker
54
+ msg = unpacker.feed(body).read
53
55
  env['msgpack-rpc.type'] = msg[0]
54
56
  case msg[0]
55
57
  when REQUEST
@@ -5,8 +5,9 @@ module MessagePack
5
5
  # Rack Middleware that packs a result of a handler method and
6
6
  # create a HTTP Responce.
7
7
  class ResponsePacker
8
- def initialize(dispatcher)
8
+ def initialize(dispatcher, factory = nil)
9
9
  @dispatcher = dispatcher
10
+ @factory = factory || MessagePack::Factory.new
10
11
  end
11
12
 
12
13
  def call(env)
@@ -20,20 +21,26 @@ module MessagePack
20
21
  res.body = body
21
22
  return [res.status.to_i, res.header, body]
22
23
  else
23
- res.write self.class.pack(msgid, nil, body)
24
+ res.write pack(msgid, nil, body)
24
25
  return res.finish
25
26
  end
26
27
  rescue RPCOverHTTP::RemoteError => ex
27
- res.write self.class.pack(msgid, ex.class.name, ex.message)
28
+ res.write pack(msgid, ex.class.name, ex.message)
28
29
  return res.finish
29
30
  rescue ::RuntimeError => ex
30
- res.write(self.class.pack(
31
- msgid, RPCOverHTTP::RuntimeError.name, ex.message))
31
+ res.write(pack(msgid, RPCOverHTTP::RuntimeError.name, ex.message))
32
32
  return res.finish
33
33
  end
34
34
 
35
- def self.pack(msgid, error, result)
36
- return MessagePack.pack([RESPONSE, msgid, error, result])
35
+ def pack(msgid, error, result)
36
+ packer = @factory.packer
37
+ packer.write([RESPONSE, msgid, error, result]).to_s
38
+ end
39
+
40
+ def self.pack(msgid, error, result, factory: nil)
41
+ factory ||= MessagePack::DefaultFactory
42
+ packer = factory.packer
43
+ packer.write([RESPONSE, msgid, error, result]).to_s
37
44
  end
38
45
  end
39
46
  end
@@ -1,5 +1,5 @@
1
1
  module MessagePack
2
2
  module RPCOverHTTP
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -20,7 +20,7 @@ Gem::Specification.new do |gem|
20
20
  end
21
21
 
22
22
  gem.add_runtime_dependency "rack"
23
- gem.add_runtime_dependency "msgpack", ">= 0.5.11"
23
+ gem.add_runtime_dependency "msgpack", "~> 1.0"
24
24
  gem.add_runtime_dependency "celluloid", "~> 0.16.0"
25
25
  gem.add_runtime_dependency "httpclient"
26
26
 
@@ -39,7 +39,7 @@ def jruby?
39
39
  end
40
40
 
41
41
  def sleep_until_http_server_is_started(host, port)
42
- timeout(30) do
42
+ timeout(30) do
43
43
  while stopped_test_app_server?(host, port)
44
44
  sleep 1
45
45
  end
@@ -8,4 +8,16 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
8
  require 'msgpack-rpc-over-http'
9
9
  require 'helper'
10
10
 
11
- run MessagePack::RPCOverHTTP::Server.app(MockHandler.new)
11
+ class Time
12
+ def self.from_msgpack_ext(data)
13
+ sec, usec = MessagePack.unpack(data)
14
+ Time.at(sec, usec)
15
+ end
16
+ def to_msgpack_ext
17
+ [to_i, usec].to_msgpack
18
+ end
19
+ end
20
+ msgpack_factory = MessagePack::Factory.new
21
+ msgpack_factory.register_type(0x01, Time)
22
+
23
+ run MessagePack::RPCOverHTTP::Server.app(MockHandler.new, msgpack_factory)
@@ -1,5 +1,15 @@
1
1
  require 'rack'
2
2
 
3
+ class Time
4
+ def self.from_msgpack_ext(data)
5
+ sec, usec = MessagePack.unpack(data)
6
+ Time.at(sec, usec)
7
+ end
8
+ def to_msgpack_ext
9
+ [to_i, usec].to_msgpack
10
+ end
11
+ end
12
+
3
13
  module MessagePack::RPCOverHTTP
4
14
  class TestClient < Test::Unit::TestCase
5
15
  def self.unused_port
@@ -13,7 +23,9 @@ module MessagePack::RPCOverHTTP
13
23
  @@is_stopped_test_app_server = true
14
24
 
15
25
  def setup
16
- @client = Client.new("http://0.0.0.0:#{@@server_port}/")
26
+ msgpack_factory = MessagePack::Factory.new
27
+ msgpack_factory.register_type(0x01, Time)
28
+ @client = Client.new("http://0.0.0.0:#{@@server_port}/", factory: msgpack_factory)
17
29
  if @@is_stopped_test_app_server
18
30
  if /java/ =~ RUBY_PLATFORM
19
31
  mizuno = nil
@@ -22,7 +34,7 @@ module MessagePack::RPCOverHTTP
22
34
  require 'mizuno/server'
23
35
  app = Rack::Builder.new {
24
36
  # MockHandler is in helper.rb
25
- run MessagePack::RPCOverHTTP::Server.app(MockHandler.new)
37
+ run MessagePack::RPCOverHTTP::Server.app(MockHandler.new, msgpack_factory)
26
38
  }
27
39
  mizuno = Mizuno::Server.new
28
40
  mizuno.run(app, embedded: true, threads: 1, port: @@server_port, host: '0.0.0.0')
@@ -34,7 +46,7 @@ module MessagePack::RPCOverHTTP
34
46
  end
35
47
  else
36
48
  pid = fork {
37
- Rack::Server.start(config: "test/mock_server.ru", Port: @@server_port)
49
+ Rack::Server.start(config: "test/mock_server.ru", Port: @@server_port, Host: '0.0.0.0')
38
50
  exit 0
39
51
  }
40
52
  at_exit do
@@ -42,7 +54,7 @@ module MessagePack::RPCOverHTTP
42
54
  Process.waitpid(pid)
43
55
  end
44
56
  end
45
- sleep_until_http_server_is_started("0.0.0.0", @@server_port)
57
+ sleep_until_http_server_is_started("127.0.0.1", @@server_port)
46
58
  @@is_stopped_test_app_server = false
47
59
  end
48
60
  end
@@ -57,6 +69,13 @@ module MessagePack::RPCOverHTTP
57
69
  end
58
70
  end
59
71
 
72
+ def test_call_with_ext_type
73
+ require 'time'
74
+ t1 = Time.parse('2017-03-08 13:59:00')
75
+ t2 = Time.now
76
+ assert_equal [t1, t2], @client.call(:test, t1, t2)
77
+ end
78
+
60
79
  def test_call_async
61
80
  pend "msgpack-rpc-over-http does not support Client#async for jruby" if jruby?
62
81
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: msgpack-rpc-over-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Narihiro Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-19 00:00:00.000000000 Z
11
+ date: 2017-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: msgpack
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.5.11
33
+ version: '1.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.5.11
40
+ version: '1.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: celluloid
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -139,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
139
  version: '0'
140
140
  requirements: []
141
141
  rubyforge_project:
142
- rubygems_version: 2.5.1
142
+ rubygems_version: 2.6.8
143
143
  signing_key:
144
144
  specification_version: 4
145
145
  summary: This library provides MessagePack-RPC via HTTP