You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by kc...@apache.org on 2008/06/18 03:14:48 UTC

svn commit: r668991 - in /incubator/thrift/trunk/lib/rb/lib/thrift: client.rb exceptions.rb processor.rb protocol.rb protocol/binaryprotocol.rb server.rb server/httpserver.rb struct.rb transport.rb

Author: kclark
Date: Tue Jun 17 18:14:48 2008
New Revision: 668991

URL: http://svn.apache.org/viewvc?rev=668991&view=rev
Log:
Make a lot of miscellaneous ruby-styling changes

Modified:
    incubator/thrift/trunk/lib/rb/lib/thrift/client.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/exceptions.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/processor.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/protocol.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/server.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/server/httpserver.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/client.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/client.rb?rev=668991&r1=668990&r2=668991&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/client.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/client.rb Tue Jun 17 18:14:48 2008
@@ -23,7 +23,7 @@
       result = result_klass.new
       result.read(@iprot)
       @iprot.read_message_end
-      return result
+      result
     end
 
     def handle_exception(mtype)

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/exceptions.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/exceptions.rb?rev=668991&r1=668990&r2=668991&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/exceptions.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/exceptions.rb Tue Jun 17 18:14:48 2008
@@ -29,21 +29,13 @@
       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 @@
 
     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

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/processor.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/processor.rb?rev=668991&r1=668990&r2=668991&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/processor.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/processor.rb Tue Jun 17 18:14:48 2008
@@ -8,16 +8,16 @@
       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
 

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/protocol.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/protocol.rb?rev=668991&r1=668990&r2=668991&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/protocol.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/protocol.rb Tue Jun 17 18:14:48 2008
@@ -206,48 +206,46 @@
     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)

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb?rev=668991&r1=668990&r2=668991&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/protocol/binaryprotocol.rb Tue Jun 17 18:14:48 2008
@@ -46,15 +46,11 @@
     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 @@
       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 @@
       if (val > 0x7f)
         val = 0 - ((val - 1) ^ 0xff)
       end
-      return val
+      val
     end
 
     def read_i16
@@ -139,7 +136,7 @@
       if (val > 0x7fff)
         val = 0 - ((val - 1) ^ 0xffff)
       end
-      return val
+      val
     end
 
     def read_i32
@@ -148,31 +145,31 @@
       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

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/server.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/server.rb?rev=668991&r1=668990&r2=668991&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/server.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/server.rb Tue Jun 17 18:14:48 2008
@@ -31,16 +31,15 @@
     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 @@
     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 @@
       @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

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/server/httpserver.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/server/httpserver.rb?rev=668991&r1=668990&r2=668991&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/server/httpserver.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/server/httpserver.rb Tue Jun 17 18:14:48 2008
@@ -14,15 +14,15 @@
       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

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb?rev=668991&r1=668990&r2=668991&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb Tue Jun 17 18:14:48 2008
@@ -22,7 +22,7 @@
       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 @@
     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 @@
 
     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 @@
     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 @@
           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 @@
     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])

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb?rev=668991&r1=668990&r2=668991&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb Tue Jun 17 18:14:48 2008
@@ -38,16 +38,14 @@
     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 @@
     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 @@
     # 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 @@
     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