thrift 0.9.2.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/ext/binary_protocol_accelerated.c +12 -12
  3. data/ext/struct.c +5 -1
  4. data/lib/thrift.rb +8 -4
  5. data/lib/thrift/multiplexed_processor.rb +76 -0
  6. data/lib/thrift/processor.rb +24 -6
  7. data/lib/thrift/protocol/base_protocol.rb +11 -3
  8. data/lib/thrift/protocol/binary_protocol.rb +8 -1
  9. data/lib/thrift/protocol/binary_protocol_accelerated.rb +8 -0
  10. data/lib/thrift/protocol/compact_protocol.rb +8 -0
  11. data/lib/thrift/protocol/json_protocol.rb +21 -4
  12. data/lib/thrift/protocol/multiplexed_protocol.rb +44 -0
  13. data/lib/thrift/protocol/protocol_decorator.rb +194 -0
  14. data/lib/thrift/server/base_server.rb +8 -2
  15. data/lib/thrift/server/simple_server.rb +5 -1
  16. data/lib/thrift/server/thread_pool_server.rb +5 -1
  17. data/lib/thrift/server/threaded_server.rb +5 -1
  18. data/lib/thrift/transport/base_server_transport.rb +1 -1
  19. data/lib/thrift/transport/base_transport.rb +8 -0
  20. data/lib/thrift/transport/buffered_transport.rb +9 -1
  21. data/lib/thrift/transport/framed_transport.rb +9 -1
  22. data/lib/thrift/transport/http_client_transport.rb +7 -0
  23. data/lib/thrift/transport/io_stream_transport.rb +4 -1
  24. data/lib/thrift/transport/memory_buffer_transport.rb +4 -0
  25. data/lib/thrift/transport/server_socket.rb +6 -1
  26. data/lib/thrift/transport/socket.rb +21 -17
  27. data/lib/thrift/transport/ssl_server_socket.rb +41 -0
  28. data/lib/thrift/transport/ssl_socket.rb +51 -0
  29. data/lib/thrift/transport/unix_server_socket.rb +5 -1
  30. data/lib/thrift/transport/unix_socket.rb +5 -1
  31. data/lib/thrift/union.rb +3 -6
  32. data/spec/BaseService.thrift +27 -0
  33. data/spec/ExtendedService.thrift +25 -0
  34. data/spec/base_protocol_spec.rb +79 -71
  35. data/spec/base_transport_spec.rb +155 -117
  36. data/spec/binary_protocol_accelerated_spec.rb +6 -2
  37. data/spec/binary_protocol_spec.rb +16 -8
  38. data/spec/binary_protocol_spec_shared.rb +75 -72
  39. data/spec/bytes_spec.rb +38 -38
  40. data/spec/client_spec.rb +41 -42
  41. data/spec/compact_protocol_spec.rb +32 -17
  42. data/spec/exception_spec.rb +54 -54
  43. data/spec/flat_spec.rb +62 -0
  44. data/spec/http_client_spec.rb +74 -33
  45. data/spec/json_protocol_spec.rb +170 -131
  46. data/spec/namespaced_spec.rb +10 -5
  47. data/spec/nonblocking_server_spec.rb +16 -16
  48. data/spec/processor_spec.rb +26 -26
  49. data/spec/serializer_spec.rb +20 -20
  50. data/spec/server_socket_spec.rb +27 -22
  51. data/spec/server_spec.rb +91 -51
  52. data/spec/socket_spec.rb +23 -16
  53. data/spec/socket_spec_shared.rb +31 -31
  54. data/spec/spec_helper.rb +4 -1
  55. data/spec/ssl_server_socket_spec.rb +34 -0
  56. data/spec/ssl_socket_spec.rb +78 -0
  57. data/spec/struct_nested_containers_spec.rb +24 -24
  58. data/spec/struct_spec.rb +120 -120
  59. data/spec/thin_http_server_spec.rb +18 -18
  60. data/spec/types_spec.rb +56 -53
  61. data/spec/union_spec.rb +51 -40
  62. data/spec/unix_socket_spec.rb +43 -34
  63. metadata +191 -143
