hessian2 1.1.1 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -2
  3. data/LICENSE.txt +22 -0
  4. data/README.md +161 -0
  5. data/hessian2.gemspec +3 -2
  6. data/lib/hessian2.rb +12 -4
  7. data/lib/hessian2/class_wrapper.rb +89 -0
  8. data/lib/hessian2/client.rb +38 -0
  9. data/lib/hessian2/constants.rb +164 -0
  10. data/lib/hessian2/em_client.rb +52 -0
  11. data/lib/hessian2/fault.rb +3 -0
  12. data/lib/hessian2/handler.rb +15 -0
  13. data/lib/hessian2/hessian_client.rb +3 -38
  14. data/lib/hessian2/parser.rb +619 -0
  15. data/lib/hessian2/struct_wrapper.rb +55 -0
  16. data/lib/hessian2/type_wrapper.rb +49 -8
  17. data/lib/hessian2/version.rb +1 -1
  18. data/lib/hessian2/writer.rb +498 -0
  19. data/spec/binary_spec.rb +51 -0
  20. data/spec/boolean_spec.rb +26 -0
  21. data/spec/class_wrapper_spec.rb +52 -0
  22. data/spec/create_monkeys.rb +14 -0
  23. data/spec/date_spec.rb +45 -0
  24. data/spec/double_spec.rb +78 -0
  25. data/spec/int_spec.rb +54 -0
  26. data/spec/list_spec.rb +66 -0
  27. data/spec/long_spec.rb +68 -0
  28. data/spec/map_spec.rb +36 -0
  29. data/spec/null_spec.rb +17 -0
  30. data/spec/object_spec.rb +65 -0
  31. data/spec/ref_spec.rb +43 -0
  32. data/spec/spec_helper.rb +23 -0
  33. data/spec/string_spec.rb +61 -0
  34. data/spec/struct_wrapper_spec.rb +47 -0
  35. data/spec/type_wrapper_spec.rb +102 -0
  36. data/test/app.rb +15 -0
  37. data/test/client.rb +9 -0
  38. data/test/config.ru +2 -0
  39. data/test/monkey_service.rb +12 -0
  40. metadata +79 -19
  41. data/README.rdoc +0 -21
  42. data/lib/hessian2/hessian_exception.rb +0 -3
  43. data/lib/hessian2/hessian_parser.rb +0 -75
  44. data/lib/hessian2/hessian_writer.rb +0 -130
  45. data/test/test_hessian_parser.rb +0 -56
metadata CHANGED
@@ -1,18 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hessian2
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
5
- prerelease:
4
+ version: 2.0.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - takafan
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-12-08 00:00:00.000000000 Z
13
- dependencies: []
14
- description: implement hessian 1.0.2 specification. refactor Christer Sandberg's hessian,
15
- ruby 1.9.3 required.
11
+ date: 2013-10-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bigdecimal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: like json, additionally, 麻绳2 parse your object as a struct.
16
28
  email:
17
29
  - takafan@163.com
18
30
  executables: []
@@ -21,39 +33,87 @@ extra_rdoc_files: []
21
33
  files:
22
34
  - .gitignore
23
35
  - Gemfile
24
- - README.rdoc
36
+ - LICENSE.txt
37
+ - README.md
25
38
  - Rakefile
26
39
  - hessian2.gemspec
27
40
  - lib/hessian2.rb
41
+ - lib/hessian2/class_wrapper.rb
42
+ - lib/hessian2/client.rb
43
+ - lib/hessian2/constants.rb
44
+ - lib/hessian2/em_client.rb
45
+ - lib/hessian2/fault.rb
46
+ - lib/hessian2/handler.rb
28
47
  - lib/hessian2/hessian_client.rb
29
- - lib/hessian2/hessian_exception.rb
30
- - lib/hessian2/hessian_parser.rb
31
- - lib/hessian2/hessian_writer.rb
48
+ - lib/hessian2/parser.rb
49
+ - lib/hessian2/struct_wrapper.rb
32
50
  - lib/hessian2/type_wrapper.rb
33
51
  - lib/hessian2/version.rb
