From d1d1542e1e307756f7637de0d759d9fc02677a93 Mon Sep 17 00:00:00 2001 From: Bryan Duxbury Date: Sat, 4 Apr 2009 00:58:03 +0000 Subject: [PATCH] THRIFT-276. rb: Ruby libraries should have one class per file This monster of a patch moves all the classes into their own files and specs as appropriate. Additionally, it concentrates all the requires into thrift.rb, removing the need to require any other file. (Changes were made to the compiler to reflect this reduced requirement.) git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@761849 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_rb_generator.cc | 14 +- lib/rb/Manifest | 42 +-- lib/rb/benchmark/benchmark.rb | 2 - lib/rb/benchmark/client.rb | 1 - lib/rb/benchmark/server.rb | 5 +- lib/rb/benchmark/thin_server.rb | 5 +- lib/rb/ext/memory_buffer.c | 2 +- lib/rb/ext/protocol.c | 2 +- lib/rb/lib/thrift.rb | 35 +- .../base_protocol.rb} | 8 +- .../{binaryprotocol.rb => binary_protocol.rb} | 6 +- ...ated.rb => binary_protocol_accelerated.rb} | 9 +- .../lib/thrift/protocol/compact_protocol.rb | 6 +- lib/rb/lib/thrift/serializer/deserializer.rb | 33 ++ .../lib/thrift/{ => serializer}/serializer.rb | 20 +- lib/rb/lib/thrift/server.rb | 142 --------- lib/rb/lib/thrift/server/base_server.rb | 31 ++ .../{httpserver.rb => mongrel_http_server.rb} | 6 +- ...lockingserver.rb => nonblocking_server.rb} | 47 ++- lib/rb/lib/thrift/server/simple_server.rb | 43 +++ .../lib/thrift/server/thread_pool_server.rb | 75 +++++ lib/rb/lib/thrift/server/threaded_server.rb | 47 +++ lib/rb/lib/thrift/struct.rb | 1 - lib/rb/lib/thrift/transport.rb | 300 ------------------ .../thrift/transport/base_server_transport.rb | 37 +++ lib/rb/lib/thrift/transport/base_transport.rb | 70 ++++ .../thrift/transport/buffered_transport.rb | 77 +++++ .../lib/thrift/transport/framed_transport.rb | 90 ++++++ ...httpclient.rb => http_client_transport.rb} | 6 +- .../thrift/transport/io_stream_transport.rb | 39 +++ .../transport/memory_buffer_transport.rb | 93 ++++++ lib/rb/lib/thrift/transport/server_socket.rb | 63 ++++ lib/rb/lib/thrift/transport/socket.rb | 47 +-- .../{unixsocket.rb => unix_server_socket.rb} | 23 +- lib/rb/lib/thrift/transport/unix_socket.rb | 40 +++ lib/rb/script/proto_benchmark.rb | 10 +- lib/rb/script/read_struct.rb | 1 - lib/rb/script/write_struct.rb | 1 - ...protocol_spec.rb => base_protocol_spec.rb} | 10 +- ...ansport_spec.rb => base_transport_spec.rb} | 24 +- ...rb => binary_protocol_accelerated_spec.rb} | 3 +- ...otocol_spec.rb => binary_protocol_spec.rb} | 3 +- ...ared.rb => binary_protocol_spec_shared.rb} | 24 +- lib/rb/spec/compact_protocol_spec.rb | 11 +- ...httpclient_spec.rb => http_client_spec.rb} | 7 +- ...er_spec.rb => mongrel_http_server_spec.rb} | 14 +- ...ver_spec.rb => nonblocking_server_spec.rb} | 7 +- lib/rb/spec/serializer_spec.rb | 17 +- lib/rb/spec/server_socket_spec.rb | 80 +++++ lib/rb/spec/server_spec.rb | 14 +- lib/rb/spec/socket_spec.rb | 55 ---- lib/rb/spec/spec_helper.rb | 2 +- lib/rb/spec/struct_spec.rb | 12 +- ...unixsocket_spec.rb => unix_socket_spec.rb} | 1 - 54 files changed, 1001 insertions(+), 762 deletions(-) rename lib/rb/lib/thrift/{protocol.rb => protocol/base_protocol.rb} (99%) rename lib/rb/lib/thrift/protocol/{binaryprotocol.rb => binary_protocol.rb} (97%) rename lib/rb/lib/thrift/protocol/{binaryprotocolaccelerated.rb => binary_protocol_accelerated.rb} (80%) create mode 100644 lib/rb/lib/thrift/serializer/deserializer.rb rename lib/rb/lib/thrift/{ => serializer}/serializer.rb (67%) delete mode 100644 lib/rb/lib/thrift/server.rb create mode 100644 lib/rb/lib/thrift/server/base_server.rb rename lib/rb/lib/thrift/server/{httpserver.rb => mongrel_http_server.rb} (93%) rename lib/rb/lib/thrift/server/{nonblockingserver.rb => nonblocking_server.rb} (83%) create mode 100644 lib/rb/lib/thrift/server/simple_server.rb create mode 100644 lib/rb/lib/thrift/server/thread_pool_server.rb create mode 100644 lib/rb/lib/thrift/server/threaded_server.rb delete mode 100644 lib/rb/lib/thrift/transport.rb create mode 100644 lib/rb/lib/thrift/transport/base_server_transport.rb create mode 100644 lib/rb/lib/thrift/transport/base_transport.rb create mode 100644 lib/rb/lib/thrift/transport/buffered_transport.rb create mode 100644 lib/rb/lib/thrift/transport/framed_transport.rb rename lib/rb/lib/thrift/transport/{httpclient.rb => http_client_transport.rb} (93%) create mode 100644 lib/rb/lib/thrift/transport/io_stream_transport.rb create mode 100644 lib/rb/lib/thrift/transport/memory_buffer_transport.rb create mode 100644 lib/rb/lib/thrift/transport/server_socket.rb rename lib/rb/lib/thrift/transport/{unixsocket.rb => unix_server_socket.rb} (75%) create mode 100644 lib/rb/lib/thrift/transport/unix_socket.rb rename lib/rb/spec/{protocol_spec.rb => base_protocol_spec.rb} (95%) rename lib/rb/spec/{transport_spec.rb => base_transport_spec.rb} (94%) rename lib/rb/spec/{binaryprotocolaccelerated_spec.rb => binary_protocol_accelerated_spec.rb} (92%) rename lib/rb/spec/{binaryprotocol_spec.rb => binary_protocol_spec.rb} (95%) rename lib/rb/spec/{binaryprotocol_spec_shared.rb => binary_protocol_spec_shared.rb} (94%) rename lib/rb/spec/{httpclient_spec.rb => http_client_spec.rb} (89%) rename lib/rb/spec/{httpserver_spec.rb => mongrel_http_server_spec.rb} (92%) rename lib/rb/spec/{nonblockingserver_spec.rb => nonblocking_server_spec.rb} (97%) create mode 100644 lib/rb/spec/server_socket_spec.rb rename lib/rb/spec/{unixsocket_spec.rb => unix_socket_spec.rb} (98%) diff --git a/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc index 5f9aa683..f3f11ec6 100644 --- a/compiler/cpp/src/generate/t_rb_generator.cc +++ b/compiler/cpp/src/generate/t_rb_generator.cc @@ -155,7 +155,6 @@ class t_rb_generator : public t_oop_generator { */ std::string rb_autogen_comment(); - std::string rb_imports(); std::string render_includes(); std::string declare_field(t_field* tfield); std::string type_name(t_type* ttype); @@ -214,13 +213,11 @@ void t_rb_generator::init_generator() { // Print header f_types_ << rb_autogen_comment() << endl << - rb_imports() << endl << render_includes() << endl; begin_namespace(f_types_, ruby_modules(program_)); f_consts_ << rb_autogen_comment() << endl << - rb_imports() << endl << "require File.dirname(__FILE__) + '/" << program_name_ << "_types'" << endl << endl; begin_namespace(f_consts_, ruby_modules(program_)); @@ -254,14 +251,6 @@ string t_rb_generator::rb_autogen_comment() { "#\n"; } -/** - * Prints standard thrift imports - */ -string t_rb_generator::rb_imports() { - return - string("require 'thrift/protocol'"); -} - /** * Closes the type files */ @@ -638,8 +627,7 @@ void t_rb_generator::generate_service(t_service* tservice) { f_service_ << rb_autogen_comment() << endl << - "require 'thrift'" << endl << - rb_imports() << endl; + "require 'thrift'" << endl; if (tservice->get_extends() != NULL) { f_service_ << diff --git a/lib/rb/Manifest b/lib/rb/Manifest index 17439ed2..d952c50f 100644 --- a/lib/rb/Manifest +++ b/lib/rb/Manifest @@ -1,9 +1,6 @@ benchmark/benchmark.rb benchmark/Benchmark.thrift benchmark/client.rb -benchmark/gen-rb/Benchmark_constants.rb -benchmark/gen-rb/Benchmark_types.rb -benchmark/gen-rb/BenchmarkService.rb benchmark/server.rb benchmark/thin_server.rb CHANGELOG @@ -26,36 +23,45 @@ lib/thrift/core_ext/fixnum.rb lib/thrift/core_ext.rb lib/thrift/exceptions.rb lib/thrift/processor.rb -lib/thrift/protocol/binaryprotocol.rb -lib/thrift/protocol/binaryprotocolaccelerated.rb +lib/thrift/protocol/binary_protocol_accelerated.rb +lib/thrift/protocol/binary_protocol.rb lib/thrift/protocol/compact_protocol.rb lib/thrift/protocol.rb -lib/thrift/serializer.rb -lib/thrift/server/httpserver.rb -lib/thrift/server/nonblockingserver.rb +lib/thrift/serializer/serializer.rb +lib/thrift/serializer/deserializer.rb +lib/thrift/server/mongrel_http_server.rb +lib/thrift/server/nonblocking_server.rb lib/thrift/server.rb lib/thrift/struct.rb lib/thrift/thrift_native.rb -lib/thrift/transport/httpclient.rb -lib/thrift/transport/socket.rb -lib/thrift/transport/unixsocket.rb +lib/thrift/transport/buffered_transport.rb +lib/thrift/transport/framed_transport.rb +lib/thrift/transport/http_client_transport.rb +lib/thrift/transport/io_stream_transport.rb +lib/thrift/transport/memory_buffer_transport.rb lib/thrift/transport.rb lib/thrift/types.rb lib/thrift.rb Manifest +Rakefile README -spec/binaryprotocol_spec.rb -spec/binaryprotocol_spec_shared.rb -spec/binaryprotocolaccelerated_spec.rb +script/proto_benchmark.rb +script/read_struct.rb +script/write_struct.rb +setup.rb +spec/binary_protocol_accelerated_spec.rb +spec/binary_protocol_spec.rb +spec/binary_protocol_spec_shared.rb spec/client_spec.rb spec/compact_protocol_spec.rb spec/exception_spec.rb -spec/httpclient_spec.rb -spec/httpserver_spec.rb -spec/nonblockingserver_spec.rb +spec/http_client_spec.rb +spec/mongrel_http_server_spec.rb +spec/nonblocking_server_spec.rb spec/processor_spec.rb spec/protocol_spec.rb spec/serializer_spec.rb +spec/server_socket_spec.rb spec/server_spec.rb spec/socket_spec.rb spec/socket_spec_shared.rb @@ -64,4 +70,4 @@ spec/struct_spec.rb spec/ThriftSpec.thrift spec/transport_spec.rb spec/types_spec.rb -spec/unixsocket_spec.rb +spec/unix_socket_spec.rb diff --git a/lib/rb/benchmark/benchmark.rb b/lib/rb/benchmark/benchmark.rb index f5fe3235..3dc67dd8 100644 --- a/lib/rb/benchmark/benchmark.rb +++ b/lib/rb/benchmark/benchmark.rb @@ -20,8 +20,6 @@ require 'rubygems' $:.unshift File.dirname(__FILE__) + '/../lib' require 'thrift' -require 'thrift/server/nonblockingserver' -require 'thrift/transport/unixsocket' require 'stringio' HOST = '127.0.0.1' diff --git a/lib/rb/benchmark/client.rb b/lib/rb/benchmark/client.rb index bd92bde9..948f08d6 100644 --- a/lib/rb/benchmark/client.rb +++ b/lib/rb/benchmark/client.rb @@ -19,7 +19,6 @@ $:.unshift File.dirname(__FILE__) + '/../lib' require 'thrift' -require 'thrift/server/nonblockingserver' $:.unshift File.dirname(__FILE__) + "/gen-rb" require 'BenchmarkService' diff --git a/lib/rb/benchmark/server.rb b/lib/rb/benchmark/server.rb index 5b7da7eb..d44fa886 100644 --- a/lib/rb/benchmark/server.rb +++ b/lib/rb/benchmark/server.rb @@ -19,7 +19,6 @@ $:.unshift File.dirname(__FILE__) + '/../lib' require 'thrift' -require 'thrift/server/nonblockingserver' $:.unshift File.dirname(__FILE__) + "/gen-rb" require 'BenchmarkService' @@ -41,8 +40,8 @@ module Server handler = BenchmarkHandler.new processor = ThriftBenchmark::BenchmarkService::Processor.new(handler) transport = ServerSocket.new(host, port) - transportFactory = FramedTransportFactory.new - args = [processor, transport, transportFactory, nil, 20] + transport_factory = FramedTransportFactory.new + args = [processor, transport, transport_factory, nil, 20] if serverClass == NonblockingServer logger = Logger.new(STDERR) logger.level = Logger::WARN diff --git a/lib/rb/benchmark/thin_server.rb b/lib/rb/benchmark/thin_server.rb index ab8db9f1..47126add 100644 --- a/lib/rb/benchmark/thin_server.rb +++ b/lib/rb/benchmark/thin_server.rb @@ -19,7 +19,6 @@ $:.unshift File.dirname(__FILE__) + '/../lib' require 'thrift' -require 'thrift/server/nonblockingserver' $:.unshift File.dirname(__FILE__) + "/gen-rb" require 'BenchmarkService' HOST = 'localhost' @@ -39,7 +38,7 @@ end handler = BenchmarkHandler.new processor = ThriftBenchmark::BenchmarkService::Processor.new(handler) transport = Thrift::ServerSocket.new(HOST, PORT) -transportFactory = Thrift::FramedTransportFactory.new +transport_factory = Thrift::FramedTransportFactory.new logger = Logger.new(STDERR) logger.level = Logger::WARN -Thrift::NonblockingServer.new(processor, transport, transportFactory, nil, 20, logger).serve +Thrift::NonblockingServer.new(processor, transport, transport_factory, nil, 20, logger).serve diff --git a/lib/rb/ext/memory_buffer.c b/lib/rb/ext/memory_buffer.c index 12dab312..624012d4 100644 --- a/lib/rb/ext/memory_buffer.c +++ b/lib/rb/ext/memory_buffer.c @@ -59,7 +59,7 @@ VALUE rb_thrift_memory_buffer_read(VALUE self, VALUE length_value) { } void Init_memory_buffer() { - VALUE thrift_memory_buffer_class = rb_const_get(thrift_module, rb_intern("MemoryBuffer")); + VALUE thrift_memory_buffer_class = rb_const_get(thrift_module, rb_intern("MemoryBufferTransport")); rb_define_method(thrift_memory_buffer_class, "write", rb_thrift_memory_buffer_write, 1); rb_define_method(thrift_memory_buffer_class, "read", rb_thrift_memory_buffer_read, 1); diff --git a/lib/rb/ext/protocol.c b/lib/rb/ext/protocol.c index 756f8b2c..c1876541 100644 --- a/lib/rb/ext/protocol.c +++ b/lib/rb/ext/protocol.c @@ -144,7 +144,7 @@ VALUE rb_thift_read_set_end(VALUE self) { } void Init_protocol() { - VALUE c_protocol = rb_const_get(thrift_module, rb_intern("Protocol")); + VALUE c_protocol = rb_const_get(thrift_module, rb_intern("BaseProtocol")); rb_define_method(c_protocol, "skip", rb_thrift_protocol_skip, 1); rb_define_method(c_protocol, "write_message_end", rb_thrift_write_message_end, 0); diff --git a/lib/rb/lib/thrift.rb b/lib/rb/lib/thrift.rb index ec15adc4..88562e13 100644 --- a/lib/rb/lib/thrift.rb +++ b/lib/rb/lib/thrift.rb @@ -25,10 +25,35 @@ require 'thrift/types' require 'thrift/processor' require 'thrift/client' require 'thrift/struct' -require 'thrift/protocol' -require 'thrift/protocol/binaryprotocol' + +# serializer +require 'thrift/serializer/serializer' +require 'thrift/serializer/deserializer' + +# protocol +require 'thrift/protocol/base_protocol' +require 'thrift/protocol/binary_protocol' +require 'thrift/protocol/binary_protocol_accelerated' require 'thrift/protocol/compact_protocol' -require 'thrift/transport' + +# transport +require 'thrift/transport/base_transport' +require 'thrift/transport/base_server_transport' require 'thrift/transport/socket' -require 'thrift/server' -require "thrift/thrift_native" \ No newline at end of file +require 'thrift/transport/server_socket' +require 'thrift/transport/unix_socket' +require 'thrift/transport/unix_server_socket' +require 'thrift/transport/buffered_transport' +require 'thrift/transport/framed_transport' +require 'thrift/transport/http_client_transport' +require 'thrift/transport/io_stream_transport' +require 'thrift/transport/memory_buffer_transport' + +# server +require 'thrift/server/base_server' +require 'thrift/server/nonblocking_server' +require 'thrift/server/simple_server' +require 'thrift/server/threaded_server' +require 'thrift/server/thread_pool_server' + +require 'thrift/thrift_native' \ No newline at end of file diff --git a/lib/rb/lib/thrift/protocol.rb b/lib/rb/lib/thrift/protocol/base_protocol.rb similarity index 99% rename from lib/rb/lib/thrift/protocol.rb rename to lib/rb/lib/thrift/protocol/base_protocol.rb index adeddefd..b19909d5 100644 --- a/lib/rb/lib/thrift/protocol.rb +++ b/lib/rb/lib/thrift/protocol/base_protocol.rb @@ -35,10 +35,9 @@ module Thrift super(message) @type = type end - end - class Protocol + class BaseProtocol attr_reader :trans @@ -281,12 +280,11 @@ module Thrift read_list_end end end - end - class ProtocolFactory + class BaseProtocolFactory def get_protocol(trans) raise NotImplementedError end end -end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/protocol/binaryprotocol.rb b/lib/rb/lib/thrift/protocol/binary_protocol.rb similarity index 97% rename from lib/rb/lib/thrift/protocol/binaryprotocol.rb rename to lib/rb/lib/thrift/protocol/binary_protocol.rb index ca9ffead..04d149ac 100644 --- a/lib/rb/lib/thrift/protocol/binaryprotocol.rb +++ b/lib/rb/lib/thrift/protocol/binary_protocol.rb @@ -17,10 +17,8 @@ # under the License. # -require 'thrift/protocol' - module Thrift - class BinaryProtocol < Protocol + class BinaryProtocol < BaseProtocol VERSION_MASK = 0xffff0000 VERSION_1 = 0x80010000 TYPE_MASK = 0x000000ff @@ -219,7 +217,7 @@ module Thrift end - class BinaryProtocolFactory < ProtocolFactory + class BinaryProtocolFactory < BaseProtocolFactory def get_protocol(trans) return Thrift::BinaryProtocol.new(trans) end diff --git a/lib/rb/lib/thrift/protocol/binaryprotocolaccelerated.rb b/lib/rb/lib/thrift/protocol/binary_protocol_accelerated.rb similarity index 80% rename from lib/rb/lib/thrift/protocol/binaryprotocolaccelerated.rb rename to lib/rb/lib/thrift/protocol/binary_protocol_accelerated.rb index 9bb690de..eaf64f6b 100644 --- a/lib/rb/lib/thrift/protocol/binaryprotocolaccelerated.rb +++ b/lib/rb/lib/thrift/protocol/binary_protocol_accelerated.rb @@ -17,20 +17,17 @@ # under the License. # -require 'thrift/protocol/binaryprotocol' -require 'thrift_native' - =begin -The only change required for a transport to support TBinaryProtocolAccelerated is to implement 2 methods: +The only change required for a transport to support BinaryProtocolAccelerated is to implement 2 methods: * borrow(size), which takes an optional argument and returns atleast _size_ bytes from the transport, or the default buffer size if no argument is given * consume!(size), which removes size bytes from the front of the buffer -See TMemoryBuffer and TBufferedTransport for examples. +See MemoryBuffer and BufferedTransport for examples. =end module Thrift - class BinaryProtocolAcceleratedFactory < ProtocolFactory + class BinaryProtocolAcceleratedFactory < BaseProtocolFactory def get_protocol(trans) BinaryProtocolAccelerated.new(trans) end diff --git a/lib/rb/lib/thrift/protocol/compact_protocol.rb b/lib/rb/lib/thrift/protocol/compact_protocol.rb index 1f8ec5bf..c8f43655 100644 --- a/lib/rb/lib/thrift/protocol/compact_protocol.rb +++ b/lib/rb/lib/thrift/protocol/compact_protocol.rb @@ -17,10 +17,8 @@ # under the License. # -require 'thrift/protocol' - module Thrift - class CompactProtocol < Protocol + class CompactProtocol < BaseProtocol PROTOCOL_ID = [0x82].pack('c').unpack('c').first VERSION = 1 @@ -416,7 +414,7 @@ module Thrift end end - class CompactProtocolFactory < ProtocolFactory + class CompactProtocolFactory < BaseProtocolFactory def get_protocol(trans) CompactProtocol.new(trans) end diff --git a/lib/rb/lib/thrift/serializer/deserializer.rb b/lib/rb/lib/thrift/serializer/deserializer.rb new file mode 100644 index 00000000..d2ee325a --- /dev/null +++ b/lib/rb/lib/thrift/serializer/deserializer.rb @@ -0,0 +1,33 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +module Thrift + class Deserializer + def initialize(protocol_factory = BinaryProtocolFactory.new) + @transport = MemoryBufferTransport.new + @protocol = protocol_factory.get_protocol(@transport) + end + + def deserialize(base, buffer) + @transport.reset_buffer(buffer) + base.read(@protocol) + base + end + end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/serializer.rb b/lib/rb/lib/thrift/serializer/serializer.rb similarity index 67% rename from lib/rb/lib/thrift/serializer.rb rename to lib/rb/lib/thrift/serializer/serializer.rb index 11c754ce..22316395 100644 --- a/lib/rb/lib/thrift/serializer.rb +++ b/lib/rb/lib/thrift/serializer/serializer.rb @@ -19,9 +19,9 @@ module Thrift class Serializer - def initialize(protocolFactory = BinaryProtocolFactory.new) - @transport = MemoryBuffer.new - @protocol = protocolFactory.get_protocol(@transport) + def initialize(protocol_factory = BinaryProtocolFactory.new) + @transport = MemoryBufferTransport.new + @protocol = protocol_factory.get_protocol(@transport) end def serialize(base) @@ -30,17 +30,5 @@ module Thrift @transport.read(@transport.available) end end - - class Deserializer - def initialize(protocolFactory = BinaryProtocolFactory.new) - @transport = MemoryBuffer.new - @protocol = protocolFactory.get_protocol(@transport) - end - - def deserialize(base, buffer) - @transport.reset_buffer(buffer) - base.read(@protocol) - base - end - end end + diff --git a/lib/rb/lib/thrift/server.rb b/lib/rb/lib/thrift/server.rb deleted file mode 100644 index bd428549..00000000 --- a/lib/rb/lib/thrift/server.rb +++ /dev/null @@ -1,142 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# - -require 'thrift/protocol' -require 'thrift/protocol/binaryprotocol' -require 'thrift/transport' - -module Thrift - class Server - def initialize(processor, serverTransport, transportFactory=nil, protocolFactory=nil) - @processor = processor - @serverTransport = serverTransport - @transportFactory = transportFactory ? transportFactory : Thrift::TransportFactory.new - @protocolFactory = protocolFactory ? protocolFactory : Thrift::BinaryProtocolFactory.new - end - - def serve; nil; end - end - - class SimpleServer < Server - def serve - begin - @serverTransport.listen - loop do - client = @serverTransport.accept - trans = @transportFactory.get_transport(client) - prot = @protocolFactory.get_protocol(trans) - begin - loop do - @processor.process(prot, prot) - end - rescue Thrift::TransportException, Thrift::ProtocolException - ensure - trans.close - end - end - ensure - @serverTransport.close - end - end - end -end - -# do *not* use fastthread -# it has a bug that triggers on NonblockingServer -require 'thread' - -module Thrift - class ThreadedServer < Server - def serve - begin - @serverTransport.listen - loop do - client = @serverTransport.accept - trans = @transportFactory.get_transport(client) - prot = @protocolFactory.get_protocol(trans) - Thread.new(prot, trans) do |p, t| - begin - loop do - @processor.process(p, p) - end - rescue Thrift::TransportException, Thrift::ProtocolException - ensure - t.close - end - end - end - ensure - @serverTransport.close - end - end - end - - class ThreadPoolServer < Server - def initialize(processor, serverTransport, transportFactory=nil, protocolFactory=nil, num=20) - super(processor, serverTransport, transportFactory, protocolFactory) - @thread_q = SizedQueue.new(num) - @exception_q = Queue.new - @running = false - end - - ## exceptions that happen in worker threads will be relayed here and - ## must be caught. 'retry' can be used to continue. (threads will - ## continue to run while the exception is being handled.) - def rescuable_serve - Thread.new { serve } unless @running - @running = true - raise @exception_q.pop - end - - ## exceptions that happen in worker threads simply cause that thread - ## to die and another to be spawned in its place. - def serve - @serverTransport.listen - - begin - loop do - @thread_q.push(:token) - Thread.new do - begin - loop do - client = @serverTransport.accept - trans = @transportFactory.get_transport(client) - prot = @protocolFactory.get_protocol(trans) - begin - loop do - @processor.process(prot, prot) - end - rescue Thrift::TransportException, Thrift::ProtocolException => e - ensure - trans.close - end - end - rescue => e - @exception_q.push(e) - ensure - @thread_q.pop # thread died! - end - end - end - ensure - @serverTransport.close - end - end - end -end diff --git a/lib/rb/lib/thrift/server/base_server.rb b/lib/rb/lib/thrift/server/base_server.rb new file mode 100644 index 00000000..1ee12133 --- /dev/null +++ b/lib/rb/lib/thrift/server/base_server.rb @@ -0,0 +1,31 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +module Thrift + class BaseServer + def initialize(processor, server_transport, transport_factory=nil, protocol_factory=nil) + @processor = processor + @server_transport = server_transport + @transport_factory = transport_factory ? transport_factory : Thrift::BaseTransportFactory.new + @protocol_factory = protocol_factory ? protocol_factory : Thrift::BinaryProtocolFactory.new + end + + def serve; nil; end + end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/server/httpserver.rb b/lib/rb/lib/thrift/server/mongrel_http_server.rb similarity index 93% rename from lib/rb/lib/thrift/server/httpserver.rb rename to lib/rb/lib/thrift/server/mongrel_http_server.rb index f26329f4..84eacf0d 100644 --- a/lib/rb/lib/thrift/server/httpserver.rb +++ b/lib/rb/lib/thrift/server/mongrel_http_server.rb @@ -17,15 +17,11 @@ # under the License. # -require 'thrift/protocol' -require 'thrift/protocol/binaryprotocol' -require 'thrift/transport' - require 'mongrel' ## Sticks a service on a URL, using mongrel to do the HTTP work module Thrift - class SimpleMongrelHTTPServer + class MongrelHTTPServer < BaseServer class Handler < Mongrel::HttpHandler def initialize(processor, protocol_factory) @processor = processor diff --git a/lib/rb/lib/thrift/server/nonblockingserver.rb b/lib/rb/lib/thrift/server/nonblocking_server.rb similarity index 83% rename from lib/rb/lib/thrift/server/nonblockingserver.rb rename to lib/rb/lib/thrift/server/nonblocking_server.rb index 9689b8ff..5425f6de 100644 --- a/lib/rb/lib/thrift/server/nonblockingserver.rb +++ b/lib/rb/lib/thrift/server/nonblocking_server.rb @@ -17,15 +17,14 @@ # under the License. # -require 'thrift/server' require 'logger' require 'thread' module Thrift # this class expects to always use a FramedTransport for reading messages - class NonblockingServer < Server - def initialize(processor, serverTransport, transportFactory=nil, protocolFactory=nil, num=20, logger = nil) - super(processor, serverTransport, transportFactory, protocolFactory) + class NonblockingServer < BaseServer + def initialize(processor, server_transport, transport_factory=nil, protocol_factory=nil, num=20, logger=nil) + super(processor, server_transport, transport_factory, protocol_factory) @num_threads = num if logger.nil? @logger = Logger.new(STDERR) @@ -39,15 +38,15 @@ module Thrift def serve @logger.info "Starting #{self}" - @serverTransport.listen + @server_transport.listen @io_manager = start_io_manager begin loop do - break if @serverTransport.closed? - rd, = select([@serverTransport], nil, nil, 0.1) + break if @server_transport.closed? + rd, = select([@server_transport], nil, nil, 0.1) next if rd.nil? - socket = @serverTransport.accept + socket = @server_transport.accept @logger.debug "Accepted socket: #{socket.inspect}" @io_manager.add_connection socket end @@ -57,7 +56,7 @@ module Thrift @logger.info "#{self} is shutting down, goodbye" ensure @transport_semaphore.synchronize do - @serverTransport.close + @server_transport.close end @io_manager.ensure_closed unless @io_manager.nil? end @@ -72,7 +71,7 @@ module Thrift shutdown_proc = lambda do @io_manager.shutdown(timeout) @transport_semaphore.synchronize do - @serverTransport.close # this will break the accept loop + @server_transport.close # this will break the accept loop end end if block @@ -85,7 +84,7 @@ module Thrift private def start_io_manager - iom = IOManager.new(@processor, @serverTransport, @transportFactory, @protocolFactory, @num_threads, @logger) + iom = IOManager.new(@processor, @server_transport, @transport_factory, @protocol_factory, @num_threads, @logger) iom.spawn iom end @@ -93,11 +92,11 @@ module Thrift class IOManager # :nodoc: DEFAULT_BUFFER = 2**20 - def initialize(processor, serverTransport, transportFactory, protocolFactory, num, logger) + def initialize(processor, server_transport, transport_factory, protocol_factory, num, logger) @processor = processor - @serverTransport = serverTransport - @transportFactory = transportFactory - @protocolFactory = protocolFactory + @server_transport = server_transport + @transport_factory = transport_factory + @protocol_factory = protocol_factory @num_threads = num @logger = logger @connections = [] @@ -177,7 +176,7 @@ module Thrift end def spin_thread - Worker.new(@processor, @transportFactory, @protocolFactory, @logger, @worker_queue).spawn + Worker.new(@processor, @transport_factory, @protocol_factory, @logger, @worker_queue).spawn end def signal(msg) @@ -252,10 +251,10 @@ module Thrift end class Worker # :nodoc: - def initialize(processor, transportFactory, protocolFactory, logger, queue) + def initialize(processor, transport_factory, protocol_factory, logger, queue) @processor = processor - @transportFactory = transportFactory - @protocolFactory = protocolFactory + @transport_factory = transport_factory + @protocol_factory = protocol_factory @logger = logger @queue = queue end @@ -279,11 +278,11 @@ module Thrift when :frame fd, frame = args begin - otrans = @transportFactory.get_transport(fd) - oprot = @protocolFactory.get_protocol(otrans) - membuf = MemoryBuffer.new(frame) - itrans = @transportFactory.get_transport(membuf) - iprot = @protocolFactory.get_protocol(itrans) + otrans = @transport_factory.get_transport(fd) + oprot = @protocol_factory.get_protocol(otrans) + membuf = MemoryBufferTransport.new(frame) + itrans = @transport_factory.get_transport(membuf) + iprot = @protocol_factory.get_protocol(itrans) @processor.process(iprot, oprot) rescue => e @logger.error "#{Thread.current.inspect} raised error: #{e.inspect}\n#{e.backtrace.join("\n")}" diff --git a/lib/rb/lib/thrift/server/simple_server.rb b/lib/rb/lib/thrift/server/simple_server.rb new file mode 100644 index 00000000..21e86592 --- /dev/null +++ b/lib/rb/lib/thrift/server/simple_server.rb @@ -0,0 +1,43 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +module Thrift + class SimpleServer < BaseServer + def serve + begin + @server_transport.listen + loop do + client = @server_transport.accept + trans = @transport_factory.get_transport(client) + prot = @protocol_factory.get_protocol(trans) + begin + loop do + @processor.process(prot, prot) + end + rescue Thrift::TransportException, Thrift::ProtocolException + ensure + trans.close + end + end + ensure + @server_transport.close + end + end + end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/server/thread_pool_server.rb b/lib/rb/lib/thrift/server/thread_pool_server.rb new file mode 100644 index 00000000..8cec805a --- /dev/null +++ b/lib/rb/lib/thrift/server/thread_pool_server.rb @@ -0,0 +1,75 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require 'thread' + +module Thrift + class ThreadPoolServer < BaseServer + def initialize(processor, server_transport, transport_factory=nil, protocol_factory=nil, num=20) + super(processor, server_transport, transport_factory, protocol_factory) + @thread_q = SizedQueue.new(num) + @exception_q = Queue.new + @running = false + end + + ## exceptions that happen in worker threads will be relayed here and + ## must be caught. 'retry' can be used to continue. (threads will + ## continue to run while the exception is being handled.) + def rescuable_serve + Thread.new { serve } unless @running + @running = true + raise @exception_q.pop + end + + ## exceptions that happen in worker threads simply cause that thread + ## to die and another to be spawned in its place. + def serve + @server_transport.listen + + begin + loop do + @thread_q.push(:token) + Thread.new do + begin + loop do + client = @server_transport.accept + trans = @transport_factory.get_transport(client) + prot = @protocol_factory.get_protocol(trans) + begin + loop do + @processor.process(prot, prot) + end + rescue Thrift::TransportException, Thrift::ProtocolException => e + ensure + trans.close + end + end + rescue => e + @exception_q.push(e) + ensure + @thread_q.pop # thread died! + end + end + end + ensure + @server_transport.close + end + end + end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/server/threaded_server.rb b/lib/rb/lib/thrift/server/threaded_server.rb new file mode 100644 index 00000000..a2c917cb --- /dev/null +++ b/lib/rb/lib/thrift/server/threaded_server.rb @@ -0,0 +1,47 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require 'thread' + +module Thrift + class ThreadedServer < BaseServer + def serve + begin + @server_transport.listen + loop do + client = @server_transport.accept + trans = @transport_factory.get_transport(client) + prot = @protocol_factory.get_protocol(trans) + Thread.new(prot, trans) do |p, t| + begin + loop do + @processor.process(p, p) + end + rescue Thrift::TransportException, Thrift::ProtocolException + ensure + t.close + end + end + end + ensure + @server_transport.close + end + end + end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb index 9789a41e..01aae56b 100644 --- a/lib/rb/lib/thrift/struct.rb +++ b/lib/rb/lib/thrift/struct.rb @@ -17,7 +17,6 @@ # under the License. # -require 'thrift/types' require 'set' module Thrift diff --git a/lib/rb/lib/thrift/transport.rb b/lib/rb/lib/thrift/transport.rb deleted file mode 100644 index 4aa50d03..00000000 --- a/lib/rb/lib/thrift/transport.rb +++ /dev/null @@ -1,300 +0,0 @@ -# encoding: ascii-8bit -# -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# - -module Thrift - class TransportException < Exception - UNKNOWN = 0 - NOT_OPEN = 1 - ALREADY_OPEN = 2 - TIMED_OUT = 3 - END_OF_FILE = 4 - - attr_reader :type - - def initialize(type=UNKNOWN, message=nil) - super(message) - @type = type - end - end - - class Transport - def open?; end - - def open; end - - def close; end - - def read(sz) - raise NotImplementedError - end - - def read_all(size) - buf = '' - - while (buf.length < size) - chunk = read(size - buf.length) - buf << chunk - end - - buf - end - - def write(buf); end - alias_method :<<, :write - - def flush; end - end - - class ServerTransport - def listen - raise NotImplementedError - end - - def accept - raise NotImplementedError - end - - def close; nil; end - - def closed? - raise NotImplementedError - end - end - - class TransportFactory - def get_transport(trans) - return trans - end - end - - class BufferedTransport < Transport - DEFAULT_BUFFER = 4096 - - def initialize(transport) - @transport = transport - @wbuf = '' - @rbuf = '' - @index = 0 - end - - def open? - return @transport.open? - end - - def open - @transport.open - end - - def close - flush - @transport.close - end - - def read(sz) - @index += sz - ret = @rbuf.slice(@index - sz, sz) || '' - - if ret.length == 0 - @rbuf = @transport.read([sz, DEFAULT_BUFFER].max) - @index = sz - ret = @rbuf.slice(0, sz) || '' - end - - ret - end - - def write(buf) - @wbuf << buf - end - - def flush - if @wbuf != '' - @transport.write(@wbuf) - @wbuf = '' - end - - @transport.flush - end - end - - class BufferedTransportFactory < TransportFactory - def get_transport(transport) - return BufferedTransport.new(transport) - end - end - - class FramedTransport < Transport - def initialize(transport, read=true, write=true) - @transport = transport - @rbuf = '' - @wbuf = '' - @read = read - @write = write - @index = 0 - end - - def open? - @transport.open? - end - - def open - @transport.open - end - - def close - @transport.close - end - - def read(sz) - return @transport.read(sz) unless @read - - return '' if sz <= 0 - - read_frame if @index >= @rbuf.length - - @index += sz - @rbuf.slice(@index - sz, sz) || '' - end - - def write(buf,sz=nil) - return @transport.write(buf) unless @write - - @wbuf << (sz ? buf[0...sz] : buf) - end - - # - # Writes the output buffer to the stream in the format of a 4-byte length - # followed by the actual data. - # - def flush - return @transport.flush unless @write - - out = [@wbuf.length].pack('N') - out << @wbuf - @transport.write(out) - @transport.flush - @wbuf = '' - end - - private - - def read_frame - sz = @transport.read_all(4).unpack('N').first - - @index = 0 - @rbuf = @transport.read_all(sz) - end - end - - class FramedTransportFactory < TransportFactory - def get_transport(transport) - return FramedTransport.new(transport) - end - end - - class MemoryBuffer < Transport - GARBAGE_BUFFER_SIZE = 4*(2**10) # 4kB - - # If you pass a string to this, you should #dup that string - # unless you want it to be modified by #read and #write - #-- - # this behavior is no longer required. If you wish to change it - # go ahead, just make sure the specs pass - def initialize(buffer = nil) - @buf = buffer || '' - @index = 0 - end - - def open? - return true - end - - def open - end - - def close - end - - def peek - @index < @buf.size - end - - # this method does not use the passed object directly but copies it - def reset_buffer(new_buf = '') - @buf.replace new_buf - @index = 0 - end - - def available - @buf.length - @index - end - - def read(len) - data = @buf.slice(@index, len) - @index += len - @index = @buf.size if @index > @buf.size - if @index >= GARBAGE_BUFFER_SIZE - @buf = @buf.slice(@index..-1) - @index = 0 - end - data - end - - def write(wbuf) - @buf << wbuf - end - - def flush - end - - def inspect_buffer - out = [] - for idx in 0...(@buf.size) - # if idx != 0 - # out << " " - # end - - if idx == @index - out << ">" - end - - out << @buf[idx].to_s(16) - end - out.join(" ") - end - end - - ## Very very simple implementation of wrapping two objects, one with a #read - ## method and one with a #write method, into a transport for thrift. - ## - ## Assumes both objects are open, remain open, don't require flushing, etc. - class IOStreamTransport < Transport - def initialize(input, output) - @input = input - @output = output - end - - def open?; not @input.closed? or not @output.closed? end - def read(sz); @input.read(sz) end - def write(buf); @output.write(buf) end - def close; @input.close; @output.close end - def to_io; @input end # we're assuming this is used in a IO.select for reading - end -end diff --git a/lib/rb/lib/thrift/transport/base_server_transport.rb b/lib/rb/lib/thrift/transport/base_server_transport.rb new file mode 100644 index 00000000..68c5af07 --- /dev/null +++ b/lib/rb/lib/thrift/transport/base_server_transport.rb @@ -0,0 +1,37 @@ +# encoding: ascii-8bit +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +module Thrift + class BaseServerTransport + def listen + raise NotImplementedError + end + + def accept + raise NotImplementedError + end + + def close; nil; end + + def closed? + raise NotImplementedError + end + end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/transport/base_transport.rb b/lib/rb/lib/thrift/transport/base_transport.rb new file mode 100644 index 00000000..08a71dab --- /dev/null +++ b/lib/rb/lib/thrift/transport/base_transport.rb @@ -0,0 +1,70 @@ +# encoding: ascii-8bit +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +module Thrift + class TransportException < Exception + UNKNOWN = 0 + NOT_OPEN = 1 + ALREADY_OPEN = 2 + TIMED_OUT = 3 + END_OF_FILE = 4 + + attr_reader :type + + def initialize(type=UNKNOWN, message=nil) + super(message) + @type = type + end + end + + class BaseTransport + def open?; end + + def open; end + + def close; end + + def read(sz) + raise NotImplementedError + end + + def read_all(size) + buf = '' + + while (buf.length < size) + chunk = read(size - buf.length) + buf << chunk + end + + buf + end + + def write(buf); end + alias_method :<<, :write + + def flush; end + end + + class BaseTransportFactory + def get_transport(trans) + return trans + end + end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/transport/buffered_transport.rb b/lib/rb/lib/thrift/transport/buffered_transport.rb new file mode 100644 index 00000000..8dead4e0 --- /dev/null +++ b/lib/rb/lib/thrift/transport/buffered_transport.rb @@ -0,0 +1,77 @@ +# encoding: ascii-8bit +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +module Thrift + class BufferedTransport < BaseTransport + DEFAULT_BUFFER = 4096 + + def initialize(transport) + @transport = transport + @wbuf = '' + @rbuf = '' + @index = 0 + end + + def open? + return @transport.open? + end + + def open + @transport.open + end + + def close + flush + @transport.close + end + + def read(sz) + @index += sz + ret = @rbuf.slice(@index - sz, sz) || '' + + if ret.length == 0 + @rbuf = @transport.read([sz, DEFAULT_BUFFER].max) + @index = sz + ret = @rbuf.slice(0, sz) || '' + end + + ret + end + + def write(buf) + @wbuf << buf + end + + def flush + if @wbuf != '' + @transport.write(@wbuf) + @wbuf = '' + end + + @transport.flush + end + end + + class BufferedTransportFactory < BaseTransportFactory + def get_transport(transport) + return BufferedTransport.new(transport) + end + end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/transport/framed_transport.rb b/lib/rb/lib/thrift/transport/framed_transport.rb new file mode 100644 index 00000000..558af744 --- /dev/null +++ b/lib/rb/lib/thrift/transport/framed_transport.rb @@ -0,0 +1,90 @@ +# encoding: ascii-8bit +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +module Thrift + class FramedTransport < BaseTransport + def initialize(transport, read=true, write=true) + @transport = transport + @rbuf = '' + @wbuf = '' + @read = read + @write = write + @index = 0 + end + + def open? + @transport.open? + end + + def open + @transport.open + end + + def close + @transport.close + end + + def read(sz) + return @transport.read(sz) unless @read + + return '' if sz <= 0 + + read_frame if @index >= @rbuf.length + + @index += sz + @rbuf.slice(@index - sz, sz) || '' + end + + def write(buf,sz=nil) + return @transport.write(buf) unless @write + + @wbuf << (sz ? buf[0...sz] : buf) + end + + # + # Writes the output buffer to the stream in the format of a 4-byte length + # followed by the actual data. + # + def flush + return @transport.flush unless @write + + out = [@wbuf.length].pack('N') + out << @wbuf + @transport.write(out) + @transport.flush + @wbuf = '' + end + + private + + def read_frame + sz = @transport.read_all(4).unpack('N').first + + @index = 0 + @rbuf = @transport.read_all(sz) + end + end + + class FramedTransportFactory < BaseTransportFactory + def get_transport(transport) + return FramedTransport.new(transport) + end + end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/transport/httpclient.rb b/lib/rb/lib/thrift/transport/http_client_transport.rb similarity index 93% rename from lib/rb/lib/thrift/transport/httpclient.rb rename to lib/rb/lib/thrift/transport/http_client_transport.rb index 8a62ca20..a190a983 100644 --- a/lib/rb/lib/thrift/transport/httpclient.rb +++ b/lib/rb/lib/thrift/transport/http_client_transport.rb @@ -1,3 +1,4 @@ +# encoding: ascii-8bit # # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file @@ -17,16 +18,13 @@ # under the License. # -require 'thrift/transport' - require 'net/http' require 'net/https' require 'uri' require 'stringio' -## Very simple HTTP client module Thrift - class HTTPClient < Transport + class HTTPClientTransport < BaseTransport def initialize(url) @url = URI url @outbuf = "" diff --git a/lib/rb/lib/thrift/transport/io_stream_transport.rb b/lib/rb/lib/thrift/transport/io_stream_transport.rb new file mode 100644 index 00000000..be348aa0 --- /dev/null +++ b/lib/rb/lib/thrift/transport/io_stream_transport.rb @@ -0,0 +1,39 @@ +# encoding: ascii-8bit +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +# Very very simple implementation of wrapping two objects, one with a #read +# method and one with a #write method, into a transport for thrift. +# +# Assumes both objects are open, remain open, don't require flushing, etc. +# +module Thrift + class IOStreamTransport < BaseTransport + def initialize(input, output) + @input = input + @output = output + end + + def open?; not @input.closed? or not @output.closed? end + def read(sz); @input.read(sz) end + def write(buf); @output.write(buf) end + def close; @input.close; @output.close end + def to_io; @input end # we're assuming this is used in a IO.select for reading + end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/transport/memory_buffer_transport.rb b/lib/rb/lib/thrift/transport/memory_buffer_transport.rb new file mode 100644 index 00000000..33d732d1 --- /dev/null +++ b/lib/rb/lib/thrift/transport/memory_buffer_transport.rb @@ -0,0 +1,93 @@ +# encoding: ascii-8bit +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +module Thrift + class MemoryBufferTransport < BaseTransport + GARBAGE_BUFFER_SIZE = 4*(2**10) # 4kB + + # If you pass a string to this, you should #dup that string + # unless you want it to be modified by #read and #write + #-- + # this behavior is no longer required. If you wish to change it + # go ahead, just make sure the specs pass + def initialize(buffer = nil) + @buf = buffer || '' + @index = 0 + end + + def open? + return true + end + + def open + end + + def close + end + + def peek + @index < @buf.size + end + + # this method does not use the passed object directly but copies it + def reset_buffer(new_buf = '') + @buf.replace new_buf + @index = 0 + end + + def available + @buf.length - @index + end + + def read(len) + data = @buf.slice(@index, len) + @index += len + @index = @buf.size if @index > @buf.size + if @index >= GARBAGE_BUFFER_SIZE + @buf = @buf.slice(@index..-1) + @index = 0 + end + data + end + + def write(wbuf) + @buf << wbuf + end + + def flush + end + + def inspect_buffer + out = [] + for idx in 0...(@buf.size) + # if idx != 0 + # out << " " + # end + + if idx == @index + out << ">" + end + + out << @buf[idx].to_s(16) + end + out.join(" ") + end + end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/transport/server_socket.rb b/lib/rb/lib/thrift/transport/server_socket.rb new file mode 100644 index 00000000..7feb9ab0 --- /dev/null +++ b/lib/rb/lib/thrift/transport/server_socket.rb @@ -0,0 +1,63 @@ +# encoding: ascii-8bit +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require 'socket' + +module Thrift + class ServerSocket < BaseServerTransport + # call-seq: initialize(host = nil, port) + def initialize(host_or_port, port = nil) + if port + @host = host_or_port + @port = port + else + @host = nil + @port = host_or_port + end + @handle = nil + end + + attr_reader :handle + + def listen + @handle = TCPServer.new(@host, @port) + end + + def accept + unless @handle.nil? + sock = @handle.accept + trans = Socket.new + trans.handle = sock + trans + end + end + + def close + @handle.close unless @handle.nil? or @handle.closed? + @handle = nil + end + + def closed? + @handle.nil? or @handle.closed? + end + + alias to_io handle + end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/transport/socket.rb b/lib/rb/lib/thrift/transport/socket.rb index 49247ce4..06c937e5 100644 --- a/lib/rb/lib/thrift/transport/socket.rb +++ b/lib/rb/lib/thrift/transport/socket.rb @@ -1,3 +1,4 @@ +# encoding: ascii-8bit # # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file @@ -16,11 +17,11 @@ # specific language governing permissions and limitations # under the License. # -require 'thrift/transport' + require 'socket' module Thrift - class Socket < Transport + class Socket < BaseTransport def initialize(host='localhost', port=9090, timeout=nil) @host = host @port = port @@ -132,44 +133,4 @@ module Thrift @handle end end - - class ServerSocket < ServerTransport - # call-seq: initialize(host = nil, port) - def initialize(host_or_port, port = nil) - if port - @host = host_or_port - @port = port - else - @host = nil - @port = host_or_port - end - @handle = nil - end - - attr_reader :handle - - def listen - @handle = TCPServer.new(@host, @port) - end - - def accept - unless @handle.nil? - sock = @handle.accept - trans = Socket.new - trans.handle = sock - trans - end - end - - def close - @handle.close unless @handle.nil? or @handle.closed? - @handle = nil - end - - def closed? - @handle.nil? or @handle.closed? - end - - alias to_io handle - end -end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/transport/unixsocket.rb b/lib/rb/lib/thrift/transport/unix_server_socket.rb similarity index 75% rename from lib/rb/lib/thrift/transport/unixsocket.rb rename to lib/rb/lib/thrift/transport/unix_server_socket.rb index 60931b90..a135d25f 100644 --- a/lib/rb/lib/thrift/transport/unixsocket.rb +++ b/lib/rb/lib/thrift/transport/unix_server_socket.rb @@ -1,3 +1,4 @@ +# encoding: ascii-8bit # # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file @@ -17,28 +18,10 @@ # under the License. # -require 'thrift/transport' require 'socket' module Thrift - class UNIXSocket < Socket - def initialize(path, timeout=nil) - @path = path - @timeout = timeout - @desc = @path # for read()'s error - @handle = nil - end - - def open - begin - @handle = ::UNIXSocket.new(@path) - rescue StandardError - raise TransportException.new(TransportException::NOT_OPEN, "Could not open UNIX socket at #{@path}") - end - end - end - - class UNIXServerSocket < ServerTransport + class UNIXServerSocket < BaseServerTransport def initialize(path) @path = path @handle = nil @@ -74,4 +57,4 @@ module Thrift alias to_io handle end -end +end \ No newline at end of file diff --git a/lib/rb/lib/thrift/transport/unix_socket.rb b/lib/rb/lib/thrift/transport/unix_socket.rb new file mode 100644 index 00000000..8f692e4c --- /dev/null +++ b/lib/rb/lib/thrift/transport/unix_socket.rb @@ -0,0 +1,40 @@ +# encoding: ascii-8bit +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require 'socket' + +module Thrift + class UNIXSocket < Socket + def initialize(path, timeout=nil) + @path = path + @timeout = timeout + @desc = @path # for read()'s error + @handle = nil + end + + def open + begin + @handle = ::UNIXSocket.new(@path) + rescue StandardError + raise TransportException.new(TransportException::NOT_OPEN, "Could not open UNIX socket at #{@path}") + end + end + end +end \ No newline at end of file diff --git a/lib/rb/script/proto_benchmark.rb b/lib/rb/script/proto_benchmark.rb index 09adf819..4ff6a550 100644 --- a/lib/rb/script/proto_benchmark.rb +++ b/lib/rb/script/proto_benchmark.rb @@ -19,7 +19,7 @@ require File.dirname(__FILE__) + "/../spec/spec_helper.rb" require "lib/thrift/serializer" -require "lib/thrift/protocol/binaryprotocolaccelerated" +require "lib/thrift/protocol/binary_protocol_accelerated" require "benchmark" # require "ruby-prof" @@ -83,7 +83,7 @@ Benchmark.bm(60) do |reporter| # f = File.new("/tmp/testfile", "w") - # proto = Thrift::BinaryProtocolAccelerated.new(Thrift::IOStreamTransport.new(Thrift::MemoryBuffer.new, f)) + # proto = Thrift::BinaryProtocolAccelerated.new(Thrift::IOStreamTransport.new(Thrift::MemoryBufferTransport.new, f)) # reporter.report("accelerated binary protocol, write (to disk)") do # HOW_MANY.times do # obj.write(proto) @@ -93,7 +93,7 @@ Benchmark.bm(60) do |reporter| # f.close # # f = File.new("/tmp/testfile", "r") - # proto = Thrift::BinaryProtocolAccelerated.new(Thrift::IOStreamTransport.new(f, Thrift::MemoryBuffer.new)) + # proto = Thrift::BinaryProtocolAccelerated.new(Thrift::IOStreamTransport.new(f, Thrift::MemoryBufferTransport.new)) # reporter.report("accelerated binary protocol, read (from disk)") do # HOW_MANY.times do # obj.read(proto) @@ -103,7 +103,7 @@ Benchmark.bm(60) do |reporter| # # f = File.new("/tmp/testfile", "w") # reporter.report("compact protocol, write (to disk)") do - # proto = Thrift::CompactProtocol.new(Thrift::IOStreamTransport.new(Thrift::MemoryBuffer.new, f)) + # proto = Thrift::CompactProtocol.new(Thrift::IOStreamTransport.new(Thrift::MemoryBufferTransport.new, f)) # HOW_MANY.times do # obj.write(proto) # end @@ -113,7 +113,7 @@ Benchmark.bm(60) do |reporter| # # f = File.new("/tmp/testfile", "r") # reporter.report("compact protocol, read (from disk)") do - # proto = Thrift::CompactProtocol.new(Thrift::IOStreamTransport.new(f, Thrift::MemoryBuffer.new)) + # proto = Thrift::CompactProtocol.new(Thrift::IOStreamTransport.new(f, Thrift::MemoryBufferTransport.new)) # HOW_MANY.times do # obj.read(proto) # end diff --git a/lib/rb/script/read_struct.rb b/lib/rb/script/read_struct.rb index fd2dfbef..831fcec9 100644 --- a/lib/rb/script/read_struct.rb +++ b/lib/rb/script/read_struct.rb @@ -18,7 +18,6 @@ # require "spec/spec_helper" -require "lib/thrift/serializer" path, factory_class = ARGV diff --git a/lib/rb/script/write_struct.rb b/lib/rb/script/write_struct.rb index b4bdec1e..da142197 100644 --- a/lib/rb/script/write_struct.rb +++ b/lib/rb/script/write_struct.rb @@ -18,7 +18,6 @@ # require "spec/spec_helper" -require "lib/thrift/serializer" path, factory_class = ARGV diff --git a/lib/rb/spec/protocol_spec.rb b/lib/rb/spec/base_protocol_spec.rb similarity index 95% rename from lib/rb/spec/protocol_spec.rb rename to lib/rb/spec/base_protocol_spec.rb index 57b64ab5..efb16d8c 100644 --- a/lib/rb/spec/protocol_spec.rb +++ b/lib/rb/spec/base_protocol_spec.rb @@ -19,15 +19,15 @@ require File.dirname(__FILE__) + '/spec_helper' -class ThriftProtocolSpec < Spec::ExampleGroup +class ThriftBaseProtocolSpec < Spec::ExampleGroup include Thrift before(:each) do @trans = mock("MockTransport") - @prot = Protocol.new(@trans) + @prot = BaseProtocol.new(@trans) end - describe Protocol do + describe BaseProtocol do # most of the methods are stubs, so we can ignore them it "should make trans accessible" do @@ -151,10 +151,10 @@ class ThriftProtocolSpec < Spec::ExampleGroup end end - describe ProtocolFactory do + describe BaseProtocolFactory do it "should raise NotImplementedError" do # returning nil since Protocol is just an abstract class - lambda {ProtocolFactory.new.get_protocol(mock("MockTransport"))}.should raise_error(NotImplementedError) + lambda {BaseProtocolFactory.new.get_protocol(mock("MockTransport"))}.should raise_error(NotImplementedError) end end end diff --git a/lib/rb/spec/transport_spec.rb b/lib/rb/spec/base_transport_spec.rb similarity index 94% rename from lib/rb/spec/transport_spec.rb rename to lib/rb/spec/base_transport_spec.rb index 709a93eb..71897759 100644 --- a/lib/rb/spec/transport_spec.rb +++ b/lib/rb/spec/base_transport_spec.rb @@ -19,7 +19,7 @@ require File.dirname(__FILE__) + '/spec_helper' -class ThriftTransportSpec < Spec::ExampleGroup +class ThriftBaseTransportSpec < Spec::ExampleGroup include Thrift describe TransportException do @@ -30,9 +30,9 @@ class ThriftTransportSpec < Spec::ExampleGroup end end - describe Transport do + describe BaseTransport do it "should read the specified size" do - transport = Transport.new + transport = BaseTransport.new transport.should_receive(:read).with(40).ordered.and_return("10 letters") transport.should_receive(:read).with(30).ordered.and_return("fifteen letters") transport.should_receive(:read).with(15).ordered.and_return("more characters") @@ -42,27 +42,27 @@ class ThriftTransportSpec < Spec::ExampleGroup it "should stub out the rest of the methods" do # can't test for stubbiness, so just make sure they're defined [:open?, :open, :close, :read, :write, :flush].each do |sym| - Transport.method_defined?(sym).should be_true + BaseTransport.method_defined?(sym).should be_true end end it "should alias << to write" do - Transport.instance_method(:<<).should == Transport.instance_method(:write) + BaseTransport.instance_method(:<<).should == BaseTransport.instance_method(:write) end end - describe ServerTransport do + describe BaseServerTransport do it "should stub out its methods" do [:listen, :accept, :close].each do |sym| - ServerTransport.method_defined?(sym).should be_true + BaseServerTransport.method_defined?(sym).should be_true end end end - describe TransportFactory do + describe BaseTransportFactory do it "should return the transport it's given" do transport = mock("Transport") - TransportFactory.new.get_transport(transport).should eql(transport) + BaseTransportFactory.new.get_transport(transport).should eql(transport) end end @@ -250,14 +250,14 @@ class ThriftTransportSpec < Spec::ExampleGroup end end - describe MemoryBuffer do + describe MemoryBufferTransport do before(:each) do - @buffer = MemoryBuffer.new + @buffer = MemoryBufferTransport.new end it "should accept a buffer on input and use it directly" do s = "this is a test" - @buffer = MemoryBuffer.new(s) + @buffer = MemoryBufferTransport.new(s) @buffer.read(4).should == "this" s.slice!(-4..-1) @buffer.read(@buffer.available).should == " is a " diff --git a/lib/rb/spec/binaryprotocolaccelerated_spec.rb b/lib/rb/spec/binary_protocol_accelerated_spec.rb similarity index 92% rename from lib/rb/spec/binaryprotocolaccelerated_spec.rb rename to lib/rb/spec/binary_protocol_accelerated_spec.rb index f8f9b904..a834f7c8 100644 --- a/lib/rb/spec/binaryprotocolaccelerated_spec.rb +++ b/lib/rb/spec/binary_protocol_accelerated_spec.rb @@ -18,8 +18,7 @@ # require File.dirname(__FILE__) + '/spec_helper' -require 'thrift/protocol/binaryprotocolaccelerated' -require File.dirname(__FILE__) + '/binaryprotocol_spec_shared' +require File.dirname(__FILE__) + '/binary_protocol_spec_shared' require File.dirname(__FILE__) + '/gen-rb/ThriftSpec_types' class ThriftBinaryProtocolAcceleratedSpec < Spec::ExampleGroup diff --git a/lib/rb/spec/binaryprotocol_spec.rb b/lib/rb/spec/binary_protocol_spec.rb similarity index 95% rename from lib/rb/spec/binaryprotocol_spec.rb rename to lib/rb/spec/binary_protocol_spec.rb index 3a0d691b..0abccb89 100644 --- a/lib/rb/spec/binaryprotocol_spec.rb +++ b/lib/rb/spec/binary_protocol_spec.rb @@ -18,8 +18,7 @@ # require File.dirname(__FILE__) + '/spec_helper' -require 'thrift/protocol/binaryprotocol' -require File.dirname(__FILE__) + '/binaryprotocol_spec_shared' +require File.dirname(__FILE__) + '/binary_protocol_spec_shared' class ThriftBinaryProtocolSpec < Spec::ExampleGroup include Thrift diff --git a/lib/rb/spec/binaryprotocol_spec_shared.rb b/lib/rb/spec/binary_protocol_spec_shared.rb similarity index 94% rename from lib/rb/spec/binaryprotocol_spec_shared.rb rename to lib/rb/spec/binary_protocol_spec_shared.rb index 18ea8e8d..c6608e01 100644 --- a/lib/rb/spec/binaryprotocol_spec_shared.rb +++ b/lib/rb/spec/binary_protocol_spec_shared.rb @@ -21,7 +21,7 @@ require File.dirname(__FILE__) + '/spec_helper' shared_examples_for 'a binary protocol' do before(:each) do - @trans = Thrift::MemoryBuffer.new + @trans = Thrift::MemoryBufferTransport.new @prot = protocol_class.new(@trans) end @@ -203,17 +203,18 @@ shared_examples_for 'a binary protocol' do lambda { @prot.write_string(nil) }.should raise_error end - it "should read message header correctly" do - @trans.write([protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::CALL, "testMessage".size, "testMessage", 17].pack("NNa11N")) - @prot.read_message_begin().should == ['testMessage', Thrift::MessageTypes::CALL, 17] + it "should write the message header without version when writes are not strict" do + @prot = protocol_class.new(@trans, true, false) # no strict write + @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17) + @trans.read(1000).should == "\000\000\000\vtestMessage\001\000\000\000\021" end - - it "should read the message header without version when writes are not strict" do - @prot = protocol_class.new(@trans, false, true) # no strict write - @trans.write("\000\000\000\vtestMessage\001\000\000\000\021") - @prot.read_message_begin().should == ['testMessage', Thrift::MessageTypes::CALL, 17] + + it "should write the message header with a version when writes are strict" do + @prot = protocol_class.new(@trans) # strict write + @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17) + @trans.read(1000).should == "\200\001\000\001\000\000\000\vtestMessage\000\000\000\021" end - + # message footer is a noop it "should read a field header" do @@ -345,7 +346,7 @@ shared_examples_for 'a binary protocol' do processor = Srv::Processor.new(SrvHandler.new) client = Srv::Client.new(clientproto, clientproto) - + # first block firstblock.call(client) @@ -371,5 +372,4 @@ shared_examples_for 'a binary protocol' do Fixtures::COMPACT_PROTOCOL_TEST_STRUCT end end - end diff --git a/lib/rb/spec/compact_protocol_spec.rb b/lib/rb/spec/compact_protocol_spec.rb index 9071fb1d..78b1a2a2 100644 --- a/lib/rb/spec/compact_protocol_spec.rb +++ b/lib/rb/spec/compact_protocol_spec.rb @@ -18,7 +18,6 @@ # require File.dirname(__FILE__) + '/spec_helper' -require "thrift/protocol/compact_protocol" describe Thrift::CompactProtocol do TESTS = { @@ -36,7 +35,7 @@ describe Thrift::CompactProtocol do TESTS.each_pair do |primitive_type, test_values| test_values.each do |value| # puts "testing #{value}" if primitive_type == :i64 - trans = Thrift::MemoryBuffer.new + trans = Thrift::MemoryBufferTransport.new proto = Thrift::CompactProtocol.new(trans) proto.send(writer(primitive_type), value) @@ -53,7 +52,7 @@ describe Thrift::CompactProtocol do thrift_type = Thrift::Types.const_get(final_primitive_type.to_s.upcase) # puts primitive_type test_values.each do |value| - trans = Thrift::MemoryBuffer.new + trans = Thrift::MemoryBufferTransport.new proto = Thrift::CompactProtocol.new(trans) proto.write_field_begin(nil, thrift_type, 15) @@ -72,7 +71,7 @@ describe Thrift::CompactProtocol do end it "should encode and decode a monster struct correctly" do - trans = Thrift::MemoryBuffer.new + trans = Thrift::MemoryBufferTransport.new proto = Thrift::CompactProtocol.new(trans) struct = CompactProtoTestStruct.new @@ -96,10 +95,10 @@ describe Thrift::CompactProtocol do end it "should make method calls correctly" do - client_out_trans = Thrift::MemoryBuffer.new + client_out_trans = Thrift::MemoryBufferTransport.new client_out_proto = Thrift::CompactProtocol.new(client_out_trans) - client_in_trans = Thrift::MemoryBuffer.new + client_in_trans = Thrift::MemoryBufferTransport.new client_in_proto = Thrift::CompactProtocol.new(client_in_trans) processor = Srv::Processor.new(JankyHandler.new) diff --git a/lib/rb/spec/httpclient_spec.rb b/lib/rb/spec/http_client_spec.rb similarity index 89% rename from lib/rb/spec/httpclient_spec.rb rename to lib/rb/spec/http_client_spec.rb index d48073f7..94526deb 100644 --- a/lib/rb/spec/httpclient_spec.rb +++ b/lib/rb/spec/http_client_spec.rb @@ -18,14 +18,13 @@ # require File.dirname(__FILE__) + '/spec_helper' -require 'thrift/transport/httpclient' -class ThriftHTTPClientSpec < Spec::ExampleGroup +class ThriftHTTPClientTransportSpec < Spec::ExampleGroup include Thrift - describe HTTPClient do + describe HTTPClientTransport do before(:each) do - @client = HTTPClient.new("http://my.domain.com/path/to/service") + @client = HTTPClientTransport.new("http://my.domain.com/path/to/service") end it "should always be open" do diff --git a/lib/rb/spec/httpserver_spec.rb b/lib/rb/spec/mongrel_http_server_spec.rb similarity index 92% rename from lib/rb/spec/httpserver_spec.rb rename to lib/rb/spec/mongrel_http_server_spec.rb index e03f1c1c..c994491c 100644 --- a/lib/rb/spec/httpserver_spec.rb +++ b/lib/rb/spec/mongrel_http_server_spec.rb @@ -18,14 +18,14 @@ # require File.dirname(__FILE__) + '/spec_helper' -require 'thrift/server/httpserver' +require 'thrift/server/mongrel_http_server' class ThriftHTTPServerSpec < Spec::ExampleGroup include Thrift - Handler = SimpleMongrelHTTPServer::Handler + Handler = MongrelHTTPServer::Handler - describe SimpleMongrelHTTPServer do + describe MongrelHTTPServer do it "should have appropriate defaults" do mock_factory = mock("BinaryProtocolFactory") mock_proc = mock("Processor") @@ -37,7 +37,7 @@ class ThriftHTTPServerSpec < Spec::ExampleGroup mock.should_receive(:register).with("/", handler) end end - SimpleMongrelHTTPServer.new(mock_proc) + MongrelHTTPServer.new(mock_proc) end it "should understand :ip, :port, :path, and :protocol_factory" do @@ -50,7 +50,7 @@ class ThriftHTTPServerSpec < Spec::ExampleGroup mock.should_receive(:register).with("/foo", handler) end end - SimpleMongrelHTTPServer.new(mock_proc, :ip => "1.2.3.4", :port => 1234, :path => "foo", + MongrelHTTPServer.new(mock_proc, :ip => "1.2.3.4", :port => 1234, :path => "foo", :protocol_factory => mock_factory) end @@ -67,11 +67,11 @@ class ThriftHTTPServerSpec < Spec::ExampleGroup end end end - SimpleMongrelHTTPServer.new(nil).serve + MongrelHTTPServer.new(nil).serve end end - describe SimpleMongrelHTTPServer::Handler do + describe MongrelHTTPServer::Handler do before(:each) do @processor = mock("Processor") @factory = mock("ProtocolFactory") diff --git a/lib/rb/spec/nonblockingserver_spec.rb b/lib/rb/spec/nonblocking_server_spec.rb similarity index 97% rename from lib/rb/spec/nonblockingserver_spec.rb rename to lib/rb/spec/nonblocking_server_spec.rb index 02666dba..22c9280e 100644 --- a/lib/rb/spec/nonblockingserver_spec.rb +++ b/lib/rb/spec/nonblocking_server_spec.rb @@ -18,7 +18,6 @@ # require File.dirname(__FILE__) + '/spec_helper' -require 'thrift/server/nonblockingserver' require File.dirname(__FILE__) + '/gen-rb/NonblockingService' class ThriftNonblockingServerSpec < Spec::ExampleGroup @@ -57,7 +56,7 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup end end - class SpecTransport < Transport + class SpecTransport < BaseTransport def initialize(transport, queue) @transport = transport @queue = queue @@ -110,10 +109,10 @@ class ThriftNonblockingServerSpec < Spec::ExampleGroup processor = NonblockingService::Processor.new(handler) queue = Queue.new @transport = SpecServerSocket.new('localhost', @port, queue) - transportFactory = FramedTransportFactory.new + transport_factory = FramedTransportFactory.new logger = Logger.new(STDERR) logger.level = Logger::WARN - @server = NonblockingServer.new(processor, @transport, transportFactory, nil, 5, logger) + @server = NonblockingServer.new(processor, @transport, transport_factory, nil, 5, logger) handler.server = @server @server_thread = Thread.new(Thread.current) do |master_thread| begin diff --git a/lib/rb/spec/serializer_spec.rb b/lib/rb/spec/serializer_spec.rb index 3411c53f..db521338 100644 --- a/lib/rb/spec/serializer_spec.rb +++ b/lib/rb/spec/serializer_spec.rb @@ -18,7 +18,6 @@ # require File.dirname(__FILE__) + '/spec_helper' -require 'thrift/serializer' require File.dirname(__FILE__) + '/gen-rb/ThriftSpec_types' class ThriftSerializerSpec < Spec::ExampleGroup @@ -33,16 +32,16 @@ class ThriftSerializerSpec < Spec::ExampleGroup end it "should serialize structs to the given protocol" do - protocol = Protocol.new(mock("transport")) + protocol = BaseProtocol.new(mock("transport")) protocol.should_receive(:write_struct_begin).with("SpecNamespace::Hello") protocol.should_receive(:write_field_begin).with("greeting", Types::STRING, 1) protocol.should_receive(:write_string).with("Good day") protocol.should_receive(:write_field_end) protocol.should_receive(:write_field_stop) protocol.should_receive(:write_struct_end) - protocolFactory = mock("ProtocolFactory") - protocolFactory.stub!(:get_protocol).and_return(protocol) - serializer = Serializer.new(protocolFactory) + protocol_factory = mock("ProtocolFactory") + protocol_factory.stub!(:get_protocol).and_return(protocol) + serializer = Serializer.new(protocol_factory) serializer.serialize(Hello.new(:greeting => "Good day")) end end @@ -55,16 +54,16 @@ class ThriftSerializerSpec < Spec::ExampleGroup end it "should deserialize structs from the given protocol" do - protocol = Protocol.new(mock("transport")) + protocol = BaseProtocol.new(mock("transport")) protocol.should_receive(:read_struct_begin).and_return("SpecNamespace::Hello") protocol.should_receive(:read_field_begin).and_return(["greeting", Types::STRING, 1], [nil, Types::STOP, 0]) protocol.should_receive(:read_string).and_return("Good day") protocol.should_receive(:read_field_end) protocol.should_receive(:read_struct_end) - protocolFactory = mock("ProtocolFactory") - protocolFactory.stub!(:get_protocol).and_return(protocol) - deserializer = Deserializer.new(protocolFactory) + protocol_factory = mock("ProtocolFactory") + protocol_factory.stub!(:get_protocol).and_return(protocol) + deserializer = Deserializer.new(protocol_factory) deserializer.deserialize(Hello.new, "").should == Hello.new(:greeting => "Good day") end end diff --git a/lib/rb/spec/server_socket_spec.rb b/lib/rb/spec/server_socket_spec.rb new file mode 100644 index 00000000..fce50134 --- /dev/null +++ b/lib/rb/spec/server_socket_spec.rb @@ -0,0 +1,80 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require File.dirname(__FILE__) + '/spec_helper' +require File.dirname(__FILE__) + "/socket_spec_shared" + +class ThriftServerSocketSpec < Spec::ExampleGroup + include Thrift + + describe ServerSocket do + before(:each) do + @socket = ServerSocket.new(1234) + end + + it "should create a handle when calling listen" do + TCPServer.should_receive(:new).with(nil, 1234) + @socket.listen + end + + it "should accept an optional host argument" do + @socket = ServerSocket.new('localhost', 1234) + TCPServer.should_receive(:new).with('localhost', 1234) + @socket.listen + end + + it "should create a Thrift::Socket to wrap accepted sockets" do + handle = mock("TCPServer") + TCPServer.should_receive(:new).with(nil, 1234).and_return(handle) + @socket.listen + sock = mock("sock") + handle.should_receive(:accept).and_return(sock) + trans = mock("Socket") + Socket.should_receive(:new).and_return(trans) + trans.should_receive(:handle=).with(sock) + @socket.accept.should == trans + end + + it "should close the handle when closed" do + handle = mock("TCPServer", :closed? => false) + TCPServer.should_receive(:new).with(nil, 1234).and_return(handle) + @socket.listen + handle.should_receive(:close) + @socket.close + end + + it "should return nil when accepting if there is no handle" do + @socket.accept.should be_nil + end + + it "should return true for closed? when appropriate" do + handle = mock("TCPServer", :closed? => false) + TCPServer.stub!(:new).and_return(handle) + @socket.listen + @socket.should_not be_closed + handle.stub!(:close) + @socket.close + @socket.should be_closed + @socket.listen + @socket.should_not be_closed + handle.stub!(:closed?).and_return(true) + @socket.should be_closed + end + end +end diff --git a/lib/rb/spec/server_spec.rb b/lib/rb/spec/server_spec.rb index 7c6ebd45..ffe9bffa 100644 --- a/lib/rb/spec/server_spec.rb +++ b/lib/rb/spec/server_spec.rb @@ -22,11 +22,11 @@ require File.dirname(__FILE__) + '/spec_helper' class ThriftServerSpec < Spec::ExampleGroup include Thrift - describe Server do - it "should default to TransportFactory and BinaryProtocolFactory when not specified" do - server = Server.new(mock("Processor"), mock("ServerTransport")) - server.instance_variable_get(:'@transportFactory').should be_an_instance_of(TransportFactory) - server.instance_variable_get(:'@protocolFactory').should be_an_instance_of(BinaryProtocolFactory) + describe BaseServer do + it "should default to BaseTransportFactory and BinaryProtocolFactory when not specified" do + server = BaseServer.new(mock("Processor"), mock("BaseServerTransport")) + server.instance_variable_get(:'@transport_factory').should be_an_instance_of(BaseTransportFactory) + server.instance_variable_get(:'@protocol_factory').should be_an_instance_of(BinaryProtocolFactory) end # serve is a noop, so can't test that @@ -36,8 +36,8 @@ class ThriftServerSpec < Spec::ExampleGroup before(:each) do @processor = mock("Processor") @serverTrans = mock("ServerTransport") - @trans = mock("Transport") - @prot = mock("Protocol") + @trans = mock("BaseTransport") + @prot = mock("BaseProtocol") @client = mock("Client") @server = server_type.new(@processor, @serverTrans, @trans, @prot) end diff --git a/lib/rb/spec/socket_spec.rb b/lib/rb/spec/socket_spec.rb index 30b46483..dd8b0f92 100644 --- a/lib/rb/spec/socket_spec.rb +++ b/lib/rb/spec/socket_spec.rb @@ -58,59 +58,4 @@ class ThriftSocketSpec < Spec::ExampleGroup Socket.new('localhost', 8080, 5).timeout.should == 5 end end - - describe ServerSocket do - before(:each) do - @socket = ServerSocket.new(1234) - end - - it "should create a handle when calling listen" do - TCPServer.should_receive(:new).with(nil, 1234) - @socket.listen - end - - it "should accept an optional host argument" do - @socket = ServerSocket.new('localhost', 1234) - TCPServer.should_receive(:new).with('localhost', 1234) - @socket.listen - end - - it "should create a Thrift::Socket to wrap accepted sockets" do - handle = mock("TCPServer") - TCPServer.should_receive(:new).with(nil, 1234).and_return(handle) - @socket.listen - sock = mock("sock") - handle.should_receive(:accept).and_return(sock) - trans = mock("Socket") - Socket.should_receive(:new).and_return(trans) - trans.should_receive(:handle=).with(sock) - @socket.accept.should == trans - end - - it "should close the handle when closed" do - handle = mock("TCPServer", :closed? => false) - TCPServer.should_receive(:new).with(nil, 1234).and_return(handle) - @socket.listen - handle.should_receive(:close) - @socket.close - end - - it "should return nil when accepting if there is no handle" do - @socket.accept.should be_nil - end - - it "should return true for closed? when appropriate" do - handle = mock("TCPServer", :closed? => false) - TCPServer.stub!(:new).and_return(handle) - @socket.listen - @socket.should_not be_closed - handle.stub!(:close) - @socket.close - @socket.should be_closed - @socket.listen - @socket.should_not be_closed - handle.stub!(:closed?).and_return(true) - @socket.should be_closed - end - end end diff --git a/lib/rb/spec/spec_helper.rb b/lib/rb/spec/spec_helper.rb index d0994914..41bf631e 100644 --- a/lib/rb/spec/spec_helper.rb +++ b/lib/rb/spec/spec_helper.rb @@ -24,7 +24,7 @@ require 'spec' $:.unshift File.join(File.dirname(__FILE__), *%w[.. ext]) -# pretend we already loaded fastthread, otherwise the nonblockingserver_spec +# pretend we already loaded fastthread, otherwise the nonblocking_server_spec # will get screwed up # $" << 'fastthread.bundle' diff --git a/lib/rb/spec/struct_spec.rb b/lib/rb/spec/struct_spec.rb index 69826e7b..bfcf0ea5 100644 --- a/lib/rb/spec/struct_spec.rb +++ b/lib/rb/spec/struct_spec.rb @@ -20,8 +20,6 @@ require File.dirname(__FILE__) + '/spec_helper' require File.dirname(__FILE__) + '/gen-rb/ThriftSpec_types' -# require "binaryprotocolaccelerated" - class ThriftStructSpec < Spec::ExampleGroup include Thrift include SpecNamespace @@ -67,7 +65,7 @@ class ThriftStructSpec < Spec::ExampleGroup it "should read itself off the wire" do struct = Foo.new - prot = Protocol.new(mock("transport")) + prot = BaseProtocol.new(mock("transport")) prot.should_receive(:read_struct_begin).twice prot.should_receive(:read_struct_end).twice prot.should_receive(:read_field_begin).and_return( @@ -113,7 +111,7 @@ class ThriftStructSpec < Spec::ExampleGroup it "should skip unexpected fields in structs and use default values" do struct = Foo.new - prot = Protocol.new(mock("transport")) + prot = BaseProtocol.new(mock("transport")) prot.should_receive(:read_struct_begin) prot.should_receive(:read_struct_end) prot.should_receive(:read_field_begin).and_return( @@ -143,7 +141,7 @@ class ThriftStructSpec < Spec::ExampleGroup end it "should write itself to the wire" do - prot = Protocol.new(mock("transport")) #mock("Protocol") + prot = BaseProtocol.new(mock("transport")) #mock("Protocol") prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo") prot.should_receive(:write_struct_begin).with("SpecNamespace::Hello") prot.should_receive(:write_struct_end).twice @@ -218,7 +216,7 @@ class ThriftStructSpec < Spec::ExampleGroup e.message.should == "something happened" e.code.should == 1 # ensure it gets serialized properly, this is the really important part - prot = Protocol.new(mock("trans")) + prot = BaseProtocol.new(mock("trans")) prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception") prot.should_receive(:write_struct_end) prot.should_receive(:write_field_begin).with('message', Types::STRING, 1)#, "something happened") @@ -238,7 +236,7 @@ class ThriftStructSpec < Spec::ExampleGroup rescue Thrift::Exception => e e.message.should == "something happened" e.code.should == 5 - prot = Protocol.new(mock("trans")) + prot = BaseProtocol.new(mock("trans")) prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception") prot.should_receive(:write_struct_end) prot.should_receive(:write_field_begin).with('message', Types::STRING, 1) diff --git a/lib/rb/spec/unixsocket_spec.rb b/lib/rb/spec/unix_socket_spec.rb similarity index 98% rename from lib/rb/spec/unixsocket_spec.rb rename to lib/rb/spec/unix_socket_spec.rb index 574e2bbc..df239d7a 100644 --- a/lib/rb/spec/unixsocket_spec.rb +++ b/lib/rb/spec/unix_socket_spec.rb @@ -18,7 +18,6 @@ # require File.dirname(__FILE__) + '/spec_helper' -require 'thrift/transport/unixsocket' require File.dirname(__FILE__) + "/socket_spec_shared" class ThriftUNIXSocketSpec < Spec::ExampleGroup -- 2.17.1