thrift-mavericks 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +15 -0
  2. data/CHANGELOG +1 -0
  3. data/README +43 -0
  4. data/benchmark/Benchmark.thrift +24 -0
  5. data/benchmark/benchmark.rb +271 -0
  6. data/benchmark/client.rb +74 -0
  7. data/benchmark/gen-rb/benchmark_constants.rb +11 -0
  8. data/benchmark/gen-rb/benchmark_service.rb +80 -0
  9. data/benchmark/gen-rb/benchmark_types.rb +10 -0
  10. data/benchmark/server.rb +82 -0
  11. data/benchmark/thin_server.rb +44 -0
  12. data/ext/binary_protocol_accelerated.c +441 -0
  13. data/ext/binary_protocol_accelerated.h +20 -0
  14. data/ext/compact_protocol.c +618 -0
  15. data/ext/compact_protocol.h +20 -0
  16. data/ext/constants.h +96 -0
  17. data/ext/extconf.rb +30 -0
  18. data/ext/macros.h +41 -0
  19. data/ext/memory_buffer.c +131 -0
  20. data/ext/memory_buffer.h +20 -0
  21. data/ext/protocol.c +185 -0
  22. data/ext/protocol.h +20 -0
  23. data/ext/strlcpy.c +41 -0
  24. data/ext/strlcpy.h +32 -0
  25. data/ext/struct.c +691 -0
  26. data/ext/struct.h +25 -0
  27. data/ext/thrift_native.c +196 -0
  28. data/lib/thrift.rb +64 -0
  29. data/lib/thrift/client.rb +62 -0
  30. data/lib/thrift/core_ext.rb +23 -0
  31. data/lib/thrift/core_ext/fixnum.rb +29 -0
  32. data/lib/thrift/exceptions.rb +84 -0
  33. data/lib/thrift/processor.rb +57 -0
  34. data/lib/thrift/protocol/base_protocol.rb +290 -0
  35. data/lib/thrift/protocol/binary_protocol.rb +229 -0
  36. data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
  37. data/lib/thrift/protocol/compact_protocol.rb +426 -0
  38. data/lib/thrift/serializer/deserializer.rb +33 -0
  39. data/lib/thrift/serializer/serializer.rb +34 -0
  40. data/lib/thrift/server/base_server.rb +31 -0
  41. data/lib/thrift/server/mongrel_http_server.rb +58 -0
  42. data/lib/thrift/server/nonblocking_server.rb +305 -0
  43. data/lib/thrift/server/simple_server.rb +43 -0
  44. data/lib/thrift/server/thread_pool_server.rb +75 -0
  45. data/lib/thrift/server/threaded_server.rb +47 -0
  46. data/lib/thrift/struct.rb +237 -0
  47. data/lib/thrift/struct_union.rb +192 -0
  48. data/lib/thrift/thrift_native.rb +24 -0
  49. data/lib/thrift/transport/base_server_transport.rb +37 -0
  50. data/lib/thrift/transport/base_transport.rb +107 -0
  51. data/lib/thrift/transport/buffered_transport.rb +108 -0
  52. data/lib/thrift/transport/framed_transport.rb +116 -0
  53. data/lib/thrift/transport/http_client_transport.rb +51 -0
  54. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  55. data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
  56. data/lib/thrift/transport/server_socket.rb +63 -0
  57. data/lib/thrift/transport/socket.rb +137 -0
  58. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  59. data/lib/thrift/transport/unix_socket.rb +40 -0
  60. data/lib/thrift/types.rb +101 -0
  61. data/lib/thrift/union.rb +179 -0
  62. data/spec/ThriftSpec.thrift +132 -0
  63. data/spec/base_protocol_spec.rb +160 -0
  64. data/spec/base_transport_spec.rb +351 -0
  65. data/spec/binary_protocol_accelerated_spec.rb +46 -0
  66. data/spec/binary_protocol_spec.rb +61 -0
  67. data/spec/binary_protocol_spec_shared.rb +375 -0
  68. data/spec/client_spec.rb +100 -0
  69. data/spec/compact_protocol_spec.rb +144 -0
  70. data/spec/exception_spec.rb +142 -0
  71. data/spec/gen-rb/nonblocking_service.rb +272 -0
  72. data/spec/gen-rb/thrift_spec_constants.rb +11 -0
  73. data/spec/gen-rb/thrift_spec_types.rb +346 -0
  74. data/spec/http_client_spec.rb +64 -0
  75. data/spec/mongrel_http_server_spec.rb +117 -0
  76. data/spec/nonblocking_server_spec.rb +265 -0
  77. data/spec/processor_spec.rb +83 -0
  78. data/spec/serializer_spec.rb +69 -0
  79. data/spec/server_socket_spec.rb +80 -0
  80. data/spec/server_spec.rb +159 -0
  81. data/spec/socket_spec.rb +61 -0
  82. data/spec/socket_spec_shared.rb +104 -0
  83. data/spec/spec_helper.rb +58 -0
  84. data/spec/struct_spec.rb +295 -0
  85. data/spec/types_spec.rb +116 -0
  86. data/spec/union_spec.rb +193 -0
  87. data/spec/unix_socket_spec.rb +108 -0
  88. data/test/debug_proto/gen-rb/debug_proto_test_constants.rb +274 -0
  89. data/test/debug_proto/gen-rb/debug_proto_test_types.rb +761 -0
  90. data/test/debug_proto/gen-rb/empty_service.rb +24 -0
  91. data/test/debug_proto/gen-rb/inherited.rb +79 -0
  92. data/test/debug_proto/gen-rb/reverse_order_service.rb +82 -0
  93. data/test/debug_proto/gen-rb/service_for_exception_with_a_map.rb +81 -0
  94. data/test/debug_proto/gen-rb/srv.rb +330 -0
  95. metadata +281 -0
