thrift 0.0.751142 → 0.0.810255.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/CHANGELOG +3 -1
  2. data/Manifest +47 -44
  3. data/README +19 -6
  4. data/Rakefile +1 -1
  5. data/benchmark/Benchmark.thrift +19 -0
  6. data/benchmark/benchmark.rb +20 -3
  7. data/benchmark/client.rb +20 -2
  8. data/benchmark/server.rb +22 -4
  9. data/benchmark/thin_server.rb +22 -4
  10. data/ext/binary_protocol_accelerated.c +32 -21
  11. data/ext/binary_protocol_accelerated.h +19 -0
  12. data/ext/compact_protocol.c +665 -0
  13. data/ext/compact_protocol.h +20 -0
  14. data/ext/constants.h +19 -1
  15. data/ext/extconf.rb +19 -0
  16. data/ext/macros.h +41 -0
  17. data/ext/memory_buffer.c +29 -5
  18. data/ext/memory_buffer.h +19 -0
  19. data/ext/protocol.c +20 -1
  20. data/ext/protocol.h +19 -0
  21. data/ext/struct.c +115 -83
  22. data/ext/struct.h +19 -0
  23. data/ext/thrift_native.c +23 -2
  24. data/lib/thrift/client.rb +19 -1
  25. data/lib/thrift/core_ext/fixnum.rb +29 -0
  26. data/lib/thrift/core_ext.rb +23 -0
  27. data/lib/thrift/exceptions.rb +19 -2
  28. data/lib/thrift/processor.rb +19 -1
  29. data/lib/thrift/protocol/base_protocol.rb +290 -0
  30. data/lib/thrift/protocol/{binaryprotocol.rb → binary_protocol.rb} +27 -15
  31. data/lib/thrift/protocol/binary_protocol_accelerated.rb +35 -0
  32. data/lib/thrift/protocol/compact_protocol.rb +422 -0
  33. data/lib/thrift/serializer/deserializer.rb +33 -0
  34. data/lib/thrift/serializer/serializer.rb +34 -0
  35. data/lib/thrift/server/base_server.rb +31 -0
  36. data/lib/thrift/server/{httpserver.rb → mongrel_http_server.rb} +19 -5
  37. data/lib/thrift/server/{nonblockingserver.rb → nonblocking_server.rb} +42 -24
  38. data/lib/thrift/server/simple_server.rb +43 -0
  39. data/lib/thrift/server/thread_pool_server.rb +75 -0
  40. data/lib/thrift/server/threaded_server.rb +47 -0
  41. data/lib/thrift/struct.rb +73 -47
  42. data/lib/thrift/thrift_native.rb +24 -0
  43. data/lib/thrift/transport/base_server_transport.rb +37 -0
  44. data/lib/thrift/transport/base_transport.rb +70 -0
  45. data/lib/thrift/transport/buffered_transport.rb +77 -0
  46. data/lib/thrift/transport/framed_transport.rb +90 -0
  47. data/lib/thrift/transport/http_client_transport.rb +45 -0
  48. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  49. data/lib/thrift/transport/memory_buffer_transport.rb +96 -0
  50. data/lib/thrift/transport/server_socket.rb +63 -0
  51. data/lib/thrift/transport/socket.rb +22 -53
  52. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  53. data/lib/thrift/transport/unix_socket.rb +40 -0
  54. data/lib/thrift/types.rb +20 -2
  55. data/lib/thrift.rb +50 -19
  56. data/script/proto_benchmark.rb +121 -0
  57. data/script/read_struct.rb +43 -0
  58. data/script/write_struct.rb +30 -0
  59. data/spec/ThriftSpec.thrift +40 -2
  60. data/spec/{protocol_spec.rb → base_protocol_spec.rb} +25 -7
  61. data/spec/{transport_spec.rb → base_transport_spec.rb} +44 -52
  62. data/spec/binary_protocol_accelerated_spec.rb +41 -0
  63. data/spec/{binaryprotocol_spec.rb → binary_protocol_spec.rb} +20 -2
  64. data/spec/{binaryprotocol_spec_shared.rb → binary_protocol_spec_shared.rb} +117 -16
  65. data/spec/client_spec.rb +23 -4
  66. data/spec/compact_protocol_spec.rb +117 -0
  67. data/spec/exception_spec.rb +19 -0
  68. data/spec/http_client_spec.rb +49 -0
  69. data/spec/{httpserver_spec.rb → mongrel_http_server_spec.rb} +26 -7
  70. data/spec/{nonblockingserver_spec.rb → nonblocking_server_spec.rb} +25 -5
  71. data/spec/processor_spec.rb +19 -0
  72. data/spec/serializer_spec.rb +27 -10
  73. data/spec/server_socket_spec.rb +80 -0
  74. data/spec/server_spec.rb +35 -16
  75. data/spec/socket_spec.rb +19 -55
  76. data/spec/socket_spec_shared.rb +19 -0
  77. data/spec/spec_helper.rb +34 -9
  78. data/spec/struct_spec.rb +26 -18
  79. data/spec/types_spec.rb +19 -1
  80. data/spec/{unixsocket_spec.rb → unix_socket_spec.rb} +19 -1
  81. data/thrift.gemspec +6 -6
  82. data.tar.gz.sig +0 -0
  83. metadata +80 -70
  84. metadata.gz.sig +0 -0
  85. data/COPYING +0 -14
  86. data/LICENSE +0 -14
  87. data/Makefile.am +0 -15
  88. data/benchmark/gen-rb/BenchmarkService.rb +0 -81
  89. data/benchmark/gen-rb/Benchmark_constants.rb +0 -11
  90. data/benchmark/gen-rb/Benchmark_types.rb +0 -10
  91. data/lib/thrift/deprecation.rb +0 -155
  92. data/lib/thrift/protocol/binaryprotocolaccelerated.rb +0 -19
  93. data/lib/thrift/protocol/tbinaryprotocol.rb +0 -2
  94. data/lib/thrift/protocol/tprotocol.rb +0 -2
  95. data/lib/thrift/protocol.rb +0 -270
  96. data/lib/thrift/serializer.rb +0 -27
  97. data/lib/thrift/server/thttpserver.rb +0 -2
  98. data/lib/thrift/server/tserver.rb +0 -2
  99. data/lib/thrift/server.rb +0 -135
  100. data/lib/thrift/thrift.rb +0 -14
  101. data/lib/thrift/transport/httpclient.rb +0 -29
  102. data/lib/thrift/transport/thttpclient.rb +0 -2
  103. data/lib/thrift/transport/tsocket.rb +0 -2
  104. data/lib/thrift/transport/ttransport.rb +0 -2
  105. data/lib/thrift/transport/unixsocket.rb +0 -58
  106. data/lib/thrift/transport.rb +0 -319
  107. data/spec/backwards_compatibility_spec.rb +0 -136
  108. data/spec/binaryprotocolaccelerated_spec.rb +0 -101
  109. data/spec/deprecation_spec.rb +0 -443
  110. data/spec/gen-rb/NonblockingService.rb +0 -268
  111. data/spec/gen-rb/ThriftSpec_constants.rb +0 -11
  112. data/spec/gen-rb/ThriftSpec_types.rb +0 -134
  113. data/spec/httpclient_spec.rb +0 -31
