From 2960044415917973aeeb8c6d9132319eef69139a Mon Sep 17 00:00:00 2001 From: Kevin Clark Date: Wed, 18 Jun 2008 00:54:13 +0000 Subject: [PATCH] rb: Add more classes/modules to the Thrift namespace. Provide backwards compatable mappings. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@668904 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_rb_generator.cc | 32 +- lib/rb/lib/thrift.rb | 2 + lib/rb/lib/thrift/client.rb | 6 +- lib/rb/lib/thrift/exceptions.rb | 12 +- lib/rb/lib/thrift/processor.rb | 8 +- lib/rb/lib/thrift/protocol/tbinaryprotocol.rb | 8 +- lib/rb/lib/thrift/protocol/tprotocol.rb | 299 +++++++++--------- lib/rb/lib/thrift/server/tserver.rb | 6 +- lib/rb/lib/thrift/struct.rb | 18 +- lib/rb/lib/thrift/transport/ttransport.rb | 2 +- test/rb/core/test_backwards_compatability.rb | 32 ++ test/rb/core/test_exceptions.rb | 4 +- tutorial/rb/RubyClient.rb | 8 +- 13 files changed, 236 insertions(+), 201 deletions(-) create mode 100644 test/rb/core/test_backwards_compatability.rb diff --git a/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc index c3d1d419..81e7d662 100644 --- a/compiler/cpp/src/generate/t_rb_generator.cc +++ b/compiler/cpp/src/generate/t_rb_generator.cc @@ -452,7 +452,7 @@ void t_rb_generator::generate_rb_struct(std::ofstream& out, t_struct* tstruct, b out << endl; indent_up(); - indent(out) << "include ThriftStruct" << endl; + indent(out) << "include Thrift::Struct" << endl; if (is_exception) { generate_rb_simple_exception_constructor(out, tstruct); @@ -677,7 +677,7 @@ void t_rb_generator::generate_service_client(t_service* tservice) { indent_up(); indent(f_service_) << - "include ThriftClient" << endl << endl; + "include Thrift::Client" << endl << endl; // Generate client method implementations vector functions = tservice->get_functions(); @@ -774,7 +774,7 @@ void t_rb_generator::generate_service_client(t_service* tservice) { "return" << endl; } else { f_service_ << - indent() << "raise TApplicationException.new(TApplicationException::MISSING_RESULT, '" << (*f_iter)->get_name() << " failed: unknown result')" << endl; + indent() << "raise Thrift::ApplicationException.new(Thrift::ApplicationException::MISSING_RESULT, '" << (*f_iter)->get_name() << " failed: unknown result')" << endl; } // Close function @@ -810,7 +810,7 @@ void t_rb_generator::generate_service_server(t_service* tservice) { indent_up(); f_service_ << - indent() << "include TProcessor" << endl << + indent() << "include Thrift::Processor" << endl << endl; // Generate the process subfunctions @@ -969,30 +969,30 @@ string t_rb_generator::type_to_enum(t_type* type) { case t_base_type::TYPE_VOID: throw "NO T_VOID CONSTRUCT"; case t_base_type::TYPE_STRING: - return "TType::STRING"; + return "Thrift::Types::STRING"; case t_base_type::TYPE_BOOL: - return "TType::BOOL"; + return "Thrift::Types::BOOL"; case t_base_type::TYPE_BYTE: - return "TType::BYTE"; + return "Thrift::Types::BYTE"; case t_base_type::TYPE_I16: - return "TType::I16"; + return "Thrift::Types::I16"; case t_base_type::TYPE_I32: - return "TType::I32"; + return "Thrift::Types::I32"; case t_base_type::TYPE_I64: - return "TType::I64"; + return "Thrift::Types::I64"; case t_base_type::TYPE_DOUBLE: - return "TType::DOUBLE"; + return "Thrift::Types::DOUBLE"; } } else if (type->is_enum()) { - return "TType::I32"; + return "Thrift::Types::I32"; } else if (type->is_struct() || type->is_xception()) { - return "TType::STRUCT"; + return "Thrift::Types::STRUCT"; } else if (type->is_map()) { - return "TType::MAP"; + return "Thrift::Types::MAP"; } else if (type->is_set()) { - return "TType::SET"; + return "Thrift::Types::SET"; } else if (type->is_list()) { - return "TType::LIST"; + return "Thrift::Types::LIST"; } throw "INVALID TYPE IN type_to_enum: " + type->get_name(); diff --git a/lib/rb/lib/thrift.rb b/lib/rb/lib/thrift.rb index b9a65d2f..210e60bd 100644 --- a/lib/rb/lib/thrift.rb +++ b/lib/rb/lib/thrift.rb @@ -28,6 +28,8 @@ require 'thrift/struct' ThriftStruct = Thrift::Struct require 'thrift/protocol/tprotocol' +TProtocol = Thrift::Protocol +TProtocolException = Thrift::ProtocolException require 'thrift/transport/tsocket' require 'thrift/transport/ttransport' diff --git a/lib/rb/lib/thrift/client.rb b/lib/rb/lib/thrift/client.rb index 168c4323..7f1297c1 100644 --- a/lib/rb/lib/thrift/client.rb +++ b/lib/rb/lib/thrift/client.rb @@ -7,7 +7,7 @@ module Thrift end def send_message(name, args_class, args = {}) - @oprot.writeMessageBegin(name, TMessageType::CALL, @seqid) + @oprot.writeMessageBegin(name, MessageTypes::CALL, @seqid) data = args_class.new args.each do |k, v| data.send("#{k.to_s}=", v) @@ -27,8 +27,8 @@ module Thrift end def handle_exception(mtype) - if mtype == TMessageType::EXCEPTION - x = TApplicationException.new() + if mtype == MessageTypes::EXCEPTION + x = ApplicationException.new() x.read(@iprot) @iprot.readMessageEnd() raise x diff --git a/lib/rb/lib/thrift/exceptions.rb b/lib/rb/lib/thrift/exceptions.rb index 673e9e3b..972d5710 100644 --- a/lib/rb/lib/thrift/exceptions.rb +++ b/lib/rb/lib/thrift/exceptions.rb @@ -28,17 +28,17 @@ module Thrift iprot.readStructBegin() while true fname, ftype, fid = iprot.readFieldBegin() - if (ftype === TType::STOP) + if (ftype === Types::STOP) break end if (fid == 1) - if (ftype === TType::STRING) + if (ftype === Types::STRING) @message = iprot.readString(); else iprot.skip(ftype) end elsif (fid == 2) - if (ftype === TType::I32) + if (ftype === Types::I32) @type = iprot.readI32(); else iprot.skip(ftype) @@ -52,14 +52,14 @@ module Thrift end def write(oprot) - oprot.writeStructBegin('TApplicationException') + oprot.writeStructBegin('Thrift::ApplicationException') if (@message != nil) - oprot.writeFieldBegin('message', TType::STRING, 1) + oprot.writeFieldBegin('message', Types::STRING, 1) oprot.writeString(@message) oprot.writeFieldEnd() end if (@type != nil) - oprot.writeFieldBegin('type', TType::I32, 2) + oprot.writeFieldBegin('type', Types::I32, 2) oprot.writeI32(@type) oprot.writeFieldEnd() end diff --git a/lib/rb/lib/thrift/processor.rb b/lib/rb/lib/thrift/processor.rb index a234c615..5ec881f5 100644 --- a/lib/rb/lib/thrift/processor.rb +++ b/lib/rb/lib/thrift/processor.rb @@ -10,10 +10,10 @@ module Thrift send("process_#{name}", seqid, iprot, oprot) return true else - iprot.skip(TType::STRUCT) + iprot.skip(Types::STRUCT) iprot.readMessageEnd() - x = TApplicationException.new(TApplicationException::UNKNOWN_METHOD, 'Unknown function '+name) - oprot.writeMessageBegin(name, TMessageType::EXCEPTION, seqid) + x = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, 'Unknown function '+name) + oprot.writeMessageBegin(name, MessageTypes::EXCEPTION, seqid) x.write(oprot) oprot.writeMessageEnd() oprot.trans.flush() @@ -29,7 +29,7 @@ module Thrift end def write_result(result, oprot, name, seqid) - oprot.writeMessageBegin(name, TMessageType::REPLY, seqid) + oprot.writeMessageBegin(name, MessageTypes::REPLY, seqid) result.write(oprot) oprot.writeMessageEnd() oprot.trans.flush() diff --git a/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb b/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb index ce0b1bb7..87211f26 100644 --- a/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb +++ b/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb @@ -10,7 +10,7 @@ # require 'thrift/protocol/tprotocol' -class TBinaryProtocol < TProtocol +class TBinaryProtocol < Thrift::Protocol VERSION_MASK = 0xffff0000 VERSION_1 = 0x80010000 @@ -31,7 +31,7 @@ class TBinaryProtocol < TProtocol end def writeFieldStop() - writeByte(TType::STOP) + writeByte(Thrift::Types::STOP) end def writeMapBegin(ktype, vtype, size) @@ -88,7 +88,7 @@ class TBinaryProtocol < TProtocol def readMessageBegin() version = readI32() if (version & VERSION_MASK != VERSION_1) - raise TProtocolException.new(TProtocolException::BAD_VERSION, 'Missing version identifier') + raise Thrift::ProtocolException.new(Thrift::ProtocolException::BAD_VERSION, 'Missing version identifier') end type = version & 0x000000ff name = readString() @@ -98,7 +98,7 @@ class TBinaryProtocol < TProtocol def readFieldBegin() type = readByte() - if (type === TType::STOP) + if (type === Thrift::Types::STOP) return nil, type, 0 end id = readI16() diff --git a/lib/rb/lib/thrift/protocol/tprotocol.rb b/lib/rb/lib/thrift/protocol/tprotocol.rb index a06b4cd1..edc894cb 100644 --- a/lib/rb/lib/thrift/protocol/tprotocol.rb +++ b/lib/rb/lib/thrift/protocol/tprotocol.rb @@ -11,213 +11,214 @@ require 'thrift/thrift' -class TProtocolException < TException +module Thrift + class ProtocolException < Exception - UNKNOWN = 0 - INVALID_DATA = 1 - NEGATIVE_SIZE = 2 - SIZE_LIMIT = 3 - BAD_VERSION = 4 + UNKNOWN = 0 + INVALID_DATA = 1 + NEGATIVE_SIZE = 2 + SIZE_LIMIT = 3 + BAD_VERSION = 4 - attr_reader :type + attr_reader :type - def initialize(type=UNKNOWN, message=nil) - super(message) - @type = type - end + def initialize(type=UNKNOWN, message=nil) + super(message) + @type = type + end -end + end -class TProtocol + class Protocol - attr_reader :trans + attr_reader :trans - def initialize(trans) - @trans = trans - end + def initialize(trans) + @trans = trans + end - def writeMessageBegin(name, type, seqid); nil; end + def writeMessageBegin(name, type, seqid); nil; end - def writeMessageEnd; nil; end + def writeMessageEnd; nil; end - def writeStructBegin(name); nil; end + def writeStructBegin(name); nil; end - def writeStructEnd(); nil; end + def writeStructEnd(); nil; end - def writeFieldBegin(name, type, id); nil; end + def writeFieldBegin(name, type, id); nil; end - def writeFieldEnd(); nil; end + def writeFieldEnd(); nil; end - def writeFieldStop(); nil; end + def writeFieldStop(); nil; end - def writeMapBegin(ktype, vtype, size); nil; end + def writeMapBegin(ktype, vtype, size); nil; end - def writeMapEnd(); nil; end + def writeMapEnd(); nil; end - def writeListBegin(etype, size); nil; end + def writeListBegin(etype, size); nil; end - def writeListEnd(); nil; end + def writeListEnd(); nil; end - def writeSetBegin(etype, size); nil; end + def writeSetBegin(etype, size); nil; end - def writeSetEnd(); nil; end + def writeSetEnd(); nil; end - def writeBool(bool); nil; end + def writeBool(bool); nil; end - def writeByte(byte); nil; end + def writeByte(byte); nil; end - def writeI16(i16); nil; end + def writeI16(i16); nil; end - def writeI32(i32); nil; end + def writeI32(i32); nil; end - def writeI64(i64); nil; end + def writeI64(i64); nil; end - def writeDouble(dub); nil; end + def writeDouble(dub); nil; end - def writeString(str); nil; end + def writeString(str); nil; end - def readMessageBegin(); nil; end + def readMessageBegin(); nil; end - def readMessageEnd(); nil; end + def readMessageEnd(); nil; end - def readStructBegin(); nil; end + def readStructBegin(); nil; end - def readStructEnd(); nil; end + def readStructEnd(); nil; end - def readFieldBegin(); nil; end + def readFieldBegin(); nil; end - def readFieldEnd(); nil; end + def readFieldEnd(); nil; end - def readMapBegin(); nil; end + def readMapBegin(); nil; end - def readMapEnd(); nil; end + def readMapEnd(); nil; end - def readListBegin(); nil; end + def readListBegin(); nil; end - def readListEnd(); nil; end + def readListEnd(); nil; end - def readSetBegin(); nil; end + def readSetBegin(); nil; end - def readSetEnd(); nil; end + def readSetEnd(); nil; end - def readBool(); nil; end + def readBool(); nil; end - def readByte(); nil; end + def readByte(); nil; end - def readI16(); nil; end + def readI16(); nil; end - def readI32(); nil; end + def readI32(); nil; end - def readI64(); nil; end + def readI64(); nil; end - def readDouble(); nil; end + def readDouble(); nil; end - def readString(); nil; end + def readString(); nil; end - def write_field(name, type, fid, value) - writeFieldBegin(name, type, fid) - write_type(type, value) - writeFieldEnd - end + def write_field(name, type, fid, value) + writeFieldBegin(name, type, fid) + write_type(type, value) + writeFieldEnd + end - def write_type(type, value) - case type - when TType::BOOL - writeBool(value) - when TType::BYTE - writeByte(value) - when TType::DOUBLE - writeDouble(value) - when TType::I16 - writeI16(value) - when TType::I32 - writeI32(value) - when TType::I64 - writeI64(value) - when TType::STRING - writeString(value) - when TType::STRUCT - value.write(self) - else - raise NotImplementedError + def write_type(type, value) + case type + when Types::BOOL + writeBool(value) + when Types::BYTE + writeByte(value) + when Types::DOUBLE + writeDouble(value) + when Types::I16 + writeI16(value) + when Types::I32 + writeI32(value) + when Types::I64 + writeI64(value) + when Types::STRING + writeString(value) + when Types::STRUCT + value.write(self) + else + raise NotImplementedError + end end - end - def read_type(type) - case type - when TType::BOOL - readBool - when TType::BYTE - readByte - when TType::DOUBLE - readDouble - when TType::I16 - readI16 - when TType::I32 - readI32 - when TType::I64 - readI64 - when TType::STRING - readString - else - raise NotImplementedError + def read_type(type) + case type + when Types::BOOL + readBool + when Types::BYTE + readByte + when Types::DOUBLE + readDouble + when Types::I16 + readI16 + when Types::I32 + readI32 + when Types::I64 + readI64 + when Types::STRING + readString + else + raise NotImplementedError + end end - end - def skip(type) - if type === TType::STOP - nil - elsif type === TType::BOOL - readBool() - elsif type === TType::BYTE - readByte() - elsif type === TType::I16 - readI16() - elsif type === TType::I32 - readI32() - elsif type === TType::I64 - readI64() - elsif type === TType::DOUBLE - readDouble() - elsif type === TType::STRING - readString() - elsif type === TType::STRUCT - readStructBegin() - while true - name, type, id = readFieldBegin() - if type === TType::STOP - break - else - skip(type) - readFieldEnd() + def skip(type) + if type === Types::STOP + nil + elsif type === Types::BOOL + readBool() + elsif type === Types::BYTE + readByte() + elsif type === Types::I16 + readI16() + elsif type === Types::I32 + readI32() + elsif type === Types::I64 + readI64() + elsif type === Types::DOUBLE + readDouble() + elsif type === Types::STRING + readString() + elsif type === Types::STRUCT + readStructBegin() + while true + name, type, id = readFieldBegin() + if type === Types::STOP + break + else + skip(type) + readFieldEnd() + end + readStructEnd() end - readStructEnd() - end - elsif type === TType::MAP - ktype, vtype, size = readMapBegin() - for i in 1..size - skip(ktype) - skip(vtype) - end - readMapEnd() - elsif type === TType::SET - etype, size = readSetBegin() - for i in 1..size - skip(etype) - end - readSetEnd() - elsif type === TType::LIST - etype, size = readListBegin() - for i in 1..size - skip(etype) + elsif type === Types::MAP + ktype, vtype, size = readMapBegin() + for i in 1..size + skip(ktype) + skip(vtype) + end + readMapEnd() + elsif type === Types::SET + etype, size = readSetBegin() + for i in 1..size + skip(etype) + end + readSetEnd() + elsif type === Types::LIST + etype, size = readListBegin() + for i in 1..size + skip(etype) + end + readListEnd() end - readListEnd() end - end + end end class TProtocolFactory def getProtocol(trans); nil; end end - diff --git a/lib/rb/lib/thrift/server/tserver.rb b/lib/rb/lib/thrift/server/tserver.rb index f2239c82..788467ea 100644 --- a/lib/rb/lib/thrift/server/tserver.rb +++ b/lib/rb/lib/thrift/server/tserver.rb @@ -42,7 +42,7 @@ class TSimpleServer < TServer while (true) @processor.process(prot, prot) end - rescue TTransportException, TProtocolException => ttx + rescue TTransportException, Thrift::ProtocolException => ttx #print ttx,"\n" ensure trans.close() @@ -73,7 +73,7 @@ class TThreadedServer < TServer while (true) @processor.process(p, p) end - rescue TTransportException, TProtocolException => e + rescue TTransportException, Thrift::ProtocolException => e ensure t.close() end @@ -119,7 +119,7 @@ class TThreadPoolServer < TServer while (true) @processor.process(prot, prot) end - rescue TTransportException, TProtocolException => e + rescue TTransportException, Thrift::ProtocolException => e ensure trans.close() end diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb index 04b581ff..35b34154 100644 --- a/lib/rb/lib/thrift/struct.rb +++ b/lib/rb/lib/thrift/struct.rb @@ -20,7 +20,7 @@ module Thrift iprot.readStructBegin() loop do fname, ftype, fid = iprot.readFieldBegin() - break if (ftype === TType::STOP) + break if (ftype === Types::STOP) handle_message(iprot, fid, ftype) iprot.readFieldEnd() end @@ -57,10 +57,10 @@ module Thrift end def read_field(iprot, field = {}) - if field[:type] == TType::STRUCT + if field[:type] == Types::STRUCT value = field[:class].new value.read(iprot) - elsif field[:type] == TType::MAP + elsif field[:type] == Types::MAP key_type, val_type, size = iprot.readMapBegin value = {} size.times do @@ -69,13 +69,13 @@ module Thrift value[k] = v end iprot.readMapEnd - elsif field[:type] == TType::LIST + elsif field[:type] == Types::LIST e_type, size = iprot.readListBegin value = Array.new(size) do |n| read_field(iprot, field_info(field[:element])) end iprot.readListEnd - elsif field[:type] == TType::SET + elsif field[:type] == Types::SET e_type, size = iprot.readSetBegin value = {} size.times do @@ -98,20 +98,20 @@ module Thrift end def write_container(oprot, value, field = {}) - if field[:type] == TType::MAP + if field[:type] == Types::MAP oprot.writeMapBegin(field[:key][:type], field[:value][:type], value.size) value.each do |k, v| write_data(oprot, k, field[:key]) write_data(oprot, v, field[:value]) end oprot.writeMapEnd - elsif field[:type] == TType::LIST + elsif field[:type] == Types::LIST oprot.writeListBegin(field[:element][:type], value.size) value.each do |elem| write_data(oprot, elem, field[:element]) end oprot.writeListEnd - elsif field[:type] == TType::SET + elsif field[:type] == Types::SET oprot.writeSetBegin(field[:element][:type], value.size) value.each do |k, v| write_data(oprot, k, field[:element]) @@ -123,7 +123,7 @@ module Thrift end def is_container?(type) - [TType::LIST, TType::MAP, TType::SET].include? type + [Types::LIST, Types::MAP, Types::SET].include? type end def field_info(field) diff --git a/lib/rb/lib/thrift/transport/ttransport.rb b/lib/rb/lib/thrift/transport/ttransport.rb index a5f6a816..9702ac20 100644 --- a/lib/rb/lib/thrift/transport/ttransport.rb +++ b/lib/rb/lib/thrift/transport/ttransport.rb @@ -11,7 +11,7 @@ require 'thrift/thrift' -class TTransportException < TException +class TTransportException < Thrift::Exception UNKNOWN = 0 NOT_OPEN = 1 diff --git a/test/rb/core/test_backwards_compatability.rb b/test/rb/core/test_backwards_compatability.rb new file mode 100644 index 00000000..a826f202 --- /dev/null +++ b/test/rb/core/test_backwards_compatability.rb @@ -0,0 +1,32 @@ +require File.join(File.dirname(__FILE__), '../test_helper') + +require 'thrift/thrift' + +class TestTException < Test::Unit::TestCase + def test_has_accessible_message + msg = "hi there thrift" + assert_equal msg, TException.new(msg).message + end + +end + +class TestConstRemapping < Test::Unit::TestCase + def test_remappings + maps = { + TException => Thrift::Exception, + TApplicationException => Thrift::ApplicationException, + TType => Thrift::Types, + TMessageType => Thrift::MessageTypes, + TProcessor => Thrift::Processor, + ThriftClient => Thrift::Client, + ThriftStruct => Thrift::Struct, + TProtocol => Thrift::Protocol, + TProtocolException => Thrift::ProtocolException + } + + maps.each do |k, v| + assert_equal k, v + end + end +end + diff --git a/test/rb/core/test_exceptions.rb b/test/rb/core/test_exceptions.rb index eb63963b..88aacb0b 100644 --- a/test/rb/core/test_exceptions.rb +++ b/test/rb/core/test_exceptions.rb @@ -2,10 +2,10 @@ require File.join(File.dirname(__FILE__), '../test_helper') require 'thrift/thrift' -class TestTException < Test::Unit::TestCase +class TestException < Test::Unit::TestCase def test_has_accessible_message msg = "hi there thrift" - assert_equal msg, TException.new(msg).message + assert_equal msg, Thrift::Exception.new(msg).message end end diff --git a/tutorial/rb/RubyClient.rb b/tutorial/rb/RubyClient.rb index d40ff496..1b5d5db5 100755 --- a/tutorial/rb/RubyClient.rb +++ b/tutorial/rb/RubyClient.rb @@ -2,8 +2,8 @@ $:.push('../gen-rb') -require 'thrift/transport/tsocket.rb' -require 'thrift/protocol/tbinaryprotocol.rb' +require 'thrift/transport/tsocket' +require 'thrift/protocol/tbinaryprotocol' require 'Calculator' @@ -51,6 +51,6 @@ begin transport.close() -rescue TException => tx - print 'TException: ', tx.message, "\n" +rescue Thrift::Exception => tx + print 'Thrift::Exception: ', tx.message, "\n" end -- 2.17.1