From: Kevin Clark Date: Wed, 18 Jun 2008 00:57:26 +0000 (+0000) Subject: Move thrift/protocol/tprotocol to thrift/protocol and thrift/protocol/tbinaryprotocol... X-Git-Tag: 0.2.0~635 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=6fda4b76e2c8a963fcc28edac725da48fffd1784;p=common%2Fthrift.git Move thrift/protocol/tprotocol to thrift/protocol and thrift/protocol/tbinaryprotocol to thrift/protocol/binaryprotocol. Leave shim files behind for backwards compatibility git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@668914 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/rb/lib/thrift/protocol.rb b/lib/rb/lib/thrift/protocol.rb new file mode 100644 index 00000000..47fe7233 --- /dev/null +++ b/lib/rb/lib/thrift/protocol.rb @@ -0,0 +1,223 @@ +# +# Copyright (c) 2006- Facebook +# Distributed under the Thrift Software License +# +# See accompanying file LICENSE or visit the Thrift site at: +# http://developers.facebook.com/thrift/ +# +# Author: Mark Slee +# + +module Thrift + class ProtocolException < Exception + + UNKNOWN = 0 + INVALID_DATA = 1 + NEGATIVE_SIZE = 2 + SIZE_LIMIT = 3 + BAD_VERSION = 4 + + attr_reader :type + + def initialize(type=UNKNOWN, message=nil) + super(message) + @type = type + end + + end + deprecate_class! :TProtocolException => ProtocolException + + class Protocol + + attr_reader :trans + + def initialize(trans) + @trans = trans + end + + def writeMessageBegin(name, type, seqid); nil; end + + def writeMessageEnd; nil; end + + def writeStructBegin(name); nil; end + + def writeStructEnd(); nil; end + + def writeFieldBegin(name, type, id); nil; end + + def writeFieldEnd(); nil; end + + def writeFieldStop(); nil; end + + def writeMapBegin(ktype, vtype, size); nil; end + + def writeMapEnd(); nil; end + + def writeListBegin(etype, size); nil; end + + def writeListEnd(); nil; end + + def writeSetBegin(etype, size); nil; end + + def writeSetEnd(); nil; end + + def writeBool(bool); nil; end + + def writeByte(byte); nil; end + + def writeI16(i16); nil; end + + def writeI32(i32); nil; end + + def writeI64(i64); nil; end + + def writeDouble(dub); nil; end + + def writeString(str); nil; end + + def readMessageBegin(); nil; end + + def readMessageEnd(); nil; end + + def readStructBegin(); nil; end + + def readStructEnd(); nil; end + + def readFieldBegin(); nil; end + + def readFieldEnd(); nil; end + + def readMapBegin(); nil; end + + def readMapEnd(); nil; end + + def readListBegin(); nil; end + + def readListEnd(); nil; end + + def readSetBegin(); nil; end + + def readSetEnd(); nil; end + + def readBool(); nil; end + + def readByte(); nil; end + + def readI16(); nil; end + + def readI32(); nil; end + + def readI64(); nil; end + + def readDouble(); 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_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 + + 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 + + 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 + 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 + end + + end + deprecate_class! :TProtocol => Protocol +end + +class TProtocolFactory + def getProtocol(trans); nil; end +end diff --git a/lib/rb/lib/thrift/protocol/binaryprotocol.rb b/lib/rb/lib/thrift/protocol/binaryprotocol.rb new file mode 100644 index 00000000..98323839 --- /dev/null +++ b/lib/rb/lib/thrift/protocol/binaryprotocol.rb @@ -0,0 +1,191 @@ +# +# Copyright (c) 2006- Facebook +# Distributed under the Thrift Software License +# +# See accompanying file LICENSE or visit the Thrift site at: +# http://developers.facebook.com/thrift/ +# +# Author: Mark Slee +# +require 'thrift/protocol' + +module Thrift + class BinaryProtocol < Protocol + VERSION_MASK = 0xffff0000 + VERSION_1 = 0x80010000 + + def initialize(trans) + super(trans) + end + + def writeMessageBegin(name, type, seqid) + writeI32(VERSION_1 | type) + writeString(name) + writeI32(seqid) + end + + def writeFieldBegin(name, type, id) + writeByte(type) + writeI16(id) + end + + def writeFieldStop() + writeByte(Thrift::Types::STOP) + end + + def writeMapBegin(ktype, vtype, size) + writeByte(ktype) + writeByte(vtype) + writeI32(size) + end + + def writeListBegin(etype, size) + writeByte(etype) + writeI32(size) + end + + def writeSetBegin(etype, size) + writeByte(etype) + writeI32(size) + end + + def writeBool(bool) + if (bool) + writeByte(1) + else + writeByte(0) + end + end + + def writeByte(byte) + trans.write([byte].pack('n')[1..1]) + end + + def writeI16(i16) + trans.write([i16].pack('n')) + end + + def writeI32(i32) + trans.write([i32].pack('N')) + end + + def writeI64(i64) + hi = i64 >> 32 + lo = i64 & 0xffffffff + trans.write([hi, lo].pack('N2')) + end + + def writeDouble(dub) + trans.write([dub].pack('G')) + end + + def writeString(str) + writeI32(str.length) + trans.write(str) + end + + def readMessageBegin() + version = readI32() + if (version & VERSION_MASK != VERSION_1) + raise ProtocolException.new(ProtocolException::BAD_VERSION, 'Missing version identifier') + end + type = version & 0x000000ff + name = readString() + seqid = readI32() + return name, type, seqid + end + + def readFieldBegin() + type = readByte() + if (type === Types::STOP) + return nil, type, 0 + end + id = readI16() + return nil, type, id + end + + def readMapBegin() + ktype = readByte() + vtype = readByte() + size = readI32() + return ktype, vtype, size + end + + def readListBegin() + etype = readByte() + size = readI32() + return etype, size + end + + def readSetBegin() + etype = readByte() + size = readI32() + return etype, size + end + + def readBool() + byte = readByte() + return byte != 0 + end + + def readByte() + dat = trans.readAll(1) + val = dat[0] + if (val > 0x7f) + val = 0 - ((val - 1) ^ 0xff) + end + return val + end + + def readI16() + dat = trans.readAll(2) + val, = dat.unpack('n') + if (val > 0x7fff) + val = 0 - ((val - 1) ^ 0xffff) + end + return val + end + + def readI32() + dat = trans.readAll(4) + val, = dat.unpack('N') + if (val > 0x7fffffff) + val = 0 - ((val - 1) ^ 0xffffffff) + end + return val + end + + def readI64() + dat = trans.readAll(8) + hi, lo = dat.unpack('N2') + if (hi > 0x7fffffff) + hi = hi ^ 0xffffffff + lo = lo ^ 0xffffffff + return 0 - hi*4294967296 - lo - 1 + else + return hi*4294967296 + lo + end + end + + def readDouble() + dat = trans.readAll(8) + val, = dat.unpack('G') + return val + end + + def readString() + sz = readI32() + dat = trans.readAll(sz) + return dat + end + + end + deprecate_class! :TBinaryProtocol => BinaryProtocol +end + +class TBinaryProtocolFactory < TProtocolFactory + def getProtocol(trans) + return TBinaryProtocol.new(trans) + end +end + diff --git a/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb b/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb index adfeb812..038e1903 100644 --- a/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb +++ b/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb @@ -1,192 +1 @@ -#!/usr/bin/env ruby -# -# Copyright (c) 2006- Facebook -# Distributed under the Thrift Software License -# -# See accompanying file LICENSE or visit the Thrift site at: -# http://developers.facebook.com/thrift/ -# -# Author: Mark Slee -# -require 'thrift/protocol/tprotocol' - -module Thrift - class BinaryProtocol < Protocol - VERSION_MASK = 0xffff0000 - VERSION_1 = 0x80010000 - - def initialize(trans) - super(trans) - end - - def writeMessageBegin(name, type, seqid) - writeI32(VERSION_1 | type) - writeString(name) - writeI32(seqid) - end - - def writeFieldBegin(name, type, id) - writeByte(type) - writeI16(id) - end - - def writeFieldStop() - writeByte(Thrift::Types::STOP) - end - - def writeMapBegin(ktype, vtype, size) - writeByte(ktype) - writeByte(vtype) - writeI32(size) - end - - def writeListBegin(etype, size) - writeByte(etype) - writeI32(size) - end - - def writeSetBegin(etype, size) - writeByte(etype) - writeI32(size) - end - - def writeBool(bool) - if (bool) - writeByte(1) - else - writeByte(0) - end - end - - def writeByte(byte) - trans.write([byte].pack('n')[1..1]) - end - - def writeI16(i16) - trans.write([i16].pack('n')) - end - - def writeI32(i32) - trans.write([i32].pack('N')) - end - - def writeI64(i64) - hi = i64 >> 32 - lo = i64 & 0xffffffff - trans.write([hi, lo].pack('N2')) - end - - def writeDouble(dub) - trans.write([dub].pack('G')) - end - - def writeString(str) - writeI32(str.length) - trans.write(str) - end - - def readMessageBegin() - version = readI32() - if (version & VERSION_MASK != VERSION_1) - raise ProtocolException.new(ProtocolException::BAD_VERSION, 'Missing version identifier') - end - type = version & 0x000000ff - name = readString() - seqid = readI32() - return name, type, seqid - end - - def readFieldBegin() - type = readByte() - if (type === Types::STOP) - return nil, type, 0 - end - id = readI16() - return nil, type, id - end - - def readMapBegin() - ktype = readByte() - vtype = readByte() - size = readI32() - return ktype, vtype, size - end - - def readListBegin() - etype = readByte() - size = readI32() - return etype, size - end - - def readSetBegin() - etype = readByte() - size = readI32() - return etype, size - end - - def readBool() - byte = readByte() - return byte != 0 - end - - def readByte() - dat = trans.readAll(1) - val = dat[0] - if (val > 0x7f) - val = 0 - ((val - 1) ^ 0xff) - end - return val - end - - def readI16() - dat = trans.readAll(2) - val, = dat.unpack('n') - if (val > 0x7fff) - val = 0 - ((val - 1) ^ 0xffff) - end - return val - end - - def readI32() - dat = trans.readAll(4) - val, = dat.unpack('N') - if (val > 0x7fffffff) - val = 0 - ((val - 1) ^ 0xffffffff) - end - return val - end - - def readI64() - dat = trans.readAll(8) - hi, lo = dat.unpack('N2') - if (hi > 0x7fffffff) - hi = hi ^ 0xffffffff - lo = lo ^ 0xffffffff - return 0 - hi*4294967296 - lo - 1 - else - return hi*4294967296 + lo - end - end - - def readDouble() - dat = trans.readAll(8) - val, = dat.unpack('G') - return val - end - - def readString() - sz = readI32() - dat = trans.readAll(sz) - return dat - end - - end - deprecate_class! :TBinaryProtocol => BinaryProtocol -end - -class TBinaryProtocolFactory < TProtocolFactory - def getProtocol(trans) - return TBinaryProtocol.new(trans) - end -end - +require 'thrift/protocol/binaryprotocol' diff --git a/lib/rb/lib/thrift/protocol/tprotocol.rb b/lib/rb/lib/thrift/protocol/tprotocol.rb index 905b754c..48bf2616 100644 --- a/lib/rb/lib/thrift/protocol/tprotocol.rb +++ b/lib/rb/lib/thrift/protocol/tprotocol.rb @@ -1,224 +1 @@ -#!/usr/bin/env ruby -# -# Copyright (c) 2006- Facebook -# Distributed under the Thrift Software License -# -# See accompanying file LICENSE or visit the Thrift site at: -# http://developers.facebook.com/thrift/ -# -# Author: Mark Slee -# - -module Thrift - class ProtocolException < Exception - - UNKNOWN = 0 - INVALID_DATA = 1 - NEGATIVE_SIZE = 2 - SIZE_LIMIT = 3 - BAD_VERSION = 4 - - attr_reader :type - - def initialize(type=UNKNOWN, message=nil) - super(message) - @type = type - end - - end - deprecate_class! :TProtocolException => ProtocolException - - class Protocol - - attr_reader :trans - - def initialize(trans) - @trans = trans - end - - def writeMessageBegin(name, type, seqid); nil; end - - def writeMessageEnd; nil; end - - def writeStructBegin(name); nil; end - - def writeStructEnd(); nil; end - - def writeFieldBegin(name, type, id); nil; end - - def writeFieldEnd(); nil; end - - def writeFieldStop(); nil; end - - def writeMapBegin(ktype, vtype, size); nil; end - - def writeMapEnd(); nil; end - - def writeListBegin(etype, size); nil; end - - def writeListEnd(); nil; end - - def writeSetBegin(etype, size); nil; end - - def writeSetEnd(); nil; end - - def writeBool(bool); nil; end - - def writeByte(byte); nil; end - - def writeI16(i16); nil; end - - def writeI32(i32); nil; end - - def writeI64(i64); nil; end - - def writeDouble(dub); nil; end - - def writeString(str); nil; end - - def readMessageBegin(); nil; end - - def readMessageEnd(); nil; end - - def readStructBegin(); nil; end - - def readStructEnd(); nil; end - - def readFieldBegin(); nil; end - - def readFieldEnd(); nil; end - - def readMapBegin(); nil; end - - def readMapEnd(); nil; end - - def readListBegin(); nil; end - - def readListEnd(); nil; end - - def readSetBegin(); nil; end - - def readSetEnd(); nil; end - - def readBool(); nil; end - - def readByte(); nil; end - - def readI16(); nil; end - - def readI32(); nil; end - - def readI64(); nil; end - - def readDouble(); 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_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 - - 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 - - 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 - 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 - end - - end - deprecate_class! :TProtocol => Protocol -end - -class TProtocolFactory - def getProtocol(trans); nil; end -end +require 'thrift/protocol' diff --git a/lib/rb/lib/thrift/thrift.rb b/lib/rb/lib/thrift/thrift.rb index dfad6504..a6761602 100644 --- a/lib/rb/lib/thrift/thrift.rb +++ b/lib/rb/lib/thrift/thrift.rb @@ -1,8 +1,9 @@ # This file kept for backwards compatability # require File.join(File.dirname(__FILE__), '../thrift') -require File.join(File.dirname(__FILE__), 'deprecation') -require File.join(File.dirname(__FILE__), 'types') -require File.join(File.dirname(__FILE__), 'processor') -require File.join(File.dirname(__FILE__), 'exceptions') -require File.join(File.dirname(__FILE__), 'client') -require File.join(File.dirname(__FILE__), 'struct') +$:.unshift File.dirname(File.dirname(__FILE__)) +require 'thrift/deprecation' +require 'thrift/types' +require 'thrift/processor' +require 'thrift/exceptions' +require 'thrift/client' +require 'thrift/struct'