@@ -0,0 +1,422 @@
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 CompactProtocol < BaseProtocol
22
+
23
+ PROTOCOL_ID = [0x82].pack('c').unpack('c').first
24
+ VERSION = 1
25
+ VERSION_MASK = 0x1f
26
+ TYPE_MASK = 0xE0
27
+ TYPE_SHIFT_AMOUNT = 5
28
+
29
+ TSTOP = ["", Types::STOP, 0]
30
+
31
+ #
32
+ # All of the on-wire type codes.
33
+ #
34
+ class CompactTypes
35
+ BOOLEAN_TRUE = 0x01
36
+ BOOLEAN_FALSE = 0x02
37
+ BYTE = 0x03
38
+ I16 = 0x04
39
+ I32 = 0x05
40
+ I64 = 0x06
41
+ DOUBLE = 0x07
42
+ BINARY = 0x08
43
+ LIST = 0x09
44
+ SET = 0x0A
45
+ MAP = 0x0B
46
+ STRUCT = 0x0C
47
+
48
+ def self.is_bool_type?(b)
49
+ (b & 0x0f) == BOOLEAN_TRUE || (b & 0x0f) == BOOLEAN_FALSE
50
+ end
51
+
52
+ COMPACT_TO_TTYPE = {
53
+ Types::STOP => Types::STOP,
54
+ BOOLEAN_FALSE => Types::BOOL,
55
+ BOOLEAN_TRUE => Types::BOOL,
56
+ BYTE => Types::BYTE,
57
+ I16 => Types::I16,
58
+ I32 => Types::I32,
59
+ I64 => Types::I64,
60
+ DOUBLE => Types::DOUBLE,
61
+ BINARY => Types::STRING,
62
+ LIST => Types::LIST,
63
+ SET => Types::SET,
64
+ MAP => Types::MAP,
65
+ STRUCT => Types::STRUCT
66
+ }
67
+
68
+ TTYPE_TO_COMPACT = {
69
+ Types::STOP => Types::STOP,
70
+ Types::BOOL => BOOLEAN_TRUE,
71
+ Types::BYTE => BYTE,
72
+ Types::I16 => I16,
73
+ Types::I32 => I32,
74
+ Types::I64 => I64,
75
+ Types::DOUBLE => DOUBLE,
76
+ Types::STRING => BINARY,
77
+ Types::LIST => LIST,
78
+ Types::SET => SET,
79
+ Types::MAP => MAP,
80
+ Types::STRUCT => STRUCT
81
+ }
82
+
83
+ def self.get_ttype(compact_type)
84
+ val = COMPACT_TO_TTYPE[compact_type & 0x0f]
85
+ raise "don't know what type: #{compact_type & 0x0f}" unless val
86
+ val
87
+ end
88
+
89
+ def self.get_compact_type(ttype)
90
+ val = TTYPE_TO_COMPACT[ttype]
91
+ raise "don't know what type: #{ttype & 0x0f}" unless val
92
+ val
93
+ end
94
+ end
95
+
96
+ def initialize(transport)
97
+ super(transport)
98
+
99
+ @last_field = [0]
100
+ @boolean_value = nil
101
+ end
102
+
103
+ def write_message_begin(name, type, seqid)
104
+ write_byte(PROTOCOL_ID)
105
+ write_byte((VERSION & VERSION_MASK) | ((type << TYPE_SHIFT_AMOUNT) & TYPE_MASK))
106
+ write_varint32(seqid)
107
+ write_string(name)
108
+ nil
109
+ end
110
+
111
+ def write_struct_begin(name)
112
+ @last_field.push(0)
113
+ nil
114
+ end
115
+
116
+ def write_struct_end
117
+ @last_field.pop
118
+ nil
119
+ end
120
+
121
+ def write_field_begin(name, type, id)
122
+ if type == Types::BOOL
123
+ # we want to possibly include the value, so we'll wait.
124
+ @boolean_field = [type, id]
125
+ else
126
+ write_field_begin_internal(type, id)
127
+ end
128
+ nil
129
+ end
130
+
131
+ #
132
+ # The workhorse of writeFieldBegin. It has the option of doing a
133
+ # 'type override' of the type header. This is used specifically in the
134
+ # boolean field case.
135
+ #
136
+ def write_field_begin_internal(type, id, type_override=nil)
137
+ last_id = @last_field.pop
138
+
139
+ # if there's a type override, use that.
140
+ typeToWrite = type_override || CompactTypes.get_compact_type(type)
141
+
142
+ # check if we can use delta encoding for the field id
143
+ if id > last_id && id - last_id <= 15
144
+ # write them together
145
+ write_byte((id - last_id) << 4 | typeToWrite)
146
+ else
147
+ # write them separate
148
+ write_byte(typeToWrite)
149
+ write_i16(id)
150
+ end
151
+
152
+ @last_field.push(id)
153
+ nil
154
+ end
155
+
156
+ def write_field_stop
157
+ write_byte(Types::STOP)
158
+ end
159
+
160
+ def write_map_begin(ktype, vtype, size)
161
+ if (size == 0)
162
+ write_byte(0)
163
+ else
164
+ write_varint32(size)
165
+ write_byte(CompactTypes.get_compact_type(ktype) << 4 | CompactTypes.get_compact_type(vtype))
166
+ end
167
+ end
168
+
169
+ def write_list_begin(etype, size)
170
+ write_collection_begin(etype, size)
171
+ end
172
+
173
+ def write_set_begin(etype, size)
174
+ write_collection_begin(etype, size);
175
+ end
176
+
177
+ def write_bool(bool)
178
+ type = bool ? CompactTypes::BOOLEAN_TRUE : CompactTypes::BOOLEAN_FALSE
179
+ unless @boolean_field.nil?
180
+ # we haven't written the field header yet
181
+ write_field_begin_internal(@boolean_field.first, @boolean_field.last, type)
182
+ @boolean_field = nil
183
+ else
184
+ # we're not part of a field, so just write the value.
185
+ write_byte(type)
186
+ end
187
+ end
188
+
189
+ def write_byte(byte)
190
+ @trans.write([byte].pack('c'))
191
+ end
192
+
193
+ def write_i16(i16)
194
+ write_varint32(int_to_zig_zag(i16))
195
+ end
196
+
197
+ def write_i32(i32)
198
+ write_varint32(int_to_zig_zag(i32))
199
+ end
200
+
201
+ def write_i64(i64)
202
+ write_varint64(long_to_zig_zag(i64))
203
+ end
204
+
205
+ def write_double(dub)
206
+ @trans.write([dub].pack("G").reverse)
207
+ end
208
+
209
+ def write_string(str)
210
+ write_varint32(str.length)
211
+ @trans.write(str)
212
+ end
213
+
214
+ def read_message_begin
215
+ protocol_id = read_byte()
216
+ if protocol_id != PROTOCOL_ID
217
+ raise ProtocolException.new("Expected protocol id #{PROTOCOL_ID} but got #{protocol_id}")
218
+ end
219
+
220
+ version_and_type = read_byte()
221
+ version = version_and_type & VERSION_MASK
222
+ if (version != VERSION)
223
+ raise ProtocolException.new("Expected version #{VERSION} but got #{version}");
224
+ end
225
+
226
+ type = (version_and_type >> TYPE_SHIFT_AMOUNT) & 0x03
227
+ seqid = read_varint32()
228
+ messageName = read_string()
229
+ [messageName, type, seqid]
230
+ end
231
+
232
+ def read_struct_begin
233
+ @last_field.push(0)
234
+ ""
235
+ end
236
+
237
+ def read_struct_end
238
+ @last_field.pop()
239
+ nil
240
+ end
241
+
242
+ def read_field_begin
243
+ type = read_byte()
244
+
245
+ # if it's a stop, then we can return immediately, as the struct is over.
246
+ if (type & 0x0f) == Types::STOP
247
+ TSTOP
248
+ else
249
+ field_id = nil
250
+
251
+ # mask off the 4 MSB of the type header. it could contain a field id delta.
252
+ modifier = (type & 0xf0) >> 4
253
+ if modifier == 0
254
+ # not a delta. look ahead for the zigzag varint field id.
255
+ field_id = read_i16()
256
+ else
257
+ # has a delta. add the delta to the last read field id.
258
+ field_id = @last_field.pop + modifier
259
+ end
260
+
261
+ # if this happens to be a boolean field, the value is encoded in the type
262
+ if CompactTypes.is_bool_type?(type)
263
+ # save the boolean value in a special instance variable.
264
+ @bool_value = (type & 0x0f) == CompactTypes::BOOLEAN_TRUE
265
+ end
266
+
267
+ # push the new field onto the field stack so we can keep the deltas going.
268
+ @last_field.push(field_id)
269
+ ["", CompactTypes.get_ttype(type & 0x0f), field_id]
270
+ end
271
+ end
272
+
273
+ def read_map_begin
274
+ size = read_varint32()
275
+ key_and_value_type = size == 0 ? 0 : read_byte()
276
+ [CompactTypes.get_ttype(key_and_value_type >> 4), CompactTypes.get_ttype(key_and_value_type & 0xf), size]
277
+ end
278
+
279
+ def read_list_begin
280
+ size_and_type = read_byte()
281
+ size = (size_and_type >> 4) & 0x0f
282
+ if size == 15
283
+ size = read_varint32()
284
+ end
285
+ type = CompactTypes.get_ttype(size_and_type)
286
+ [type, size]
287
+ end
288
+
289
+ def read_set_begin
290
+ read_list_begin
291
+ end
292
+
293
+ def read_bool
294
+ unless @bool_value.nil?
295
+ bv = @bool_value
296
+ @bool_value = nil
297
+ bv
298
+ else
299
+ read_byte() == CompactTypes::BOOLEAN_TRUE
300
+ end
301
+ end
302
+
303
+ def read_byte
304
+ dat = trans.read_all(1)
305
+ val = dat[0]
306
+ if (val > 0x7f)
307
+ val = 0 - ((val - 1) ^ 0xff)
308
+ end
309
+ val
310
+ end
311
+
312
+ def read_i16
313
+ zig_zag_to_int(read_varint32())
314
+ end
315
+
316
+ def read_i32
317
+ zig_zag_to_int(read_varint32())
318
+ end
319
+
320
+ def read_i64
321
+ zig_zag_to_long(read_varint64())
322
+ end
323
+
324
+ def read_double
325
+ dat = trans.read_all(8)
326
+ val = dat.reverse.unpack('G').first
327
+ val
328
+ end
329
+
330
+ def read_string
331
+ size = read_varint32()
332
+ trans.read_all(size)
333
+ end
334
+
335
+
336
+ private
337
+
338
+ #
339
+ # Abstract method for writing the start of lists and sets. List and sets on
340
+ # the wire differ only by the type indicator.
341
+ #
342
+ def write_collection_begin(elem_type, size)
343
+ if size <= 14
344
+ write_byte(size << 4 | CompactTypes.get_compact_type(elem_type))
345
+ else
346
+ write_byte(0xf0 | CompactTypes.get_compact_type(elem_type))
347
+ write_varint32(size)
348
+ end
349
+ end
350
+
351
+ def write_varint32(n)
352
+ # int idx = 0;
353
+ while true
354
+ if (n & ~0x7F) == 0
355
+ # i32buf[idx++] = (byte)n;
356
+ write_byte(n)
357
+ break
358
+ # return;
359
+ else
360
+ # i32buf[idx++] = (byte)((n & 0x7F) | 0x80);
361
+ write_byte((n & 0x7F) | 0x80)
362
+ n = n >> 7
363
+ end
364
+ end
365
+ # trans_.write(i32buf, 0, idx);
366
+ end
367
+
368
+ SEVEN_BIT_MASK = 0x7F
369
+ EVERYTHING_ELSE_MASK = ~SEVEN_BIT_MASK
370
+
371
+ def write_varint64(n)
372
+ while true
373
+ if (n & EVERYTHING_ELSE_MASK) == 0 #TODO need to find a way to make this into a long...
374
+ write_byte(n)
375
+ break
376
+ else
377
+ write_byte((n & SEVEN_BIT_MASK) | 0x80)
378
+ n >>= 7
379
+ end
380
+ end
381
+ end
382
+
383
+ def read_varint32()
384
+ read_varint64()
385
+ end
386
+
387
+ def read_varint64()
388
+ shift = 0
389
+ result = 0
390
+ while true
391
+ b = read_byte()
392
+ result |= (b & 0x7f) << shift
393
+ break if (b & 0x80) != 0x80
394
+ shift += 7
395
+ end
396
+ result
397
+ end
398
+
399
+ def int_to_zig_zag(n)
400
+ (n << 1) ^ (n >> 31)
401
+ end
402
+
403
+ def long_to_zig_zag(l)
404
+ # puts "zz encoded #{l} to #{(l << 1) ^ (l >> 63)}"
405
+ (l << 1) ^ (l >> 63)
406
+ end
407
+
408
+ def zig_zag_to_int(n)
409
+ (n >> 1) ^ -(n & 1)
410
+ end
411
+
412
+ def zig_zag_to_long(n)
413
+ (n >> 1) ^ -(n & 1)
414
+ end
415
+ end
416
+
417
+ class CompactProtocolFactory < BaseProtocolFactory
418
+ def get_protocol(trans)
419
+ CompactProtocol.new(trans)
420
+ end
421
+ end
422
+ end
@@ -0,0 +1,33 @@
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 Deserializer
22
+ def initialize(protocol_factory = BinaryProtocolFactory.new)
23
+ @transport = MemoryBufferTransport.new
24
+ @protocol = protocol_factory.get_protocol(@transport)
25
+ end
26
+
27
+ def deserialize(base, buffer)
28
+ @transport.reset_buffer(buffer)
29
+ base.read(@protocol)
30
+ base
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,34 @@
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 Serializer
22
+ def initialize(protocol_factory = BinaryProtocolFactory.new)
23
+ @transport = MemoryBufferTransport.new
24
+ @protocol = protocol_factory.get_protocol(@transport)
25
+ end
26
+
27
+ def serialize(base)
28
+ @transport.reset_buffer
29
+ base.write(@protocol)
30
+ @transport.read(@transport.available)
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,31 @@
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 BaseServer
22
+ def initialize(processor, server_transport, transport_factory=nil, protocol_factory=nil)
23
+ @processor = processor
24
+ @server_transport = server_transport
25
+ @transport_factory = transport_factory ? transport_factory : Thrift::BaseTransportFactory.new
26
+ @protocol_factory = protocol_factory ? protocol_factory : Thrift::BinaryProtocolFactory.new
27
+ end
28
+
29
+ def serve; nil; end
30
+ end
31
+ end
@@ -1,12 +1,27 @@
1
- require 'thrift/protocol'
2
- require 'thrift/protocol/binaryprotocol'
3
- require 'thrift/transport'
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
+ #
4
19
 
