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,43 @@
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 SimpleServer < BaseServer
22
+ def serve
23
+ begin
24
+ @server_transport.listen
25
+ loop do
26
+ client = @server_transport.accept
27
+ trans = @transport_factory.get_transport(client)
28
+ prot = @protocol_factory.get_protocol(trans)
29
+ begin
30
+ loop do
31
+ @processor.process(prot, prot)
32
+ end
33
+ rescue Thrift::TransportException, Thrift::ProtocolException
34
+ ensure
35
+ trans.close
36
+ end
37
+ end
38
+ ensure
39
+ @server_transport.close
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,75 @@
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
+ require 'thread'
21
+
22
+ module Thrift
23
+ class ThreadPoolServer < BaseServer
24
+ def initialize(processor, server_transport, transport_factory=nil, protocol_factory=nil, num=20)
25
+ super(processor, server_transport, transport_factory, protocol_factory)
26
+ @thread_q = SizedQueue.new(num)
27
+ @exception_q = Queue.new
28
+ @running = false
29
+ end
30
+
31
+ ## exceptions that happen in worker threads will be relayed here and
32
+ ## must be caught. 'retry' can be used to continue. (threads will
33
+ ## continue to run while the exception is being handled.)
34
+ def rescuable_serve
35
+ Thread.new { serve } unless @running
36
+ @running = true
37
+ raise @exception_q.pop
38
+ end
39
+
40
+ ## exceptions that happen in worker threads simply cause that thread
41
+ ## to die and another to be spawned in its place.
42
+ def serve
43
+ @server_transport.listen
44
+
45
+ begin
46
+ loop do
47
+ @thread_q.push(:token)
48
+ Thread.new do
49
+ begin
50
+ loop do
51
+ client = @server_transport.accept
52
+ trans = @transport_factory.get_transport(client)
53
+ prot = @protocol_factory.get_protocol(trans)
54
+ begin
55
+ loop do
56
+ @processor.process(prot, prot)
57
+ end
58
+ rescue Thrift::TransportException, Thrift::ProtocolException => e
59
+ ensure
60
+ trans.close
61
+ end
62
+ end
63
+ rescue => e
64
+ @exception_q.push(e)
65
+ ensure
66
+ @thread_q.pop # thread died!
67
+ end
68
+ end
69
+ end
70
+ ensure
71
+ @server_transport.close
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,47 @@
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
+ require 'thread'
21
+
22
+ module Thrift
23
+ class ThreadedServer < BaseServer
24
+ def serve
25
+ begin
26
+ @server_transport.listen
27
+ loop do
28
+ client = @server_transport.accept
29
+ trans = @transport_factory.get_transport(client)
30
+ prot = @protocol_factory.get_protocol(trans)
31
+ Thread.new(prot, trans) do |p, t|
32
+ begin
33
+ loop do
34
+ @processor.process(p, p)
35
+ end
36
+ rescue Thrift::TransportException, Thrift::ProtocolException
37
+ ensure
38
+ t.close
39
+ end
40
+ end
41
+ end
42
+ ensure
43
+ @server_transport.close
44
+ end
45
+ end
46
+ end
47
+ end
data/lib/thrift/struct.rb CHANGED
@@ -1,4 +1,22 @@
1
- require 'thrift/types'
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 'set'
3
21
 
4
22
  module Thrift
@@ -39,7 +57,7 @@ module Thrift
39
57
  unless fields_with_default_values
40
58
  fields_with_default_values = {}
41
59
  struct_fields.each do |fid, field_def|
42
- if field_def[:default]
60
+ unless field_def[:default].nil?
43
61
  fields_with_default_values[field_def[:name]] = field_def[:default]
44
62
  end
45
63
  end
@@ -60,23 +78,19 @@ module Thrift
60
78
  names_to_ids[name]
61
79
  end
62
80
 
63
- # Obsoleted by THRIFT-246, which generates this method inline
64
- # TODO: Should be removed at some point. -- Kevin Clark
65
- def struct_fields
66
- self.class.const_get(:FIELDS)
67
- end
68
-
69
81
  def each_field
70
- struct_fields.each do |fid, data|
71
- yield fid, data[:type], data[:name], data[:default], data[:optional]
82
+ struct_fields.keys.sort.each do |fid|
83
+ data = struct_fields[fid]
84
+ yield fid, data
72
85
  end
73
86
  end
74
87
 
75
88
  def inspect(skip_optional_nulls = true)
76
89
  fields = []
77
- each_field do |fid, type, name, default, optional|
90
+ each_field do |fid, field_info|
91
+ name = field_info[:name]
78
92
  value = instance_variable_get("@#{name}")
79
- unless skip_optional_nulls && optional && value.nil?
93
+ unless skip_optional_nulls && field_info[:optional] && value.nil?
80
94
  fields << "#{name}:#{value.inspect}"
81
95
  end
82
96
  end
@@ -84,48 +98,40 @@ module Thrift
84
98
  end
85
99
 
86
100
  def read(iprot)
