tiny_thrift 1.0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. checksums.yaml +7 -0
  2. data/README +43 -0
  3. data/benchmark/Benchmark.thrift +24 -0
  4. data/benchmark/benchmark.rb +271 -0
  5. data/benchmark/client.rb +74 -0
  6. data/benchmark/server.rb +82 -0
  7. data/benchmark/thin_server.rb +44 -0
  8. data/ext/binary_protocol_accelerated.c +460 -0
  9. data/ext/binary_protocol_accelerated.h +20 -0
  10. data/ext/bytes.c +36 -0
  11. data/ext/bytes.h +31 -0
  12. data/ext/compact_protocol.c +635 -0
  13. data/ext/compact_protocol.h +20 -0
  14. data/ext/constants.h +96 -0
  15. data/ext/extconf.rb +32 -0
  16. data/ext/macros.h +41 -0
  17. data/ext/memory_buffer.c +134 -0
  18. data/ext/memory_buffer.h +20 -0
  19. data/ext/protocol.c +0 -0
  20. data/ext/protocol.h +0 -0
  21. data/ext/strlcpy.c +41 -0
  22. data/ext/strlcpy.h +34 -0
  23. data/ext/struct.c +688 -0
  24. data/ext/struct.h +25 -0
  25. data/ext/thrift_native.c +195 -0
  26. data/lib/thrift.rb +66 -0
  27. data/lib/thrift/bytes.rb +131 -0
  28. data/lib/thrift/client.rb +62 -0
  29. data/lib/thrift/core_ext.rb +23 -0
  30. data/lib/thrift/core_ext/fixnum.rb +29 -0
  31. data/lib/thrift/exceptions.rb +87 -0
  32. data/lib/thrift/processor.rb +57 -0
  33. data/lib/thrift/protocol/base_protocol.rb +377 -0
  34. data/lib/thrift/protocol/binary_protocol.rb +237 -0
  35. data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
  36. data/lib/thrift/protocol/compact_protocol.rb +434 -0
  37. data/lib/thrift/protocol/json_protocol.rb +769 -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 +60 -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/thin_http_server.rb +91 -0
  45. data/lib/thrift/server/thread_pool_server.rb +75 -0
  46. data/lib/thrift/server/threaded_server.rb +47 -0
  47. data/lib/thrift/struct.rb +237 -0
  48. data/lib/thrift/struct_union.rb +192 -0
  49. data/lib/thrift/thrift_native.rb +24 -0
  50. data/lib/thrift/transport/base_server_transport.rb +37 -0
  51. data/lib/thrift/transport/base_transport.rb +109 -0
  52. data/lib/thrift/transport/buffered_transport.rb +114 -0
  53. data/lib/thrift/transport/framed_transport.rb +117 -0
  54. data/lib/thrift/transport/http_client_transport.rb +56 -0
  55. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  56. data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
  57. data/lib/thrift/transport/server_socket.rb +63 -0
  58. data/lib/thrift/transport/socket.rb +139 -0
  59. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  60. data/lib/thrift/transport/unix_socket.rb +40 -0
  61. data/lib/thrift/types.rb +101 -0
  62. data/lib/thrift/union.rb +179 -0
  63. data/lib/tiny_thrift.rb +1 -0
  64. data/spec/ThriftSpec.thrift +183 -0
  65. data/spec/base_protocol_spec.rb +217 -0
  66. data/spec/base_transport_spec.rb +350 -0
  67. data/spec/binary_protocol_accelerated_spec.rb +42 -0
  68. data/spec/binary_protocol_spec.rb +66 -0
  69. data/spec/binary_protocol_spec_shared.rb +455 -0
  70. data/spec/bytes_spec.rb +160 -0
  71. data/spec/client_spec.rb +99 -0
  72. data/spec/compact_protocol_spec.rb +143 -0
  73. data/spec/exception_spec.rb +141 -0
  74. data/spec/http_client_spec.rb +120 -0
  75. data/spec/json_protocol_spec.rb +513 -0
  76. data/spec/nonblocking_server_spec.rb +263 -0
  77. data/spec/processor_spec.rb +80 -0
  78. data/spec/serializer_spec.rb +67 -0
  79. data/spec/server_socket_spec.rb +79 -0
  80. data/spec/server_spec.rb +147 -0
  81. data/spec/socket_spec.rb +61 -0
  82. data/spec/socket_spec_shared.rb +104 -0
  83. data/spec/spec_helper.rb +61 -0
  84. data/spec/struct_nested_containers_spec.rb +191 -0
  85. data/spec/struct_spec.rb +293 -0
  86. data/spec/thin_http_server_spec.rb +141 -0
  87. data/spec/types_spec.rb +115 -0
  88. data/spec/union_spec.rb +203 -0
  89. data/spec/unix_socket_spec.rb +107 -0
  90. metadata +313 -0
