thrift 0.8.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +7 -0
  2. data/{README → README.md} +0 -0
  3. data/ext/binary_protocol_accelerated.c +33 -14
  4. data/ext/bytes.c +36 -0
  5. data/ext/bytes.h +31 -0
  6. data/ext/compact_protocol.c +27 -8
  7. data/ext/constants.h +8 -5
  8. data/ext/extconf.rb +5 -1
  9. data/ext/memory_buffer.c +12 -9
  10. data/ext/protocol.c +0 -185
  11. data/ext/protocol.h +0 -20
  12. data/ext/strlcpy.h +7 -3
  13. data/ext/struct.c +27 -7
  14. data/ext/thrift_native.c +16 -11
  15. data/lib/thrift.rb +10 -4
  16. data/lib/thrift/bytes.rb +131 -0
  17. data/lib/thrift/client.rb +13 -4
  18. data/lib/thrift/exceptions.rb +3 -0
  19. data/lib/thrift/multiplexed_processor.rb +76 -0
  20. data/lib/thrift/processor.rb +24 -6
  21. data/lib/thrift/protocol/base_protocol.rb +109 -12
  22. data/lib/thrift/protocol/binary_protocol.rb +22 -7
  23. data/lib/thrift/protocol/binary_protocol_accelerated.rb +8 -0
  24. data/lib/thrift/protocol/compact_protocol.rb +23 -6
  25. data/lib/thrift/protocol/json_protocol.rb +786 -0
  26. data/lib/thrift/protocol/multiplexed_protocol.rb +44 -0
  27. data/lib/thrift/protocol/protocol_decorator.rb +194 -0
  28. data/lib/thrift/server/base_server.rb +8 -2
  29. data/lib/thrift/server/mongrel_http_server.rb +2 -0
  30. data/lib/thrift/server/simple_server.rb +5 -1
  31. data/lib/thrift/server/thin_http_server.rb +91 -0
  32. data/lib/thrift/server/thread_pool_server.rb +5 -1
  33. data/lib/thrift/server/threaded_server.rb +5 -1
  34. data/lib/thrift/struct.rb +1 -1
  35. data/lib/thrift/struct_union.rb +2 -2
  36. data/lib/thrift/transport/base_server_transport.rb +1 -1
  37. data/lib/thrift/transport/base_transport.rb +30 -20
  38. data/lib/thrift/transport/buffered_transport.rb +25 -11
  39. data/lib/thrift/transport/framed_transport.rb +20 -11
  40. data/lib/thrift/transport/http_client_transport.rb +16 -6
  41. data/lib/thrift/transport/io_stream_transport.rb +5 -2
  42. data/lib/thrift/transport/memory_buffer_transport.rb +10 -6
  43. data/lib/thrift/transport/server_socket.rb +6 -1
  44. data/lib/thrift/transport/socket.rb +23 -17
  45. data/lib/thrift/transport/ssl_server_socket.rb +41 -0
  46. data/lib/thrift/transport/ssl_socket.rb +51 -0
  47. data/lib/thrift/transport/unix_server_socket.rb +5 -1
  48. data/lib/thrift/transport/unix_socket.rb +5 -1
  49. data/lib/thrift/union.rb +3 -6
  50. data/spec/BaseService.thrift +27 -0
  51. data/spec/ExtendedService.thrift +25 -0
  52. data/spec/Referenced.thrift +44 -0
  53. data/spec/ThriftNamespacedSpec.thrift +53 -0
  54. data/spec/ThriftSpec.thrift +52 -1
  55. data/spec/base_protocol_spec.rb +158 -93
  56. data/spec/base_transport_spec.rb +194 -157
  57. data/spec/binary_protocol_accelerated_spec.rb +14 -14
  58. data/spec/binary_protocol_spec.rb +29 -16
  59. data/spec/binary_protocol_spec_shared.rb +148 -65
  60. data/spec/bytes_spec.rb +160 -0
  61. data/spec/client_spec.rb +45 -47
  62. data/spec/compact_protocol_spec.rb +36 -22
  63. data/spec/exception_spec.rb +79 -80
  64. data/spec/flat_spec.rb +62 -0
  65. data/spec/http_client_spec.rb +91 -16
  66. data/spec/json_protocol_spec.rb +552 -0
  67. data/spec/namespaced_spec.rb +67 -0
  68. data/spec/nonblocking_server_spec.rb +26 -28
  69. data/spec/processor_spec.rb +29 -32
  70. data/spec/serializer_spec.rb +31 -33
  71. data/spec/server_socket_spec.rb +32 -28
  72. data/spec/server_spec.rb +112 -84
  73. data/spec/socket_spec.rb +27 -20
  74. data/spec/socket_spec_shared.rb +32 -32
  75. data/spec/spec_helper.rb +17 -11
  76. data/spec/ssl_server_socket_spec.rb +34 -0
  77. data/spec/ssl_socket_spec.rb +78 -0
  78. data/spec/struct_nested_containers_spec.rb +191 -0
  79. data/spec/struct_spec.rb +159 -161
  80. data/spec/thin_http_server_spec.rb +141 -0
  81. data/spec/types_spec.rb +71 -69
  82. data/spec/union_spec.rb +97 -76
  83. data/spec/unix_socket_spec.rb +49 -41
  84. metadata +268 -188
  85. data/CHANGELOG +0 -1
  86. data/benchmark/gen-rb/benchmark_constants.rb +0 -10
  87. data/benchmark/gen-rb/benchmark_service.rb +0 -80
  88. data/benchmark/gen-rb/benchmark_types.rb +0 -9
  89. data/spec/gen-rb/nonblocking_service.rb +0 -272
  90. data/spec/gen-rb/thrift_spec_constants.rb +0 -10
  91. data/spec/gen-rb/thrift_spec_types.rb +0 -345
  92. data/spec/mongrel_http_server_spec.rb +0 -117
  93. data/test/debug_proto/gen-rb/debug_proto_test_constants.rb +0 -273
  94. data/test/debug_proto/gen-rb/debug_proto_test_types.rb +0 -760
  95. data/test/debug_proto/gen-rb/empty_service.rb +0 -24
  96. data/test/debug_proto/gen-rb/inherited.rb +0 -79
  97. data/test/debug_proto/gen-rb/reverse_order_service.rb +0 -82
  98. data/test/debug_proto/gen-rb/service_for_exception_with_a_map.rb +0 -81
  99. data/test/debug_proto/gen-rb/srv.rb +0 -330
