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
diff --git a/lib/rb/Manifest b/lib/rb/Manifest
index 17439ed..d952c50 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.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/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 f5fe323..3dc67dd 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 bd92bde..948f08d 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 5b7da7e..d44fa88 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 @@
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 ab8db9f..47126ad 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 @@
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 12dab31..624012d 100644
--- a/lib/rb/ext/memory_buffer.c
+++ b/lib/rb/ext/memory_buffer.c
@@ -59,7 +59,7 @@
}
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 756f8b2..c187654 100644
--- a/lib/rb/ext/protocol.c
+++ b/lib/rb/ext/protocol.c
@@ -144,7 +144,7 @@
}
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 ec15adc..88562e1 100644
--- a/lib/rb/lib/thrift.rb
+++ b/lib/rb/lib/thrift.rb
@@ -25,10 +25,35 @@
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 98%
rename from lib/rb/lib/thrift/protocol.rb
rename to lib/rb/lib/thrift/protocol/base_protocol.rb
index adeddef..b19909d 100644
--- a/lib/rb/lib/thrift/protocol.rb
+++ b/lib/rb/lib/thrift/protocol/base_protocol.rb
@@ -35,10 +35,9 @@
super(message)
@type = type
end
-
end
- class Protocol
+ class BaseProtocol
attr_reader :trans
@@ -281,12 +280,11 @@
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 ca9ffea..04d149a 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 @@
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 9bb690d..eaf64f6 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 1f8ec5b..c8f4365 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 @@
end
end
- class CompactProtocolFactory < ProtocolFactory
+ class CompactProtocolFactory < BaseProtocolFactory
def get_protocol(trans)
CompactProtocol.new(trans)
end
diff --git a/lib/rb/lib/thrift/serializer.rb b/lib/rb/lib/thrift/serializer/deserializer.rb
similarity index 66%
copy from lib/rb/lib/thrift/serializer.rb
copy to lib/rb/lib/thrift/serializer/deserializer.rb
index 11c754c..d2ee325 100644
--- a/lib/rb/lib/thrift/serializer.rb
+++ b/lib/rb/lib/thrift/serializer/deserializer.rb
@@ -18,23 +18,10 @@
#
module Thrift
- class Serializer
- def initialize(protocolFactory = BinaryProtocolFactory.new)
- @transport = MemoryBuffer.new
- @protocol = protocolFactory.get_protocol(@transport)
- end
-
- def serialize(base)
- @transport.reset_buffer
- base.write(@protocol)
- @transport.read(@transport.available)
- end
- end
-
class Deserializer
- 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 deserialize(base, buffer)
@@ -43,4 +30,4 @@
base
end
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 11c754c..2231639 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 @@
@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 bd42854..0000000
--- 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 0000000..1ee1213
--- /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 f26329f..84eacf0 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 9689b8f..5425f6d 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 @@
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 @@
@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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 0000000..21e8659
--- /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 0000000..8cec805
--- /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 0000000..a2c917c
--- /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 9789a41..01aae56 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 4aa50d0..0000000
--- 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 0000000..68c5af0
--- /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 0000000..08a71da
--- /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 0000000..8dead4e
--- /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 0000000..558af74
--- /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 8a62ca2..a190a98 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 0000000..be348aa
--- /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 0000000..33d732d
--- /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 0000000..7feb9ab
--- /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 49247ce..06c937e 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 @@
@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 60931b9..a135d25 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 @@
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_socket.rb
similarity index 63%
copy from lib/rb/lib/thrift/transport/unixsocket.rb
copy to lib/rb/lib/thrift/transport/unix_socket.rb
index 60931b9..8f692e4 100644
--- a/lib/rb/lib/thrift/transport/unixsocket.rb
+++ b/lib/rb/lib/thrift/transport/unix_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,7 +18,6 @@
# under the License.
#
-require 'thrift/transport'
require 'socket'
module Thrift
@@ -37,41 +37,4 @@
end
end
end
-
- class UNIXServerSocket < ServerTransport
- def initialize(path)
- @path = path
- @handle = nil
- end
-
- attr_accessor :handle
-
- def listen
- @handle = ::UNIXServer.new(@path)
- end
-
- def accept
- unless @handle.nil?
- sock = @handle.accept
- trans = UNIXSocket.new(nil)
- trans.handle = sock
- trans
- end
- end
-
- def close
- if @handle
- @handle.close unless @handle.closed?
- @handle = nil
- # UNIXServer doesn't delete the socket file, so we have to do it ourselves
- File.delete(@path)
- end
- 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/script/proto_benchmark.rb b/lib/rb/script/proto_benchmark.rb
index 09adf81..4ff6a55 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 @@
# 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 @@
# 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 @@
#
# 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 @@
#
# 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 fd2dfbe..831fcec 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 b4bdec1..da14219 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 57b64ab..efb16d8 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 @@
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 709a93e..7189775 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 @@
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 @@
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 @@
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 f8f9b90..a834f7c 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 3a0d691..0abccb8 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 18ea8e8..c6608e0 100644
--- a/lib/rb/spec/binaryprotocol_spec_shared.rb
+++ b/lib/rb/spec/binary_protocol_spec_shared.rb
@@ -21,7 +21,7 @@
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 @@
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 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
- 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]
- end
-
# message footer is a noop
it "should read a field header" do
@@ -345,7 +346,7 @@
processor = Srv::Processor.new(SrvHandler.new)
client = Srv::Client.new(clientproto, clientproto)
-
+
# first block
firstblock.call(client)
@@ -371,5 +372,4 @@
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 9071fb1..78b1a2a 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 @@
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 @@
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 @@
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 @@
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 d48073f..94526de 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 e03f1c1..c994491 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 @@
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 @@
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 @@
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 02666db..22c9280 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 @@
end
end
- class SpecTransport < Transport
+ class SpecTransport < BaseTransport
def initialize(transport, queue)
@transport = transport
@queue = queue
@@ -110,10 +109,10 @@
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 3411c53..db52133 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 @@
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 @@
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 0000000..fce5013
--- /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 7c6ebd4..ffe9bff 100644
--- a/lib/rb/spec/server_spec.rb
+++ b/lib/rb/spec/server_spec.rb
@@ -22,11 +22,11 @@
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 @@
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 30b4648..dd8b0f9 100644
--- a/lib/rb/spec/socket_spec.rb
+++ b/lib/rb/spec/socket_spec.rb
@@ -58,59 +58,4 @@
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 d099491..41bf631 100644
--- a/lib/rb/spec/spec_helper.rb
+++ b/lib/rb/spec/spec_helper.rb
@@ -24,7 +24,7 @@
$:.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 69826e7..bfcf0ea 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 @@
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 @@
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 @@
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 @@
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 @@
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 574e2bb..df239d7 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