34
- - test/test_hessian_parser.rb
52
+ - lib/hessian2/writer.rb
53
+ - spec/binary_spec.rb
54
+ - spec/boolean_spec.rb
55
+ - spec/class_wrapper_spec.rb
56
+ - spec/create_monkeys.rb
57
+ - spec/date_spec.rb
58
+ - spec/double_spec.rb
59
+ - spec/int_spec.rb
60
+ - spec/list_spec.rb
61
+ - spec/long_spec.rb
62
+ - spec/map_spec.rb
63
+ - spec/null_spec.rb
64
+ - spec/object_spec.rb
65
+ - spec/ref_spec.rb
66
+ - spec/spec_helper.rb
67
+ - spec/string_spec.rb
68
+ - spec/struct_wrapper_spec.rb
69
+ - spec/type_wrapper_spec.rb
70
+ - test/app.rb
71
+ - test/client.rb
72
+ - test/config.ru
73
+ - test/monkey_service.rb
35
74
  homepage: http://github.com/takafan/hessian2
36
- licenses: []
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
37
78
  post_install_message:
38
79
  rdoc_options: []
39
80
  require_paths:
40
81
  - lib
41
82
  required_ruby_version: !ruby/object:Gem::Requirement
42
- none: false
43
83
  requirements:
44
- - - ! '>='
84
+ - - '>='
45
85
  - !ruby/object:Gem::Version
46
86
  version: '0'
47
87
  required_rubygems_version: !ruby/object:Gem::Requirement
48
- none: false
49
88
  requirements:
50
- - - ! '>='
89
+ - - '>='
51
90
  - !ruby/object:Gem::Version
52
91
  version: '0'
53
92
  requirements: []
54
93
  rubyforge_project: hessian2
55
- rubygems_version: 1.8.24
94
+ rubygems_version: 2.1.5
56
95
  signing_key:
57
- specification_version: 3
96
+ specification_version: 4
58
97
  summary: Hessian2