@@ -0,0 +1,44 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+
19
+ require 'thrift/protocol/protocol_decorator'
20
+
21
+ module Thrift
22
+ class MultiplexedProtocol < BaseProtocol
23
+
24
+ include ProtocolDecorator
25
+
26
+ def initialize(protocol, service_name)
27
+ super(protocol)
28
+ @service_name = service_name
29
+ end
30
+
31
+ def write_message_begin(name, type, seqid)
32
+ case type
33
+ when MessageTypes::CALL, MessageTypes::ONEWAY
34
+ @protocol.write_message_begin("#{@service_name}:#{name}", type, seqid)
35
+ else
36
+ @protocol.write_message_begin(name, type, seqid)
37
+ end
38
+ end
39
+
40
+ def to_s
41
+ "multiplexed(#{@service_name=@protocol.to_s})"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,194 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+
19
+ module Thrift
20
+ module ProtocolDecorator
21
+
22
+ def initialize(protocol)
23
+ @protocol = protocol
24
+ end
25
+
26
+ def trans
27
+ @protocol.trans
28
+ end
29
+
30
+ def write_message_begin(name, type, seqid)
31
+ @protocol.write_message_begin
32
+ end
33
+
34
+ def write_message_end
35
+ @protocol.write_message_end
36
+ end
37
+
38
+ def write_struct_begin(name)
39
+ @protocol.write_struct_begin(name)
40
+ end
41
+
42
+ def write_struct_end
43
+ @protocol.write_struct_end
44
+ end
45
+
46
+ def write_field_begin(name, type, id)
47
+ @protocol.write_field_begin(name, type, id)
48
+ end
49
+
50
+ def write_field_end
51
+ @protocol.write_field_end
52
+ end
53
+
54
+ def write_field_stop
55
+ @protocol.write_field_stop
56
+ end
57
+
58
+ def write_map_begin(ktype, vtype, size)
59
+ @protocol.write_map_begin(ktype, vtype, size)
60
+ end
61
+
62
+ def write_map_end
63
+ @protocol.write_map_end
64
+ end
65
+
66
+ def write_list_begin(etype, size)
67
+ @protocol.write_list_begin(etype, size)
68
+ end
69
+
70
+ def write_list_end
71
+ @protocol.write_list_end
72
+ end
73
+
74
+ def write_set_begin(etype, size)
75
+ @protocol.write_set_begin(etype, size)
76
+ end
77
+
78
+ def write_set_end
79
+ @protocol.write_set_end
80
+ end
81
+
82
+ def write_bool(bool)
83
+ @protocol.write_bool(bool)
84
+ end
85
+
86
+ def write_byte(byte)
87
+ @protocol.write_byte(byte)
88
+ end
89
+
90
+ def write_i16(i16)
91
+ @protocol.write_i16(i16)
92
+ end
93
+
94
+ def write_i32(i32)
95
+ @protocol.write_i32(i32)
96
+ end
97
+
98
+ def write_i64(i64)
99
+ @protocol.write_i64(i64)
100
+ end
101
+
102
+ def write_double(dub)
103
+ @protocol.write_double(dub)
104
+ end
105
+
106
+ def write_string(str)
107
+ @protocol.write_string(str)
108
+ end
109
+
110
+ def write_binary(buf)
111
+ @protocol.write_binary(buf)
112
+ end
113
+
114
+ def read_message_begin
115
+ @protocol.read_message_begin
116
+ end
117
+
118
+ def read_message_end
119
+ @protocol.read_message_end
120
+ end
121
+
122
+ def read_struct_begin
123
+ @protocol.read_struct_begin
124
+ end
125
+
126
+ def read_struct_end
127
+ @protocol.read_struct_end
128
+ end
129
+
130
+ def read_field_begin
131
+ @protocol.read_field_begin
132
+ end
133
+
134
+ def read_field_end
135
+ @protocol.read_field_end
136
+ end
137
+
138
+ def read_map_begin
139
+ @protocol.read_map_begin
140
+ end
141
+
142
+ def read_map_end
143
+ @protocol.read_map_end
144
+ end
145
+
146
+ def read_list_begin
147
+ @protocol.read_list_begin
148
+ end
149
+
150
+ def read_list_end
151
+ @protocol.read_list_end
152
+ end
153
+
154
+ def read_set_begin
155
+ @protocol.read_set_begin
156
+ end
157
+
158
+ def read_set_end
159
+ @protocol.read_set_end
160
+ end
161
+
162
+ def read_bool
163
+ @protocol.read_bool
164
+ end
165
+
166
+ def read_byte
167
+ @protocol.read_byte
168
+ end
169
+
170
+ def read_i16
171
+ @protocol.read_i16
172
+ end
173
+
174
+ def read_i32
175
+ @protocol.read_i32
176
+ end
177
+
178
+ def read_i64
179
+ @protocol.read_i64
180
+ end
181
+
182
+ def read_double
183
+ @protocol.read_double
184
+ end
185
+
186
+ def read_string
187
+ @protocol.read_string
188
+ end
189
+
190
+ def read_binary
191
+ @protocol.read_binary
192
+ end
193
+ end
194
+ end
@@ -26,6 +26,12 @@ module Thrift
26
26
  @protocol_factory = protocol_factory ? protocol_factory : Thrift::BinaryProtocolFactory.new