@@ -0,0 +1,194 @@
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
+ module Thrift
20
+ module ProtocolDecorator
21
+
22
+ def initialize(protocol)
23
+ @protocol = protocol
24
+ end
25
+
26
+ def trans
27
+ @protocol.trans
28
+ end
29
+
30
+ def write_message_begin(name, type, seqid)
31
+ @protocol.write_message_begin
32
+ end
33
+
34
+ def write_message_end
35
+ @protocol.write_message_end
36
+ end
37
+
38
+ def write_struct_begin(name)
39
+ @protocol.write_struct_begin(name)
40
+ end
41
+
42
+ def write_struct_end
43
+ @protocol.write_struct_end
44
+ end
45
+
46
+ def write_field_begin(name, type, id)
47
+ @protocol.write_field_begin(name, type, id)
48
+ end
49
+
50
+ def write_field_end
51
+ @protocol.write_field_end
52
+ end
53
+
54
+ def write_field_stop
55
+ @protocol.write_field_stop
56
+ end
57
+
58
+ def write_map_begin(ktype, vtype, size)
59
+ @protocol.write_map_begin(ktype, vtype, size)
60
+ end
61
+
62
+ def write_map_end
63
+ @protocol.write_map_end
64
+ end
65
+
66
+ def write_list_begin(etype, size)
67
+ @protocol.write_list_begin(etype, size)
68
+ end
69
+
70
+ def write_list_end
71
+ @protocol.write_list_end
72
+ end
73
+
74
+ def write_set_begin(etype, size)
75
+ @protocol.write_set_begin(etype, size)
76
+ end
77
+
78
+ def write_set_end
79
+ @protocol.write_set_end
80
+ end
81
+
82
+ def write_bool(bool)
83
+ @protocol.write_bool(bool)
84
+ end
85
+
86
+ def write_byte(byte)
87
+ @protocol.write_byte(byte)
88
+ end
89
+
90
+ def write_i16(i16)
91
+ @protocol.write_i16(i16)
92
+ end
93
+
94
+ def write_i32(i32)
95
+ @protocol.write_i32(i32)
96
+ end
97
+
98
+ def write_i64(i64)
99
+ @protocol.write_i64(i64)
100
+ end
101
+
102
+ def write_double(dub)
103
+ @protocol.write_double(dub)
104
+ end
105
+
106
+ def write_string(str)
107
+ @protocol.write_string(str)
108
+ end
109
+
110
+ def write_binary(buf)
111
+ @protocol.write_binary(buf)
112
+ end
113
+
114
+ def read_message_begin
115
+ @protocol.read_message_begin
116
+ end
117
+
118
+ def read_message_end
119
+ @protocol.read_message_end
120
+ end
121
+
122
+ def read_struct_begin
123
+ @protocol.read_struct_begin
124
+ end
125
+
126
+ def read_struct_end
127
+ @protocol.read_struct_end
128
+ end
129
+
130
+ def read_field_begin
131
+ @protocol.read_field_begin
132
+ end
133
+
134
+ def read_field_end
135
+ @protocol.read_field_end
136
+ end
137
+
138
+ def read_map_begin
139
+ @protocol.read_map_begin
140
+ end
141
+
142
+ def read_map_end
143
+ @protocol.read_map_end
144
+ end
145
+
146
+ def read_list_begin
147
+ @protocol.read_list_begin
148
+ end
149
+
150
+ def read_list_end
151
+ @protocol.read_list_end
152
+ end
153
+
154
+ def read_set_begin
155
+ @protocol.read_set_begin
156
+ end
157
+
158
+ def read_set_end
159
+ @protocol.read_set_end
160
+ end
161
+
162
+ def read_bool
163
+ @protocol.read_bool
164
+ end
165
+
166
+ def read_byte
167
+ @protocol.read_byte
168
+ end
169
+
170
+ def read_i16
171
+ @protocol.read_i16
172
+ end
173
+
174
+ def read_i32
175
+ @protocol.read_i32
176
+ end
177
+
178
+ def read_i64
179
+ @protocol.read_i64
180
+ end
181
+
182
+ def read_double
183
+ @protocol.read_double
184
+ end
185
+
186
+ def read_string
187
+ @protocol.read_string
188
+ end
189
+
190
+ def read_binary
191
+ @protocol.read_binary
192
+ end
193
+ end
194
+ end
@@ -26,6 +26,12 @@ module Thrift
26
26
  @protocol_factory = protocol_factory ? protocol_factory : Thrift::BinaryProtocolFactory.new