5
20
  require 'mongrel'
6
21
 
7
22
  ## Sticks a service on a URL, using mongrel to do the HTTP work
8
23
  module Thrift
9
- class SimpleMongrelHTTPServer
24
+ class MongrelHTTPServer < BaseServer
10
25
  class Handler < Mongrel::HttpHandler
11
26
  def initialize(processor, protocol_factory)
12
27
  @processor = processor
@@ -40,5 +55,4 @@ module Thrift
40
55
  @server.run.join
41
56
  end
42
57
  end
43
- deprecate_class! :TSimpleMongrelHTTPServer => SimpleMongrelHTTPServer
44
58
  end
@@ -1,12 +1,30 @@
1
- require 'thrift/server'
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
+
2
20
  require 'logger'
3
21
  require 'thread'
4
22
 
5
23
  module Thrift
6
24
  # this class expects to always use a FramedTransport for reading messages
7
- class NonblockingServer < Server
8
- def initialize(processor, serverTransport, transportFactory=nil, protocolFactory=nil, num=20, logger = nil)
9
- super(processor, serverTransport, transportFactory, protocolFactory)
25
+ class NonblockingServer < BaseServer
26
+ def initialize(processor, server_transport, transport_factory=nil, protocol_factory=nil, num=20, logger=nil)
27
+ super(processor, server_transport, transport_factory, protocol_factory)
10
28
  @num_threads = num
