upfluence-thrift 1.0.1

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 (128) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +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/gen-rb/benchmark_constants.rb +11 -0
  7. data/benchmark/gen-rb/benchmark_service.rb +80 -0
  8. data/benchmark/gen-rb/benchmark_types.rb +10 -0
  9. data/benchmark/server.rb +82 -0
  10. data/benchmark/thin_server.rb +44 -0
  11. data/ext/binary_protocol_accelerated.c +460 -0
  12. data/ext/binary_protocol_accelerated.h +20 -0
  13. data/ext/bytes.c +36 -0
  14. data/ext/bytes.h +31 -0
  15. data/ext/compact_protocol.c +637 -0
  16. data/ext/compact_protocol.h +20 -0
  17. data/ext/constants.h +99 -0
  18. data/ext/extconf.rb +34 -0
  19. data/ext/macros.h +41 -0
  20. data/ext/memory_buffer.c +134 -0
  21. data/ext/memory_buffer.h +20 -0
  22. data/ext/protocol.c +0 -0
  23. data/ext/protocol.h +0 -0
  24. data/ext/strlcpy.c +41 -0
  25. data/ext/strlcpy.h +34 -0
  26. data/ext/struct.c +707 -0
  27. data/ext/struct.h +25 -0
  28. data/ext/thrift_native.c +201 -0
  29. data/lib/thrift.rb +68 -0
  30. data/lib/thrift/bytes.rb +131 -0
  31. data/lib/thrift/client.rb +71 -0
  32. data/lib/thrift/core_ext.rb +23 -0
  33. data/lib/thrift/core_ext/fixnum.rb +29 -0
  34. data/lib/thrift/exceptions.rb +87 -0
  35. data/lib/thrift/multiplexed_processor.rb +76 -0
  36. data/lib/thrift/processor.rb +57 -0
  37. data/lib/thrift/protocol/base_protocol.rb +379 -0
  38. data/lib/thrift/protocol/binary_protocol.rb +237 -0
  39. data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
  40. data/lib/thrift/protocol/compact_protocol.rb +435 -0
  41. data/lib/thrift/protocol/json_protocol.rb +769 -0
  42. data/lib/thrift/protocol/multiplexed_protocol.rb +40 -0
  43. data/lib/thrift/protocol/protocol_decorator.rb +194 -0
  44. data/lib/thrift/serializer/deserializer.rb +33 -0
  45. data/lib/thrift/serializer/serializer.rb +34 -0
  46. data/lib/thrift/server/base_server.rb +31 -0
  47. data/lib/thrift/server/mongrel_http_server.rb +60 -0
  48. data/lib/thrift/server/nonblocking_server.rb +305 -0
  49. data/lib/thrift/server/rack_application.rb +61 -0
  50. data/lib/thrift/server/simple_server.rb +43 -0
  51. data/lib/thrift/server/thin_http_server.rb +51 -0
  52. data/lib/thrift/server/thread_pool_server.rb +75 -0
  53. data/lib/thrift/server/threaded_server.rb +47 -0
  54. data/lib/thrift/struct.rb +237 -0
  55. data/lib/thrift/struct_union.rb +192 -0
  56. data/lib/thrift/thrift_native.rb +24 -0
  57. data/lib/thrift/transport/base_server_transport.rb +37 -0
  58. data/lib/thrift/transport/base_transport.rb +109 -0
  59. data/lib/thrift/transport/buffered_transport.rb +114 -0
  60. data/lib/thrift/transport/framed_transport.rb +117 -0
  61. data/lib/thrift/transport/http_client_transport.rb +56 -0
  62. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  63. data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
  64. data/lib/thrift/transport/server_socket.rb +63 -0
  65. data/lib/thrift/transport/socket.rb +139 -0
  66. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  67. data/lib/thrift/transport/unix_socket.rb +40 -0
  68. data/lib/thrift/types.rb +101 -0
  69. data/lib/thrift/union.rb +179 -0
  70. data/spec/BaseService.thrift +27 -0
  71. data/spec/ExtendedService.thrift +25 -0
  72. data/spec/Referenced.thrift +44 -0
  73. data/spec/ThriftNamespacedSpec.thrift +53 -0
  74. data/spec/ThriftSpec.thrift +183 -0
  75. data/spec/base_protocol_spec.rb +217 -0
  76. data/spec/base_transport_spec.rb +350 -0
  77. data/spec/binary_protocol_accelerated_spec.rb +42 -0
  78. data/spec/binary_protocol_spec.rb +66 -0
  79. data/spec/binary_protocol_spec_shared.rb +455 -0
  80. data/spec/bytes_spec.rb +160 -0
  81. data/spec/client_spec.rb +99 -0
  82. data/spec/compact_protocol_spec.rb +143 -0
  83. data/spec/exception_spec.rb +141 -0
  84. data/spec/flat_spec.rb +62 -0
  85. data/spec/gen-rb/base/base_service.rb +80 -0
  86. data/spec/gen-rb/base/base_service_constants.rb +11 -0
  87. data/spec/gen-rb/base/base_service_types.rb +26 -0
  88. data/spec/gen-rb/extended/extended_service.rb +78 -0
  89. data/spec/gen-rb/extended/extended_service_constants.rb +11 -0
  90. data/spec/gen-rb/extended/extended_service_types.rb +12 -0
  91. data/spec/gen-rb/flat/namespaced_nonblocking_service.rb +272 -0
  92. data/spec/gen-rb/flat/referenced_constants.rb +11 -0
  93. data/spec/gen-rb/flat/referenced_types.rb +17 -0
  94. data/spec/gen-rb/flat/thrift_namespaced_spec_constants.rb +11 -0
  95. data/spec/gen-rb/flat/thrift_namespaced_spec_types.rb +28 -0
  96. data/spec/gen-rb/namespaced_spec_namespace/namespaced_nonblocking_service.rb +272 -0
  97. data/spec/gen-rb/namespaced_spec_namespace/thrift_namespaced_spec_constants.rb +11 -0
  98. data/spec/gen-rb/namespaced_spec_namespace/thrift_namespaced_spec_types.rb +28 -0
  99. data/spec/gen-rb/nonblocking_service.rb +272 -0
  100. data/spec/gen-rb/other_namespace/referenced_constants.rb +11 -0
  101. data/spec/gen-rb/other_namespace/referenced_types.rb +17 -0
  102. data/spec/gen-rb/thrift_spec_constants.rb +11 -0
  103. data/spec/gen-rb/thrift_spec_types.rb +538 -0
  104. data/spec/http_client_spec.rb +120 -0
  105. data/spec/json_protocol_spec.rb +513 -0
  106. data/spec/namespaced_spec.rb +67 -0
  107. data/spec/nonblocking_server_spec.rb +263 -0
  108. data/spec/processor_spec.rb +80 -0
  109. data/spec/serializer_spec.rb +67 -0
  110. data/spec/server_socket_spec.rb +79 -0
  111. data/spec/server_spec.rb +147 -0
  112. data/spec/socket_spec.rb +61 -0
  113. data/spec/socket_spec_shared.rb +104 -0
  114. data/spec/spec_helper.rb +64 -0
  115. data/spec/struct_nested_containers_spec.rb +191 -0
  116. data/spec/struct_spec.rb +293 -0
  117. data/spec/thin_http_server_spec.rb +141 -0
  118. data/spec/types_spec.rb +115 -0
  119. data/spec/union_spec.rb +203 -0
  120. data/spec/unix_socket_spec.rb +107 -0
  121. data/test/debug_proto/gen-rb/debug_proto_test_constants.rb +274 -0
  122. data/test/debug_proto/gen-rb/debug_proto_test_types.rb +761 -0
  123. data/test/debug_proto/gen-rb/empty_service.rb +24 -0
  124. data/test/debug_proto/gen-rb/inherited.rb +79 -0
  125. data/test/debug_proto/gen-rb/reverse_order_service.rb +82 -0
  126. data/test/debug_proto/gen-rb/service_for_exception_with_a_map.rb +81 -0
  127. data/test/debug_proto/gen-rb/srv.rb +330 -0
  128. metadata +388 -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,76 @@
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
+ require 'thrift/protocol/base_protocol'
21
+
22
+ module Thrift
23
+ class MultiplexedProcessor
24
+ def initialize
25
+ @actual_processors = {}
26
+ end
27
+
28
+ def register_processor(service_name, processor)
29
+ @actual_processors[service_name] = processor
30
+ end
31
+
32
+ def process(iprot, oprot)
33
+ name, type, seqid = iprot.read_message_begin
34
+ check_type(type)
35
+ check_separator(name)
36
+ service_name, method = name.split(':')
37
+ processor(service_name).process(StoredMessageProtocol.new(iprot, [method, type, seqid]), oprot)
38
+ end
39
+
40
+ protected
41
+
42
+ def processor(service_name)
43
+ if @actual_processors.has_key?(service_name)
44
+ @actual_processors[service_name]
45
+ else
46
+ raise Thrift::Exception.new("Service name not found: #{service_name}. Did you forget to call #{self.class.name}#register_processor?")
47
+ end
48
+ end
49
+
50
+ def check_type(type)
51
+ unless [MessageTypes::CALL, MessageTypes::ONEWAY].include?(type)
52
+ raise Thrift::Exception.new('This should not have happened!?')
53
+ end
54
+ end
55
+
56
+ def check_separator(name)
57
+ if name.count(':') < 1
58
+ raise Thrift::Exception.new("Service name not found in message name: #{name}. Did you forget to use a Thrift::Protocol::MultiplexedProtocol in your client?")
59
+ end
60
+ end
61
+ end
62
+
63
+ class StoredMessageProtocol < BaseProtocol
64
+
65
+ include ProtocolDecorator
66
+
67
+ def initialize(protocol, message_begin)
68
+ super(protocol)
69
+ @message_begin = message_begin
70
+ end
71
+
72
+ def read_message_begin
73
+ @message_begin
74
+ end
75
+ end
76
+ 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,379 @@
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
+ NOT_IMPLEMENTED = 5
32
+ DEPTH_LIMIT = 6
33
+
34
+ attr_reader :type
35
+
36
+ def initialize(type=UNKNOWN, message=nil)
37
+ super(message)
38
+ @type = type
39
+ end
40
+ end
41
+
42
+ class BaseProtocol
43
+
44
+ attr_reader :trans
45
+
46
+ def initialize(trans)
47
+ @trans = trans
48
+ end
49
+
50
+ def native?
51
+ puts "wrong method is being called!"
52
+ false
53
+ end
54
+
55
+ def write_message_begin(name, type, seqid)
56
+ raise NotImplementedError
57
+ end
58
+
59
+ def write_message_end; nil; end
60
+
61
+ def write_struct_begin(name)
62
+ raise NotImplementedError
63
+ end
64
+
65
+ def write_struct_end; nil; end
66
+
67
+ def write_field_begin(name, type, id)
68
+ raise NotImplementedError
69
+ end
70
+
71
+ def write_field_end; nil; end
72
+
73
+ def write_field_stop
74
+ raise NotImplementedError
75
+ end
76
+
77
+ def write_map_begin(ktype, vtype, size)
78
+ raise NotImplementedError
79
+ end
80
+
81
+ def write_map_end; nil; end
82
+
83
+ def write_list_begin(etype, size)
84
+ raise NotImplementedError
85
+ end
86
+
87
+ def write_list_end; nil; end
88
+
89
+ def write_set_begin(etype, size)
90
+ raise NotImplementedError
91
+ end
92
+
93
+ def write_set_end; nil; end
94
+
95
+ def write_bool(bool)
96
+ raise NotImplementedError
97
+ end
98
+
99
+ def write_byte(byte)
100
+ raise NotImplementedError
101
+ end
102
+
103
+ def write_i16(i16)
104
+ raise NotImplementedError
105
+ end
106
+
107
+ def write_i32(i32)
108
+ raise NotImplementedError
109
+ end
110
+
111
+ def write_i64(i64)
112
+ raise NotImplementedError
113
+ end
114
+
115
+ def write_double(dub)
116
+ raise NotImplementedError
117
+ end
118
+
119
+ # Writes a Thrift String. In Ruby 1.9+, the String passed will be transcoded to UTF-8.
120
+ #
121
+ # str - The String to write.
122
+ #
123
+ # Raises EncodingError if the transcoding to UTF-8 fails.
124
+ #
125
+ # Returns nothing.
126
+ def write_string(str)
127
+ raise NotImplementedError
128
+ end
129
+
130
+ # Writes a Thrift Binary (Thrift String with no encoding). In Ruby 1.9+, the String passed
131
+ # will forced into BINARY encoding.
132
+ #
133
+ # buf - The String to write.
134
+ #
135
+ # Returns nothing.
136
+ def write_binary(buf)
137
+ raise NotImplementedError
138
+ end
139
+
140
+ def read_message_begin
141
+ raise NotImplementedError
142
+ end
143
+
144
+ def read_message_end; nil; end
145
+
146
+ def read_struct_begin
147
+ raise NotImplementedError
148
+ end
149
+
150
+ def read_struct_end; nil; end
151
+
152
+ def read_field_begin
153
+ raise NotImplementedError
154
+ end
155
+
156
+ def read_field_end; nil; end
157
+
158
+ def read_map_begin
159
+ raise NotImplementedError
160
+ end
161
+
162
+ def read_map_end; nil; end
163
+
164
+ def read_list_begin
165
+ raise NotImplementedError
166
+ end
167
+
168
+ def read_list_end; nil; end
169
+
170
+ def read_set_begin
171
+ raise NotImplementedError
172
+ end
173
+
174
+ def read_set_end; nil; end
175
+
176
+ def read_bool
177
+ raise NotImplementedError
178
+ end
179
+
180
+ def read_byte
181
+ raise NotImplementedError
182
+ end
183
+
184
+ def read_i16
185
+ raise NotImplementedError
186
+ end
187
+
188
+ def read_i32
189
+ raise NotImplementedError
190
+ end
191
+
192
+ def read_i64
193
+ raise NotImplementedError
194
+ end
195
+
196
+ def read_double
197
+ raise NotImplementedError
198
+ end
199
+
200
+ # Reads a Thrift String. In Ruby 1.9+, all Strings will be returned with an Encoding of UTF-8.
201
+ #
202
+ # Returns a String.
203
+ def read_string
204
+ raise NotImplementedError
205
+ end
206
+
207
+ # Reads a Thrift Binary (Thrift String without encoding). In Ruby 1.9+, all Strings will be returned
208
+ # with an Encoding of BINARY.
209
+ #
210
+ # Returns a String.
211
+ def read_binary
212
+ raise NotImplementedError
213
+ end
214
+
215
+ # Writes a field based on the field information, field ID and value.
216
+ #
217
+ # field_info - A Hash containing the definition of the field:
218
+ # :name - The name of the field.
219
+ # :type - The type of the field, which must be a Thrift::Types constant.
220
+ # :binary - A Boolean flag that indicates if Thrift::Types::STRING is a binary string (string without encoding).
221
+ # fid - The ID of the field.
222
+ # value - The field's value to write; object type varies based on :type.
223
+ #
224
+ # Returns nothing.
225
+ def write_field(*args)
226
+ if args.size == 3
227
+ # handles the documented method signature - write_field(field_info, fid, value)
228
+ field_info = args[0]
229
+ fid = args[1]
230
+ value = args[2]
231
+ elsif args.size == 4
232
+ # handles the deprecated method signature - write_field(name, type, fid, value)
233
+ field_info = {:name => args[0], :type => args[1]}
234
+ fid = args[2]
235
+ value = args[3]
236
+ else
237
+ raise ArgumentError, "wrong number of arguments (#{args.size} for 3)"
238
+ end
239
+
240
+ write_field_begin(field_info[:name], field_info[:type], fid)
241
+ write_type(field_info, value)
242
+ write_field_end
243
+ end
244
+
245
+ # Writes a field value based on the field information.
246
+ #
247
+ # field_info - A Hash containing the definition of the field:
248
+ # :type - The Thrift::Types constant that determines how the value is written.
249
+ # :binary - A Boolean flag that indicates if Thrift::Types::STRING is a binary string (string without encoding).
250
+ # value - The field's value to write; object type varies based on field_info[:type].
251
+ #
252
+ # Returns nothing.
253
+ def write_type(field_info, value)
254
+ # if field_info is a Fixnum, assume it is a Thrift::Types constant
255
+ # convert it into a field_info Hash for backwards compatibility
256
+ if field_info.is_a? Fixnum
257
+ field_info = {:type => field_info}
258
+ end
259
+
260
+ case field_info[:type]
261
+ when Types::BOOL
262
+ write_bool(value)
263
+ when Types::BYTE
264
+ write_byte(value)
265
+ when Types::DOUBLE
266
+ write_double(value)
267
+ when Types::I16
268
+ write_i16(value)
269
+ when Types::I32
270
+ write_i32(value)
271
+ when Types::I64
272
+ write_i64(value)
273
+ when Types::STRING
274
+ if field_info[:binary]
275
+ write_binary(value)
276
+ else
277
+ write_string(value)
278
+ end
279
+ when Types::STRUCT
280
+ value.write(self)
281
+ else
282
+ raise NotImplementedError
283
+ end
284
+ end
285
+
286
+ # Reads a field value based on the field information.
287
+ #
288
+ # field_info - A Hash containing the pertinent data to write:
289
+ # :type - The Thrift::Types constant that determines how the value is written.
290
+ # :binary - A flag that indicates if Thrift::Types::STRING is a binary string (string without encoding).
291
+ #
292
+ # Returns the value read; object type varies based on field_info[:type].
293
+ def read_type(field_info)
294
+ # if field_info is a Fixnum, assume it is a Thrift::Types constant
295
+ # convert it into a field_info Hash for backwards compatibility
296
+ if field_info.is_a? Fixnum
297
+ field_info = {:type => field_info}
298
+ end
299
+
300
+ case field_info[:type]
301
+ when Types::BOOL
302
+ read_bool
303
+ when Types::BYTE
304
+ read_byte
305
+ when Types::DOUBLE
306
+ read_double
307
+ when Types::I16
308
+ read_i16
309
+ when Types::I32
310
+ read_i32
311
+ when Types::I64
312
+ read_i64
313
+ when Types::STRING
314
+ if field_info[:binary]
315
+ read_binary
316
+ else
317
+ read_string
318
+ end
319
+ else
320
+ raise NotImplementedError
321
+ end
322
+ end
323
+
324
+ def skip(type)
325
+ case type
326
+ when Types::STOP
327
+ nil
328
+ when Types::BOOL
329
+ read_bool
330
+ when Types::BYTE
331
+ read_byte
332
+ when Types::I16
333
+ read_i16
334
+ when Types::I32
335
+ read_i32
336
+ when Types::I64
337
+ read_i64
338
+ when Types::DOUBLE
339
+ read_double
340
+ when Types::STRING
341
+ read_string
342
+ when Types::STRUCT
343
+ read_struct_begin
344
+ while true
345
+ name, type, id = read_field_begin
346
+ break if type == Types::STOP
347
+ skip(type)
348
+ read_field_end
349
+ end
350
+ read_struct_end
351
+ when Types::MAP
352
+ ktype, vtype, size = read_map_begin
353
+ size.times do
354
+ skip(ktype)
355
+ skip(vtype)
356
+ end
357
+ read_map_end
358
+ when Types::SET
359
+ etype, size = read_set_begin
360
+ size.times do
361
+ skip(etype)
362
+ end
363
+ read_set_end
364
+ when Types::LIST
365
+ etype, size = read_list_begin
366
+ size.times do
367
+ skip(etype)
368
+ end
369
+ read_list_end
370
+ end
371
+ end
372
+ end
373
+
374
+ class BaseProtocolFactory
375
+ def get_protocol(trans)
376
+ raise NotImplementedError
377
+ end
378
+ end
379
+ end