investtools-thrift 0.9.2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) 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 +67 -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/processor.rb +91 -0
  36. data/lib/thrift/protocol/base_protocol.rb +379 -0
  37. data/lib/thrift/protocol/binary_protocol.rb +237 -0
  38. data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
  39. data/lib/thrift/protocol/compact_protocol.rb +435 -0
  40. data/lib/thrift/protocol/json_protocol.rb +769 -0
  41. data/lib/thrift/protocol/multiplexed_protocol.rb +206 -0
  42. data/lib/thrift/serializer/deserializer.rb +33 -0
  43. data/lib/thrift/serializer/serializer.rb +34 -0
  44. data/lib/thrift/server/base_server.rb +31 -0
  45. data/lib/thrift/server/mongrel_http_server.rb +60 -0
  46. data/lib/thrift/server/nonblocking_server.rb +305 -0
  47. data/lib/thrift/server/simple_server.rb +43 -0
  48. data/lib/thrift/server/thin_http_server.rb +91 -0
  49. data/lib/thrift/server/thread_pool_server.rb +75 -0
  50. data/lib/thrift/server/threaded_server.rb +47 -0
  51. data/lib/thrift/struct.rb +237 -0
  52. data/lib/thrift/struct_union.rb +192 -0
  53. data/lib/thrift/thrift_native.rb +24 -0
  54. data/lib/thrift/transport/base_server_transport.rb +37 -0
  55. data/lib/thrift/transport/base_transport.rb +109 -0
  56. data/lib/thrift/transport/buffered_transport.rb +114 -0
  57. data/lib/thrift/transport/framed_transport.rb +117 -0
  58. data/lib/thrift/transport/http_client_transport.rb +56 -0
  59. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  60. data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
  61. data/lib/thrift/transport/server_socket.rb +63 -0
  62. data/lib/thrift/transport/socket.rb +139 -0
  63. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  64. data/lib/thrift/transport/unix_socket.rb +40 -0
  65. data/lib/thrift/types.rb +101 -0
  66. data/lib/thrift/union.rb +179 -0
  67. data/spec/Referenced.thrift +44 -0
  68. data/spec/ThriftNamespacedSpec.thrift +53 -0
  69. data/spec/ThriftSpec.thrift +183 -0
  70. data/spec/base_protocol_spec.rb +217 -0
  71. data/spec/base_transport_spec.rb +350 -0
  72. data/spec/binary_protocol_accelerated_spec.rb +42 -0
  73. data/spec/binary_protocol_spec.rb +66 -0
  74. data/spec/binary_protocol_spec_shared.rb +455 -0
  75. data/spec/bytes_spec.rb +160 -0
  76. data/spec/client_spec.rb +99 -0
  77. data/spec/compact_protocol_spec.rb +143 -0
  78. data/spec/exception_spec.rb +141 -0
  79. data/spec/gen-rb/namespaced_spec_namespace/namespaced_nonblocking_service.rb +272 -0
  80. data/spec/gen-rb/namespaced_spec_namespace/thrift_namespaced_spec_constants.rb +11 -0
  81. data/spec/gen-rb/namespaced_spec_namespace/thrift_namespaced_spec_types.rb +28 -0
  82. data/spec/gen-rb/nonblocking_service.rb +272 -0
  83. data/spec/gen-rb/other_namespace/referenced_constants.rb +11 -0
  84. data/spec/gen-rb/other_namespace/referenced_types.rb +17 -0
  85. data/spec/gen-rb/thrift_spec_constants.rb +11 -0
  86. data/spec/gen-rb/thrift_spec_types.rb +538 -0
  87. data/spec/http_client_spec.rb +120 -0
  88. data/spec/json_protocol_spec.rb +513 -0
  89. data/spec/namespaced_spec.rb +62 -0
  90. data/spec/nonblocking_server_spec.rb +263 -0
  91. data/spec/processor_spec.rb +80 -0
  92. data/spec/serializer_spec.rb +67 -0
  93. data/spec/server_socket_spec.rb +79 -0
  94. data/spec/server_spec.rb +147 -0
  95. data/spec/socket_spec.rb +61 -0
  96. data/spec/socket_spec_shared.rb +104 -0
  97. data/spec/spec_helper.rb +61 -0
  98. data/spec/struct_nested_containers_spec.rb +191 -0
  99. data/spec/struct_spec.rb +293 -0
  100. data/spec/thin_http_server_spec.rb +141 -0
  101. data/spec/types_spec.rb +115 -0
  102. data/spec/union_spec.rb +203 -0
  103. data/spec/unix_socket_spec.rb +107 -0
  104. data/test/debug_proto/gen-rb/debug_proto_test_constants.rb +274 -0
  105. data/test/debug_proto/gen-rb/debug_proto_test_types.rb +761 -0
  106. data/test/debug_proto/gen-rb/empty_service.rb +24 -0
  107. data/test/debug_proto/gen-rb/inherited.rb +79 -0
  108. data/test/debug_proto/gen-rb/reverse_order_service.rb +82 -0
  109. data/test/debug_proto/gen-rb/service_for_exception_with_a_map.rb +81 -0
  110. data/test/debug_proto/gen-rb/srv.rb +330 -0
  111. metadata +369 -0