11
29
  if logger.nil?
12
30
  @logger = Logger.new(STDERR)
@@ -20,15 +38,15 @@ module Thrift
20
38
 
21
39
  def serve
22
40
  @logger.info "Starting #{self}"
23
- @serverTransport.listen
41
+ @server_transport.listen
24
42
  @io_manager = start_io_manager
25
43
 
26
44
  begin
27
45
  loop do
28
- break if @serverTransport.closed?
29
- rd, = select([@serverTransport], nil, nil, 0.1)
46
+ break if @server_transport.closed?
47
+ rd, = select([@server_transport], nil, nil, 0.1)
30
48
  next if rd.nil?
31
- socket = @serverTransport.accept
49
+ socket = @server_transport.accept
32
50
  @logger.debug "Accepted socket: #{socket.inspect}"
33
51
  @io_manager.add_connection socket
34
52
  end
@@ -38,7 +56,7 @@ module Thrift
38
56
  @logger.info "#{self} is shutting down, goodbye"
39
57
  ensure
40
58
  @transport_semaphore.synchronize do
41
- @serverTransport.close
59
+ @server_transport.close
42
60
  end
43
61
  @io_manager.ensure_closed unless @io_manager.nil?
44
62
  end
@@ -53,7 +71,7 @@ module Thrift
53
71
  shutdown_proc = lambda do