@@ -0,0 +1,23 @@
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
+ Dir[File.dirname(__FILE__) + "/core_ext/*.rb"].each do |file|
21
+ name = File.basename(file, '.rb')
22
+ require "thrift/core_ext/#{name}"
23
+ end
@@ -0,0 +1,29 @@
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
+ # Versions of ruby pre 1.8.7 do not have an .ord method available in the Fixnum
21
+ # class.
22
+ #
23
+ if RUBY_VERSION < "1.8.7"
24
+ class Fixnum
25
+ def ord
26
+ self
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,87 @@
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 Exception < StandardError
22
+ def initialize(message)
23
+ super
24
+ @message = message
25
+ end
26
+
27
+ attr_reader :message
28
+ end
29
+
30
+ class ApplicationException < Exception
31
+
32
+ UNKNOWN = 0
33
+ UNKNOWN_METHOD = 1
34
+ INVALID_MESSAGE_TYPE = 2
35
+ WRONG_METHOD_NAME = 3
36
+ BAD_SEQUENCE_ID = 4
37
+ MISSING_RESULT = 5
38
+ INTERNAL_ERROR = 6
39
+ PROTOCOL_ERROR = 7
40
+ INVALID_TRANSFORM = 8
41
+ INVALID_PROTOCOL = 9
42
+ UNSUPPORTED_CLIENT_TYPE = 10
43
+
44
+ attr_reader :type
45
+
46
+ def initialize(type=UNKNOWN, message=nil)
47
+ super(message)
48
+ @type = type
49
+ end
50
+
51
+ def read(iprot)
52
+ iprot.read_struct_begin
53
+ while true
54
+ fname, ftype, fid = iprot.read_field_begin
55
+ if ftype == Types::STOP
56
+ break
57
+ end
58
+ if fid == 1 and ftype == Types::STRING
59
+ @message = iprot.read_string
60
+ elsif fid == 2 and ftype == Types::I32
61
+ @type = iprot.read_i32
62
+ else
63
+ iprot.skip(ftype)
64
+ end
65
+ iprot.read_field_end
66
+ end
67
+ iprot.read_struct_end
68
+ end
69
+
70
+ def write(oprot)
71
+ oprot.write_struct_begin('Thrift::ApplicationException')
72
+ unless @message.nil?
73
+ oprot.write_field_begin('message', Types::STRING, 1)
74
+ oprot.write_string(@message)
75
+ oprot.write_field_end
76
+ end
77
+ unless @type.nil?
78
+ oprot.write_field_begin('type', Types::I32, 2)
79
+ oprot.write_i32(@type)
80
+ oprot.write_field_end
81
+ end
82
+ oprot.write_field_stop
83
+ oprot.write_struct_end
84
+ end
85
+
86
+ end
87
+ end
@@ -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,377 @@
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
+ # Writes a Thrift String. In Ruby 1.9+, the String passed will be transcoded to UTF-8.
118
+ #
119
+ # str - The String to write.
120
+ #
121
+ # Raises EncodingError if the transcoding to UTF-8 fails.
122
+ #
123
+ # Returns nothing.
124
+ def write_string(str)
125
+ raise NotImplementedError
126
+ end
127
+
128
+ # Writes a Thrift Binary (Thrift String with no encoding). In Ruby 1.9+, the String passed
129
+ # will forced into BINARY encoding.
130
+ #
131
+ # buf - The String to write.
132
+ #
133
+ # Returns nothing.
134
+ def write_binary(buf)
135
+ raise NotImplementedError
136
+ end
137
+
138
+ def read_message_begin
139
+ raise NotImplementedError
140
+ end
141
+
142
+ def read_message_end; nil; end
143
+
144
+ def read_struct_begin
145
+ raise NotImplementedError
146
+ end
147
+
148
+ def read_struct_end; nil; end
149
+
150
+ def read_field_begin
151
+ raise NotImplementedError
152
+ end
153
+
154
+ def read_field_end; nil; end
155
+
156
+ def read_map_begin
157
+ raise NotImplementedError
158
+ end
159
+
160
+ def read_map_end; nil; end
161
+
162
+ def read_list_begin
163
+ raise NotImplementedError
164
+ end
165
+
166
+ def read_list_end; nil; end
167
+
168
+ def read_set_begin
169
+ raise NotImplementedError
170
+ end
171
+
172
+ def read_set_end; nil; end
173
+
174
+ def read_bool
175
+ raise NotImplementedError
176
+ end
177
+
178
+ def read_byte
179
+ raise NotImplementedError
180
+ end
181
+
182
+ def read_i16
183
+ raise NotImplementedError
184
+ end
185
+
186
+ def read_i32
187
+ raise NotImplementedError
188
+ end
189
+
190
+ def read_i64
191
+ raise NotImplementedError
192
+ end
193
+
194
+ def read_double
195
+ raise NotImplementedError
196
+ end
197
+
198
+ # Reads a Thrift String. In Ruby 1.9+, all Strings will be returned with an Encoding of UTF-8.
199
+ #
200
+ # Returns a String.
201
+ def read_string
202
+ raise NotImplementedError
203
+ end
204
+
205
+ # Reads a Thrift Binary (Thrift String without encoding). In Ruby 1.9+, all Strings will be returned
206
+ # with an Encoding of BINARY.
207
+ #
208
+ # Returns a String.
209
+ def read_binary
210
+ raise NotImplementedError
211
+ end
212
+
213
+ # Writes a field based on the field information, field ID and value.
214
+ #
215
+ # field_info - A Hash containing the definition of the field:
216
+ # :name - The name of the field.
217
+ # :type - The type of the field, which must be a Thrift::Types constant.
218
+ # :binary - A Boolean flag that indicates if Thrift::Types::STRING is a binary string (string without encoding).
219
+ # fid - The ID of the field.
220
+ # value - The field's value to write; object type varies based on :type.
221
+ #
222
+ # Returns nothing.
223
+ def write_field(*args)
224
+ if args.size == 3
225
+ # handles the documented method signature - write_field(field_info, fid, value)
226
+ field_info = args[0]
227
+ fid = args[1]
228
+ value = args[2]
229
+ elsif args.size == 4
230
+ # handles the deprecated method signature - write_field(name, type, fid, value)
231
+ field_info = {:name => args[0], :type => args[1]}
232
+ fid = args[2]
233
+ value = args[3]
234
+ else
235
+ raise ArgumentError, "wrong number of arguments (#{args.size} for 3)"
236
+ end
237
+
238
+ write_field_begin(field_info[:name], field_info[:type], fid)
239
+ write_type(field_info, value)
240
+ write_field_end
241
+ end
242
+
243
+ # Writes a field value based on the field information.
244
+ #
245
+ # field_info - A Hash containing the definition of the field:
246
+ # :type - The Thrift::Types constant that determines how the value is written.
247
+ # :binary - A Boolean flag that indicates if Thrift::Types::STRING is a binary string (string without encoding).
248
+ # value - The field's value to write; object type varies based on field_info[:type].
249
+ #
250
+ # Returns nothing.
251
+ def write_type(field_info, value)
252
+ # if field_info is a Fixnum, assume it is a Thrift::Types constant
253
+ # convert it into a field_info Hash for backwards compatibility
254
+ if field_info.is_a? Fixnum
255
+ field_info = {:type => field_info}
256
+ end
257
+
258
+ case field_info[:type]
259
+ when Types::BOOL
260
+ write_bool(value)
261
+ when Types::BYTE
262
+ write_byte(value)
263
+ when Types::DOUBLE
264
+ write_double(value)
265
+ when Types::I16
266
+ write_i16(value)
267
+ when Types::I32
268
+ write_i32(value)
269
+ when Types::I64
270
+ write_i64(value)
271
+ when Types::STRING
272
+ if field_info[:binary]
273
+ write_binary(value)
274
+ else
275
+ write_string(value)
276
+ end
277
+ when Types::STRUCT
278
+ value.write(self)
279
+ else
280
+ raise NotImplementedError
281
+ end
282
+ end
283
+
284
+ # Reads a field value based on the field information.
285
+ #
286
+ # field_info - A Hash containing the pertinent data to write:
287
+ # :type - The Thrift::Types constant that determines how the value is written.
288
+ # :binary - A flag that indicates if Thrift::Types::STRING is a binary string (string without encoding).
289
+ #
290
+ # Returns the value read; object type varies based on field_info[:type].
291
+ def read_type(field_info)
292
+ # if field_info is a Fixnum, assume it is a Thrift::Types constant
293
+ # convert it into a field_info Hash for backwards compatibility
294
+ if field_info.is_a? Fixnum
295
+ field_info = {:type => field_info}
296
+ end
297
+
298
+ case field_info[:type]
299
+ when Types::BOOL
300
+ read_bool
301
+ when Types::BYTE
302
+ read_byte
303
+ when Types::DOUBLE
304
+ read_double
305
+ when Types::I16
306
+ read_i16
307
+ when Types::I32
308
+ read_i32
309
+ when Types::I64
310
+ read_i64
311
+ when Types::STRING
312
+ if field_info[:binary]
313
+ read_binary
314
+ else
315
+ read_string
316
+ end
317
+ else
318
+ raise NotImplementedError
319
+ end
320
+ end
321
+
322
+ def skip(type)
323
+ case type
324
+ when Types::STOP
325
+ nil
326
+ when Types::BOOL
327
+ read_bool
328
+ when Types::BYTE
329
+ read_byte
330
+ when Types::I16
331
+ read_i16
332
+ when Types::I32
333
+ read_i32
334
+ when Types::I64
335
+ read_i64
336
+ when Types::DOUBLE
337
+ read_double
338
+ when Types::STRING
339
+ read_string
340
+ when Types::STRUCT
341
+ read_struct_begin
342
+ while true
343
+ name, type, id = read_field_begin
344
+ break if type == Types::STOP
345
+ skip(type)
346
+ read_field_end
347
+ end
348
+ read_struct_end
349
+ when Types::MAP
350
+ ktype, vtype, size = read_map_begin
351
+ size.times do
352
+ skip(ktype)
353
+ skip(vtype)
354
+ end
355
+ read_map_end
356
+ when Types::SET
357
+ etype, size = read_set_begin
358
+ size.times do
359
+ skip(etype)
360
+ end
361
+ read_set_end
362
+ when Types::LIST
363
+ etype, size = read_list_begin
364
+ size.times do
365
+ skip(etype)
366
+ end
367
+ read_list_end
368
+ end
369
+ end
370
+ end
371
+
372
+ class BaseProtocolFactory
373
+ def get_protocol(trans)
374
+ raise NotImplementedError
375
+ end
376
+ end
377
+ end