From: Kevin Clark Date: Wed, 18 Jun 2008 01:14:48 +0000 (+0000) Subject: Make a lot of miscellaneous ruby-styling changes X-Git-Tag: 0.2.0~560 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=5a2d0ad20a12defdb688d78aad2c247b275ec2ea;p=common%2Fthrift.git Make a lot of miscellaneous ruby-styling changes git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@668991 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/rb/lib/thrift/client.rb b/lib/rb/lib/thrift/client.rb index 1a34543b..8f9b96a9 100644 --- a/lib/rb/lib/thrift/client.rb +++ b/lib/rb/lib/thrift/client.rb @@ -23,7 +23,7 @@ module Thrift result = result_klass.new result.read(@iprot) @iprot.read_message_end - return result + result end def handle_exception(mtype) diff --git a/lib/rb/lib/thrift/exceptions.rb b/lib/rb/lib/thrift/exceptions.rb index eed06159..7bb9238a 100644 --- a/lib/rb/lib/thrift/exceptions.rb +++ b/lib/rb/lib/thrift/exceptions.rb @@ -29,21 +29,13 @@ module Thrift iprot.read_struct_begin while true fname, ftype, fid = iprot.read_field_begin - if (ftype === Types::STOP) + if ftype == Types::STOP break end - if (fid == 1) - if (ftype === Types::STRING) - @message = iprot.read_string; - else - iprot.skip(ftype) - end - elsif (fid == 2) - if (ftype === Types::I32) - @type = iprot.read_i32; - else - iprot.skip(ftype) - end + if fid == 1 and ftype == Types::STRING + @message = iprot.read_string + elsif fid == 2 and ftype == Types::I32 + @type = iprot.read_i32 else iprot.skip(ftype) end @@ -54,12 +46,12 @@ module Thrift def write(oprot) oprot.write_struct_begin('Thrift::ApplicationException') - if (@message != nil) + unless @message.nil? oprot.write_field_begin('message', Types::STRING, 1) oprot.write_string(@message) oprot.write_field_end end - if (@type != nil) + unless @type.nil? oprot.write_field_begin('type', Types::I32, 2) oprot.write_i32(@type) oprot.write_field_end diff --git a/lib/rb/lib/thrift/processor.rb b/lib/rb/lib/thrift/processor.rb index 92a9a40f..a28d1efe 100644 --- a/lib/rb/lib/thrift/processor.rb +++ b/lib/rb/lib/thrift/processor.rb @@ -8,16 +8,16 @@ module Thrift name, type, seqid = iprot.read_message_begin if respond_to?("process_#{name}") send("process_#{name}", seqid, iprot, oprot) - return true + true else iprot.skip(Types::STRUCT) iprot.read_message_end x = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, 'Unknown function '+name) oprot.write_message_begin(name, MessageTypes::EXCEPTION, seqid) - x.write(oprot) + x.write(oprot) oprot.write_message_end oprot.trans.flush - return + false end end diff --git a/lib/rb/lib/thrift/protocol.rb b/lib/rb/lib/thrift/protocol.rb index b5143817..d28ae512 100644 --- a/lib/rb/lib/thrift/protocol.rb +++ b/lib/rb/lib/thrift/protocol.rb @@ -206,48 +206,46 @@ module Thrift end def skip(type) - if type === Types::STOP + case type + when Types::STOP nil - elsif type === Types::BOOL + when Types::BOOL read_bool - elsif type === Types::BYTE + when Types::BYTE read_byte - elsif type === Types::I16 + when Types::I16 read_i16 - elsif type === Types::I32 + when Types::I32 read_i32 - elsif type === Types::I64 + when Types::I64 read_i64 - elsif type === Types::DOUBLE + when Types::DOUBLE read_double - elsif type === Types::STRING + when Types::STRING read_string - elsif type === Types::STRUCT + when Types::STRUCT read_struct_begin while true name, type, id = read_field_begin - if type === Types::STOP - break - else - skip(type) - read_field_end - end - end + break if type == Types::STOP + skip(type) + read_field_end + end read_struct_end - elsif type === Types::MAP + when Types::MAP ktype, vtype, size = read_map_begin size.times do skip(ktype) skip(vtype) end read_map_end - elsif type === Types::SET + when Types::SET etype, size = read_set_begin size.times do skip(etype) end read_set_end - elsif type === Types::LIST + when Types::LIST etype, size = read_list_begin size.times do skip(etype) diff --git a/lib/rb/lib/thrift/protocol/binaryprotocol.rb b/lib/rb/lib/thrift/protocol/binaryprotocol.rb index 9339b94e..b6f1adac 100644 --- a/lib/rb/lib/thrift/protocol/binaryprotocol.rb +++ b/lib/rb/lib/thrift/protocol/binaryprotocol.rb @@ -46,15 +46,11 @@ module Thrift end def write_bool(bool) - if (bool) - write_byte(1) - else - write_byte(0) - end + write_byte(bool ? 1 : 0) end def write_byte(byte) - trans.write([byte].pack('n')[1..1]) + trans.write([byte].pack('c')) end def write_i16(i16) @@ -88,40 +84,41 @@ module Thrift type = version & 0x000000ff name = read_string seqid = read_i32 - return name, type, seqid + [name, type, seqid] end def read_field_begin type = read_byte - if (type === Types::STOP) - return nil, type, 0 + if (type == Types::STOP) + [nil, type, 0] + else + id = read_i16 + [nil, type, id] end - id = read_i16 - return nil, type, id end def read_map_begin ktype = read_byte vtype = read_byte size = read_i32 - return ktype, vtype, size + [ktype, vtype, size] end def read_list_begin etype = read_byte size = read_i32 - return etype, size + [etype, size] end def read_set_begin etype = read_byte size = read_i32 - return etype, size + [etype, size] end def read_bool byte = read_byte - return byte != 0 + byte != 0 end def read_byte @@ -130,7 +127,7 @@ module Thrift if (val > 0x7f) val = 0 - ((val - 1) ^ 0xff) end - return val + val end def read_i16 @@ -139,7 +136,7 @@ module Thrift if (val > 0x7fff) val = 0 - ((val - 1) ^ 0xffff) end - return val + val end def read_i32 @@ -148,31 +145,31 @@ module Thrift if (val > 0x7fffffff) val = 0 - ((val - 1) ^ 0xffffffff) end - return val + val end def read_i64 dat = trans.read_all(8) hi, lo = dat.unpack('N2') if (hi > 0x7fffffff) - hi = hi ^ 0xffffffff - lo = lo ^ 0xffffffff - return 0 - hi*4294967296 - lo - 1 + hi ^= 0xffffffff + lo ^= 0xffffffff + 0 - (hi << 32) - lo - 1 else - return hi*4294967296 + lo + (hi << 32) + lo end end def read_double dat = trans.read_all(8) - val, = dat.unpack('G') - return val + val = dat.unpack('G').first + val end def read_string sz = read_i32 dat = trans.read_all(sz) - return dat + dat end end diff --git a/lib/rb/lib/thrift/server.rb b/lib/rb/lib/thrift/server.rb index 8dde80d1..5d179ef0 100644 --- a/lib/rb/lib/thrift/server.rb +++ b/lib/rb/lib/thrift/server.rb @@ -31,16 +31,15 @@ module Thrift def serve begin @serverTransport.listen - while (true) + loop do client = @serverTransport.accept trans = @transportFactory.get_transport(client) prot = @protocolFactory.get_protocol(trans) begin - while (true) + loop do @processor.process(prot, prot) end - rescue Thrift::TransportException, Thrift::ProtocolException => ttx - #print ttx,"\n" + rescue Thrift::TransportException, Thrift::ProtocolException ensure trans.close end @@ -64,16 +63,16 @@ module Thrift def serve begin @serverTransport.listen - while (true) + loop do client = @serverTransport.accept trans = @transportFactory.get_transport(client) prot = @protocolFactory.get_protocol(trans) Thread.new(prot, trans) do |p, t| begin - while (true) + loop do @processor.process(p, p) end - rescue Thrift::TransportException, Thrift::ProtocolException => e + rescue Thrift::TransportException, Thrift::ProtocolException ensure t.close end @@ -109,16 +108,16 @@ module Thrift @serverTransport.listen begin - while (true) + loop do @thread_q.push(:token) Thread.new do begin - while (true) + loop do client = @serverTransport.accept trans = @transportFactory.get_transport(client) prot = @protocolFactory.get_protocol(trans) begin - while (true) + loop do @processor.process(prot, prot) end rescue Thrift::TransportException, Thrift::ProtocolException => e diff --git a/lib/rb/lib/thrift/server/httpserver.rb b/lib/rb/lib/thrift/server/httpserver.rb index 8763e4c5..7f2fbbf6 100644 --- a/lib/rb/lib/thrift/server/httpserver.rb +++ b/lib/rb/lib/thrift/server/httpserver.rb @@ -14,15 +14,15 @@ module Thrift end def process(request, response) - unless request.params["REQUEST_METHOD"] == "POST" - response.start(404) { } # better way? - return - end - response.start(200) do |head, out| - head["Content-Type"] = "application/x-thrift" - transport = IOStreamTransport.new request.body, out - protocol = @protocol_factory.get_protocol transport - @processor.process protocol, protocol + if request.params["REQUEST_METHOD"] == "POST" + response.start(200) do |head, out| + head["Content-Type"] = "application/x-thrift" + transport = IOStreamTransport.new request.body, out + protocol = @protocol_factory.get_protocol transport + @processor.process protocol, protocol + end + else + response.start(404) { } end end end diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb index 5a782ca5..30f99cb3 100644 --- a/lib/rb/lib/thrift/struct.rb +++ b/lib/rb/lib/thrift/struct.rb @@ -22,7 +22,7 @@ module Thrift iprot.read_struct_begin loop do fname, ftype, fid = iprot.read_field_begin - break if (ftype === Types::STOP) + break if (ftype == Types::STOP) handle_message(iprot, fid, ftype) iprot.read_field_end end @@ -32,7 +32,7 @@ module Thrift def write(oprot) oprot.write_struct_begin(self.class.name) each_field do |fid, type, name| - if ((value = instance_variable_get("@#{name}")) != nil) + unless (value = instance_variable_get("@#{name}")).nil? if is_container? type oprot.write_field_begin(name, type, fid) write_container(oprot, value, struct_fields[fid]) @@ -58,7 +58,7 @@ module Thrift def handle_message(iprot, fid, ftype) field = struct_fields[fid] - if field && field[:type] == ftype + if field and field[:type] == ftype value = read_field(iprot, field) instance_variable_set("@#{field[:name]}", value) else @@ -67,10 +67,11 @@ module Thrift end def read_field(iprot, field = {}) - if field[:type] == Types::STRUCT + case field[:type] + when Types::STRUCT value = field[:class].new value.read(iprot) - elsif field[:type] == Types::MAP + when Types::MAP key_type, val_type, size = iprot.read_map_begin value = {} size.times do @@ -79,13 +80,13 @@ module Thrift value[k] = v end iprot.read_map_end - elsif field[:type] == Types::LIST + when Types::LIST e_type, size = iprot.read_list_begin value = Array.new(size) do |n| read_field(iprot, field_info(field[:element])) end iprot.read_list_end - elsif field[:type] == Types::SET + when Types::SET e_type, size = iprot.read_set_begin value = Set.new size.times do @@ -108,20 +109,21 @@ module Thrift end def write_container(oprot, value, field = {}) - if field[:type] == Types::MAP + case field[:type] + when Types::MAP oprot.write_map_begin(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.write_map_end - elsif field[:type] == Types::LIST + when Types::LIST oprot.write_list_begin(field[:element][:type], value.size) value.each do |elem| write_data(oprot, elem, field[:element]) end oprot.write_list_end - elsif field[:type] == Types::SET + when Types::SET oprot.write_set_begin(field[:element][:type], value.size) value.each do |v,| # the , is to preserve compatibility with the old Hash-style sets write_data(oprot, v, field[:element]) diff --git a/lib/rb/lib/thrift/transport.rb b/lib/rb/lib/thrift/transport.rb index 439740d1..7dd663ae 100644 --- a/lib/rb/lib/thrift/transport.rb +++ b/lib/rb/lib/thrift/transport.rb @@ -38,16 +38,14 @@ module Thrift def read(sz); end def read_all(size) - buff = '' - have = 0 + buf = '' - while (have < size) - chunk = read(size - have) - have += chunk.length - buff << chunk + while (buf.length < size) + chunk = read(size - buf.length) + buf << chunk end - buff + buf end deprecate! :readAll => :read_all @@ -137,41 +135,22 @@ module Thrift end def read(sz) - if !@read - return @transport.read(sz) - end + return @transport.read(sz) unless @read - if (sz <= 0) - return '' - end + return '' if sz <= 0 - if (@rbuf.length == 0) - read_frame - end + read_frame if @rbuf.empty? # return full buf - if (sz > @rbuf.length) - out = @rbuf - @rbuf = '' - return out - end - - # Return substr - out = @rbuf.slice(0, sz) - @rbuf = @rbuf.slice(sz, @rbuf.length) - return out + out = @rbuf.slice(0...sz) + @rbuf = @rbuf.slice(sz..-1) || '' + out end def write(buf,sz=nil) - if !@write - return @transport.write(buf); - end - - if (sz != nil && sz < buf.length) - buf = buf.slice(0,sz) - end + return @transport.write(buf) unless @write - @wbuf << buf + @wbuf << (sz ? buf[0...sz] : buf) end # @@ -179,9 +158,7 @@ module Thrift # followed by the actual data. # def flush - if !@write - return @transport.flush - end + return @transport.flush unless @write out = [@wbuf.length].pack('N') out << @wbuf @@ -193,9 +170,7 @@ module Thrift private def read_frame - buf = @transport.read_all(4) - val = buf.unpack('N') - sz = val[0] + sz = @transport.read_all(4).unpack('N').first @rbuf = @transport.read_all(sz) end