27
27
  end
28
28
 
29
- def serve; nil; end
29
+ def serve
30
+ raise NotImplementedError
31
+ end
32
+
33
+ def to_s
34
+ "server(#{@protocol_factory.to_s}(#{@transport_factory.to_s}(#{@server_transport.to_s})))"
35
+ end
30
36
  end
31
- end
37
+ end
@@ -20,6 +20,7 @@
20
20
  require 'mongrel'
21
21
 
22
22
  ## Sticks a service on a URL, using mongrel to do the HTTP work
23
+ # <b>DEPRECATED:</b> Please use <tt>Thrift::ThinHTTPServer</tt> instead.
23
24
  module Thrift
24
25
  class MongrelHTTPServer < BaseServer
25
26
  class Handler < Mongrel::HttpHandler
@@ -43,6 +44,7 @@ module Thrift
43
44
  end
44
45
 
45
46
  def initialize(processor, opts={})
47
+ Kernel.warn "[DEPRECATION WARNING] `Thrift::MongrelHTTPServer` is deprecated. Please use `Thrift::ThinHTTPServer` instead."
46
48
  port = opts[:port] || 80
47
49
  ip = opts[:ip] || "0.0.0.0"
48
50
  path = opts[:path] || ""
@@ -39,5 +39,9 @@ module Thrift
39
39
  @server_transport.close