54
72
  @io_manager.shutdown(timeout)
55
73
  @transport_semaphore.synchronize do
56
- @serverTransport.close # this will break the accept loop
74
+ @server_transport.close # this will break the accept loop
57
75
  end
58
76
  end
59
77
  if block
@@ -66,7 +84,7 @@ module Thrift
66
84
  private
67
85
 
68
86
  def start_io_manager
69
- iom = IOManager.new(@processor, @serverTransport, @transportFactory, @protocolFactory, @num_threads, @logger)
87
+ iom = IOManager.new(@processor, @server_transport, @transport_factory, @protocol_factory, @num_threads, @logger)
70
88
  iom.spawn
71
89
  iom
72
90
  end
@@ -74,11 +92,11 @@ module Thrift
74
92
  class IOManager # :nodoc:
75
93
  DEFAULT_BUFFER = 2**20
76
94
 
77
- def initialize(processor, serverTransport, transportFactory, protocolFactory, num, logger)
95
+ def initialize(processor, server_transport, transport_factory, protocol_factory, num, logger)
78
96
  @processor = processor
79
- @serverTransport = serverTransport
80
- @transportFactory = transportFactory
81
- @protocolFactory = protocolFactory
97
+ @server_transport = server_transport
98
+ @transport_factory = transport_factory
99
+ @protocol_factory = protocol_factory
82
100
  @num_threads = num