87
- # TODO(kevinclark): Make sure transport is C readable
88
- if iprot.respond_to?(:decode_binary)
89
- iprot.decode_binary(self, iprot.trans)
90
- else
91
- iprot.read_struct_begin
92
- loop do
93
- fname, ftype, fid = iprot.read_field_begin
94
- break if (ftype == Types::STOP)
95
- handle_message(iprot, fid, ftype)
96
- iprot.read_field_end
97
- end
98
- iprot.read_struct_end
101
+ iprot.read_struct_begin
102
+ loop do
103
+ fname, ftype, fid = iprot.read_field_begin
104
+ break if (ftype == Types::STOP)
105
+ handle_message(iprot, fid, ftype)
106
+ iprot.read_field_end
99
107
  end
108
+ iprot.read_struct_end
100
109
  validate
101
110
  end
102
111
 
103
112
  def write(oprot)
104
113
  validate
105
- # if oprot.respond_to?(:encode_binary)
106
- # # TODO(kevinclark): Clean this so I don't have to access the transport.
107
- # oprot.trans.write oprot.encode_binary(self)
108
- # else
109
- oprot.write_struct_begin(self.class.name)
110
- each_field do |fid, type, name|
111
- unless (value = instance_variable_get("@#{name}")).nil?
112
- if is_container? type
113
- oprot.write_field_begin(name, type, fid)
114
- write_container(oprot, value, struct_fields[fid])
115
- oprot.write_field_end
116
- else
117
- oprot.write_field(name, type, fid, value)
118
- end
114
+ oprot.write_struct_begin(self.class.name)
115
+ each_field do |fid, field_info|
116
+ name = field_info[:name]
117
+ type = field_info[:type]
118
+ if (value = instance_variable_get("@#{name}"))
119
+ if is_container? type
120
+ oprot.write_field_begin(name, type, fid)
121
+ write_container(oprot, value, field_info)
122
+ oprot.write_field_end
123
+ else
124
+ oprot.write_field(name, type, fid, value)
119
125
  end
120
126
  end
121
- oprot.write_field_stop
122
- oprot.write_struct_end
123
- # end
127
+ end
128
+ oprot.write_field_stop
129
+ oprot.write_struct_end
124
130
  end
125
131
 
126
132
  def ==(other)
127
- return false unless other.is_a?(self.class)
128
- each_field do |fid, type, name, default|
133
+ each_field do |fid, field_info|
134
+ name = field_info[:name]
129
135
  return false unless self.instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}")
130
136
  end
131
137
  true
@@ -135,9 +141,26 @@ module Thrift
135
141
  self.class == other.class && self == other
136
142
  end
137
143
 
138
- # for the time being, we're ok with a naive hash. this could definitely be improved upon.
139
144
  def hash
140
- 0
145
+ field_values = []
146
+ each_field do |fid, field_info|
147
+ name = field_info[:name]
148
+ field_values << self.instance_variable_get("@#{name}")
149
+ end
150
+ field_values.hash
151
+ end
152
+
153
+ def differences(other)
154
+ diffs = []
155
+ unless other.is_a?(self.class)
156
+ diffs << "Different class!"
157
+ else
158
+ each_field do |fid, field_info|
159
+ name = field_info[:name]
160
+ diffs << "#{name} differs!" unless self.instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}")
161
+ end
162
+ end
163
+ diffs
141
164
  end
142
165
 
143
166
  def self.field_accessor(klass, *fields)
@@ -256,8 +279,12 @@ module Thrift
256
279
  end
257
280
  end
258
281
 
282
+ CONTAINER_TYPES = []
283
+ CONTAINER_TYPES[Types::LIST] = true
284
+ CONTAINER_TYPES[Types::MAP] = true
285
+ CONTAINER_TYPES[Types::SET] = true
259
286
  def is_container?(type)
260
- [Types::LIST, Types::MAP, Types::SET].include? type
287
+ CONTAINER_TYPES[type]
261
288
  end
262
289
 
263
290
  def field_info(field)
@@ -268,5 +295,4 @@ module Thrift
268
295
  :element => field[:element] }
269
296
  end
270
297
  end
271
- deprecate_module! :ThriftStruct => Struct
272
298
  end
