thrift-mavericks 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +15 -0
  2. data/CHANGELOG +1 -0
  3. data/README +43 -0
  4. data/benchmark/Benchmark.thrift +24 -0
  5. data/benchmark/benchmark.rb +271 -0
  6. data/benchmark/client.rb +74 -0
  7. data/benchmark/gen-rb/benchmark_constants.rb +11 -0
  8. data/benchmark/gen-rb/benchmark_service.rb +80 -0
  9. data/benchmark/gen-rb/benchmark_types.rb +10 -0
  10. data/benchmark/server.rb +82 -0
  11. data/benchmark/thin_server.rb +44 -0
  12. data/ext/binary_protocol_accelerated.c +441 -0
  13. data/ext/binary_protocol_accelerated.h +20 -0
  14. data/ext/compact_protocol.c +618 -0
  15. data/ext/compact_protocol.h +20 -0
  16. data/ext/constants.h +96 -0
  17. data/ext/extconf.rb +30 -0
  18. data/ext/macros.h +41 -0
  19. data/ext/memory_buffer.c +131 -0
  20. data/ext/memory_buffer.h +20 -0
  21. data/ext/protocol.c +185 -0
  22. data/ext/protocol.h +20 -0
  23. data/ext/strlcpy.c +41 -0
  24. data/ext/strlcpy.h +32 -0
  25. data/ext/struct.c +691 -0
  26. data/ext/struct.h +25 -0
  27. data/ext/thrift_native.c +196 -0
  28. data/lib/thrift.rb +64 -0
  29. data/lib/thrift/client.rb +62 -0
  30. data/lib/thrift/core_ext.rb +23 -0
  31. data/lib/thrift/core_ext/fixnum.rb +29 -0
  32. data/lib/thrift/exceptions.rb +84 -0
  33. data/lib/thrift/processor.rb +57 -0
  34. data/lib/thrift/protocol/base_protocol.rb +290 -0
  35. data/lib/thrift/protocol/binary_protocol.rb +229 -0
  36. data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
  37. data/lib/thrift/protocol/compact_protocol.rb +426 -0
  38. data/lib/thrift/serializer/deserializer.rb +33 -0
  39. data/lib/thrift/serializer/serializer.rb +34 -0
  40. data/lib/thrift/server/base_server.rb +31 -0
  41. data/lib/thrift/server/mongrel_http_server.rb +58 -0
  42. data/lib/thrift/server/nonblocking_server.rb +305 -0
  43. data/lib/thrift/server/simple_server.rb +43 -0
  44. data/lib/thrift/server/thread_pool_server.rb +75 -0
  45. data/lib/thrift/server/threaded_server.rb +47 -0
  46. data/lib/thrift/struct.rb +237 -0
  47. data/lib/thrift/struct_union.rb +192 -0
  48. data/lib/thrift/thrift_native.rb +24 -0
  49. data/lib/thrift/transport/base_server_transport.rb +37 -0
  50. data/lib/thrift/transport/base_transport.rb +107 -0
  51. data/lib/thrift/transport/buffered_transport.rb +108 -0
  52. data/lib/thrift/transport/framed_transport.rb +116 -0
  53. data/lib/thrift/transport/http_client_transport.rb +51 -0
  54. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  55. data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
  56. data/lib/thrift/transport/server_socket.rb +63 -0
  57. data/lib/thrift/transport/socket.rb +137 -0
  58. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  59. data/lib/thrift/transport/unix_socket.rb +40 -0
  60. data/lib/thrift/types.rb +101 -0
  61. data/lib/thrift/union.rb +179 -0
  62. data/spec/ThriftSpec.thrift +132 -0
  63. data/spec/base_protocol_spec.rb +160 -0
  64. data/spec/base_transport_spec.rb +351 -0
  65. data/spec/binary_protocol_accelerated_spec.rb +46 -0
  66. data/spec/binary_protocol_spec.rb +61 -0
  67. data/spec/binary_protocol_spec_shared.rb +375 -0
  68. data/spec/client_spec.rb +100 -0
  69. data/spec/compact_protocol_spec.rb +144 -0
  70. data/spec/exception_spec.rb +142 -0
  71. data/spec/gen-rb/nonblocking_service.rb +272 -0
  72. data/spec/gen-rb/thrift_spec_constants.rb +11 -0
  73. data/spec/gen-rb/thrift_spec_types.rb +346 -0
  74. data/spec/http_client_spec.rb +64 -0
  75. data/spec/mongrel_http_server_spec.rb +117 -0
  76. data/spec/nonblocking_server_spec.rb +265 -0
  77. data/spec/processor_spec.rb +83 -0
  78. data/spec/serializer_spec.rb +69 -0
  79. data/spec/server_socket_spec.rb +80 -0
  80. data/spec/server_spec.rb +159 -0
  81. data/spec/socket_spec.rb +61 -0
  82. data/spec/socket_spec_shared.rb +104 -0
  83. data/spec/spec_helper.rb +58 -0
  84. data/spec/struct_spec.rb +295 -0
  85. data/spec/types_spec.rb +116 -0
  86. data/spec/union_spec.rb +193 -0
  87. data/spec/unix_socket_spec.rb +108 -0
  88. data/test/debug_proto/gen-rb/debug_proto_test_constants.rb +274 -0
  89. data/test/debug_proto/gen-rb/debug_proto_test_types.rb +761 -0
  90. data/test/debug_proto/gen-rb/empty_service.rb +24 -0
  91. data/test/debug_proto/gen-rb/inherited.rb +79 -0
  92. data/test/debug_proto/gen-rb/reverse_order_service.rb +82 -0
  93. data/test/debug_proto/gen-rb/service_for_exception_with_a_map.rb +81 -0
  94. data/test/debug_proto/gen-rb/srv.rb +330 -0
  95. metadata +281 -0
