Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 1 | from TProtocol import * |
| 2 | from struct import pack, unpack |
| 3 | |
| 4 | class TBinaryProtocol(TProtocolBase): |
| 5 | |
| 6 | """Binary implementation of the Thrift protocol driver.""" |
| 7 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 8 | def __init__(self, itrans, otrans=None): |
| 9 | TProtocolBase.__init__(self, itrans, otrans) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 10 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 11 | def writeMessageBegin(self, name, type, seqid): |
| 12 | self.writeString(name) |
| 13 | self.writeByte(type) |
| 14 | self.writeI32(seqid) |
| 15 | |
| 16 | def writeMessageEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 17 | pass |
| 18 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 19 | def writeStructBegin(self, name): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 20 | pass |
| 21 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 22 | def writeStructEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 23 | pass |
| 24 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 25 | def writeFieldBegin(self, name, type, id): |
| 26 | self.writeByte(type) |
| 27 | self.writeI16(id) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 28 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 29 | def writeFieldEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 30 | pass |
| 31 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 32 | def writeFieldStop(self): |
| 33 | self.writeByte(TType.STOP); |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 34 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 35 | def writeMapBegin(self, ktype, vtype, size): |
| 36 | self.writeByte(ktype) |
| 37 | self.writeByte(vtype) |
| 38 | self.writeI32(size) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 39 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 40 | def writeMapEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 41 | pass |
| 42 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 43 | def writeListBegin(self, etype, size): |
| 44 | self.writeByte(etype) |
| 45 | self.writeI32(size) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 46 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 47 | def writeListEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 48 | pass |
| 49 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 50 | def writeSetBegin(self, etype, size): |
| 51 | self.writeByte(etype) |
| 52 | self.writeI32(size) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 53 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 54 | def writeSetEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 55 | pass |
| 56 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 57 | def writeBool(self, bool): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 58 | if bool: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 59 | self.writeByte(1) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 60 | else: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 61 | self.writeByte(0) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 62 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 63 | def writeByte(self, byte): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 64 | buff = pack("!b", byte) |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 65 | self.otrans.write(buff) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 66 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 67 | def writeI16(self, i16): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 68 | buff = pack("!h", i16) |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 69 | self.otrans.write(buff) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 70 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 71 | def writeI32(self, i32): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 72 | buff = pack("!i", i32) |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 73 | self.otrans.write(buff) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 74 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 75 | def writeI64(self, i64): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 76 | buff = pack("!q", i64) |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 77 | self.otrans.write(buff) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 78 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 79 | def writeDouble(self, dub): |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 80 | buff = pack("!d", dub) |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 81 | self.otrans.write(buff) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 82 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 83 | def writeString(self, str): |
| 84 | self.writeI32(len(str)) |
| 85 | self.otrans.write(str) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 86 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 87 | def readMessageBegin(self): |
| 88 | name = self.readString() |
| 89 | type = self.readByte() |
| 90 | seqid = self.readI32() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 91 | return (name, type, seqid) |
| 92 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 93 | def readMessageEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 94 | pass |
| 95 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 96 | def readStructBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 97 | pass |
| 98 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 99 | def readStructEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 100 | pass |
| 101 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 102 | def readFieldBegin(self): |
| 103 | type = self.readByte() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 104 | if type == TType.STOP: |
| 105 | return (None, type, 0) |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 106 | id = self.readI16() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 107 | return (None, type, id) |
| 108 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 109 | def readFieldEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 110 | pass |
| 111 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 112 | def readMapBegin(self): |
| 113 | ktype = self.readByte() |
| 114 | vtype = self.readByte() |
| 115 | size = self.readI32() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 116 | return (ktype, vtype, size) |
| 117 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 118 | def readMapEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 119 | pass |
| 120 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 121 | def readListBegin(self): |
| 122 | etype = self.readByte() |
| 123 | size = self.readI32() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 124 | return (etype, size) |
| 125 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 126 | def readListEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 127 | pass |
| 128 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 129 | def readSetBegin(self): |
| 130 | etype = self.readByte() |
| 131 | size = self.readI32() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 132 | return (etype, size) |
| 133 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 134 | def readSetEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 135 | pass |
| 136 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 137 | def readBool(self): |
| 138 | byte = self.readByte() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 139 | if byte == 0: |
| 140 | return False |
| 141 | return True |
| 142 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 143 | def readByte(self): |
| 144 | buff = self.itrans.readAll(1) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 145 | val, = unpack('!b', buff) |
| 146 | return val |
| 147 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 148 | def readI16(self): |
| 149 | buff = self.itrans.readAll(2) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 150 | val, = unpack('!h', buff) |
| 151 | return val |
| 152 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 153 | def readI32(self): |
| 154 | buff = self.itrans.readAll(4) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 155 | val, = unpack('!i', buff) |
| 156 | return val |
| 157 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 158 | def readI64(self): |
| 159 | buff = self.itrans.readAll(8) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 160 | val, = unpack('!q', buff) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 161 | return val |
| 162 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 163 | def readDouble(self): |
| 164 | buff = self.itrans.readAll(8) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 165 | val, = unpack('!d', buff) |
| 166 | return val |
| 167 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 168 | def readString(self): |
| 169 | len = self.readI32() |
| 170 | str = self.itrans.readAll(len) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 171 | return str |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame^] | 172 | |
| 173 | class TBinaryProtocolFactory: |
| 174 | def getIOProtocols(self, itrans, otrans): |
| 175 | prot = TBinaryProtocol(itrans, otrans) |
| 176 | return (prot, prot) |