thrift 0.0.751142

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/CHANGELOG +2 -0
  2. data/COPYING +14 -0
  3. data/LICENSE +14 -0
  4. data/Makefile.am +15 -0
  5. data/Manifest +78 -0
  6. data/README +30 -0
  7. data/Rakefile +102 -0
  8. data/benchmark/Benchmark.thrift +5 -0
  9. data/benchmark/benchmark.rb +254 -0
  10. data/benchmark/client.rb +56 -0
  11. data/benchmark/gen-rb/BenchmarkService.rb +81 -0
  12. data/benchmark/gen-rb/Benchmark_constants.rb +11 -0
  13. data/benchmark/gen-rb/Benchmark_types.rb +10 -0
  14. data/benchmark/server.rb +64 -0
  15. data/benchmark/thin_server.rb +26 -0
  16. data/ext/binary_protocol_accelerated.c +463 -0
  17. data/ext/binary_protocol_accelerated.h +1 -0
  18. data/ext/constants.h +77 -0
  19. data/ext/extconf.rb +7 -0
  20. data/ext/memory_buffer.c +52 -0
  21. data/ext/memory_buffer.h +1 -0
  22. data/ext/protocol.c +166 -0
  23. data/ext/protocol.h +1 -0
  24. data/ext/struct.c +574 -0
  25. data/ext/struct.h +48 -0
  26. data/ext/thrift_native.c +173 -0
  27. data/lib/thrift/client.rb +44 -0
  28. data/lib/thrift/deprecation.rb +155 -0
  29. data/lib/thrift/exceptions.rb +65 -0
  30. data/lib/thrift/processor.rb +39 -0
  31. data/lib/thrift/protocol/binaryprotocol.rb +213 -0
  32. data/lib/thrift/protocol/binaryprotocolaccelerated.rb +19 -0
  33. data/lib/thrift/protocol/tbinaryprotocol.rb +2 -0
  34. data/lib/thrift/protocol/tprotocol.rb +2 -0
  35. data/lib/thrift/protocol.rb +270 -0
  36. data/lib/thrift/serializer.rb +27 -0
  37. data/lib/thrift/server/httpserver.rb +44 -0
  38. data/lib/thrift/server/nonblockingserver.rb +278 -0
  39. data/lib/thrift/server/thttpserver.rb +2 -0
  40. data/lib/thrift/server/tserver.rb +2 -0
  41. data/lib/thrift/server.rb +135 -0
  42. data/lib/thrift/struct.rb +272 -0
  43. data/lib/thrift/thrift.rb +14 -0
  44. data/lib/thrift/transport/httpclient.rb +29 -0
  45. data/lib/thrift/transport/socket.rb +167 -0
  46. data/lib/thrift/transport/thttpclient.rb +2 -0
  47. data/lib/thrift/transport/tsocket.rb +2 -0
  48. data/lib/thrift/transport/ttransport.rb +2 -0
  49. data/lib/thrift/transport/unixsocket.rb +58 -0
  50. data/lib/thrift/transport.rb +319 -0
  51. data/lib/thrift/types.rb +83 -0
  52. data/lib/thrift.rb +28 -0
  53. data/setup.rb +1585 -0
  54. data/spec/ThriftSpec.thrift +46 -0
  55. data/spec/backwards_compatibility_spec.rb +136 -0
  56. data/spec/binaryprotocol_spec.rb +45 -0
  57. data/spec/binaryprotocol_spec_shared.rb +274 -0
  58. data/spec/binaryprotocolaccelerated_spec.rb +101 -0
  59. data/spec/client_spec.rb +81 -0
  60. data/spec/deprecation_spec.rb +443 -0
  61. data/spec/exception_spec.rb +123 -0
  62. data/spec/gen-rb/NonblockingService.rb +268 -0
  63. data/spec/gen-rb/ThriftSpec_constants.rb +11 -0
  64. data/spec/gen-rb/ThriftSpec_types.rb +134 -0
  65. data/spec/httpclient_spec.rb +31 -0
  66. data/spec/httpserver_spec.rb +98 -0
  67. data/spec/nonblockingserver_spec.rb +245 -0
  68. data/spec/processor_spec.rb +64 -0
  69. data/spec/protocol_spec.rb +142 -0
  70. data/spec/serializer_spec.rb +52 -0
  71. data/spec/server_spec.rb +141 -0
  72. data/spec/socket_spec.rb +97 -0
  73. data/spec/socket_spec_shared.rb +85 -0
  74. data/spec/spec_helper.rb +35 -0
  75. data/spec/struct_spec.rb +244 -0
  76. data/spec/transport_spec.rb +359 -0
  77. data/spec/types_spec.rb +98 -0
  78. data/spec/unixsocket_spec.rb +90 -0
  79. data/thrift.gemspec +33 -0
  80. data.tar.gz.sig +0 -0
  81. metadata +200 -0
  82. metadata.gz.sig +0 -0