@@ -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,107 @@
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
+ module TransportUtils
38
+ if RUBY_VERSION >= '1.9'
39
+ def self.get_string_byte(string, index)
40
+ string.getbyte(index)
41
+ end
42
+
43
+ def self.set_string_byte(string, index, byte)
44
+ string.setbyte(index, byte)
45
+ end
46
+ else
47
+ def self.get_string_byte(string, index)
48
+ string[index]
49
+ end
50
+
51
+ def self.set_string_byte(string, index, byte)
52
+ string[index] = byte
53
+ end
54
+ end
55
+ end
56
+
57
+ class BaseTransport
58
+ def open?; end
59
+
60
+ def open; end
61
+
62
+ def close; end
63
+
64
+ def read(sz)
65
+ raise NotImplementedError
66
+ end
67
+
68
+ # Returns an unsigned byte as a Fixnum in the range (0..255).
69
+ def read_byte
70
+ buf = read_all(1)
71
+ return ::Thrift::TransportUtils.get_string_byte(buf, 0)
72
+ end
73
+
74
+ # Reads size bytes and copies them into buffer[0..size].
75
+ def read_into_buffer(buffer, size)
76
+ tmp = read_all(size)
77
+ i = 0
78
+ tmp.each_byte do |byte|
79
+ ::Thrift::TransportUtils.set_string_byte(buffer, i, byte)
80
+ i += 1
81
+ end
82
+ i
83
+ end
84
+
85
+ def read_all(size)
86
+ return '' if size <= 0
87
+ buf = read(size)
88
+ while (buf.length < size)
89
+ chunk = read(size - buf.length)
90
+ buf << chunk
91
+ end
92
+
93
+ buf
94
+ end
95
+
96
+ def write(buf); end
97
+ alias_method :<<, :write
98
+
99
+ def flush; end
100
+ end
101
+
102
+ class BaseTransportFactory
103
+ def get_transport(trans)
104
+ return trans
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,108 @@
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 read_byte
59
+ # If the read buffer is exhausted, try to read up to DEFAULT_BUFFER more bytes into it.
60
+ if @index >= @rbuf.size
61
+ @rbuf = @transport.read(DEFAULT_BUFFER)
62
+ @index = 0
63
+ end
64
+
65
+ # The read buffer has some data now, read a single byte. Using get_string_byte() avoids
66
+ # allocating a temp string of size 1 unnecessarily.
67
+ @index += 1
68
+ return ::Thrift::TransportUtils.get_string_byte(@rbuf, @index - 1)
69
+ end
70
+
71
+ def read_into_buffer(buffer, size)
72
+ i = 0
73
+ while i < size
74
+ # If the read buffer is exhausted, try to read up to DEFAULT_BUFFER more bytes into it.
75
+ if @index >= @rbuf.size
76
+ @rbuf = @transport.read(DEFAULT_BUFFER)
77
+ @index = 0
78
+ end
79
+
80
+ # The read buffer has some data now, so copy bytes over to the output buffer.
81
+ byte = ::Thrift::TransportUtils.get_string_byte(@rbuf, @index)
82
+ ::Thrift::TransportUtils.set_string_byte(buffer, i, byte)
83
+ @index += 1
84
+ i += 1
85
+ end
86
+ i
87
+ end
88
+
89
+ def write(buf)
90
+ @wbuf << buf
91
+ end
92
+
93
+ def flush
94
+ if @wbuf != ''
95
+ @transport.write(@wbuf)
96
+ @wbuf = ''
97
+ end
98
+
99
+ @transport.flush
100
+ end
101
+ end
102
+
103
+ class BufferedTransportFactory < BaseTransportFactory
104
+ def get_transport(transport)
105
+ return BufferedTransport.new(transport)
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,116 @@
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 read_byte
56
+ return @transport.read_byte() unless @read
57
+
58
+ read_frame if @index >= @rbuf.length
59
+
60
+ # The read buffer has some data now, read a single byte. Using get_string_byte() avoids
61
+ # allocating a temp string of size 1 unnecessarily.
62
+ @index += 1
63
+ return ::Thrift::TransportUtils.get_string_byte(@rbuf, @index - 1)
64
+ end
65
+
66
+ def read_into_buffer(buffer, size)
67
+ i = 0
68
+ while i < size
69
+ read_frame if @index >= @rbuf.length
70
+
71
+ # The read buffer has some data now, so copy bytes over to the output buffer.
72
+ byte = ::Thrift::TransportUtils.get_string_byte(@rbuf, @index)
73
+ ::Thrift::TransportUtils.set_string_byte(buffer, i, byte)
74
+ @index += 1
75
+ i += 1
76
+ end
77
+ i
78
+ end
79
+
80
+
81
+ def write(buf,sz=nil)
82
+ return @transport.write(buf) unless @write
83
+
84
+ @wbuf << (sz ? buf[0...sz] : buf)
85
+ end
86
+
87
+ #
88
+ # Writes the output buffer to the stream in the format of a 4-byte length
89
+ # followed by the actual data.
90
+ #
91
+ def flush
92
+ return @transport.flush unless @write
93
+
94
+ out = [@wbuf.length].pack('N')
95
+ out << @wbuf
96
+ @transport.write(out)
97
+ @transport.flush
98
+ @wbuf = ''
99
+ end
100
+
101
+ private
102
+
103
+ def read_frame
104
+ sz = @transport.read_all(4).unpack('N').first
105
+
106
+ @index = 0
107
+ @rbuf = @transport.read_all(sz)
108
+ end
109
+ end
110
+
111
+ class FramedTransportFactory < BaseTransportFactory
112
+ def get_transport(transport)
113
+ return FramedTransport.new(transport)
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,51 @@
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
+
29
+ def initialize(url)
30
+ @url = URI url
31
+ @headers = {'Content-Type' => 'application/x-thrift'}
32
+ @outbuf = ""
33
+ end
34
+
35
+ def open?; true end
36
+ def read(sz); @inbuf.read sz end
37
+ def write(buf); @outbuf << buf end
38
+
39
+ def add_headers(headers)
40
+ @headers = @headers.merge(headers)
41
+ end
42
+
43
+ def flush
44
+ http = Net::HTTP.new @url.host, @url.port
45
+ http.use_ssl = @url.scheme == "https"
46
+ resp, data = http.post(@url.request_uri, @outbuf, @headers)
47
+ @inbuf = StringIO.new data
48
+ @outbuf = ""
49
+ end
50
+ end
51
+ end