mimi-messaging 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mimi/messaging/msgpack/msgpack_ext.rb +14 -0
- data/lib/mimi/messaging/msgpack/type_packer.rb +104 -0
- data/lib/mimi/messaging/version.rb +1 -1
- data/lib/mimi/messaging.rb +2 -2
- data/mimi-messaging.gemspec +1 -0
- metadata +17 -2
- data/lib/mimi/messaging/packer.rb +0 -92
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99d219b26de0275984ae4a58a8ad2e87558db4a3
|
4
|
+
data.tar.gz: 2eb4e88a3bedabc884dd57eff6d9afe8971cbf45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30b69c9b1945d0a65a3541d469034c36aac02769e95a276ade278bd26f9a1872d6e58ffbc0bd84c0387bd666f1fd293dda364fec73ee5d15b7f5c11b11f68b5e
|
7
|
+
data.tar.gz: 871493f0e32e1d2aeb488e875558ad192c065e105a785707180e67a76953680bcd68264e904fb9bfe77151e2733b907a423ef4851fae7d2c2bb1c338a6f428c4
|
@@ -0,0 +1,14 @@
|
|
1
|
+
#
|
2
|
+
# MessagePack extensions for common types
|
3
|
+
#
|
4
|
+
Mimi::Messaging::TypePacker.register(
|
5
|
+
Time,
|
6
|
+
from_bytes: -> (b) { Time.at(b.unpack('D').first).utc },
|
7
|
+
to_bytes: -> (v) { [v.utc.to_f].pack('D') }
|
8
|
+
)
|
9
|
+
|
10
|
+
Mimi::Messaging::TypePacker.register(
|
11
|
+
BigDecimal,
|
12
|
+
from_bytes: -> (b) { BigDecimal.new(b) },
|
13
|
+
to_bytes: -> (v) { v.to_s }
|
14
|
+
)
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'msgpack'
|
2
|
+
|
3
|
+
module Mimi
|
4
|
+
module Messaging
|
5
|
+
class TypePacker
|
6
|
+
APPLICATION_TYPE_EXT = 0x00
|
7
|
+
|
8
|
+
# Registers a new type packer for msgpack.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# TypePacker.register(
|
12
|
+
# Time,
|
13
|
+
# from_bytes: -> (b) { Time.at(b.unpack('D').first) },
|
14
|
+
# to_bytes: -> (v) { [v.to_f].pack('D') }
|
15
|
+
# )
|
16
|
+
#
|
17
|
+
def self.register(type, opts = {})
|
18
|
+
raise ArgumentError, 'Invalid :from_bytes, proc expected' unless opts[:from_bytes].is_a?(Proc)
|
19
|
+
raise ArgumentError, 'Invalid :to_bytes, proc expected' unless opts[:to_bytes].is_a?(Proc)
|
20
|
+
type_name = type.to_s
|
21
|
+
params = opts.dup.merge(type: type, type_name: type_name)
|
22
|
+
type_packers[type] = params
|
23
|
+
type.send(:define_method, :to_msgpack_ext) { Mimi::Messaging::TypePacker.pack(self) }
|
24
|
+
MessagePack::DefaultFactory.register_type(
|
25
|
+
APPLICATION_TYPE_EXT,
|
26
|
+
type,
|
27
|
+
packer: :to_msgpack_ext
|
28
|
+
)
|
29
|
+
|
30
|
+
MessagePack::DefaultFactory.register_type(
|
31
|
+
APPLICATION_TYPE_EXT,
|
32
|
+
self,
|
33
|
+
unpacker: :unpack
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns a set of registered type packers
|
38
|
+
#
|
39
|
+
def self.type_packers
|
40
|
+
@type_packers ||= {}
|
41
|
+
end
|
42
|
+
|
43
|
+
# Pack a value using a type packer, registered for the value class
|
44
|
+
#
|
45
|
+
def self.pack(value)
|
46
|
+
type_packer = type_packers.values.find { |p| value.is_a?(p[:type]) }
|
47
|
+
raise "No packer registered for type #{value.class}" unless type_packer
|
48
|
+
bytes = type_packer[:to_bytes].call(value)
|
49
|
+
"#{type_packer[:type_name]}=#{bytes}"
|
50
|
+
end
|
51
|
+
|
52
|
+
# Unpack a value, using a registered type packer
|
53
|
+
#
|
54
|
+
def self.unpack(value)
|
55
|
+
type_name, _, bytes = value.partition('=') # splits "Type=bytes" into ['Type', '=', 'bytes']
|
56
|
+
type_packer = type_packers.values.find { |p| p[:type_name] == type_name }
|
57
|
+
raise "No unpacker registered for type #{type_name}" unless type_packer
|
58
|
+
type_packer[:from_bytes].call(bytes)
|
59
|
+
end
|
60
|
+
|
61
|
+
# def self.register_type_packer!
|
62
|
+
# return if @type_packer_registered
|
63
|
+
# # pk = MessagePack::Packer.new
|
64
|
+
# # pk.register_type(APPLICATION_TYPE_EXT) { |v| TypePacker.pack(v) }
|
65
|
+
# uk = MessagePack::Unpacker.new
|
66
|
+
# uk.register_type(APPLICATION_TYPE_EXT) { |b| TypePacker.unpack(b) }
|
67
|
+
# @type_packer_registered = true
|
68
|
+
# end
|
69
|
+
end # class TypePacker
|
70
|
+
end # module Messaging
|
71
|
+
|
72
|
+
end # module Mimi
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
# class A
|
77
|
+
# class B
|
78
|
+
# attr_reader :value
|
79
|
+
# def initialize(value)
|
80
|
+
# @value = value
|
81
|
+
# end
|
82
|
+
# end # class B
|
83
|
+
# end # class A
|
84
|
+
|
85
|
+
# TypePacker.register(
|
86
|
+
# Time,
|
87
|
+
# from_bytes: -> (b) { Time.at(b.unpack('D').first) },
|
88
|
+
# to_bytes: -> (v) { [v.to_f].pack('D') }
|
89
|
+
# )
|
90
|
+
|
91
|
+
# TypePacker.register(
|
92
|
+
# A::B,
|
93
|
+
# from_bytes: -> (b) { A::B.new(b.unpack('D').first) },
|
94
|
+
# to_bytes: -> (v) { [v.value.to_f].pack('D') }
|
95
|
+
# )
|
96
|
+
|
97
|
+
# TypePacker.register(
|
98
|
+
# DateTime,
|
99
|
+
# from_bytes: -> (b) { Time.at(b.unpack('D').first).to_datetime },
|
100
|
+
# to_bytes: -> (v) { [v.to_time.to_f].pack('D') }
|
101
|
+
# )
|
102
|
+
|
103
|
+
# m = "\x83\xA1a\xC7\r\x00Time=\x1D@\x95\xC7\x80\xCA\xD5A\xA1b\x02\xA1c\xC7\x11\x00DateTime=X@\x95\xC7\x80\xCA\xD5A"
|
104
|
+
|
data/lib/mimi/messaging.rb
CHANGED
@@ -107,8 +107,6 @@ module Mimi
|
|
107
107
|
end # module Mimi
|
108
108
|
|
109
109
|
require_relative 'messaging/version'
|
110
|
-
require_relative 'messaging/packer'
|
111
|
-
|
112
110
|
require_relative 'messaging/errors'
|
113
111
|
require_relative 'messaging/connection'
|
114
112
|
require_relative 'messaging/message'
|
@@ -119,3 +117,5 @@ require_relative 'messaging/model'
|
|
119
117
|
require_relative 'messaging/model_provider'
|
120
118
|
require_relative 'messaging/notification'
|
121
119
|
require_relative 'messaging/listener'
|
120
|
+
require_relative 'messaging/msgpack/type_packer'
|
121
|
+
require_relative 'messaging/msgpack/msgpack_ext'
|
data/mimi-messaging.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mimi-messaging
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Kukushkin
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mimi-logger
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.1'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bunny
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -132,8 +146,9 @@ files:
|
|
132
146
|
- lib/mimi/messaging/message.rb
|
133
147
|
- lib/mimi/messaging/model.rb
|
134
148
|
- lib/mimi/messaging/model_provider.rb
|
149
|
+
- lib/mimi/messaging/msgpack/msgpack_ext.rb
|
150
|
+
- lib/mimi/messaging/msgpack/type_packer.rb
|
135
151
|
- lib/mimi/messaging/notification.rb
|
136
|
-
- lib/mimi/messaging/packer.rb
|
137
152
|
- lib/mimi/messaging/provider.rb
|
138
153
|
- lib/mimi/messaging/request.rb
|
139
154
|
- lib/mimi/messaging/request_processor.rb
|
@@ -1,92 +0,0 @@
|
|
1
|
-
require 'msgpack'
|
2
|
-
|
3
|
-
class TypePacker
|
4
|
-
APPLICATION_TYPE_EXT = 0x00
|
5
|
-
|
6
|
-
def self.register(type, opts = {})
|
7
|
-
raise ArgumentError, 'Invalid :from_bytes, proc expected' unless opts[:from_bytes].is_a?(Proc)
|
8
|
-
raise ArgumentError, 'Invalid :to_bytes, proc expected' unless opts[:to_bytes].is_a?(Proc)
|
9
|
-
type_name = type.to_s
|
10
|
-
params = opts.dup.merge(type: type, type_name: type_name)
|
11
|
-
type_packers[type] = params
|
12
|
-
type.send(:define_method, :to_msgpack_ext) { TypePacker.pack(self) }
|
13
|
-
MessagePack::DefaultFactory.register_type(
|
14
|
-
APPLICATION_TYPE_EXT,
|
15
|
-
type,
|
16
|
-
packer: :to_msgpack_ext,
|
17
|
-
# unpacker: :from_msgpack_ext
|
18
|
-
)
|
19
|
-
|
20
|
-
MessagePack::DefaultFactory.register_type(
|
21
|
-
APPLICATION_TYPE_EXT,
|
22
|
-
self,
|
23
|
-
unpacker: :unpack,
|
24
|
-
# unpacker: :from_msgpack_ext
|
25
|
-
)
|
26
|
-
|
27
|
-
# pk = MessagePack::Packer.new
|
28
|
-
# pk.register_type(APPLICATION_TYPE_EXT, type, :to_msgpack_ext) # { |v| TypePacker.pack(v) }
|
29
|
-
|
30
|
-
# register_type_packer!
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.type_packers
|
34
|
-
@type_packers ||= {}
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.pack(value)
|
38
|
-
type_packer = type_packers.values.find { |p| value.is_a?(p[:type]) }
|
39
|
-
raise "No packer registered for type #{value.class}" unless type_packer
|
40
|
-
bytes = type_packer[:to_bytes].call(value)
|
41
|
-
"#{type_packer[:type_name]}=#{bytes}"
|
42
|
-
end
|
43
|
-
|
44
|
-
def self.unpack(value)
|
45
|
-
vp = value.partition('=') # splits "Type=bytes" into ['Type', '=', 'bytes']
|
46
|
-
type_name = vp.first
|
47
|
-
bytes = vp.last
|
48
|
-
type_packer = type_packers.values.find { |p| p[:type_name] == type_name }
|
49
|
-
raise "No unpacker registered for type #{type_name}" unless type_packer
|
50
|
-
type_packer[:from_bytes].call(bytes)
|
51
|
-
end
|
52
|
-
|
53
|
-
def self.register_type_packer!
|
54
|
-
return if @type_packer_registered
|
55
|
-
# pk = MessagePack::Packer.new
|
56
|
-
# pk.register_type(APPLICATION_TYPE_EXT) { |v| TypePacker.pack(v) }
|
57
|
-
uk = MessagePack::Unpacker.new
|
58
|
-
uk.register_type(APPLICATION_TYPE_EXT) { |b| TypePacker.unpack(b) }
|
59
|
-
@type_packer_registered = true
|
60
|
-
end
|
61
|
-
end # class TypePacker
|
62
|
-
|
63
|
-
|
64
|
-
class A
|
65
|
-
class B
|
66
|
-
attr_reader :value
|
67
|
-
def initialize(value)
|
68
|
-
@value = value
|
69
|
-
end
|
70
|
-
end # class B
|
71
|
-
end # class A
|
72
|
-
|
73
|
-
TypePacker.register(
|
74
|
-
Time,
|
75
|
-
from_bytes: -> (b) { Time.at(b.unpack('D').first) },
|
76
|
-
to_bytes: -> (v) { [v.to_f].pack('D') }
|
77
|
-
)
|
78
|
-
|
79
|
-
TypePacker.register(
|
80
|
-
A::B,
|
81
|
-
from_bytes: -> (b) { A::B.new(b.unpack('D').first) },
|
82
|
-
to_bytes: -> (v) { [v.value.to_f].pack('D') }
|
83
|
-
)
|
84
|
-
|
85
|
-
# TypePacker.register(
|
86
|
-
# DateTime,
|
87
|
-
# from_bytes: -> (b) { Time.at(b.unpack('D').first).to_datetime },
|
88
|
-
# to_bytes: -> (v) { [v.to_time.to_f].pack('D') }
|
89
|
-
# )
|
90
|
-
|
91
|
-
# m = "\x83\xA1a\xC7\r\x00Time=\x1D@\x95\xC7\x80\xCA\xD5A\xA1b\x02\xA1c\xC7\x11\x00DateTime=X@\x95\xC7\x80\xCA\xD5A"
|
92
|
-
|