@@ -0,0 +1,173 @@
1
+ #include <ruby.h>
2
+ #include <struct.h>
3
+ #include <binary_protocol_accelerated.h>
4
+ #include <protocol.h>
5
+ #include <memory_buffer.h>
6
+
7
+ // cached classes/modules
8
+ VALUE rb_cSet;
9
+ VALUE thrift_module;
10
+ VALUE thrift_types_module;
11
+
12
+ // TType constants
13
+ int TTYPE_STOP;
14
+ int TTYPE_BOOL;
15
+ int TTYPE_BYTE;
16
+ int TTYPE_I16;
17
+ int TTYPE_I32;
18
+ int TTYPE_I64;
19
+ int TTYPE_DOUBLE;
20
+ int TTYPE_STRING;
21
+ int TTYPE_MAP;
22
+ int TTYPE_SET;
23
+ int TTYPE_LIST;
24
+ int TTYPE_STRUCT;
25
+
26
+ // method ids
27
+ ID validate_method_id;
28
+ ID write_struct_begin_method_id;
29
+ ID write_struct_end_method_id;
30
+ ID write_field_begin_method_id;
31
+ ID write_field_end_method_id;
32
+ ID write_boolean_method_id;
33
+ ID write_byte_method_id;
34
+ ID write_i16_method_id;
35
+ ID write_i32_method_id;
36
+ ID write_i64_method_id;
37
+ ID write_double_method_id;
38
+ ID write_string_method_id;
39
+ ID write_map_begin_method_id;
40
+ ID write_map_end_method_id;
41
+ ID write_list_begin_method_id;
42
+ ID write_list_end_method_id;
43
+ ID write_set_begin_method_id;
44
+ ID write_set_end_method_id;
45
+ ID size_method_id;
46
+ ID read_bool_method_id;
47
+ ID read_byte_method_id;
48
+ ID read_i16_method_id;
49
+ ID read_i32_method_id;
50
+ ID read_i64_method_id;
51
+ ID read_string_method_id;
52
+ ID read_double_method_id;
53
+ ID read_map_begin_method_id;
54
+ ID read_map_end_method_id;
55
+ ID read_list_begin_method_id;
56
+ ID read_list_end_method_id;
57
+ ID read_set_begin_method_id;
58
+ ID read_set_end_method_id;
59
+ ID read_struct_begin_method_id;
60
+ ID read_struct_end_method_id;
61
+ ID read_field_begin_method_id;
62
+ ID read_field_end_method_id;
63
+ ID keys_method_id;
64
+ ID entries_method_id;
65
+ ID name_method_id;
66
+ ID sort_method_id;
67
+ ID write_field_stop_method_id;
68
+ ID skip_method_id;
69
+ ID write_method_id;
70
+ ID read_method_id;
71
+ ID native_qmark_method_id;
72
+
73
+ // constant ids
74
+ ID fields_const_id;
75
+ ID transport_ivar_id;
76
+ ID strict_read_ivar_id;
77
+ ID strict_write_ivar_id;
78
+
79
+ // cached symbols
80
+ VALUE type_sym;
81
+ VALUE name_sym;
82
+ VALUE key_sym;
83
+ VALUE value_sym;
84
+ VALUE element_sym;
85
+ VALUE class_sym;
86
+ VALUE protocol_exception_class;
87
+
88
+ void Init_thrift_native() {
89
+ // cached classes
90
+ thrift_module = rb_const_get(rb_cObject, rb_intern("Thrift"));
91
+ thrift_types_module = rb_const_get(thrift_module, rb_intern("Types"));
92
+ rb_cSet = rb_const_get(rb_cObject, rb_intern("Set"));
93
+ protocol_exception_class = rb_const_get(thrift_module, rb_intern("ProtocolException"));
94
+
95
+ // Init ttype constants
96
+ TTYPE_BOOL = FIX2INT(rb_const_get(thrift_types_module, rb_intern("BOOL")));
97
+ TTYPE_BYTE = FIX2INT(rb_const_get(thrift_types_module, rb_intern("BYTE")));
98
+ TTYPE_I16 = FIX2INT(rb_const_get(thrift_types_module, rb_intern("I16")));
99
+ TTYPE_I32 = FIX2INT(rb_const_get(thrift_types_module, rb_intern("I32")));
100
+ TTYPE_I64 = FIX2INT(rb_const_get(thrift_types_module, rb_intern("I64")));
101
+ TTYPE_DOUBLE = FIX2INT(rb_const_get(thrift_types_module, rb_intern("DOUBLE")));
102
+ TTYPE_STRING = FIX2INT(rb_const_get(thrift_types_module, rb_intern("STRING")));
103
+ TTYPE_MAP = FIX2INT(rb_const_get(thrift_types_module, rb_intern("MAP")));
104
+ TTYPE_SET = FIX2INT(rb_const_get(thrift_types_module, rb_intern("SET")));
105
+ TTYPE_LIST = FIX2INT(rb_const_get(thrift_types_module, rb_intern("LIST")));
106
+ TTYPE_STRUCT = FIX2INT(rb_const_get(thrift_types_module, rb_intern("STRUCT")));
107
+
108
+ // method ids
109
+ validate_method_id = rb_intern("validate");
110
+ write_struct_begin_method_id = rb_intern("write_struct_begin");
111
+ write_struct_end_method_id = rb_intern("write_struct_end");
112
+ write_field_begin_method_id = rb_intern("write_field_begin");
113
+ write_field_end_method_id = rb_intern("write_field_end");
114
+ write_boolean_method_id = rb_intern("write_bool");
115
+ write_byte_method_id = rb_intern("write_byte");
116
+ write_i16_method_id = rb_intern("write_i16");
117
+ write_i32_method_id = rb_intern("write_i32");
118
+ write_i64_method_id = rb_intern("write_i64");
119
+ write_double_method_id = rb_intern("write_double");
120
+ write_string_method_id = rb_intern("write_string");
121
+ write_map_begin_method_id = rb_intern("write_map_begin");
122
+ write_map_end_method_id = rb_intern("write_map_end");
123
+ write_list_begin_method_id = rb_intern("write_list_begin");
124
+ write_list_end_method_id = rb_intern("write_list_end");
125
+ write_set_begin_method_id = rb_intern("write_set_begin");
126
+ write_set_end_method_id = rb_intern("write_set_end");
127
+ size_method_id = rb_intern("size");
128
+ read_bool_method_id = rb_intern("read_bool");
129
+ read_byte_method_id = rb_intern("read_byte");
130
+ read_i16_method_id = rb_intern("read_i16");
131
+ read_i32_method_id = rb_intern("read_i32");
132
+ read_i64_method_id = rb_intern("read_i64");
133
+ read_string_method_id = rb_intern("read_string");
134
+ read_double_method_id = rb_intern("read_double");
135
+ read_map_begin_method_id = rb_intern("read_map_begin");
136
+ read_map_end_method_id = rb_intern("read_map_end");
137
+ read_list_begin_method_id = rb_intern("read_list_begin");
138
+ read_list_end_method_id = rb_intern("read_list_end");
139
+ read_set_begin_method_id = rb_intern("read_set_begin");
140
+ read_set_end_method_id = rb_intern("read_set_end");
141
+ read_struct_begin_method_id = rb_intern("read_struct_begin");
142
+ read_struct_end_method_id = rb_intern("read_struct_end");
143
+ read_field_begin_method_id = rb_intern("read_field_begin");
144
+ read_field_end_method_id = rb_intern("read_field_end");
145
+ keys_method_id = rb_intern("keys");
146
+ entries_method_id = rb_intern("entries");
147
+ name_method_id = rb_intern("name");
148
+ sort_method_id = rb_intern("sort");
149
+ write_field_stop_method_id = rb_intern("write_field_stop");
150
+ skip_method_id = rb_intern("skip");
151
+ write_method_id = rb_intern("write");
152
+ read_method_id = rb_intern("read");
153
+ native_qmark_method_id = rb_intern("native?");
154
+
155
+ // constant ids
156
+ fields_const_id = rb_intern("FIELDS");
157
+ transport_ivar_id = rb_intern("@trans");
158
+ strict_read_ivar_id = rb_intern("@strict_read");
159
+ strict_write_ivar_id = rb_intern("@strict_write");
160
+
161
+ // cached symbols
162
+ type_sym = ID2SYM(rb_intern("type"));
163
+ name_sym = ID2SYM(rb_intern("name"));
164
+ key_sym = ID2SYM(rb_intern("key"));
165
+ value_sym = ID2SYM(rb_intern("value"));
166
+ element_sym = ID2SYM(rb_intern("element"));
167
+ class_sym = ID2SYM(rb_intern("class"));
168
+
169
+ Init_protocol();
170
+ Init_struct();
171
+ Init_binary_protocol_accelerated();
172
+ Init_memory_buffer();
173
+ }
@@ -0,0 +1,44 @@
1
+ module Thrift
2
+ module Client
3
+ def initialize(iprot, oprot=nil)
4
+ @iprot = iprot
5
+ @oprot = oprot || iprot
6
+ @seqid = 0
7
+ end
8
+
9
+ def send_message(name, args_class, args = {})
10
+ @oprot.write_message_begin(name, MessageTypes::CALL, @seqid)
11
+ data = args_class.new
12
+ args.each do |k, v|
13
+ data.send("#{k.to_s}=", v)
14
+ end
15
+ begin
16
+ data.write(@oprot)
17
+ rescue StandardError => e
18
+ @oprot.trans.close
19
+ raise e
20
+ end
21
+ @oprot.write_message_end
22
+ @oprot.trans.flush
23
+ end
24
+
25
+ def receive_message(result_klass)
26
+ fname, mtype, rseqid = @iprot.read_message_begin
27
+ handle_exception(mtype)
28
+ result = result_klass.new
29
+ result.read(@iprot)
30
+ @iprot.read_message_end
31
+ result
32
+ end
33
+
34
+ def handle_exception(mtype)
35
+ if mtype == MessageTypes::EXCEPTION
36
+ x = ApplicationException.new
37
+ x.read(@iprot)
38
+ @iprot.read_message_end
39
+ raise x
40
+ end
41
+ end
42
+ end
43
+ deprecate_module! :ThriftClient => Client
44
+ end
@@ -0,0 +1,155 @@
1
+ # provide a backwards-compatible wrapper API and deprecate it
2
+
3
+ module Thrift
4
+ DEPRECATION = true unless const_defined?(:DEPRECATION)
5
+ end
6
+
7
+ class Module
8
+ # Wraps the given methods to print a warning and call the real method
9
+ # Example:
10
+ # deprecate! :readAll => :read_all
11
+ #--
12
+ # Yeah, this is ugly, passing a string to module_eval, but unfortunately
13
+ # using a block removes the ability to pass blocks to the defined method
14
+ # and breaks spec
15
+ def deprecate!(methods)
16
+ return unless Thrift::DEPRECATION
17
+ methods.each_pair do |old, new|
18
+ module_eval <<-EOF
19
+ def #{old}(*args, &block)
20
+ old, new = #{[old,new].inspect}
21
+ STDERR.puts "Warning: calling deprecated method \#{self.is_a?(Module) ? "\#{self}." : "\#{self.class}#"}\#{old}"
22
+ STDERR.puts " from \#{caller.first}"
23
+ target = (self.is_a?(Module) ? (class << self;self;end) : self.class)
24
+ target.send :define_method, old, target.instance_method(new) # unwrap
25
+ target.instance_method(new).bind(self).call(*args, &block)
26
+ end
27
+ EOF
28
+ end
29
+ end
30
+ end
31
+
32
+ module Thrift::DeprecationProxy # :nodoc:
33
+ # there's a really weird bug in Ruby where class variables behave wrong
34
+ # when used in a Class.new or #class_eval rather than in a class foo block.
35
+ CLASS_MAPPING = {}
36
+ MODULE_MAPPING = {}
37
+ def self.new_class(obj, name)
38
+ klass_id = CLASS_MAPPING.size
39
+ CLASS_MAPPING[klass_id] = [obj, name, true]
40
+ klass = Class.new(obj) do
41
+ klass = self
42
+ instance_methods.sort.reject { |x| [:__id__,:__send__].include? x.to_sym }.each do |sym|
43
+ undef_method sym
44
+ end
45
+ define_method :__thrift_deprecation_proxy_klass_id do
46
+ klass_id
47
+ end
48
+ def method_missing(sym, *args, &block)
49
+ klass_id = __thrift_deprecation_proxy_klass_id
50
+ obj, name, warned = CLASS_MAPPING[klass_id]
51
+ obj.instance_method(sym).bind(self).call(*args, &block)
52
+ end
53
+ (class << self;self;end).class_eval do
54
+ instance_methods.sort.reject { |x| [:__id__,:__send__].include? x.to_sym }.each do |sym|
55
+ undef_method sym
56
+ end
57
+ define_method :__thrift_deprecation_proxy_klass do
58
+ [klass, klass_id]
59
+ end
60
+ def method_missing(sym, *args, &block)
61
+ klass, klass_id = __thrift_deprecation_proxy_klass
62
+ obj, name, warned = CLASS_MAPPING[klass_id]
63
+ unless warned
64
+ STDERR.puts "Warning: class #{name} is deprecated"
65
+ STDERR.puts " from #{Thrift::DeprecationProxy.process_caller(caller)}"
66
+ CLASS_MAPPING[klass_id][2] = true
67
+ end
68
+ if klass.__id__ == self.__id__
69
+ obj.send sym, *args, &block
70
+ else
71
+ obj.method(sym).unbind.bind(self).call(*args, &block)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ CLASS_MAPPING[klass_id][2] = false
77
+ klass
78
+ end
79
+ def self.new_module(obj, name)
80
+ mod_id = MODULE_MAPPING.size
81
+ MODULE_MAPPING[mod_id] = [obj, name, true]
82
+ mod = Module.new do
83
+ include obj
84
+ instance_methods.sort.reject { |x| [:__id__,:__send__].include? x.to_sym }.each do |sym|
85
+ undef_method sym
86
+ end
87
+ define_method :__thrift_deprecation_proxy_module_id do
88
+ mod_id
89
+ end
90
+ def method_missing(sym, *args, &block)
91
+ mod_id = __thrift_deprecation_proxy_module_id
92
+ obj, name, warned = MODULE_MAPPING[mod_id]
93
+ unless warned
94
+ STDERR.puts "Warning: module #{name} is deprecated"
95
+ STDERR.puts " from #{Thrift::DeprecationProxy.process_caller(caller)}"
96
+ MODULE_MAPPING[mod_id][2] = true
97
+ end
98
+ obj.instance_method(sym).bind(self).call(*args, &block)
99
+ end
100
+ (class << self;self;end).class_eval do
101
+ instance_methods.sort.reject { |x| [:__id__,:__send__].include? x.to_sym }.each do |sym|
102
+ undef_method sym
103
+ end
104
+ define_method :__thrift_deprecation_proxy_module_id do
105
+ mod_id
106
+ end
107
+ def method_missing(sym, *args, &block)
108
+ mod_id = __thrift_deprecation_proxy_module_id
109
+ obj, name, warned = MODULE_MAPPING[mod_id]
110
+ unless warned
111
+ STDERR.puts "Warning: module #{name} is deprecated"
112
+ STDERR.puts " from #{Thrift::DeprecationProxy.process_caller(caller)}"
113
+ MODULE_MAPPING[mod_id][2] = true
114
+ end
115
+ obj.send sym, *args, &block
116
+ end
117
+ end
118
+ end
119
+ MODULE_MAPPING[mod_id][2] = false
120
+ mod
121
+ end
122
+ def self.process_caller(stack)
123
+ dir = File.dirname(__FILE__)
124
+ stack.find { |frame| frame[0,dir.size] != dir }
125
+ end
126
+ def self.reset_deprecation_warnings
127
+ CLASS_MAPPING.each { |k,v| v[2] = false }
128
+ MODULE_MAPPING.each { |k,v| v[2] = false }
129
+ end
130
+ end
131
+
132
+ module Kernel
133
+ # Provides an alternate name for the class for deprecation purposes
134
+ # Example:
135
+ # deprecate_class! :TBinaryProtocol => Thrift::BinaryProtocol
136
+ #--
137
+ # at the moment this only works for creating top-level constants
138
+ # if necessary, this can be extended to take something like :'Thrift::TBinaryProtocol'
139
+ # alternately, Module can be extended with a similar method
140
+ def deprecate_class!(klasses)
141
+ return unless Thrift::DEPRECATION
142
+ klasses.each_pair do |old, new|
143
+ raise "deprecate_class! expected Class, called with #{new}" unless new.is_a? Class
144
+ Object.const_set old, Thrift::DeprecationProxy.new_class(new, old)
145
+ end
146
+ end
147
+
148
+ # like deprecate_class! but for Modules
149
+ def deprecate_module!(modules)
150
+ return unless Thrift::DEPRECATION
151
+ modules.each_pair do |old, new|
152
+ Object.const_set old, Thrift::DeprecationProxy.new_module(new, old)
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,65 @@
1
+ module Thrift
2
+ class Exception < StandardError
3
+ def initialize(message)
4
+ super
5
+ @message = message
6
+ end
7
+
8
+ attr_reader :message
9
+ end
10
+ deprecate_class! :TException => Exception
11
+
12
+ class ApplicationException < Exception
13
+
14
+ UNKNOWN = 0
15
+ UNKNOWN_METHOD = 1
16
+ INVALID_MESSAGE_TYPE = 2
17
+ WRONG_METHOD_NAME = 3
18
+ BAD_SEQUENCE_ID = 4
19
+ MISSING_RESULT = 5
20
+
21
+ attr_reader :type
22
+
23
+ def initialize(type=UNKNOWN, message=nil)
24
+ super(message)
25
+ @type = type
26
+ end
27
+
28
+ def read(iprot)
29
+ iprot.read_struct_begin
30
+ while true
31
+ fname, ftype, fid = iprot.read_field_begin
32
+ if ftype == Types::STOP
33
+ break
34
+ end
35
+ if fid == 1 and ftype == Types::STRING
36
+ @message = iprot.read_string
37
+ elsif fid == 2 and ftype == Types::I32
38
+ @type = iprot.read_i32
39
+ else
40
+ iprot.skip(ftype)
41
+ end
42
+ iprot.read_field_end
43
+ end
44
+ iprot.read_struct_end
45
+ end
46
+
47
+ def write(oprot)
48
+ oprot.write_struct_begin('Thrift::ApplicationException')
49
+ unless @message.nil?
50
+ oprot.write_field_begin('message', Types::STRING, 1)
51
+ oprot.write_string(@message)
52
+ oprot.write_field_end
53
+ end
54
+ unless @type.nil?
55
+ oprot.write_field_begin('type', Types::I32, 2)
56
+ oprot.write_i32(@type)
57
+ oprot.write_field_end
58
+ end
59
+ oprot.write_field_stop
60
+ oprot.write_struct_end
61
+ end
62
+
63
+ end
64
+ deprecate_class! :TApplicationException => ApplicationException
65
+ end
@@ -0,0 +1,39 @@
1
+ module Thrift
2
+ module Processor
3
+ def initialize(handler)
4
+ @handler = handler
5
+ end
6
+
7
+ def process(iprot, oprot)
8
+ name, type, seqid = iprot.read_message_begin
9
+ if respond_to?("process_#{name}")
10
+ send("process_#{name}", seqid, iprot, oprot)
11
+ true
12
+ else
13
+ iprot.skip(Types::STRUCT)
14
+ iprot.read_message_end
15
+ x = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, 'Unknown function '+name)
16
+ oprot.write_message_begin(name, MessageTypes::EXCEPTION, seqid)
17
+ x.write(oprot)
18
+ oprot.write_message_end
19
+ oprot.trans.flush
20
+ false
21
+ end
22
+ end
23
+
24
+ def read_args(iprot, args_class)
25
+ args = args_class.new
26
+ args.read(iprot)
27
+ iprot.read_message_end
28
+ args
29
+ end
30
+
31
+ def write_result(result, oprot, name, seqid)
32
+ oprot.write_message_begin(name, MessageTypes::REPLY, seqid)
33
+ result.write(oprot)
34
+ oprot.write_message_end
35
+ oprot.trans.flush
36
+ end
37
+ end
38
+ deprecate_module! :TProcessor => Processor
39
+ end
@@ -0,0 +1,213 @@
1
+ #
2
+ # Copyright (c) 2006- Facebook
3
+ # Distributed under the Apache Software License
4
+ #
5
+ # See accompanying file LICENSE or visit the Thrift site at:
6
+ # http://developers.facebook.com/thrift/
7
+ #
8
+ # Author: Mark Slee <mcslee@facebook.com>
9
+ #
10
+ require 'thrift/protocol'
11
+
12
+ module Thrift
13
+ class BinaryProtocol < Protocol
14
+ VERSION_MASK = 0xffff0000
15
+ VERSION_1 = 0x80010000
16
+ TYPE_MASK = 0x000000ff
17
+
18
+ attr_reader :strict_read, :strict_write
19
+
20
+ def initialize(trans, strict_read=true, strict_write=true)
21
+ super(trans)
22
+ @strict_read = strict_read
23
+ @strict_write = strict_write
24
+ end
25
+
26
+ def write_message_begin(name, type, seqid)
27
+ # this is necessary because we added (needed) bounds checking to
28
+ # write_i32, and 0x80010000 is too big for that.
29
+ if strict_write
30
+ write_i16(VERSION_1 >> 16)
31
+ write_i16(type)
32
+ write_string(name)
33
+ write_i32(seqid)
34
+ else
35
+ write_string(name)
36
+ write_byte(type)
37
+ write_i32(seqid)
38
+ end
39
+ end
40
+
41
+ def write_field_begin(name, type, id)
42
+ write_byte(type)
43
+ write_i16(id)
44
+ end
45
+
46
+ def write_field_stop
47
+ write_byte(Thrift::Types::STOP)
48
+ end
49
+
50
+ def write_map_begin(ktype, vtype, size)
51
+ write_byte(ktype)
52
+ write_byte(vtype)
53
+ write_i32(size)
54
+ end
55
+
56
+ def write_list_begin(etype, size)
57
+ write_byte(etype)
58
+ write_i32(size)
59
+ end
60
+
61
+ def write_set_begin(etype, size)
62
+ write_byte(etype)
63
+ write_i32(size)
64
+ end
65
+
66
+ def write_bool(bool)
67
+ write_byte(bool ? 1 : 0)
68
+ end
69
+
70
+ def write_byte(byte)
71
+ trans.write([byte].pack('c'))
72
+ end
73
+
74
+ def write_i16(i16)
75
+ trans.write([i16].pack('n'))
76
+ end
77
+
78
+ def write_i32(i32)
79
+ raise RangeError if i32 < -2**31 || i32 >= 2**31
80
+ trans.write([i32].pack('N'))
81
+ end
82
+
83
+ def write_i64(i64)
84
+ hi = i64 >> 32
85
+ lo = i64 & 0xffffffff
86
+ trans.write([hi, lo].pack('N2'))
87
+ end
88
+
89
+ def write_double(dub)
90
+ trans.write([dub].pack('G'))
91
+ end
92
+
93
+ def write_string(str)
94
+ write_i32(str.length)
95
+ trans.write(str)
96
+ end
97
+
98
+ def read_message_begin
99
+ version = read_i32
100
+ if version < 0
101
+ if (version & VERSION_MASK != VERSION_1)
102
+ raise ProtocolException.new(ProtocolException::BAD_VERSION, 'Missing version identifier')
103
+ end
104
+ type = version & TYPE_MASK
105
+ name = read_string
106
+ seqid = read_i32
107
+ [name, type, seqid]
108
+ else
109
+ if strict_read
110
+ raise ProtocolException.new(ProtocolException::BAD_VERSION, 'No version identifier, old protocol client?')
111
+ end
112
+ name = trans.read_all(version)
113
+ type = read_byte
114
+ seqid = read_i32
115
+ [name, type, seqid]
116
+ end
117
+ end
118
+
119
+ def read_field_begin
120
+ type = read_byte
121
+ if (type == Types::STOP)
122
+ [nil, type, 0]
123
+ else
124
+ id = read_i16
125
+ [nil, type, id]
126
+ end
127
+ end
128
+
129
+ def read_map_begin
130
+ ktype = read_byte
131
+ vtype = read_byte
132
+ size = read_i32
133
+ [ktype, vtype, size]
134
+ end
135
+
136
+ def read_list_begin
137
+ etype = read_byte
138
+ size = read_i32
139
+ [etype, size]
140
+ end
141
+
142
+ def read_set_begin
143
+ etype = read_byte
144
+ size = read_i32
145
+ [etype, size]
146
+ end
147
+
148
+ def read_bool
149
+ byte = read_byte
150
+ byte != 0
151
+ end
152
+
153
+ def read_byte
154
+ dat = trans.read_all(1)
155
+ val = dat[0]
156
+ if (val > 0x7f)
157
+ val = 0 - ((val - 1) ^ 0xff)
158
+ end
159
+ val
160
+ end
161
+
162
+ def read_i16
163
+ dat = trans.read_all(2)
164
+ val, = dat.unpack('n')
165
+ if (val > 0x7fff)
166
+ val = 0 - ((val - 1) ^ 0xffff)
167
+ end
168
+ val
169
+ end
170
+
171
+ def read_i32
172
+ dat = trans.read_all(4)
173
+ val, = dat.unpack('N')
174
+ if (val > 0x7fffffff)
175
+ val = 0 - ((val - 1) ^ 0xffffffff)
176
+ end
177
+ val
178
+ end
179
+
180
+ def read_i64
181
+ dat = trans.read_all(8)
182
+ hi, lo = dat.unpack('N2')
183
+ if (hi > 0x7fffffff)
184
+ hi ^= 0xffffffff
185
+ lo ^= 0xffffffff
186
+ 0 - (hi << 32) - lo - 1
187
+ else
188
+ (hi << 32) + lo
189
+ end
190
+ end
191
+
192
+ def read_double
193
+ dat = trans.read_all(8)
194
+ val = dat.unpack('G').first
195
+ val
196
+ end
197
+
198
+ def read_string
199
+ sz = read_i32
200
+ dat = trans.read_all(sz)
201
+ dat
202
+ end
203
+
204
+ end
205
+ deprecate_class! :TBinaryProtocol => BinaryProtocol
206
+
207
+ class BinaryProtocolFactory < ProtocolFactory
208
+ def get_protocol(trans)
209
+ return Thrift::BinaryProtocol.new(trans)
210
+ end
211
+ end
212
+ deprecate_class! :TBinaryProtocolFactory => BinaryProtocolFactory
213
+ end
@@ -0,0 +1,19 @@
1
+ require 'thrift/protocol/binaryprotocol'
2
+ require 'thrift_native'
3
+
4
+ =begin
5
+ The only change required for a transport to support TBinaryProtocolAccelerated is to implement 2 methods:
6
+ * borrow(size), which takes an optional argument and returns atleast _size_ bytes from the transport,
7
+ or the default buffer size if no argument is given
8
+ * consume!(size), which removes size bytes from the front of the buffer
9
+
10
+ See TMemoryBuffer and TBufferedTransport for examples.
11
+ =end
12
+
13
+ module Thrift
14
+ class BinaryProtocolAcceleratedFactory < ProtocolFactory
15
+ def get_protocol(trans)
16
+ BinaryProtocolAccelerated.new(trans)
17
+ end
18
+ end
19
+ end