40
40
  end
41
41
  end
42
+
43
+ def to_s
44
+ "simple(#{super.to_s})"
45
+ end
42
46
  end
43
- end
47
+ end
@@ -0,0 +1,91 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+ #
19
+
20
+ require 'rack'
21
+ require 'thin'
22
+
23
+ ##
24
+ # Wraps the Thin web server to provide a Thrift server over HTTP.
25
+ module Thrift
26
+ class ThinHTTPServer < BaseServer
27
+
28
+ ##
29
+ # Accepts a Thrift::Processor
30
+ # Options include:
31
+ # * :port
32
+ # * :ip
33
+ # * :path
34
+ # * :protocol_factory
35
+ def initialize(processor, options={})
36
+ port = options[:port] || 80
37
+ ip = options[:ip] || "0.0.0.0"
38
+ path = options[:path] || "/"
39
+ protocol_factory = options[:protocol_factory] || BinaryProtocolFactory.new
40
+ app = RackApplication.for(path, processor, protocol_factory)
41
+ @server = Thin::Server.new(ip, port, app)
42
+ end
43
+
44
+ ##
45
+ # Starts the server
46
+ def serve
47
+ @server.start
48
+ end
49
+
50
+ class RackApplication
51
+
52
+ THRIFT_HEADER = "application/x-thrift"
53
+
54
+ def self.for(path, processor, protocol_factory)
55
+ Rack::Builder.new do
56
+ use Rack::CommonLogger
57
+ use Rack::ShowExceptions
58
+ use Rack::Lint
59
+ map path do
60
+ run lambda { |env|
61
+ request = Rack::Request.new(env)
62
+ if RackApplication.valid_thrift_request?(request)
63
+ RackApplication.successful_request(request, processor, protocol_factory)
64
+ else
65
+ RackApplication.failed_request
66
+ end
67
+ }
68
+ end
69
+ end
70
+ end
71
+
72
+ def self.successful_request(rack_request, processor, protocol_factory)
73
+ response = Rack::Response.new([], 200, {'Content-Type' => THRIFT_HEADER})
74
+ transport = IOStreamTransport.new rack_request.body, response
75
+ protocol = protocol_factory.get_protocol transport
76
+ processor.process protocol, protocol
77
+ response
78
+ end
79
+
80
+ def self.failed_request
81
+ Rack::Response.new(['Not Found'], 404, {'Content-Type' => THRIFT_HEADER})
82
+ end
83
+
84
+ def self.valid_thrift_request?(rack_request)
85
+ rack_request.post? && rack_request.env["CONTENT_TYPE"] == THRIFT_HEADER
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+ end
@@ -71,5 +71,9 @@ module Thrift
71
71
  @server_transport.close
72
72
  end
73
73
  end
74
+
75
+ def to_s
76
+ "threadpool(#{super.to_s})"
77
+ end
74
78
  end
75
- end
79
+ end
@@ -43,5 +43,9 @@ module Thrift
43
43
  @server_transport.close
44
44
  end
45
45
  end
46
+
47
+ def to_s
48
+ "threaded(#{super.to_s})"
49
+ end
46
50
  end
47
- end
51
+ end
@@ -105,7 +105,7 @@ module Thrift
105
105
  write_container(oprot, value, field_info)
106
106
  oprot.write_field_end
107
107
  else
108
- oprot.write_field(name, type, fid, value)
108
+ oprot.write_field(field_info, fid, value)
109
109
  end