data/ext/struct.h ADDED
@@ -0,0 +1,25 @@
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
+
21
+ #include <stdbool.h>
22
+ #include <ruby.h>
23
+
24
+ void Init_struct();
25
+ void Init_union();
@@ -0,0 +1,201 @@
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
+ #include <ruby.h>
21
+ #include <bytes.h>
22
+ #include <struct.h>
23
+ #include <binary_protocol_accelerated.h>
24
+ #include <compact_protocol.h>
25
+ #include <memory_buffer.h>
26
+
27
+ // cached classes/modules
28
+ VALUE rb_cSet;
29
+ VALUE thrift_module;
30
+ VALUE thrift_bytes_module;
31
+ VALUE thrift_types_module;
32
+
33
+ // TType constants
34
+ int TTYPE_STOP;
35
+ int TTYPE_BOOL;
36
+ int TTYPE_BYTE;
37
+ int TTYPE_I16;
38
+ int TTYPE_I32;
39
+ int TTYPE_I64;
40
+ int TTYPE_DOUBLE;
41
+ int TTYPE_STRING;
42
+ int TTYPE_MAP;
43
+ int TTYPE_SET;
44
+ int TTYPE_LIST;
45
+ int TTYPE_STRUCT;
46
+
47
+ // method ids
48
+ ID validate_method_id;
49
+ ID write_struct_begin_method_id;
50
+ ID write_struct_end_method_id;
51
+ ID write_field_begin_method_id;
52
+ ID write_field_end_method_id;
53
+ ID write_boolean_method_id;
54
+ ID write_byte_method_id;
55
+ ID write_i16_method_id;
56
+ ID write_i32_method_id;
57
+ ID write_i64_method_id;
58
+ ID write_double_method_id;
59
+ ID write_string_method_id;
60
+ ID write_binary_method_id;
61
+ ID write_map_begin_method_id;
62
+ ID write_map_end_method_id;
63
+ ID write_list_begin_method_id;
64
+ ID write_list_end_method_id;
65
+ ID write_set_begin_method_id;
66
+ ID write_set_end_method_id;
67
+ ID read_bool_method_id;
68
+ ID read_byte_method_id;
69
+ ID read_i16_method_id;
70
+ ID read_i32_method_id;
71
+ ID read_i64_method_id;
72
+ ID read_string_method_id;
73
+ ID read_binary_method_id;
74
+ ID read_double_method_id;
75
+ ID read_map_begin_method_id;
76
+ ID read_map_end_method_id;
77
+ ID read_list_begin_method_id;
78
+ ID read_list_end_method_id;
79
+ ID read_set_begin_method_id;
80
+ ID read_set_end_method_id;
81
+ ID read_struct_begin_method_id;
82
+ ID read_struct_end_method_id;
83
+ ID read_field_begin_method_id;
84
+ ID read_field_end_method_id;
85
+ ID keys_method_id;
86
+ ID entries_method_id;
87
+ ID write_field_stop_method_id;
88
+ ID skip_method_id;
89
+ ID write_method_id;
90
+ ID read_all_method_id;
91
+ ID read_into_buffer_method_id;
92
+ ID force_binary_encoding_id;
93
+ ID convert_to_utf8_byte_buffer_id;
94
+ ID convert_to_string_id;
95
+
96
+ // constant ids
97
+ ID fields_const_id;
98
+ ID transport_ivar_id;
99
+ ID strict_read_ivar_id;
100
+ ID strict_write_ivar_id;
101
+
102
+ // cached symbols
103
+ VALUE type_sym;
104
+ VALUE name_sym;
105
+ VALUE key_sym;
106
+ VALUE value_sym;
107
+ VALUE element_sym;
108
+ VALUE class_sym;
109
+ VALUE binary_sym;
110
+ VALUE protocol_exception_class;
111
+
112
+ void Init_thrift_native() {
113
+ // cached classes
114
+ thrift_module = rb_const_get(rb_cObject, rb_intern("Thrift"));
115
+ thrift_bytes_module = rb_const_get(thrift_module, rb_intern("Bytes"));
116
+ thrift_types_module = rb_const_get(thrift_module, rb_intern("Types"));
117
+ rb_cSet = rb_const_get(rb_cObject, rb_intern("Set"));
118
+ protocol_exception_class = rb_const_get(thrift_module, rb_intern("ProtocolException"));
119
+
120
+ // Init ttype constants
121
+ TTYPE_BOOL = FIX2INT(rb_const_get(thrift_types_module, rb_intern("BOOL")));
122
+ TTYPE_BYTE = FIX2INT(rb_const_get(thrift_types_module, rb_intern("BYTE")));
123
+ TTYPE_I16 = FIX2INT(rb_const_get(thrift_types_module, rb_intern("I16")));
124
+ TTYPE_I32 = FIX2INT(rb_const_get(thrift_types_module, rb_intern("I32")));
125
+ TTYPE_I64 = FIX2INT(rb_const_get(thrift_types_module, rb_intern("I64")));
126
+ TTYPE_DOUBLE = FIX2INT(rb_const_get(thrift_types_module, rb_intern("DOUBLE")));
127
+ TTYPE_STRING = FIX2INT(rb_const_get(thrift_types_module, rb_intern("STRING")));
128
+ TTYPE_MAP = FIX2INT(rb_const_get(thrift_types_module, rb_intern("MAP")));
129
+ TTYPE_SET = FIX2INT(rb_const_get(thrift_types_module, rb_intern("SET")));
130
+ TTYPE_LIST = FIX2INT(rb_const_get(thrift_types_module, rb_intern("LIST")));
131
+ TTYPE_STRUCT = FIX2INT(rb_const_get(thrift_types_module, rb_intern("STRUCT")));
132
+
133
+ // method ids
134
+ validate_method_id = rb_intern("validate");
135
+ write_struct_begin_method_id = rb_intern("write_struct_begin");
136
+ write_struct_end_method_id = rb_intern("write_struct_end");
137
+ write_field_begin_method_id = rb_intern("write_field_begin");
138
+ write_field_end_method_id = rb_intern("write_field_end");
139
+ write_boolean_method_id = rb_intern("write_bool");
140
+ write_byte_method_id = rb_intern("write_byte");
141
+ write_i16_method_id = rb_intern("write_i16");
142
+ write_i32_method_id = rb_intern("write_i32");
143
+ write_i64_method_id = rb_intern("write_i64");
144
+ write_double_method_id = rb_intern("write_double");
145
+ write_string_method_id = rb_intern("write_string");
146
+ write_binary_method_id = rb_intern("write_binary");
147
+ write_map_begin_method_id = rb_intern("write_map_begin");
148
+ write_map_end_method_id = rb_intern("write_map_end");
149
+ write_list_begin_method_id = rb_intern("write_list_begin");
150
+ write_list_end_method_id = rb_intern("write_list_end");
151
+ write_set_begin_method_id = rb_intern("write_set_begin");
152
+ write_set_end_method_id = rb_intern("write_set_end");
153
+ read_bool_method_id = rb_intern("read_bool");
154
+ read_byte_method_id = rb_intern("read_byte");
155
+ read_i16_method_id = rb_intern("read_i16");
156
+ read_i32_method_id = rb_intern("read_i32");
157
+ read_i64_method_id = rb_intern("read_i64");
158
+ read_string_method_id = rb_intern("read_string");
159
+ read_binary_method_id = rb_intern("read_binary");
160
+ read_double_method_id = rb_intern("read_double");
161
+ read_map_begin_method_id = rb_intern("read_map_begin");
162
+ read_map_end_method_id = rb_intern("read_map_end");
163
+ read_list_begin_method_id = rb_intern("read_list_begin");
164
+ read_list_end_method_id = rb_intern("read_list_end");
165
+ read_set_begin_method_id = rb_intern("read_set_begin");
166
+ read_set_end_method_id = rb_intern("read_set_end");
167
+ read_struct_begin_method_id = rb_intern("read_struct_begin");
168
+ read_struct_end_method_id = rb_intern("read_struct_end");
169
+ read_field_begin_method_id = rb_intern("read_field_begin");
170
+ read_field_end_method_id = rb_intern("read_field_end");
171
+ keys_method_id = rb_intern("keys");
172
+ entries_method_id = rb_intern("entries");
173
+ write_field_stop_method_id = rb_intern("write_field_stop");
174
+ skip_method_id = rb_intern("skip");
175
+ write_method_id = rb_intern("write");
176
+ read_all_method_id = rb_intern("read_all");
177
+ read_into_buffer_method_id = rb_intern("read_into_buffer");
178
+ force_binary_encoding_id = rb_intern("force_binary_encoding");
179
+ convert_to_utf8_byte_buffer_id = rb_intern("convert_to_utf8_byte_buffer");
180
+ convert_to_string_id = rb_intern("convert_to_string");
181
+
182
+ // constant ids
183
+ fields_const_id = rb_intern("FIELDS");
184
+ transport_ivar_id = rb_intern("@trans");
185
+ strict_read_ivar_id = rb_intern("@strict_read");
186
+ strict_write_ivar_id = rb_intern("@strict_write");
187
+
188
+ // cached symbols
189
+ type_sym = ID2SYM(rb_intern("type"));
190
+ name_sym = ID2SYM(rb_intern("name"));
191
+ key_sym = ID2SYM(rb_intern("key"));
192
+ value_sym = ID2SYM(rb_intern("value"));
193
+ element_sym = ID2SYM(rb_intern("element"));
194
+ class_sym = ID2SYM(rb_intern("class"));
195
+ binary_sym = ID2SYM(rb_intern("binary"));
196
+
197
+ Init_struct();
198
+ Init_binary_protocol_accelerated();
199
+ Init_compact_protocol();
200
+ Init_memory_buffer();
201
+ }
data/lib/thrift.rb ADDED
@@ -0,0 +1,67 @@
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
+ # Contains some contributions under the Thrift Software License.
20
+ # Please see doc/old-thrift-license.txt in the Thrift distribution for
21
+ # details.
22
+
23
+ $:.unshift File.dirname(__FILE__)
24
+
25
+ require 'thrift/bytes'
26
+ require 'thrift/core_ext'
27
+ require 'thrift/exceptions'
28
+ require 'thrift/types'
29
+ require 'thrift/processor'
30
+ require 'thrift/client'
31
+ require 'thrift/struct'
32
+ require 'thrift/union'
33
+ require 'thrift/struct_union'
34
+
35
+ # serializer
36
+ require 'thrift/serializer/serializer'
37
+ require 'thrift/serializer/deserializer'
38
+
39
+ # protocol
40
+ require 'thrift/protocol/base_protocol'
41
+ require 'thrift/protocol/binary_protocol'
42
+ require 'thrift/protocol/binary_protocol_accelerated'
43
+ require 'thrift/protocol/compact_protocol'
44
+ require 'thrift/protocol/json_protocol'
45
+ require 'thrift/protocol/multiplexed_protocol'
46
+
47
+ # transport
48
+ require 'thrift/transport/base_transport'
49
+ require 'thrift/transport/base_server_transport'
50
+ require 'thrift/transport/socket'
51
+ require 'thrift/transport/server_socket'
52
+ require 'thrift/transport/unix_socket'
53
+ require 'thrift/transport/unix_server_socket'
54
+ require 'thrift/transport/buffered_transport'
55
+ require 'thrift/transport/framed_transport'
56
+ require 'thrift/transport/http_client_transport'
57
+ require 'thrift/transport/io_stream_transport'
58
+ require 'thrift/transport/memory_buffer_transport'
59
+
60
+ # server
61
+ require 'thrift/server/base_server'
62
+ require 'thrift/server/nonblocking_server'
63
+ require 'thrift/server/simple_server'
64
+ require 'thrift/server/threaded_server'
65
+ require 'thrift/server/thread_pool_server'
66
+
67
+ require 'thrift/thrift_native'
@@ -0,0 +1,131 @@
1
+ # encoding: ascii-8bit
2
+ #
3
+ # Licensed to the Apache Software Foundation (ASF) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The ASF licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+ #
20
+
21
+ module Thrift
22
+ # A collection of utilities for working with bytes and byte buffers.
23
+ module Bytes
24
+ if RUBY_VERSION >= '1.9'
25
+ # Creates and empty byte buffer (String with BINARY encoding)
26
+ #
27
+ # size - The Integer size of the buffer (default: nil) to create
28
+ #
29
+ # Returns a String with BINARY encoding, filled with null characters
30
+ # if size is greater than zero
31
+ def self.empty_byte_buffer(size = nil)
32
+ if (size && size > 0)
33
+ "\0".force_encoding(Encoding::BINARY) * size
34
+ else
35
+ ''.force_encoding(Encoding::BINARY)
36
+ end
37
+ end
38
+
39
+ # Forces the encoding of the buffer to BINARY. If the buffer
40
+ # passed is frozen, then it will be duplicated.
41
+ #
42
+ # buffer - The String to force the encoding of.
43
+ #
44
+ # Returns the String passed with an encoding of BINARY; returned
45
+ # String may be a duplicate.
46
+ def self.force_binary_encoding(buffer)
47
+ buffer = buffer.dup if buffer.frozen?
48
+ buffer.force_encoding(Encoding::BINARY)
49
+ end
50
+
51
+ # Gets the byte value of a given position in a String.
52
+ #
53
+ # string - The String to retrive the byte value from.
54
+ # index - The Integer location of the byte value to retrieve.
55
+ #
56
+ # Returns an Integer value between 0 and 255.
57
+ def self.get_string_byte(string, index)
58
+ string.getbyte(index)
59
+ end
60
+
61
+ # Sets the byte value given to a given index in a String.
62
+ #
63
+ # string - The String to set the byte value in.
64
+ # index - The Integer location to set the byte value at.
65
+ # byte - The Integer value (0 to 255) to set in the string.
66
+ #
67
+ # Returns an Integer value of the byte value to set.
68
+ def self.set_string_byte(string, index, byte)
69
+ string.setbyte(index, byte)
70
+ end
71
+
72
+ # Converts the given String to a UTF-8 byte buffer.
73
+ #
74
+ # string - The String to convert.
75
+ #
76
+ # Returns a new String with BINARY encoding, containing the UTF-8
77
+ # bytes of the original string.
78
+ def self.convert_to_utf8_byte_buffer(string)
79
+ if string.encoding != Encoding::UTF_8
80
+ # transcode to UTF-8
81
+ string = string.encode(Encoding::UTF_8)
82
+ else
83
+ # encoding is already UTF-8, but a duplicate is needed
84
+ string = string.dup
85
+ end
86
+ string.force_encoding(Encoding::BINARY)
87
+ end
88
+
89
+ # Converts the given UTF-8 byte buffer into a String
90
+ #
91
+ # utf8_buffer - A String, with BINARY encoding, containing UTF-8 bytes
92
+ #
93
+ # Returns a new String with UTF-8 encoding,
94
+ def self.convert_to_string(utf8_buffer)
95
+ # duplicate the buffer, force encoding to UTF-8
96
+ utf8_buffer.dup.force_encoding(Encoding::UTF_8)
97
+ end
98
+ else
99
+ def self.empty_byte_buffer(size = nil)
100
+ if (size && size > 0)
101
+ "\0" * size
102
+ else
103
+ ''
104
+ end
105
+ end
106
+
107
+ def self.force_binary_encoding(buffer)
108
+ buffer
109
+ end
110
+
111
+ def self.get_string_byte(string, index)
112
+ string[index]
113
+ end
114
+
115
+ def self.set_string_byte(string, index, byte)
116
+ string[index] = byte
117
+ end
118
+
119
+ def self.convert_to_utf8_byte_buffer(string)
120
+ # This assumes $KCODE is 'UTF8'/'U', which would mean the String is already a UTF-8 byte buffer
121
+ # TODO consider handling other $KCODE values and transcoding with iconv
122
+ string
123
+ end
124
+
125
+ def self.convert_to_string(utf8_buffer)
126
+ # See comment in 'convert_to_utf8_byte_buffer' for relevant assumptions.
127
+ utf8_buffer
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,71 @@
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 Client
22
+ def initialize(iprot, oprot=nil)
23
+ @iprot = iprot
24
+ @oprot = oprot || iprot
25
+ @seqid = 0
26
+ end
27
+
28
+ def send_message(name, args_class, args = {})
29
+ @oprot.write_message_begin(name, MessageTypes::CALL, @seqid)
30
+ send_message_args(args_class, args)
31
+ end
32
+
33
+ def send_oneway_message(name, args_class, args = {})
34
+ @oprot.write_message_begin(name, MessageTypes::ONEWAY, @seqid)
35
+ send_message_args(args_class, args)
36
+ end
37
+
38
+ def send_message_args(args_class, args)
39
+ data = args_class.new
40
+ args.each do |k, v|
41
+ data.send("#{k.to_s}=", v)
42
+ end
43
+ begin
44
+ data.write(@oprot)
45
+ rescue StandardError => e
46
+ @oprot.trans.close
47
+ raise e
48
+ end
49
+ @oprot.write_message_end
50
+ @oprot.trans.flush
51
+ end
52
+
53
+ def receive_message(result_klass)
54
+ fname, mtype, rseqid = @iprot.read_message_begin
55
+ handle_exception(mtype)
56
+ result = result_klass.new
57
+ result.read(@iprot)
58
+ @iprot.read_message_end
59
+ result
60
+ end
61
+
62
+ def handle_exception(mtype)
63
+ if mtype == MessageTypes::EXCEPTION
64
+ x = ApplicationException.new
65
+ x.read(@iprot)
66
+ @iprot.read_message_end
67
+ raise x
68
+ end
69
+ end
70
+ end
71
+ end