thrift 0.0.751142
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/COPYING +14 -0
- data/LICENSE +14 -0
- data/Makefile.am +15 -0
- data/Manifest +78 -0
- data/README +30 -0
- data/Rakefile +102 -0
- data/benchmark/Benchmark.thrift +5 -0
- data/benchmark/benchmark.rb +254 -0
- data/benchmark/client.rb +56 -0
- data/benchmark/gen-rb/BenchmarkService.rb +81 -0
- data/benchmark/gen-rb/Benchmark_constants.rb +11 -0
- data/benchmark/gen-rb/Benchmark_types.rb +10 -0
- data/benchmark/server.rb +64 -0
- data/benchmark/thin_server.rb +26 -0
- data/ext/binary_protocol_accelerated.c +463 -0
- data/ext/binary_protocol_accelerated.h +1 -0
- data/ext/constants.h +77 -0
- data/ext/extconf.rb +7 -0
- data/ext/memory_buffer.c +52 -0
- data/ext/memory_buffer.h +1 -0
- data/ext/protocol.c +166 -0
- data/ext/protocol.h +1 -0
- data/ext/struct.c +574 -0
- data/ext/struct.h +48 -0
- data/ext/thrift_native.c +173 -0
- data/lib/thrift/client.rb +44 -0
- data/lib/thrift/deprecation.rb +155 -0
- data/lib/thrift/exceptions.rb +65 -0
- data/lib/thrift/processor.rb +39 -0
- data/lib/thrift/protocol/binaryprotocol.rb +213 -0
- data/lib/thrift/protocol/binaryprotocolaccelerated.rb +19 -0
- data/lib/thrift/protocol/tbinaryprotocol.rb +2 -0
- data/lib/thrift/protocol/tprotocol.rb +2 -0
- data/lib/thrift/protocol.rb +270 -0
- data/lib/thrift/serializer.rb +27 -0
- data/lib/thrift/server/httpserver.rb +44 -0
- data/lib/thrift/server/nonblockingserver.rb +278 -0
- data/lib/thrift/server/thttpserver.rb +2 -0
- data/lib/thrift/server/tserver.rb +2 -0
- data/lib/thrift/server.rb +135 -0
- data/lib/thrift/struct.rb +272 -0
- data/lib/thrift/thrift.rb +14 -0
- data/lib/thrift/transport/httpclient.rb +29 -0
- data/lib/thrift/transport/socket.rb +167 -0
- data/lib/thrift/transport/thttpclient.rb +2 -0
- data/lib/thrift/transport/tsocket.rb +2 -0
- data/lib/thrift/transport/ttransport.rb +2 -0
- data/lib/thrift/transport/unixsocket.rb +58 -0
- data/lib/thrift/transport.rb +319 -0
- data/lib/thrift/types.rb +83 -0
- data/lib/thrift.rb +28 -0
- data/setup.rb +1585 -0
- data/spec/ThriftSpec.thrift +46 -0
- data/spec/backwards_compatibility_spec.rb +136 -0
- data/spec/binaryprotocol_spec.rb +45 -0
- data/spec/binaryprotocol_spec_shared.rb +274 -0
- data/spec/binaryprotocolaccelerated_spec.rb +101 -0
- data/spec/client_spec.rb +81 -0
- data/spec/deprecation_spec.rb +443 -0
- data/spec/exception_spec.rb +123 -0
- data/spec/gen-rb/NonblockingService.rb +268 -0
- data/spec/gen-rb/ThriftSpec_constants.rb +11 -0
- data/spec/gen-rb/ThriftSpec_types.rb +134 -0
- data/spec/httpclient_spec.rb +31 -0
- data/spec/httpserver_spec.rb +98 -0
- data/spec/nonblockingserver_spec.rb +245 -0
- data/spec/processor_spec.rb +64 -0
- data/spec/protocol_spec.rb +142 -0
- data/spec/serializer_spec.rb +52 -0
- data/spec/server_spec.rb +141 -0
- data/spec/socket_spec.rb +97 -0
- data/spec/socket_spec_shared.rb +85 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/struct_spec.rb +244 -0
- data/spec/transport_spec.rb +359 -0
- data/spec/types_spec.rb +98 -0
- data/spec/unixsocket_spec.rb +90 -0
- data/thrift.gemspec +33 -0
- data.tar.gz.sig +0 -0
- metadata +200 -0
- metadata.gz.sig +0 -0
@@ -0,0 +1,319 @@
|
|
1
|
+
# Copyright (c) 2006- Facebook
|
2
|
+
# Distributed under the Apache Software License
|
3
|
+
#
|
4
|
+
# See accompanying file LICENSE or visit the Thrift site at:
|
5
|
+
# http://developers.facebook.com/thrift/
|
6
|
+
#
|
7
|
+
# Author: Mark Slee <mcslee@facebook.com>
|
8
|
+
#
|
9
|
+
|
10
|
+
module Thrift
|
11
|
+
class TransportException < Exception
|
12
|
+
UNKNOWN = 0
|
13
|
+
NOT_OPEN = 1
|
14
|
+
ALREADY_OPEN = 2
|
15
|
+
TIMED_OUT = 3
|
16
|
+
END_OF_FILE = 4
|
17
|
+
|
18
|
+
attr_reader :type
|
19
|
+
|
20
|
+
def initialize(type=UNKNOWN, message=nil)
|
21
|
+
super(message)
|
22
|
+
@type = type
|
23
|
+
end
|
24
|
+
end
|
25
|
+
deprecate_class! :TTransportException => TransportException
|
26
|
+
|
27
|
+
# Transport is basically an abstract class, but isn't raising NotImplementedError
|
28
|
+
# TODO: Think about if this is the right thing - Kevin Clark - 3/27/08
|
29
|
+
class Transport
|
30
|
+
def open?; end
|
31
|
+
deprecate! :isOpen => :open?
|
32
|
+
deprecate! :is_open? => :open?
|
33
|
+
|
34
|
+
def open; end
|
35
|
+
|
36
|
+
def close; end
|
37
|
+
|
38
|
+
def read(sz); end
|
39
|
+
|
40
|
+
def read_all(size)
|
41
|
+
buf = ''
|
42
|
+
|
43
|
+
while (buf.length < size)
|
44
|
+
chunk = read(size - buf.length)
|
45
|
+
buf << chunk
|
46
|
+
end
|
47
|
+
|
48
|
+
buf
|
49
|
+
end
|
50
|
+
deprecate! :readAll => :read_all
|
51
|
+
|
52
|
+
def write(buf); end
|
53
|
+
alias_method :<<, :write
|
54
|
+
|
55
|
+
def flush; end
|
56
|
+
end
|
57
|
+
deprecate_class! :TTransport => Transport
|
58
|
+
|
59
|
+
class ServerTransport
|
60
|
+
def listen; nil; end
|
61
|
+
|
62
|
+
def accept; nil; end
|
63
|
+
|
64
|
+
def close; nil; end
|
65
|
+
|
66
|
+
def closed?; nil; end
|
67
|
+
end
|
68
|
+
deprecate_class! :TServerTransport => ServerTransport
|
69
|
+
|
70
|
+
class TransportFactory
|
71
|
+
def get_transport(trans)
|
72
|
+
return trans
|
73
|
+
end
|
74
|
+
deprecate! :getTransport => :get_transport
|
75
|
+
end
|
76
|
+
deprecate_class! :TTransportFactory => TransportFactory
|
77
|
+
|
78
|
+
class BufferedTransport < Transport
|
79
|
+
DEFAULT_BUFFER = 4096
|
80
|
+
|
81
|
+
def initialize(transport)
|
82
|
+
@transport = transport
|
83
|
+
@wbuf = ''
|
84
|
+
@rbuf = ''
|
85
|
+
end
|
86
|
+
|
87
|
+
def open?
|
88
|
+
return @transport.open?
|
89
|
+
end
|
90
|
+
|
91
|
+
def open
|
92
|
+
@transport.open
|
93
|
+
end
|
94
|
+
|
95
|
+
def close
|
96
|
+
flush
|
97
|
+
@transport.close
|
98
|
+
end
|
99
|
+
|
100
|
+
def read(sz)
|
101
|
+
ret = @rbuf.slice!(0...sz)
|
102
|
+
if ret.length == 0
|
103
|
+
@rbuf = @transport.read([sz, DEFAULT_BUFFER].max)
|
104
|
+
@rbuf.slice!(0...sz)
|
105
|
+
else
|
106
|
+
ret
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def write(buf)
|
111
|
+
@wbuf << buf
|
112
|
+
end
|
113
|
+
|
114
|
+
def flush
|
115
|
+
if @wbuf != ''
|
116
|
+
@transport.write(@wbuf)
|
117
|
+
@wbuf = ''
|
118
|
+
end
|
119
|
+
|
120
|
+
@transport.flush
|
121
|
+
end
|
122
|
+
|
123
|
+
def borrow(requested_length = 0)
|
124
|
+
# $stderr.puts "#{Time.now.to_f} Have #{@rbuf.length} asking for #{requested_length.inspect}"
|
125
|
+
return @rbuf if @rbuf.length > requested_length
|
126
|
+
|
127
|
+
if @rbuf.length < DEFAULT_BUFFER
|
128
|
+
@rbuf << @transport.read([requested_length, DEFAULT_BUFFER].max)
|
129
|
+
end
|
130
|
+
|
131
|
+
if @rbuf.length < requested_length
|
132
|
+
@rbuf << @transport.read_all(requested_length - @rbuf.length)
|
133
|
+
end
|
134
|
+
|
135
|
+
@rbuf
|
136
|
+
end
|
137
|
+
|
138
|
+
def consume!(size)
|
139
|
+
@rbuf.slice!(0...size)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
deprecate_class! :TBufferedTransport => BufferedTransport
|
143
|
+
|
144
|
+
class BufferedTransportFactory < TransportFactory
|
145
|
+
def get_transport(transport)
|
146
|
+
return BufferedTransport.new(transport)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
deprecate_class! :TBufferedTransportFactory => BufferedTransportFactory
|
150
|
+
|
151
|
+
class FramedTransport < Transport
|
152
|
+
def initialize(transport, read=true, write=true)
|
153
|
+
@transport = transport
|
154
|
+
@rbuf = ''
|
155
|
+
@wbuf = ''
|
156
|
+
@read = read
|
157
|
+
@write = write
|
158
|
+
end
|
159
|
+
|
160
|
+
def open?
|
161
|
+
@transport.open?
|
162
|
+
end
|
163
|
+
|
164
|
+
def open
|
165
|
+
@transport.open
|
166
|
+
end
|
167
|
+
|
168
|
+
def close
|
169
|
+
@transport.close
|
170
|
+
end
|
171
|
+
|
172
|
+
def read(sz)
|
173
|
+
return @transport.read(sz) unless @read
|
174
|
+
|
175
|
+
return '' if sz <= 0
|
176
|
+
|
177
|
+
read_frame if @rbuf.empty?
|
178
|
+
|
179
|
+
@rbuf.slice!(0, sz)
|
180
|
+
end
|
181
|
+
|
182
|
+
def write(buf,sz=nil)
|
183
|
+
return @transport.write(buf) unless @write
|
184
|
+
|
185
|
+
@wbuf << (sz ? buf[0...sz] : buf)
|
186
|
+
end
|
187
|
+
|
188
|
+
#
|
189
|
+
# Writes the output buffer to the stream in the format of a 4-byte length
|
190
|
+
# followed by the actual data.
|
191
|
+
#
|
192
|
+
def flush
|
193
|
+
return @transport.flush unless @write
|
194
|
+
|
195
|
+
out = [@wbuf.length].pack('N')
|
196
|
+
out << @wbuf
|
197
|
+
@transport.write(out)
|
198
|
+
@transport.flush
|
199
|
+
@wbuf = ''
|
200
|
+
end
|
201
|
+
|
202
|
+
def borrow(requested_length = 0)
|
203
|
+
read_frame if @rbuf.empty?
|
204
|
+
# there isn't any more coming, so if it's not enough, it's an error.
|
205
|
+
raise EOFError if requested_length > @rbuf.size
|
206
|
+
@rbuf
|
207
|
+
end
|
208
|
+
|
209
|
+
def consume!(size)
|
210
|
+
@rbuf.slice!(0...size)
|
211
|
+
end
|
212
|
+
|
213
|
+
private
|
214
|
+
|
215
|
+
def read_frame
|
216
|
+
sz = @transport.read_all(4).unpack('N').first
|
217
|
+
|
218
|
+
@rbuf = @transport.read_all(sz).dup # protect against later #slice!
|
219
|
+
end
|
220
|
+
end
|
221
|
+
deprecate_class! :TFramedTransport => FramedTransport
|
222
|
+
|
223
|
+
class FramedTransportFactory < TransportFactory
|
224
|
+
def get_transport(transport)
|
225
|
+
return FramedTransport.new(transport)
|
226
|
+
end
|
227
|
+
end
|
228
|
+
deprecate_class! :TFramedTransportFactory => FramedTransportFactory
|
229
|
+
|
230
|
+
class MemoryBuffer < Transport
|
231
|
+
GARBAGE_BUFFER_SIZE = 4*(2**10) # 4kB
|
232
|
+
|
233
|
+
# If you pass a string to this, you should #dup that string
|
234
|
+
# unless you want it to be modified by #read and #write
|
235
|
+
#--
|
236
|
+
# this behavior is no longer required. If you wish to change it
|
237
|
+
# go ahead, just make sure the specs pass
|
238
|
+
def initialize(buffer = nil)
|
239
|
+
@buf = buffer || ''
|
240
|
+
@index = 0
|
241
|
+
end
|
242
|
+
|
243
|
+
def open?
|
244
|
+
return true
|
245
|
+
end
|
246
|
+
|
247
|
+
def open
|
248
|
+
end
|
249
|
+
|
250
|
+
def close
|
251
|
+
end
|
252
|
+
|
253
|
+
def peek
|
254
|
+
@index < @buf.size
|
255
|
+
end
|
256
|
+
|
257
|
+
# this method does not use the passed object directly but copies it
|
258
|
+
def reset_buffer(new_buf = '')
|
259
|
+
@buf.replace new_buf
|
260
|
+
@index = 0
|
261
|
+
end
|
262
|
+
|
263
|
+
def available
|
264
|
+
@buf.length - @index
|
265
|
+
end
|
266
|
+
|
267
|
+
def read(len)
|
268
|
+
data = @buf.slice(@index, len)
|
269
|
+
@index += len
|
270
|
+
@index = @buf.size if @index > @buf.size
|
271
|
+
if @index >= GARBAGE_BUFFER_SIZE
|
272
|
+
@buf = @buf.slice(@index..-1)
|
273
|
+
@index = 0
|
274
|
+
end
|
275
|
+
data
|
276
|
+
end
|
277
|
+
|
278
|
+
def write(wbuf)
|
279
|
+
@buf << wbuf
|
280
|
+
end
|
281
|
+
|
282
|
+
def flush
|
283
|
+
end
|
284
|
+
|
285
|
+
# For fast binary protocol access
|
286
|
+
def borrow(size = nil)
|
287
|
+
if size.nil?
|
288
|
+
@buf[@index..-1]
|
289
|
+
else
|
290
|
+
if size > available
|
291
|
+
raise EOFError # Memory buffers only get one shot.
|
292
|
+
else
|
293
|
+
@buf[@index, size]
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
alias_method :consume!, :read
|
299
|
+
end
|
300
|
+
deprecate_class! :TMemoryBuffer => MemoryBuffer
|
301
|
+
|
302
|
+
## Very very simple implementation of wrapping two objects, one with a #read
|
303
|
+
## method and one with a #write method, into a transport for thrift.
|
304
|
+
##
|
305
|
+
## Assumes both objects are open, remain open, don't require flushing, etc.
|
306
|
+
class IOStreamTransport < Transport
|
307
|
+
def initialize(input, output)
|
308
|
+
@input = input
|
309
|
+
@output = output
|
310
|
+
end
|
311
|
+
|
312
|
+
def open?; not @input.closed? or not @output.closed? end
|
313
|
+
def read(sz); @input.read(sz) end
|
314
|
+
def write(buf); @output.write(buf) end
|
315
|
+
def close; @input.close; @output.close end
|
316
|
+
def to_io; @input end # we're assuming this is used in a IO.select for reading
|
317
|
+
end
|
318
|
+
deprecate_class! :TIOStreamTransport => IOStreamTransport
|
319
|
+
end
|
data/lib/thrift/types.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module Thrift
|
4
|
+
module Types
|
5
|
+
STOP = 0
|
6
|
+
VOID = 1
|
7
|
+
BOOL = 2
|
8
|
+
BYTE = 3
|
9
|
+
DOUBLE = 4
|
10
|
+
I16 = 6
|
11
|
+
I32 = 8
|
12
|
+
I64 = 10
|
13
|
+
STRING = 11
|
14
|
+
STRUCT = 12
|
15
|
+
MAP = 13
|
16
|
+
SET = 14
|
17
|
+
LIST = 15
|
18
|
+
end
|
19
|
+
deprecate_module! :TType => Types
|
20
|
+
|
21
|
+
class << self
|
22
|
+
attr_accessor :type_checking
|
23
|
+
end
|
24
|
+
|
25
|
+
class TypeError < Exception
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.check_type(value, field, name, skip_nil=true)
|
29
|
+
return if value.nil? and skip_nil
|
30
|
+
klasses = case field[:type]
|
31
|
+
when Types::VOID
|
32
|
+
NilClass
|
33
|
+
when Types::BOOL
|
34
|
+
[TrueClass, FalseClass]
|
35
|
+
when Types::BYTE, Types::I16, Types::I32, Types::I64
|
36
|
+
Integer
|
37
|
+
when Types::DOUBLE
|
38
|
+
Float
|
39
|
+
when Types::STRING
|
40
|
+
String
|
41
|
+
when Types::STRUCT
|
42
|
+
Struct
|
43
|
+
when Types::MAP
|
44
|
+
Hash
|
45
|
+
when Types::SET
|
46
|
+
Set
|
47
|
+
when Types::LIST
|
48
|
+
Array
|
49
|
+
end
|
50
|
+
valid = klasses && [*klasses].any? { |klass| klass === value }
|
51
|
+
raise TypeError, "Expected #{type_name(field[:type])}, received #{value.class} for field #{name}" unless valid
|
52
|
+
# check elements now
|
53
|
+
case field[:type]
|
54
|
+
when Types::MAP
|
55
|
+
value.each_pair do |k,v|
|
56
|
+
check_type(k, field[:key], "#{name}.key", false)
|
57
|
+
check_type(v, field[:value], "#{name}.value", false)
|
58
|
+
end
|
59
|
+
when Types::SET, Types::LIST
|
60
|
+
value.each do |el|
|
61
|
+
check_type(el, field[:element], "#{name}.element", false)
|
62
|
+
end
|
63
|
+
when Types::STRUCT
|
64
|
+
raise TypeError, "Expected #{field[:class]}, received #{value.class} for field #{name}" unless field[:class] == value.class
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.type_name(type)
|
69
|
+
Types.constants.each do |const|
|
70
|
+
return "Types::#{const}" if Types.const_get(const) == type
|
71
|
+
end
|
72
|
+
nil
|
73
|
+
end
|
74
|
+
|
75
|
+
module MessageTypes
|
76
|
+
CALL = 1
|
77
|
+
REPLY = 2
|
78
|
+
EXCEPTION = 3
|
79
|
+
end
|
80
|
+
deprecate_module! :TMessageType => MessageTypes
|
81
|
+
end
|
82
|
+
|
83
|
+
Thrift.type_checking = false if Thrift.type_checking.nil?
|
data/lib/thrift.rb
ADDED
@@ -0,0 +1,28 @@
|
|
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
|
+
|
11
|
+
$:.unshift File.dirname(__FILE__)
|
12
|
+
|
13
|
+
module Thrift
|
14
|
+
# prevent the deprecation layer from being loaded if you require 'thrift'
|
15
|
+
DEPRECATION = false unless const_defined? :DEPRECATION
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'thrift/deprecation'
|
19
|
+
require 'thrift/exceptions'
|
20
|
+
require 'thrift/types'
|
21
|
+
require 'thrift/processor'
|
22
|
+
require 'thrift/client'
|
23
|
+
require 'thrift/struct'
|
24
|
+
require 'thrift/protocol'
|
25
|
+
require 'thrift/protocol/binaryprotocol'
|
26
|
+
require 'thrift/transport'
|
27
|
+
require 'thrift/transport/socket'
|
28
|
+
require 'thrift/server'
|