83
101
  @logger = logger
84
102
  @connections = []
@@ -158,7 +176,7 @@ module Thrift
158
176
  end
159
177
 
160
178
  def spin_thread
161
- Worker.new(@processor, @transportFactory, @protocolFactory, @logger, @worker_queue).spawn
179
+ Worker.new(@processor, @transport_factory, @protocol_factory, @logger, @worker_queue).spawn
162
180
  end
163
181
 
164
182
  def signal(msg)
@@ -233,10 +251,10 @@ module Thrift
233
251
  end
234
252
 
235
253
  class Worker # :nodoc:
236
- def initialize(processor, transportFactory, protocolFactory, logger, queue)
254
+ def initialize(processor, transport_factory, protocol_factory, logger, queue)
237
255
  @processor = processor
238
- @transportFactory = transportFactory
239
- @protocolFactory = protocolFactory
256
+ @transport_factory = transport_factory
257
+ @protocol_factory = protocol_factory
240
258
  @logger = logger
241
259
  @queue = queue
242
260
  end
@@ -260,11 +278,11 @@ module Thrift
260
278
  when :frame
261
279
  fd, frame = args
262
280
  begin
263
- otrans = @transportFactory.get_transport(fd)
264
- oprot = @protocolFactory.get_protocol(otrans)
265
- membuf = MemoryBuffer.new(frame)
266
- itrans = @transportFactory.get_transport(membuf)
267
- iprot = @protocolFactory.get_protocol(itrans)
281
+ otrans = @transport_factory.get_transport(fd)
282
+ oprot = @protocol_factory.get_protocol(otrans)
283
+ membuf = MemoryBufferTransport.new(frame)
284
+ itrans = @transport_factory.get_transport(membuf)
285
+ iprot = @protocol_factory.get_protocol(itrans)
268
286
  @processor.process(iprot, oprot)
269
287
  rescue => e
270
288
  @logger.error "#{Thread.current.inspect} raised error: #{e.inspect}\n#{e.backtrace.join("\n")}"