27
27
  end
28
28
 
29
- def serve; nil; end
29
+ def serve
30
+ raise NotImplementedError
31
+ end
32
+
33
+ def to_s
34
+ "server(#{@protocol_factory.to_s}(#{@transport_factory.to_s}(#{@server_transport.to_s})))"
35
+ end
30
36
  end
31
- end
37
+ end
@@ -39,5 +39,9 @@ module Thrift
39
39
  @server_transport.close
40
40
  end
41
41
  end
42
+
43
+ def to_s
44
+ "simple(#{super.to_s})"
45
+ end
42
46
  end
43
- end
47
+ end
@@ -71,5 +71,9 @@ module Thrift
71
71
  @server_transport.close
72
72
  end
73
73
  end
74
+
75
+ def to_s
76
+ "threadpool(#{super.to_s})"
77
+ end
74
78
  end
75
- end
79
+ end
@@ -43,5 +43,9 @@ module Thrift
43
43
  @server_transport.close
44
44
  end
45
45
  end
46
+
47
+ def to_s
48
+ "threaded(#{super.to_s})"
49
+ end
46
50
  end
47
- end
51
+ end
@@ -34,4 +34,4 @@ module Thrift
34
34
  raise NotImplementedError
35
35
  end
36
36
  end
37
- end
37
+ end
@@ -99,11 +99,19 @@ module Thrift
99
99
  alias_method :<<, :write
100
100
 
101
101
  def flush; end
102
+
103
+ def to_s
104
+ "base"
105
+ end
102
106
  end
103
107
 
104
108
  class BaseTransportFactory
105
109
  def get_transport(trans)
106
110
  return trans
107
111
  end
112
+
113
+ def to_s
114
+ "base"
115
+ end
108
116
  end
109
117
  end
@@ -104,11 +104,19 @@ module Thrift
104
104
 
105
105
  @transport.flush
106
106
  end
107
+
108
+ def to_s
109
+ "buffered(#{@transport.to_s})"
110
+ end
107
111
  end
108
112
 
109
113
  class BufferedTransportFactory < BaseTransportFactory
110
114
  def get_transport(transport)
111
115
  return BufferedTransport.new(transport)
112
116
  end
117
+
118
+ def to_s
119
+ "buffered"
120
+ end
113
121
  end
114
- end
122
+ end
@@ -99,6 +99,10 @@ module Thrift
99
99
  @wbuf = Bytes.empty_byte_buffer
100
100
  end
101
101
 
102
+ def to_s
103
+ "framed(#{@transport.to_s})"
104
+ end
105
+
102
106
  private
103
107
 
104
108
  def read_frame
@@ -113,5 +117,9 @@ module Thrift
113
117
  def get_transport(transport)
114
118
  return FramedTransport.new(transport)
115
119
  end
120
+
121
+ def to_s
122
+ "framed"
123
+ end
116
124
  end
117
- end
125
+ end
@@ -47,10 +47,17 @@ module Thrift
47
47
  http.use_ssl = @url.scheme == 'https'
48
48
  http.verify_mode = @ssl_verify_mode if @url.scheme == 'https'
49
49
  resp = http.post(@url.request_uri, @outbuf, @headers)
50
+ raise TransportException.new(TransportException::UNKNOWN, "#{self.class.name} Could not connect to #{@url}, HTTP status code #{resp.code.to_i}") unless (200..299).include?(resp.code.to_i)
51
+
50
52
  data = resp.body
51
53
  data = Bytes.force_binary_encoding(data)
52
54
  @inbuf = StringIO.new data
55
+ ensure
53
56
  @outbuf = Bytes.empty_byte_buffer
54
57
  end
58
+
59
+ def to_s
60
+ "@{self.url}"
61
+ end
55
62
  end