@@ -0,0 +1,57 @@
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
+ module Thrift
21
+ module Processor
22
+ def initialize(handler)
23
+ @handler = handler
24
+ end
25
+
26
+ def process(iprot, oprot)
27
+ name, type, seqid = iprot.read_message_begin
28
+ if respond_to?("process_#{name}")
29
+ send("process_#{name}", seqid, iprot, oprot)
30
+ true
31
+ else
32
+ iprot.skip(Types::STRUCT)
33
+ iprot.read_message_end
34
+ x = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, 'Unknown function '+name)
35
+ oprot.write_message_begin(name, MessageTypes::EXCEPTION, seqid)
36
+ x.write(oprot)
37
+ oprot.write_message_end
38
+ oprot.trans.flush
39
+ false
40
+ end
41
+ end
42
+
43
+ def read_args(iprot, args_class)
44
+ args = args_class.new
45
+ args.read(iprot)
46
+ iprot.read_message_end
47
+ args
48
+ end
49
+
50
+ def write_result(result, oprot, name, seqid)
51
+ oprot.write_message_begin(name, MessageTypes::REPLY, seqid)
52
+ result.write(oprot)
53
+ oprot.write_message_end
54
+ oprot.trans.flush
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,290 @@
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
+ # this require is to make generated struct definitions happy
21
+ require 'set'
22
+
23
+ module Thrift
24
+ class ProtocolException < Exception
25
+
26
+ UNKNOWN = 0
27
+ INVALID_DATA = 1
28
+ NEGATIVE_SIZE = 2
29
+ SIZE_LIMIT = 3
30
+ BAD_VERSION = 4
31
+
32
+ attr_reader :type
33
+
34
+ def initialize(type=UNKNOWN, message=nil)
35
+ super(message)
36
+ @type = type
37
+ end
38
+ end
39
+
40
+ class BaseProtocol
41
+
42
+ attr_reader :trans
43
+
44
+ def initialize(trans)
45
+ @trans = trans
46
+ end
47
+
48
+ def native?
49
+ puts "wrong method is being called!"
50
+ false
51
+ end
52
+
53
+ def write_message_begin(name, type, seqid)
54
+ raise NotImplementedError
55
+ end
56
+
57
+ def write_message_end; nil; end
58
+
59
+ def write_struct_begin(name)
60
+ raise NotImplementedError
61
+ end
62
+
63
+ def write_struct_end; nil; end
64
+
65
+ def write_field_begin(name, type, id)
66
+ raise NotImplementedError
67
+ end
68
+
69
+ def write_field_end; nil; end
70
+
71
+ def write_field_stop
72
+ raise NotImplementedError
73
+ end
74
+
75
+ def write_map_begin(ktype, vtype, size)
76
+ raise NotImplementedError
77
+ end
78
+
79
+ def write_map_end; nil; end
80
+
81
+ def write_list_begin(etype, size)
82
+ raise NotImplementedError
83
+ end
84
+
85
+ def write_list_end; nil; end
86
+
87
+ def write_set_begin(etype, size)
88
+ raise NotImplementedError
89
+ end
90
+
91
+ def write_set_end; nil; end
92
+
93
+ def write_bool(bool)
94
+ raise NotImplementedError
95
+ end
96
+
97
+ def write_byte(byte)
98
+ raise NotImplementedError
99
+ end
100
+
101
+ def write_i16(i16)
102
+ raise NotImplementedError
103
+ end
104
+
105
+ def write_i32(i32)
106
+ raise NotImplementedError
107
+ end
108
+
109
+ def write_i64(i64)
110
+ raise NotImplementedError
111
+ end
112
+
113
+ def write_double(dub)
114
+ raise NotImplementedError
115
+ end
116
+
117
+ def write_string(str)
118
+ raise NotImplementedError
119
+ end
120
+
121
+ def read_message_begin
122
+ raise NotImplementedError
123
+ end
124
+
125
+ def read_message_end; nil; end
126
+
127
+ def read_struct_begin
128
+ raise NotImplementedError
129
+ end
130
+
131
+ def read_struct_end; nil; end
132
+
133
+ def read_field_begin
134
+ raise NotImplementedError
135
+ end
136
+
137
+ def read_field_end; nil; end
138
+
139
+ def read_map_begin
140
+ raise NotImplementedError
141
+ end
142
+
143
+ def read_map_end; nil; end
144
+
145
+ def read_list_begin
146
+ raise NotImplementedError
147
+ end
148
+
149
+ def read_list_end; nil; end
150
+
151
+ def read_set_begin
152
+ raise NotImplementedError
153
+ end
154
+
155
+ def read_set_end; nil; end
156
+
157
+ def read_bool
158
+ raise NotImplementedError
159
+ end
160
+
161
+ def read_byte
162
+ raise NotImplementedError
163
+ end
164
+
165
+ def read_i16
166
+ raise NotImplementedError
167
+ end
168
+
169
+ def read_i32
170
+ raise NotImplementedError
171
+ end
172
+
173
+ def read_i64
174
+ raise NotImplementedError
175
+ end
176
+
177
+ def read_double
178
+ raise NotImplementedError
179
+ end
180
+
181
+ def read_string
182
+ raise NotImplementedError
183
+ end
184
+
185
+ def write_field(name, type, fid, value)
186
+ write_field_begin(name, type, fid)
187
+ write_type(type, value)
188
+ write_field_end
189
+ end
190
+
191
+ def write_type(type, value)
192
+ case type
193
+ when Types::BOOL
194
+ write_bool(value)
195
+ when Types::BYTE
196
+ write_byte(value)
197
+ when Types::DOUBLE
198
+ write_double(value)
199
+ when Types::I16
200
+ write_i16(value)
201
+ when Types::I32
202
+ write_i32(value)
203
+ when Types::I64
204
+ write_i64(value)
205
+ when Types::STRING
206
+ write_string(value)
207
+ when Types::STRUCT
208
+ value.write(self)
209
+ else
210
+ raise NotImplementedError
211
+ end
212
+ end
213
+
214
+ def read_type(type)
215
+ case type
216
+ when Types::BOOL
217
+ read_bool
218
+ when Types::BYTE
219
+ read_byte
220
+ when Types::DOUBLE
221
+ read_double
222
+ when Types::I16
223
+ read_i16
224
+ when Types::I32
225
+ read_i32
226
+ when Types::I64
227
+ read_i64
228
+ when Types::STRING
229
+ read_string
230
+ else
231
+ raise NotImplementedError
232
+ end
233
+ end
234
+
235
+ def skip(type)
236
+ case type
237
+ when Types::STOP
238
+ nil
239
+ when Types::BOOL
240
+ read_bool
241
+ when Types::BYTE
242
+ read_byte
243
+ when Types::I16
244
+ read_i16
245
+ when Types::I32
246
+ read_i32
247
+ when Types::I64
248
+ read_i64
249
+ when Types::DOUBLE
250
+ read_double
251
+ when Types::STRING
252
+ read_string
253
+ when Types::STRUCT
254
+ read_struct_begin
255
+ while true
256
+ name, type, id = read_field_begin
257
+ break if type == Types::STOP
258
+ skip(type)
259
+ read_field_end
260
+ end
261
+ read_struct_end
262
+ when Types::MAP
263
+ ktype, vtype, size = read_map_begin
264
+ size.times do
265
+ skip(ktype)
266
+ skip(vtype)
267
+ end
268
+ read_map_end
269
+ when Types::SET
270
+ etype, size = read_set_begin
271
+ size.times do
272
+ skip(etype)
273
+ end
274
+ read_set_end
275
+ when Types::LIST
276
+ etype, size = read_list_begin
277
+ size.times do
278
+ skip(etype)
279
+ end
280
+ read_list_end
281
+ end
282
+ end
283
+ end
284
+
285
+ class BaseProtocolFactory
286
+ def get_protocol(trans)
287
+ raise NotImplementedError
288
+ end
289
+ end
290
+ end
@@ -0,0 +1,229 @@
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
+ module Thrift
21
+ class BinaryProtocol < BaseProtocol
22
+ VERSION_MASK = 0xffff0000
23
+ VERSION_1 = 0x80010000
24
+ TYPE_MASK = 0x000000ff
25
+
26
+ attr_reader :strict_read, :strict_write
27
+
28
+ def initialize(trans, strict_read=true, strict_write=true)
29
+ super(trans)
30
+ @strict_read = strict_read
31
+ @strict_write = strict_write
32
+
33
+ # Pre-allocated read buffer for fixed-size read methods. Needs to be at least 8 bytes long for
34
+ # read_i64() and read_double().
35
+ @rbuf = "\0" * 8
36
+ @rbuf.force_encoding("BINARY") if @rbuf.respond_to?(:force_encoding)
37
+ end
38
+
39
+ def write_message_begin(name, type, seqid)
40
+ # this is necessary because we added (needed) bounds checking to
41
+ # write_i32, and 0x80010000 is too big for that.
42
+ if strict_write
43
+ write_i16(VERSION_1 >> 16)
44
+ write_i16(type)
45
+ write_string(name)
46
+ write_i32(seqid)
47
+ else
48
+ write_string(name)
49
+ write_byte(type)
50
+ write_i32(seqid)
51
+ end
52
+ end
53
+
54
+ def write_struct_begin(name); nil; end
55
+
56
+ def write_field_begin(name, type, id)
57
+ write_byte(type)
58
+ write_i16(id)
59
+ end
60
+
61
+ def write_field_stop
62
+ write_byte(Thrift::Types::STOP)
63
+ end
64
+
65
+ def write_map_begin(ktype, vtype, size)
66
+ write_byte(ktype)
67
+ write_byte(vtype)
68
+ write_i32(size)
69
+ end
70
+
71
+ def write_list_begin(etype, size)
72
+ write_byte(etype)
73
+ write_i32(size)
74
+ end
75
+
76
+ def write_set_begin(etype, size)
77
+ write_byte(etype)
78
+ write_i32(size)
79
+ end
80
+
81
+ def write_bool(bool)
82
+ write_byte(bool ? 1 : 0)
83
+ end
84
+
85
+ def write_byte(byte)
86
+ raise RangeError if byte < -2**31 || byte >= 2**32
87
+ trans.write([byte].pack('c'))
88
+ end
89
+
90
+ def write_i16(i16)
91
+ trans.write([i16].pack('n'))
92
+ end
93
+
94
+ def write_i32(i32)
95
+ raise RangeError if i32 < -2**31 || i32 >= 2**31
96
+ trans.write([i32].pack('N'))
97
+ end
98
+
99
+ def write_i64(i64)
100
+ raise RangeError if i64 < -2**63 || i64 >= 2**64
101
+ hi = i64 >> 32
102
+ lo = i64 & 0xffffffff
103
+ trans.write([hi, lo].pack('N2'))
104
+ end
105
+
106
+ def write_double(dub)
107
+ trans.write([dub].pack('G'))
108
+ end
109
+
110
+ def write_string(str)
111
+ write_i32(str.length)
112
+ trans.write(str)
113
+ end
114
+
115
+ def read_message_begin
116
+ version = read_i32
117
+ if version < 0
118
+ if (version & VERSION_MASK != VERSION_1)
119
+ raise ProtocolException.new(ProtocolException::BAD_VERSION, 'Missing version identifier')
120
+ end
121
+ type = version & TYPE_MASK
122
+ name = read_string
123
+ seqid = read_i32
124
+ [name, type, seqid]
125
+ else
126
+ if strict_read
127
+ raise ProtocolException.new(ProtocolException::BAD_VERSION, 'No version identifier, old protocol client?')
128
+ end
129
+ name = trans.read_all(version)
130
+ type = read_byte
131
+ seqid = read_i32
132
+ [name, type, seqid]
133
+ end
134
+ end
135
+
136
+ def read_struct_begin; nil; end
137
+
138
+ def read_field_begin
139
+ type = read_byte
140
+ if (type == Types::STOP)
141
+ [nil, type, 0]
142
+ else
143
+ id = read_i16
144
+ [nil, type, id]
145
+ end
146
+ end
147
+
148
+ def read_map_begin
149
+ ktype = read_byte
150
+ vtype = read_byte
151
+ size = read_i32
152
+ [ktype, vtype, size]
153
+ end
154
+
155
+ def read_list_begin
156
+ etype = read_byte
157
+ size = read_i32
158
+ [etype, size]
159
+ end
160
+
161
+ def read_set_begin
162
+ etype = read_byte
163
+ size = read_i32
164
+ [etype, size]
165
+ end
166
+
167
+ def read_bool
168
+ byte = read_byte
169
+ byte != 0
170
+ end
171
+
172
+ def read_byte
173
+ val = trans.read_byte
174
+ if (val > 0x7f)
175
+ val = 0 - ((val - 1) ^ 0xff)
176
+ end
177
+ val
178
+ end
179
+
180
+ def read_i16
181
+ trans.read_into_buffer(@rbuf, 2)
182
+ val, = @rbuf.unpack('n')
183
+ if (val > 0x7fff)
184
+ val = 0 - ((val - 1) ^ 0xffff)
185
+ end
186
+ val
187
+ end
188
+
189
+ def read_i32
190
+ trans.read_into_buffer(@rbuf, 4)
191
+ val, = @rbuf.unpack('N')
192
+ if (val > 0x7fffffff)
193
+ val = 0 - ((val - 1) ^ 0xffffffff)
194
+ end
195
+ val
196
+ end
197
+
198
+ def read_i64
199
+ trans.read_into_buffer(@rbuf, 8)
200
+ hi, lo = @rbuf.unpack('N2')
201
+ if (hi > 0x7fffffff)
202
+ hi ^= 0xffffffff
203
+ lo ^= 0xffffffff
204
+ 0 - (hi << 32) - lo - 1
205
+ else
206
+ (hi << 32) + lo
207
+ end
208
+ end
209
+
210
+ def read_double
211
+ trans.read_into_buffer(@rbuf, 8)
212
+ val = @rbuf.unpack('G').first
213
+ val
214
+ end
215
+
216
+ def read_string
217
+ sz = read_i32
218
+ dat = trans.read_all(sz)
219
+ dat
220
+ end
221
+
222
+ end
223
+
224
+ class BinaryProtocolFactory < BaseProtocolFactory
225
+ def get_protocol(trans)
226
+ return Thrift::BinaryProtocol.new(trans)
227
+ end
228
+ end
229
+ end