ffi-msgpack 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.gitignore +4 -0
  2. data/.specopts +1 -0
  3. data/.yardopts +1 -0
  4. data/ChangeLog.md +8 -0
  5. data/Gemfile +24 -0
  6. data/LICENSE.txt +23 -0
  7. data/README.md +91 -0
  8. data/Rakefile +39 -0
  9. data/VERSION +1 -0
  10. data/ffi-msgpack.gemspec +101 -0
  11. data/lib/ffi/msgpack.rb +3 -0
  12. data/lib/ffi/msgpack/exceptions/parse_error.rb +6 -0
  13. data/lib/ffi/msgpack/extensions.rb +8 -0
  14. data/lib/ffi/msgpack/extensions/array.rb +7 -0
  15. data/lib/ffi/msgpack/extensions/false_class.rb +7 -0
  16. data/lib/ffi/msgpack/extensions/float.rb +7 -0
  17. data/lib/ffi/msgpack/extensions/hash.rb +7 -0
  18. data/lib/ffi/msgpack/extensions/integer.rb +7 -0
  19. data/lib/ffi/msgpack/extensions/nil_class.rb +7 -0
  20. data/lib/ffi/msgpack/extensions/string.rb +7 -0
  21. data/lib/ffi/msgpack/extensions/true_class.rb +7 -0
  22. data/lib/ffi/msgpack/msg_array.rb +35 -0
  23. data/lib/ffi/msgpack/msg_key_value.rb +33 -0
  24. data/lib/ffi/msgpack/msg_map.rb +41 -0
  25. data/lib/ffi/msgpack/msg_object.rb +276 -0
  26. data/lib/ffi/msgpack/msg_object_union.rb +20 -0
  27. data/lib/ffi/msgpack/msg_raw.rb +42 -0
  28. data/lib/ffi/msgpack/msgpack.rb +127 -0
  29. data/lib/ffi/msgpack/packable.rb +20 -0
  30. data/lib/ffi/msgpack/packer.rb +153 -0
  31. data/lib/ffi/msgpack/types.rb +30 -0
  32. data/lib/ffi/msgpack/unpacker.rb +248 -0
  33. data/lib/ffi/msgpack/zone.rb +18 -0
  34. data/lib/ffi/msgpack/zone_chunk_list.rb +13 -0
  35. data/lib/ffi/msgpack/zone_finalizer.rb +14 -0
  36. data/lib/ffi/msgpack/zone_finalizer_array.rb +13 -0
  37. data/spec/msg_object_spec.rb +193 -0
  38. data/spec/packer_spec.rb +62 -0
  39. data/spec/spec_helper.rb +15 -0
  40. data/spec/unpacker_spec.rb +69 -0
  41. metadata +190 -0