56
63
  end
@@ -35,5 +35,8 @@ module Thrift
35
35
  def write(buf); @output.write(Bytes.force_binary_encoding(buf)) end
36
36
  def close; @input.close; @output.close end
37
37
  def to_io; @input end # we're assuming this is used in a IO.select for reading
38
+ def to_s
39
+ "iostream(input=#{@input.to_s},output=#{@output.to_s})"
40
+ end
38
41
  end
39
- end
42
+ end
@@ -121,5 +121,9 @@ module Thrift
121
121
  end
122
122
  out.join(" ")
123
123
  end
124
+
125
+ def to_s
126
+ "memory"
127
+ end
124
128
  end
125
129
  end
@@ -59,5 +59,10 @@ module Thrift
59
59
  end
60
60
 
61
61
  alias to_io handle
62
+
63
+ def to_s
64
+ "socket(#{@host}:#{@port})"
65
+ end
66
+
62
67
  end
63
- end
68
+ end
@@ -33,26 +33,28 @@ module Thrift
33
33
  attr_accessor :handle, :timeout
34
34
 
35
35
  def open
36
- begin
37
- addrinfo = ::Socket::getaddrinfo(@host, @port, nil, ::Socket::SOCK_STREAM).first
38
- @handle = ::Socket.new(addrinfo[4], ::Socket::SOCK_STREAM, 0)
39
- @handle.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1)
40
- sockaddr = ::Socket.sockaddr_in(addrinfo[1], addrinfo[3])
36
+ for addrinfo in ::Socket::getaddrinfo(@host, @port, nil, ::Socket::SOCK_STREAM) do
41
37
  begin
42
- @handle.connect_nonblock(sockaddr)
43
- rescue Errno::EINPROGRESS
44
- unless IO.select(nil, [ @handle ], nil, @timeout)
45
- raise TransportException.new(TransportException::NOT_OPEN, "Connection timeout to #{@desc}")
46
- end
38
+ socket = ::Socket.new(addrinfo[4], ::Socket::SOCK_STREAM, 0)
39
+ socket.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1)
40
+ sockaddr = ::Socket.sockaddr_in(addrinfo[1], addrinfo[3])
47
41
  begin
48
- @handle.connect_nonblock(sockaddr)
49
- rescue Errno::EISCONN
42
+ socket.connect_nonblock(sockaddr)
43
+ rescue Errno::EINPROGRESS
44
+ unless IO.select(nil, [ socket ], nil, @timeout)
45
+ next
46
+ end
47
+ begin
48
+ socket.connect_nonblock(sockaddr)
49
+ rescue Errno::EISCONN
50
+ end
50
51
  end
52
+ return @handle = socket
53
+ rescue StandardError => e
54
+ next
51
55
  end
52
- @handle
53
- rescue StandardError => e
54
- raise TransportException.new(TransportException::NOT_OPEN, "Could not connect to #{@desc}: #{e}")
55
56
  end
57
+ raise TransportException.new(TransportException::NOT_OPEN, "Could not connect to #{@desc}: #{e}")
56
58
  end
57
59
 
58
60
  def open?
@@ -132,8 +134,10 @@ module Thrift
132
134
  @handle = nil
133
135
  end
134
136
 
135
- def to_io
136
- @handle
137
+ alias to_io handle
138
+
139
+ def to_s
140
+ "socket(#{@host}:#{@port})"
137
141
  end
138
142
  end
139
143
  end
@@ -0,0 +1,41 @@
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
+ require 'socket'
22
+
23
+ module Thrift
24
+ class SSLServerSocket < ServerSocket
25
+ def initialize(host_or_port, port = nil, ssl_context = nil)
26
+ super(host_or_port, port)
27
+ @ssl_context = ssl_context
28
+ end
29
+
30
+ attr_accessor :ssl_context
31
+
32
+ def listen
33
+ socket = TCPServer.new(@host, @port)
34
+ @handle = OpenSSL::SSL::SSLServer.new(socket, @ssl_context)
35
+ end
36
+
37
+ def to_s
38
+ "ssl(#{super.to_s})"
39
+ end
40
+ end
41
+ end