@@ -0,0 +1,24 @@
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
+ begin
21
+ require "thrift_native"
22
+ rescue LoadError
23
+ puts "Unable to load thrift_native extension. Defaulting to pure Ruby libraries."
24
+ end
@@ -0,0 +1,37 @@
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
+ class BaseServerTransport
23
+ def listen
24
+ raise NotImplementedError
25
+ end
26
+
27
+ def accept
28
+ raise NotImplementedError
29
+ end
30
+
31
+ def close; nil; end
32
+
33
+ def closed?
34
+ raise NotImplementedError
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,70 @@
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
+ class TransportException < Exception
23
+ UNKNOWN = 0
24
+ NOT_OPEN = 1
25
+ ALREADY_OPEN = 2
26
+ TIMED_OUT = 3
27
+ END_OF_FILE = 4
28
+
29
+ attr_reader :type
30
+
31
+ def initialize(type=UNKNOWN, message=nil)
32
+ super(message)
33
+ @type = type
34
+ end
35
+ end
36
+
37
+ class BaseTransport
38
+ def open?; end
39
+
40
+ def open; end
41
+
42
+ def close; end
43
+
44
+ def read(sz)
45
+ raise NotImplementedError
46
+ end
47
+
48
+ def read_all(size)
49
+ buf = ''
50
+
51
+ while (buf.length < size)
52
+ chunk = read(size - buf.length)
53
+ buf << chunk
54
+ end
55
+
56
+ buf
57
+ end
58
+
59
+ def write(buf); end
60
+ alias_method :<<, :write
61
+
62
+ def flush; end
63
+ end
64
+
65
+ class BaseTransportFactory
66
+ def get_transport(trans)
67
+ return trans
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,77 @@
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
+ class BufferedTransport < BaseTransport
23
+ DEFAULT_BUFFER = 4096
24
+
25
+ def initialize(transport)
26
+ @transport = transport
27
+ @wbuf = ''
28
+ @rbuf = ''
29
+ @index = 0
30
+ end
31
+
32
+ def open?
33
+ return @transport.open?
34
+ end
35
+
36
+ def open
37
+ @transport.open
38
+ end
39
+
40
+ def close
41
+ flush
42
+ @transport.close
43
+ end
44
+
45
+ def read(sz)
46
+ @index += sz
47
+ ret = @rbuf.slice(@index - sz, sz) || ''
48
+
49
+ if ret.length == 0
50
+ @rbuf = @transport.read([sz, DEFAULT_BUFFER].max)
51
+ @index = sz
52
+ ret = @rbuf.slice(0, sz) || ''
53
+ end
54
+
55
+ ret
56
+ end
57
+
58
+ def write(buf)
59
+ @wbuf << buf
60
+ end
61
+
62
+ def flush
63
+ if @wbuf != ''
64
+ @transport.write(@wbuf)
65
+ @wbuf = ''
66
+ end
67
+
68
+ @transport.flush
69
+ end
70
+ end
71
+
72
+ class BufferedTransportFactory < BaseTransportFactory
73
+ def get_transport(transport)
74
+ return BufferedTransport.new(transport)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,90 @@
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
+ class FramedTransport < BaseTransport
23
+ def initialize(transport, read=true, write=true)
24
+ @transport = transport
25
+ @rbuf = ''
26
+ @wbuf = ''
27
+ @read = read
28
+ @write = write
29
+ @index = 0
30
+ end
31
+
32
+ def open?
33
+ @transport.open?
34
+ end
35
+
36
+ def open
37
+ @transport.open
38
+ end
39
+
40
+ def close
41
+ @transport.close
42
+ end
43
+
44
+ def read(sz)
45
+ return @transport.read(sz) unless @read
46
+
47
+ return '' if sz <= 0
48
+
49
+ read_frame if @index >= @rbuf.length
50
+
51
+ @index += sz
52
+ @rbuf.slice(@index - sz, sz) || ''
53
+ end
54
+
55
+ def write(buf,sz=nil)
56
+ return @transport.write(buf) unless @write
57
+
58
+ @wbuf << (sz ? buf[0...sz] : buf)
59
+ end
60
+
61
+ #
62
+ # Writes the output buffer to the stream in the format of a 4-byte length
63
+ # followed by the actual data.
64
+ #
65
+ def flush
66
+ return @transport.flush unless @write
67
+
68
+ out = [@wbuf.length].pack('N')
69
+ out << @wbuf
70
+ @transport.write(out)
71
+ @transport.flush
72
+ @wbuf = ''
73
+ end
74
+
75
+ private
76
+
77
+ def read_frame
78
+ sz = @transport.read_all(4).unpack('N').first
79
+
80
+ @index = 0
81
+ @rbuf = @transport.read_all(sz)
82
+ end
83
+ end
84
+
85
+ class FramedTransportFactory < BaseTransportFactory
86
+ def get_transport(transport)
87
+ return FramedTransport.new(transport)
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,45 @@
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 'net/http'
22
+ require 'net/https'
23
+ require 'uri'
24
+ require 'stringio'
25
+
26
+ module Thrift
27
+ class HTTPClientTransport < BaseTransport
28
+ def initialize(url)
29
+ @url = URI url
30
+ @outbuf = ""
31
+ end
32
+
33
+ def open?; true end
34
+ def read(sz); @inbuf.read sz end
35
+ def write(buf); @outbuf << buf end
36
+ def flush
37
+ http = Net::HTTP.new @url.host, @url.port
38
+ http.use_ssl = @url.scheme == "https"
39
+ headers = { 'Content-Type' => 'application/x-thrift' }
40
+ resp, data = http.post(@url.path, @outbuf, headers)
41
+ @inbuf = StringIO.new data
42
+ @outbuf = ""
43
+ end
44
+ end
45
+ end