fl-thrift 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. data/CHANGELOG +4 -0
  2. data/Manifest +81 -0
  3. data/README +43 -0
  4. data/Rakefile +102 -0
  5. data/benchmark/Benchmark.thrift +24 -0
  6. data/benchmark/benchmark.rb +271 -0
  7. data/benchmark/client.rb +74 -0
  8. data/benchmark/server.rb +82 -0
  9. data/benchmark/thin_server.rb +44 -0
  10. data/ext/binary_protocol_accelerated.c +474 -0
  11. data/ext/binary_protocol_accelerated.h +20 -0
  12. data/ext/compact_protocol.c +665 -0
  13. data/ext/compact_protocol.h +20 -0
  14. data/ext/constants.h +95 -0
  15. data/ext/extconf.rb +26 -0
  16. data/ext/macros.h +41 -0
  17. data/ext/memory_buffer.c +76 -0
  18. data/ext/memory_buffer.h +20 -0
  19. data/ext/protocol.c +185 -0
  20. data/ext/protocol.h +20 -0
  21. data/ext/struct.c +606 -0
  22. data/ext/struct.h +67 -0
  23. data/ext/thrift_native.c +194 -0
  24. data/lib/thrift.rb +59 -0
  25. data/lib/thrift/client.rb +62 -0
  26. data/lib/thrift/core_ext.rb +23 -0
  27. data/lib/thrift/core_ext/fixnum.rb +29 -0
  28. data/lib/thrift/exceptions.rb +82 -0
  29. data/lib/thrift/processor.rb +57 -0
  30. data/lib/thrift/protocol/base_protocol.rb +290 -0
  31. data/lib/thrift/protocol/binary_protocol.rb +225 -0
  32. data/lib/thrift/protocol/binary_protocol_accelerated.rb +35 -0
  33. data/lib/thrift/protocol/compact_protocol.rb +422 -0
  34. data/lib/thrift/serializer/deserializer.rb +33 -0
  35. data/lib/thrift/serializer/serializer.rb +34 -0
  36. data/lib/thrift/server/base_server.rb +31 -0
  37. data/lib/thrift/server/mongrel_http_server.rb +58 -0
  38. data/lib/thrift/server/nonblocking_server.rb +296 -0
  39. data/lib/thrift/server/simple_server.rb +43 -0
  40. data/lib/thrift/server/thread_pool_server.rb +75 -0
  41. data/lib/thrift/server/threaded_server.rb +47 -0
  42. data/lib/thrift/struct.rb +298 -0
  43. data/lib/thrift/thrift_native.rb +24 -0
  44. data/lib/thrift/transport/base_server_transport.rb +37 -0
  45. data/lib/thrift/transport/base_transport.rb +70 -0
  46. data/lib/thrift/transport/buffered_transport.rb +77 -0
  47. data/lib/thrift/transport/framed_transport.rb +90 -0
  48. data/lib/thrift/transport/http_client_transport.rb +45 -0
  49. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  50. data/lib/thrift/transport/memory_buffer_transport.rb +96 -0
  51. data/lib/thrift/transport/server_socket.rb +63 -0
  52. data/lib/thrift/transport/socket.rb +136 -0
  53. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  54. data/lib/thrift/transport/unix_socket.rb +40 -0
  55. data/lib/thrift/types.rb +101 -0
  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/setup.rb +1585 -0
  60. data/spec/ThriftSpec.thrift +84 -0
  61. data/spec/base_protocol_spec.rb +160 -0
  62. data/spec/base_transport_spec.rb +351 -0
  63. data/spec/binary_protocol_accelerated_spec.rb +41 -0
  64. data/spec/binary_protocol_spec.rb +63 -0
  65. data/spec/binary_protocol_spec_shared.rb +375 -0
  66. data/spec/client_spec.rb +100 -0
  67. data/spec/compact_protocol_spec.rb +117 -0
  68. data/spec/exception_spec.rb +142 -0
  69. data/spec/http_client_spec.rb +49 -0
  70. data/spec/mongrel_http_server_spec.rb +117 -0
  71. data/spec/nonblocking_server_spec.rb +265 -0
  72. data/spec/processor_spec.rb +83 -0
  73. data/spec/serializer_spec.rb +69 -0
  74. data/spec/server_socket_spec.rb +80 -0
  75. data/spec/server_spec.rb +160 -0
  76. data/spec/socket_spec.rb +61 -0
  77. data/spec/socket_spec_shared.rb +104 -0
  78. data/spec/spec_helper.rb +60 -0
  79. data/spec/struct_spec.rb +252 -0
  80. data/spec/types_spec.rb +116 -0
  81. data/spec/unix_socket_spec.rb +108 -0
  82. data/thrift.gemspec +32 -0
  83. metadata +202 -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,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
@@ -0,0 +1,39 @@
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
+ # Very very simple implementation of wrapping two objects, one with a #read
22
+ # method and one with a #write method, into a transport for thrift.
23
+ #
24
+ # Assumes both objects are open, remain open, don't require flushing, etc.
25
+ #
26
+ module Thrift
27
+ class IOStreamTransport < BaseTransport
28
+ def initialize(input, output)
29
+ @input = input
30
+ @output = output
31
+ end
32
+
33
+ def open?; not @input.closed? or not @output.closed? end
34
+ def read(sz); @input.read(sz) end
35
+ def write(buf); @output.write(buf) end
36
+ def close; @input.close; @output.close end
37
+ def to_io; @input end # we're assuming this is used in a IO.select for reading
38
+ end
39
+ end
@@ -0,0 +1,96 @@
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 MemoryBufferTransport < BaseTransport
23
+ GARBAGE_BUFFER_SIZE = 4*(2**10) # 4kB
24
+
25
+ # If you pass a string to this, you should #dup that string
26
+ # unless you want it to be modified by #read and #write
27
+ #--
28
+ # this behavior is no longer required. If you wish to change it
29
+ # go ahead, just make sure the specs pass
30
+ def initialize(buffer = nil)
31
+ @buf = buffer || ''
32
+ @index = 0
33
+ end
34
+
35
+ def open?
36
+ return true
37
+ end
38
+
39
+ def open
40
+ end
41
+
42
+ def close
43
+ end
44
+
45
+ def peek
46
+ @index < @buf.size
47
+ end
48
+
49
+ # this method does not use the passed object directly but copies it
50
+ def reset_buffer(new_buf = '')
51
+ @buf.replace new_buf
52
+ @index = 0
53
+ end
54
+
55
+ def available
56
+ @buf.length - @index
57
+ end
58
+
59
+ def read(len)
60
+ data = @buf.slice(@index, len)
61
+ @index += len
62
+ @index = @buf.size if @index > @buf.size
63
+ if @index >= GARBAGE_BUFFER_SIZE
64
+ @buf = @buf.slice(@index..-1)
65
+ @index = 0
66
+ end
67
+ if data.size < len
68
+ raise EOFError, "Not enough bytes remain in buffer"
69
+ end
70
+ data
71
+ end
72
+
73
+ def write(wbuf)
74
+ @buf << wbuf
75
+ end
76
+
77
+ def flush
78
+ end
79
+
80
+ def inspect_buffer
81
+ out = []
82
+ for idx in 0...(@buf.size)
83
+ # if idx != 0
84
+ # out << " "
85
+ # end
86
+
87
+ if idx == @index
88
+ out << ">"
89
+ end
90
+
91
+ out << @buf[idx].to_s(16)
92
+ end
93
+ out.join(" ")
94
+ end
95
+ end
96
+ end