59
- test_files: []
98
+ test_files:
99
+ - spec/binary_spec.rb
100
+ - spec/boolean_spec.rb
101
+ - spec/class_wrapper_spec.rb
102
+ - spec/create_monkeys.rb
103
+ - spec/date_spec.rb
104
+ - spec/double_spec.rb
105
+ - spec/int_spec.rb
106
+ - spec/list_spec.rb
107
+ - spec/long_spec.rb
108
+ - spec/map_spec.rb
109
+ - spec/null_spec.rb
110
+ - spec/object_spec.rb
111
+ - spec/ref_spec.rb
112
+ - spec/spec_helper.rb
113
+ - spec/string_spec.rb
114
+ - spec/struct_wrapper_spec.rb
115
+ - spec/type_wrapper_spec.rb
116
+ - test/app.rb
117
+ - test/client.rb
118
+ - test/config.ru
119
+ - test/monkey_service.rb
data/README.rdoc DELETED
@@ -1,21 +0,0 @@
1
- = hessian2
2
-
3
- implement hessian 1.0.2 specification. refactor Christer Sandberg's hessian, ruby 1.9.3 required.
4
-
5
- == Using
6
-
7
- gem install hessian2
8
-
9
- require 'hessian2'
10
-
11
- url = 'http://localhost:8080/person_service'
12
-
13
- client = Hessian2::HessianClient.new(url)
14
-
15
- # call remote function: public Person getPersonByUid(Long uid);
16
- uid = Hessian2::TypeWrapper.new('L', 59)
17
- puts client.getPersonByUid(uid)
18
-
19
- == Authors
20
-
21
- * {Takafan}[http://hululuu.com]
@@ -1,3 +0,0 @@
1
- module Hessian2
2
- class HessianException < RuntimeError; end
3
- end
@@ -1,75 +0,0 @@
1
- module Hessian2
2
- module HessianParser
3
-
4
- def parse(data, refs = [], chunks = [])
5
- t = data.slice!(0)
6
- case t
7
- when 'r' # reply
8
- data.slice!(0, 2)
9
- parse(data)
10
- when 'f' # fault
11
- parse(data)
12
- code = parse(data)
13
- parse(data)
14
- message = parse(data)
15
- raise HessianException.new, "#{code}: #{message}"
16
- when 'N' # null
17
- nil
18
- when 'T' # true
19
- true
20
- when 'F' # false
21
- false
22
- when 'I' # int
23
- data.slice!(0, 4).unpack('l>')[0]
24
- when 'L' # long
25
- data.slice!(0, 8).unpack('q>')[0]
26
- when 'D' # double
27
- data.slice!(0, 8).unpack('G')[0]
28
- when 'd' # date
29
- val = data.slice!(0, 8).unpack('Q>')[0]
30
- Time.at(val / 1000, val % 1000 * 1000)
31
- when 'S', 's', 'X', 'x' # string, xml
32
- len = data.slice!(0, 2).unpack('n')[0]
33
-
34
- chunk = data.unpack("U#{len}")
35
- chunks << chunk
36
- data.slice!(0, chunk.pack('U*').bytesize)
37
-
38
- if 'sx'.include?(t)
39
- parse(data, refs, chunks)
40
- else
41
- chunks.flatten.pack('U*')
42
- end
43
- when 'B', 'b' # binary
44
- len = data.slice!(0, 2).unpack('n')[0]
45
-
46
- chunk = data.slice!(0, len)
47
- chunks << chunk
48
-
49
- if t == 'b'
50
- parse(data, refs, chunks)
51
- else
52
- chunks.flatten
53
- end
54
- when 'V' # list
55
- data.slice!(0, 3 + data.unpack('an')[1]) if data[0] == 't'
56
- data.slice!(0, 5) if data[0] == 'l'
57
- refs << (list = [])
58
- list << parse(data, refs) while data[0] != 'z'
59
- data.slice!(0)
60
- list
61
- when 'M' # map
62
- data.slice!(0, 3 + data.unpack('an')[1]) if data[0] == 't'
63
- refs << (map = {})
64
- map[parse(data, refs)] = parse(data, refs) while data[0] != 'z'
65
- data.slice!(0)
66
- map
67
- when 'R' # ref
68
- refs[data.slice!(0, 4).unpack('N')[0]]
69
- else
70
- raise HessianException.new, "Invalid type: '#{t}'"
71
- end
72
- end
73
-
74
- end
75
- end
@@ -1,130 +0,0 @@
1
- module Hessian2
2
- module HessianWriter
3
- CHUNK_SIZE = 32768
4
-
5
- def write_call(method, args)
6
- refs = {}
7
- out = [ 'c', '0', '1', 'm', method.length ].pack('ahhan') << method
8
- args.each { |arg| out << write(arg, refs) }
9
- out << 'z'
10
- end
11
-
12
- private
13
- def write(val, refs = {}, chunks = [], type = nil)
14
- case val
15
- when TypeWrapper
16
- obj, hessian_type = val.object, val.hessian_type
17
- case hessian_type
18
- when 'L', 'Long', 'long' # declare as long
19
- [ 'L', obj ].pack('aq>') # long
20
- when 'X', 'x' # declare as xml
21
- if obj.size > CHUNK_SIZE
22
- chunk = obj.slice!(0, CHUNK_SIZE)
23
- if chunk.ascii_only?
24
- chunks << [ 's', CHUNK_SIZE ].pack('an') << chunk
25
- else
26
- chunks << [ 's', CHUNK_SIZE, chunk.unpack('U*') ].flatten.pack('anU*')
27
- end
28
- write(TypeWrapper.new('X', obj), refs, chunks)
29
- else
30
- if obj.ascii_only?
31
- chunks << [ 'X', obj.size ].pack('an') << obj
32
- else
33
- chunks << [ 'X', obj.size, obj.unpack('U*') ].flatten.pack('anU*')
34
- end
35
- chunks.join # xml
36
- end
37
- when 'B', 'b' # declare as binary
38
- [ 'B', obj.size ].pack('an') << obj
39
- if obj.size > CHUNK_SIZE
40
- chunk = obj.slice!(0, CHUNK_SIZE)
41
- chunks << [ 'b', CHUNK_SIZE ].pack('an') << chunk
42
- write(TypeWrapper.new('B', obj), refs, chunks)
43
- else
44
- chunks << [ 'B', obj.size ].pack('an') << obj
45
- chunks.join # binary
46
- end
47
- else # type for list, map
48
- write(obj, refs, chunks, hessian_type)
49
- end
50
- when NilClass
51
- 'N' # null
52
- when TrueClass
53
- 'T' # true
54
- when FalseClass
55
- 'F' # false
56
- when Fixnum
57
- [ 'I', val ].pack('al>') # int
58
- when Bignum
59
- [ 'L', val ].pack('aq>') # long
60
- when Float
61
- [ 'D', val ].pack('aG') # double
62
- when Time
63
- [ 'd', val.to_f * 1000 ].pack('aQ>') # date
64
- when String
65
- if val.size > CHUNK_SIZE
66
- chunk = val.slice!(0, CHUNK_SIZE)
67
- if chunk.ascii_only?
68
- chunks << [ 's', CHUNK_SIZE ].pack('an') << chunk
69
- else
70
- # unpack-pack if chunk incompatible with ASCII-8BIT
71
- chunks << [ 's', CHUNK_SIZE, chunk.unpack('U*') ].flatten.pack('anU*')
72
- end
73
- write(val, refs, chunks)
74
- else
75
- if val.ascii_only?
76
- chunks << [ 'S', val.size ].pack('an') << val
77
- else
78
- chunks << [ 'S', val.size, val.unpack('U*') ].flatten.pack('anU*')
79
- end
80
- chunks.join # string
81
- end
82
- when Symbol
83
- str = val.to_s
84
- [ 'S', str.size ].pack('an') << str # string
85
- when Array
86
- idx = refs[val.object_id]
87
- return [ 'R', idx ].pack('aN') if idx
88
-
89
- refs[val.object_id] = refs.size
90
-
91
- str = 'V'
92
- str << 't' << [ type.size, type ].pack('na*') if type
93
- str << 'l' << [ val.size ].pack('N')
94
- val.each{ |v| str << write(v, refs) }
95
- str << 'z' # list
96
- when Hash
97
- idx = refs[val.object_id]
98
- return [ 'R', idx ].pack('aN') if idx
99
-
100
- refs[val.object_id] = refs.size
101
-
102
- str = 'M'
103
- str << 't' << [ type.size, type ].pack('na*') if type
104
- val.each do |k, v|
105
- str << write(k, refs)
106
- str << write(v, refs)
107
- end
108
- str << 'z' # map
109
- else # covert val to hash
110
- h = {}.tap do |h|
111
- val.instance_variables.each {|var| h[var.to_s.delete("@")] = val.instance_variable_get(var) }
112
- end
113
-
114
- unless type
115
- # map ruby module to java package
116
- arr = val.class.to_s.split('::')
117
- if arr.size > 1
118
- klass = arr.pop
119
- type = arr.map{|m| m.downcase}.join('.') << ".#{klass}"
120
- else
121
- type = arr.first
122
- end
123
- end
124
-
125
- write(h, refs, chunks, type)
126
- end
127
- end
128
-
129
- end
130
- end
@@ -1,56 +0,0 @@
1
- lib_path = File.expand_path('../../lib', __FILE__)
2
- $:.unshift(lib_path)
3
- require 'hessian2'
4
- require 'test/unit'
5
-
6
- class HessianParserTest < Test::Unit::TestCase
7
-
8
- include Hessian2::HessianParser
9
-
10
- def test_integer
11
- assert_equal 4711, parse("r\001\000I\000\000\022gz")
12
- end
13
- def test_long
14
- assert_equal 2, parse("r\001\000L\000\000\000\000\000\000\000\002z")
15
- end
16
- def test_double
17
- assert_equal 3.4, parse("r\001\000D@\v333333z")
18
- end
19
- def test_false
20
- assert_equal false, parse("r\001\000Fz")
21
- end
22
- def test_true
23
- assert_equal true, parse("r\001\000Tz")
24
- end
25
- def test_string
26
- assert_equal "string", parse("r\001\000S\000\006stringz")
27
- end
28
- def test_null
29
- assert_equal nil, parse("r\001\000Nz")
30
- end
31
- def test_date
32
- time = parse("r\001\000d\000\000\001\010\344\036\332\360z")
33
- assert_instance_of Time, time
34
- assert_equal '2006-01-19 19:23:13', time.getutc.strftime("%Y-%m-%d %H:%M:%S")
35
- assert_equal 520000, time.usec
36
- end
37
- def test_integer_array
38
- assert_equal [ 1, 2, 3 ], parse([ "r\001\000Vt\000\004[intl\000\000\000\003",
39
- "I\000\000\000\001I\000\000\000\002I\000\000\000\003zz" ].join)
40
- end
41
- def test_array
42
- assert_equal [ 'sillen', 32 ], parse([ "r\001\000Vt\000\a[objectl\000\000\000\002",
43
- "S\000\006sillenI\000\000\000 zz" ].join)
44
- end
45
- def test_array_in_array
46
- assert_equal [ 'A list', [ 9, 3 ] ], parse([ "r\001\000Vl\000\000\000\002S\000\006",
47
- "A listVt\000\022[java.lang.Integerl\000\000\000\002I\000\000\000\t",
48
- "I\000\000\000\003zzz" ].join)
49
- end
50
- def test_map
51
- map = { 'sillen' => 32, 'numbers' => [ 1.1, 1.2, 1.3 ] }
52
- assert_equal map, parse([ "r\001\000Mt\000\000S\000\anumbersVt\000\a[double",
53
- "l\000\000\000\003D?\361\231\231\231\231\231\232D?\363333333D?",
54
- "\364\314\314\314\314\314\315zS\000\006sillenI\000\000\000 zz" ].join)
55
- end
56
- end