110
110
  end
111
111
  end
@@ -101,7 +101,7 @@ module Thrift
101
101
  end
102
102
  iprot.read_set_end
103
103
  else
104
- value = iprot.read_type(field[:type])
104
+ value = iprot.read_type(field)
105
105
  end
106
106
  value
107
107
  end
@@ -110,7 +110,7 @@ module Thrift
110
110
  if is_container? field[:type]
111
111
  write_container(oprot, value, field)
112
112
  else
113
- oprot.write_type(field[:type], value)
113
+ oprot.write_type(field, value)
114
114
  end
115
115
  end
116
116
 
@@ -34,4 +34,4 @@ module Thrift
34
34
  raise NotImplementedError
35
35
  end
36
36
  end
37
- end
37
+ end
@@ -35,22 +35,14 @@ module Thrift
35
35
  end
36
36
 
37
37
  module TransportUtils
38
- if RUBY_VERSION >= '1.9'
39
- def self.get_string_byte(string, index)
40
- string.getbyte(index)
41
- end
42
-
43
- def self.set_string_byte(string, index, byte)
44
- string.setbyte(index, byte)
45
- end
46
- else
47
- def self.get_string_byte(string, index)
48
- string[index]
49
- end
38
+ # Deprecated: Use Thrift::Bytes instead
39
+ def self.get_string_byte(string, index)
40
+ Bytes.get_string_byte(string, index)
41
+ end
50
42
 
51
- def self.set_string_byte(string, index, byte)
52
- string[index] = byte
53
- end
43
+ # Deprecated: Use Thrift::Bytes instead
44
+ def self.set_string_byte(string, index, byte)
45
+ Bytes.set_string_byte(string, index, byte)
54
46
  end
55
47
  end
56
48
 
@@ -61,6 +53,11 @@ module Thrift
61
53
 
62
54
  def close; end
63
55
 
56
+ # Reads a number of bytes from the transports. In Ruby 1.9+, the String returned will have a BINARY (aka ASCII8BIT) encoding.
57
+ #
58
+ # sz - The number of bytes to read from the transport.
59
+ #
60
+ # Returns a String acting as a byte buffer.
64
61
  def read(sz)
65
62
  raise NotImplementedError
66
63
  end
@@ -68,7 +65,7 @@ module Thrift
68
65
  # Returns an unsigned byte as a Fixnum in the range (0..255).
69
66
  def read_byte
70
67
  buf = read_all(1)
71
- return ::Thrift::TransportUtils.get_string_byte(buf, 0)
68
+ return Bytes.get_string_byte(buf, 0)
72
69
  end
73
70
 
74
71
  # Reads size bytes and copies them into buffer[0..size].
@@ -76,14 +73,14 @@ module Thrift
76
73
  tmp = read_all(size)
77
74
  i = 0
78
75
  tmp.each_byte do |byte|
79
- ::Thrift::TransportUtils.set_string_byte(buffer, i, byte)
76
+ Bytes.set_string_byte(buffer, i, byte)
80
77
  i += 1
81
78
  end
82
79
  i
83
80
  end
84
81
 
85
82
  def read_all(size)
86
- return '' if size <= 0
83
+ return Bytes.empty_byte_buffer if size <= 0
87
84
  buf = read(size)
88
85
  while (buf.length < size)
89
86
  chunk = read(size - buf.length)
@@ -92,16 +89,29 @@ module Thrift
92
89
 
93
90
  buf
94
91
  end
95
-
92
+
93
+ # Writes the byte buffer to the transport. In Ruby 1.9+, the buffer will be forced into BINARY encoding.
94
+ #
95
+ # buf - A String acting as a byte buffer.
96
+ #
97
+ # Returns nothing.
96
98
  def write(buf); end
97
99
  alias_method :<<, :write
98
100
 
99
101
  def flush; end
102
+
103
+ def to_s
104
+ "base"
105
+ end
100
106
  end
101
107
 
102
108
  class BaseTransportFactory
103
109
  def get_transport(trans)
104
110
  return trans
105
111
  end
112
+
113
+ def to_s
114
+ "base"
115
+ end
106
116
  end
107
- end
117
+ end