@@ -0,0 +1,18 @@
1
+ require 'ffi/msgpack/types'
2
+ require 'ffi/msgpack/zone_chunk_list'
3
+ require 'ffi/msgpack/zone_finalizer_array'
4
+
5
+ module FFI
6
+ module MsgPack
7
+ class Zone < FFI::Struct
8
+
9
+ # Default chunk-size
10
+ CHUNK_SIZE = 8192
11
+
12
+ layout :chunk_list, ZoneChunkList,
13
+ :finalizer_array, ZoneFinalizerArray,
14
+ :chunk_size, :size_t
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ require 'ffi/msgpack/types'
2
+
3
+ module FFI
4
+ module MsgPack
5
+ class ZoneChunkList < FFI::Struct
6
+
7
+ layout :free, :size_t,
8
+ :ptr, :pointer,
9
+ :head, :pointer
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ require 'ffi'
2
+
3
+ module FFI
4
+ module MsgPack
5
+ class ZoneFinalizer < FFI::Struct
6
+
7
+ callback :zone_finalizer_callback, [:pointer], :void
8
+
9
+ layout :func, :zone_finalizer_callback,
10
+ :data, :pointer
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ require 'ffi'
2
+
3
+ module FFI
4
+ module MsgPack
5
+ class ZoneFinalizerArray < FFI::Struct
6
+
7
+ layout :tail, :pointer,
8
+ :end, :pointer,
9
+ :array, :pointer
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,193 @@
1
+ require 'spec_helper'
2
+ require 'ffi/msgpack/msg_object'
3
+
4
+ describe MsgPack::MsgObject do
5
+ describe "nil" do
6
+ before(:all) do
7
+ @obj = MsgPack::MsgObject.new_nil
8
+ end
9
+
10
+ it "should create nil Msg Objects from NilClasses" do
11
+ obj = MsgPack::MsgObject.new_object(nil)
12
+
13
+ obj.type.should == :nil
14
+ end
15
+
16
+ it "should create new nil Msg Objects" do
17
+ @obj.type.should == :nil
18
+ end
19
+
20
+ it "should return a Ruby nil value" do
21
+ @obj.to_ruby.should == nil
22
+ end
23
+ end
24
+
25
+ describe "boolean" do
26
+ before(:all) do
27
+ @obj = MsgPack::MsgObject.new_boolean(true)
28
+ end
29
+
30
+ it "should create new boolean Msg Objects" do
31
+ @obj.type.should == :boolean
32
+
33
+ @obj[:values][:boolean].should == 1
34
+ end
35
+
36
+ it "should create boolean Msg Objects from TrueClass/FalseClass" do
37
+ obj1 = MsgPack::MsgObject.new_object(true)
38
+ obj1.type.should == :boolean
39
+
40
+ obj2 = MsgPack::MsgObject.new_object(false)
41
+ obj2.type.should == :boolean
42
+ end
43
+
44
+ it "should return a Ruby true/false value" do
45
+ @obj.to_ruby.should == true
46
+ end
47
+ end
48
+
49
+ describe "positive integer" do
50
+ before(:all) do
51
+ @obj = MsgPack::MsgObject.new_integer(10)
52
+ end
53
+
54
+ it "should create new positive integer Msg Objects" do
55
+ @obj.type.should == :positive_integer
56
+ @obj[:values][:u64].should == 10
57
+ end
58
+
59
+ it "should create positive integer Msg Objects from Integers" do
60
+ obj1 = MsgPack::MsgObject.new_object(10)
61
+
62
+ obj1.type.should == :positive_integer
63
+ end
64
+
65
+ it "should return a Ruby Integer" do
66
+ @obj.to_ruby.should == 10
67
+ end
68
+ end
69
+
70
+ describe "negative integer" do
71
+ before(:all) do
72
+ @obj = MsgPack::MsgObject.new_integer(-1)
73
+ end
74
+
75
+ it "should create new negative integer Msg Objects" do
76
+ @obj.type.should == :negative_integer
77
+ @obj[:values][:i64].should == -1
78
+ end
79
+
80
+ it "should create negative integer Msg Objects from Integers" do
81
+ obj1 = MsgPack::MsgObject.new_object(-1)
82
+
83
+ obj1.type.should == :negative_integer
84
+ end
85
+
86
+ it "should return a Ruby Integer" do
87
+ @obj.to_ruby.should == -1
88
+ end
89
+ end
90
+
91
+ describe "floating-point" do
92
+ before(:all) do
93
+ @obj = MsgPack::MsgObject.new_double(0.002)
94
+ end
95
+
96
+ it "should create new floating-point Msg Objects" do
97
+ @obj.type.should == :double
98
+ @obj[:values][:dec].should == 0.002
99
+ end
100
+
101
+ it "should create floating-point Msg Objects from Floats" do
102
+ obj1 = MsgPack::MsgObject.new_object(0.002)
103
+
104
+ obj1.type.should == :double
105
+ end
106
+
107
+ it "should return a Ruby Float" do
108
+ @obj.to_ruby.should == 0.002
109
+ end
110
+ end
111
+
112
+ describe "raw" do
113
+ before(:all) do
114
+ @binary = "\x01example\x0d"
115
+ @obj = MsgPack::MsgObject.new_raw(@binary)
116
+ end
117
+
118
+ it "should create new raw Msg Objects" do
119
+ @obj.type.should == :raw
120
+
121
+ @obj[:values][:raw].length.should == @binary.length
122
+ @obj[:values][:raw].to_s.should == @binary
123
+ end
124
+
125
+ it "should create raw Msg Objects from Strings" do
126
+ obj = MsgPack::MsgObject.new_object(@binary)
127
+
128
+ obj.type.should == :raw
129
+ end
130
+
131
+ it "should create raw Msg Objects from Symbols" do
132
+ obj = MsgPack::MsgObject.new_object(:example)
133
+
134
+ obj.type.should == :raw
135
+ end
136
+
137
+ it "should return a String" do
138
+ @obj.to_ruby.should == @binary
139
+ end
140
+ end
141
+
142
+ describe "array" do
143
+ before(:all) do
144
+ @array = [1,true,nil,0.002,'a']
145
+ @obj = MsgPack::MsgObject.new_array(@array)
146
+ end
147
+
148
+ it "should create new array Msg Objects" do
149
+ @obj.type.should == :array
150
+
151
+ @obj[:values][:array].length.should == @array.length
152
+ end
153
+
154
+ it "should create array Msg Objects from Arrays" do
155
+ obj = MsgPack::MsgObject.new_object(@array)
156
+
157
+ obj.type.should == :array
158
+ end
159
+
160
+ it "should return a Ruby Array" do
161
+ @obj.to_ruby.should == @array
162
+ end
163
+ end
164
+
165
+ describe "map" do
166
+ before(:all) do
167
+ @hash = {
168
+ 1 => 2,
169
+ 0.001 => 0.002,
170
+ nil => nil,
171
+ true => false,
172
+ 'a' => 'b'
173
+ }
174
+ @obj = MsgPack::MsgObject.new_map(@hash)
175
+ end
176
+
177
+ it "should create new map Msg Objects" do
178
+ @obj.type.should == :map
179
+
180
+ @obj[:values][:map].length.should == @hash.length
181
+ end
182
+
183
+ it "should create map Msg Objects from Hashes" do
184
+ obj = MsgPack::MsgObject.new_object(@hash)
185
+
186
+ obj.type.should == :map
187
+ end
188
+
189
+ it "should return a Ruby Hash" do
190
+ @obj.to_ruby.should == @hash
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+ require 'ffi/msgpack/packer'
3
+
4
+ describe MsgPack::Packer do
5
+ context "buffer" do
6
+ before(:each) do
7
+ @packer = MsgPack::Packer.create
8
+ @packer << 1
9
+ end
10
+
11
+ it "should write packed messages to a buffer" do
12
+ @packer.buffer.should == "\x01"
13
+ end
14
+
15
+ it "should track the number of bytes written" do
16
+ @packer.total.should == 1
17
+ end
18
+
19
+ it "should be convertable to a String" do
20
+ @packer.to_s.should == "\x01"
21
+ end
22
+ end
23
+
24
+ context "callback" do
25
+ before(:each) do
26
+ @buffer = []
27
+ end
28
+
29
+ it "should write packed messages using a callback" do
30
+ packer = MsgPack::Packer.create do |packed|
31
+ @buffer << packed
32
+ end
33
+ packer << 1
34
+
35
+ @buffer.should == ["\x01"]
36
+ end
37
+
38
+ it "should track the number of bytes written" do
39
+ packer = MsgPack::Packer.create do |packed|
40
+ @buffer << packed
41
+ end
42
+ packer << 1
43
+
44
+ packer.total.should == 1
45
+ end
46
+
47
+ it "should accept a secondary length argument" do
48
+ packer = MsgPack::Packer.create do |packed,length|
49
+ @buffer << [packed, length]
50
+ end
51
+ packer << 1
52
+
53
+ @buffer.should == [["\x01", 1]]
54
+ end
55
+
56
+ it "should not be convertable to a String" do
57
+ packer = MsgPack::Packer.create { |packed| }
58
+
59
+ packer.to_s.should == nil
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:runtime, :test)
6
+ rescue Bundler::BundlerError => e
7
+ STDERR.puts e.message
8
+ STDERR.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+
12
+ require 'spec'
13
+ require 'ffi'
14
+
15
+ include FFI
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+ require 'ffi/msgpack/unpacker'
3
+
4
+ require 'stringio'
5
+
6
+ describe MsgPack::Unpacker do
7
+ before(:all) do
8
+ @packed = "\x01\x02\x03"
9
+ end
10
+
11
+ before(:each) do
12
+ @unpacker = MsgPack::Unpacker.create(1024)
13
+ end
14
+
15
+ describe "buffer" do
16
+ it "should enqueue data into the buffer" do
17
+ size1 = @unpacker[:used]
18
+ @unpacker << @packed
19
+ size2 = @unpacker[:used]
20
+
21
+ (size2 - size1).should == @packed.length
22
+ end
23
+
24
+ it "should enqueue data from IO objects into the buffer" do
25
+ io = StringIO.new(@packed)
26
+
27
+ size1 = @unpacker[:used]
28
+ @unpacker.read(io).should == true
29
+ size2 = @unpacker[:used]
30
+
31
+ (size2 - size1).should == @packed.length
32
+ end
33
+ end
34
+
35
+ describe "each_object" do
36
+ it "should unpack each Msg Object from the buffer" do
37
+ objs = []
38
+ expected = [
39
+ [:positive_integer, 1],
40
+ [:positive_integer, 2],
41
+ [:positive_integer, 3]
42
+ ]
43
+
44
+ @unpacker << @packed
45
+ @unpacker.each_object do |obj|
46
+ objs << [obj.type, obj.values[:u64]]
47
+ end
48
+
49
+ objs.should == expected
50
+ end
51
+
52
+ it "should unpack each Msg Object from a stream" do
53
+ io = StringIO.new(@packed)
54
+ objs = []
55
+ expected = [
56
+ [:positive_integer, 1],
57
+ [:positive_integer, 2],
58
+ [:positive_integer, 3]
59
+ ]
60
+
61
+ @unpacker.stream = io
62
+ @unpacker.each_object do |obj|
63
+ objs << [obj.type, obj.values[:u64]]
64
+ end
65
+
66
+ objs.should == expected
67
+ end
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,190 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffi-msgpack
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Postmodern
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-21 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: ffi
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 0
31
+ - 6
32
+ - 0
33
+ version: 0.6.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 9
45
+ segments:
46
+ - 0
47
+ - 9
48
+ - 25
49
+ version: 0.9.25
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rake
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 49
61
+ segments:
62
+ - 0
63
+ - 8
64
+ - 7
65
+ version: 0.8.7
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ hash: 7
77
+ segments:
78
+ - 1
79
+ - 4
80
+ - 0
81
+ version: 1.4.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ name: rspec
87
+ requirement: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ~>
91
+ - !ruby/object:Gem::Version
92
+ hash: 27
93
+ segments:
94
+ - 1
95
+ - 3
96
+ - 0
97
+ version: 1.3.0
98
+ type: :development
99
+ prerelease: false
100
+ version_requirements: *id005
101
+ description: Ruby FFI bindings for the msgpack library.
102
+ email: postmodern.mod3@gmail.com
103
+ executables: []
104
+
105
+ extensions: []
106
+
107
+ extra_rdoc_files:
108
+ - ChangeLog.md
109
+ - LICENSE.txt
110
+ - README.md
111
+ files:
112
+ - .gitignore
113
+ - .specopts
114
+ - .yardopts
115
+ - ChangeLog.md
116
+ - Gemfile
117
+ - LICENSE.txt
118
+ - README.md
119
+ - Rakefile
120
+ - VERSION
121
+ - ffi-msgpack.gemspec
122
+ - lib/ffi/msgpack.rb
123
+ - lib/ffi/msgpack/exceptions/parse_error.rb
124
+ - lib/ffi/msgpack/extensions.rb
125
+ - lib/ffi/msgpack/extensions/array.rb
126
+ - lib/ffi/msgpack/extensions/false_class.rb
127
+ - lib/ffi/msgpack/extensions/float.rb
128
+ - lib/ffi/msgpack/extensions/hash.rb
129
+ - lib/ffi/msgpack/extensions/integer.rb
130
+ - lib/ffi/msgpack/extensions/nil_class.rb
131
+ - lib/ffi/msgpack/extensions/string.rb
132
+ - lib/ffi/msgpack/extensions/true_class.rb
133
+ - lib/ffi/msgpack/msg_array.rb
134
+ - lib/ffi/msgpack/msg_key_value.rb
135
+ - lib/ffi/msgpack/msg_map.rb
136
+ - lib/ffi/msgpack/msg_object.rb
137
+ - lib/ffi/msgpack/msg_object_union.rb
138
+ - lib/ffi/msgpack/msg_raw.rb
139
+ - lib/ffi/msgpack/msgpack.rb
140
+ - lib/ffi/msgpack/packable.rb
141
+ - lib/ffi/msgpack/packer.rb
142
+ - lib/ffi/msgpack/types.rb
143
+ - lib/ffi/msgpack/unpacker.rb
144
+ - lib/ffi/msgpack/zone.rb
145
+ - lib/ffi/msgpack/zone_chunk_list.rb
146
+ - lib/ffi/msgpack/zone_finalizer.rb
147
+ - lib/ffi/msgpack/zone_finalizer_array.rb
148
+ - spec/msg_object_spec.rb
149
+ - spec/packer_spec.rb
150
+ - spec/spec_helper.rb
151
+ - spec/unpacker_spec.rb
152
+ has_rdoc: yard
153
+ homepage: http://github.com/postmodern/ffi-msgpack
154
+ licenses:
155
+ - MIT
156
+ post_install_message:
157
+ rdoc_options: []
158
+
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ hash: 3
167
+ segments:
168
+ - 0
169
+ version: "0"
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ none: false
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ hash: 3
176
+ segments:
177
+ - 0
178
+ version: "0"
179
+ requirements:
180
+ - libmsgpack, 0.4.2 or greater
181
+ rubyforge_project:
182
+ rubygems_version: 1.3.7
183
+ signing_key:
184
+ specification_version: 3
185
+ summary: FFI bindings for msgpack
186
+ test_files:
187
+ - spec/msg_object_spec.rb
188
+ - spec/packer_spec.rb
189
+ - spec/spec_helper.rb
190
+ - spec/unpacker_spec.rb