Commit 1be82862 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Move packet decoding method at module level instead of Packet class as it was

done for encoding.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@500 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent d9b88d8b
...@@ -209,7 +209,7 @@ class Connection(BaseConnection): ...@@ -209,7 +209,7 @@ class Connection(BaseConnection):
while 1: while 1:
packet = None packet = None
try: try:
packet = Packet.parse(self.read_buf) packet = protocol.parse(self.read_buf)
except PacketMalformedError, msg: except PacketMalformedError, msg:
self.handler.packetMalformed(self, packet, msg) self.handler.packetMalformed(self, packet, msg)
return return
......
...@@ -321,6 +321,8 @@ UUID_NAMESPACES = { ...@@ -321,6 +321,8 @@ UUID_NAMESPACES = {
class ProtocolError(Exception): pass class ProtocolError(Exception): pass
class PacketMalformedError(ProtocolError): pass class PacketMalformedError(ProtocolError): pass
decode_table = {}
class Packet(object): class Packet(object):
"""A packet.""" """A packet."""
...@@ -328,27 +330,6 @@ class Packet(object): ...@@ -328,27 +330,6 @@ class Packet(object):
_type = None _type = None
_len = None _len = None
@classmethod
def parse(cls, msg):
# logging.debug('parsing %s', dump(msg))
if len(msg) < MIN_PACKET_SIZE:
return None
msg_id, msg_type, msg_len = unpack('!LHL', msg[:PACKET_HEADER_SIZE])
try:
msg_type = packet_types[msg_type]
except KeyError:
raise PacketMalformedError('Unknown packet type')
if msg_len > MAX_PACKET_SIZE:
raise PacketMalformedError('message too big (%d)' % msg_len)
if msg_len < MIN_PACKET_SIZE:
raise PacketMalformedError('message too small (%d)' % msg_len)
if len(msg) < msg_len:
# Not enough.
return None
packet = cls(msg_type, msg[PACKET_HEADER_SIZE:msg_len])
packet.setId(msg_id)
return packet
def __init__(self, msg_type, body=''): def __init__(self, msg_type, body=''):
self._id = None self._id = None
self._type = msg_type self._type = msg_type
...@@ -369,7 +350,6 @@ class Packet(object): ...@@ -369,7 +350,6 @@ class Packet(object):
except TypeError: except TypeError:
return PACKET_HEADER_SIZE return PACKET_HEADER_SIZE
# Encoders.
def encode(self): def encode(self):
msg = pack('!LHL', self._id, self._type, PACKET_HEADER_SIZE + len(self._body)) + self._body msg = pack('!LHL', self._id, self._type, PACKET_HEADER_SIZE + len(self._body)) + self._body
if len(msg) > MAX_PACKET_SIZE: if len(msg) > MAX_PACKET_SIZE:
...@@ -377,20 +357,38 @@ class Packet(object): ...@@ -377,20 +357,38 @@ class Packet(object):
return msg return msg
__str__ = encode __str__ = encode
# Decoders.
def decode(self): def decode(self):
try: try:
method = self.decode_table[self._type] method = decode_table[self._type]
except KeyError: except KeyError:
raise PacketMalformedError('unknown message type 0x%x' % self._type) raise PacketMalformedError('unknown message type 0x%x' % self._type)
return method(self) return method(self._body)
decode_table = {} # packet parser
def parse(msg):
# logging.debug('parsing %s', dump(msg))
if len(msg) < MIN_PACKET_SIZE:
return None
msg_id, msg_type, msg_len = unpack('!LHL', msg[:PACKET_HEADER_SIZE])
try:
msg_type = packet_types[msg_type]
except KeyError:
raise PacketMalformedError('Unknown packet type')
if msg_len > MAX_PACKET_SIZE:
raise PacketMalformedError('message too big (%d)' % msg_len)
if msg_len < MIN_PACKET_SIZE:
raise PacketMalformedError('message too small (%d)' % msg_len)
if len(msg) < msg_len:
# Not enough.
return None
packet = Packet(msg_type, msg[PACKET_HEADER_SIZE:msg_len])
packet.setId(msg_id)
return packet
def _decodeError(self): # packet decoding
def _decodeError(body):
try: try:
body = self._body body = body
code, size = unpack('!HL', body[:6]) code, size = unpack('!HL', body[:6])
message = body[6:] message = body[6:]
except struct.error, msg: except struct.error, msg:
...@@ -398,19 +396,19 @@ class Packet(object): ...@@ -398,19 +396,19 @@ class Packet(object):
if len(message) != size: if len(message) != size:
raise PacketMalformedError('invalid error message size') raise PacketMalformedError('invalid error message size')
return code, message return code, message
decode_table[ERROR] = _decodeError decode_table[ERROR] = _decodeError
def _decodePing(self): def _decodePing(body):
pass pass
decode_table[PING] = _decodePing decode_table[PING] = _decodePing
def _decodePong(self): def _decodePong(body):
pass pass
decode_table[PONG] = _decodePong decode_table[PONG] = _decodePong
def _decodeRequestNodeIdentification(self): def _decodeRequestNodeIdentification(body):
try: try:
body = self._body body = body
major, minor, node_type, uuid, ip_address, port, size \ major, minor, node_type, uuid, ip_address, port, size \
= unpack('!LLH16s4sHL', body[:36]) = unpack('!LLH16s4sHL', body[:36])
ip_address = inet_ntoa(ip_address) ip_address = inet_ntoa(ip_address)
...@@ -424,51 +422,51 @@ class Packet(object): ...@@ -424,51 +422,51 @@ class Packet(object):
if (major, minor) != PROTOCOL_VERSION: if (major, minor) != PROTOCOL_VERSION:
raise PacketMalformedError('protocol version mismatch') raise PacketMalformedError('protocol version mismatch')
return node_type, uuid, ip_address, port, name return node_type, uuid, ip_address, port, name
decode_table[REQUEST_NODE_IDENTIFICATION] = _decodeRequestNodeIdentification decode_table[REQUEST_NODE_IDENTIFICATION] = _decodeRequestNodeIdentification
def _decodeAcceptNodeIdentification(self): def _decodeAcceptNodeIdentification(body):
try: try:
node_type, uuid, ip_address, port, num_partitions, num_replicas, your_uuid \ node_type, uuid, ip_address, port, num_partitions, num_replicas, your_uuid \
= unpack('!H16s4sHLL16s', self._body) = unpack('!H16s4sHLL16s', body)
ip_address = inet_ntoa(ip_address) ip_address = inet_ntoa(ip_address)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid accept node identification') raise PacketMalformedError('invalid accept node identification')
if node_type not in VALID_NODE_TYPE_LIST: if node_type not in VALID_NODE_TYPE_LIST:
raise PacketMalformedError('invalid node type %d' % node_type) raise PacketMalformedError('invalid node type %d' % node_type)
return node_type, uuid, ip_address, port, num_partitions, num_replicas, your_uuid return node_type, uuid, ip_address, port, num_partitions, num_replicas, your_uuid
decode_table[ACCEPT_NODE_IDENTIFICATION] = _decodeAcceptNodeIdentification decode_table[ACCEPT_NODE_IDENTIFICATION] = _decodeAcceptNodeIdentification
def _decodeAskPrimaryMaster(self): def _decodeAskPrimaryMaster(body):
pass pass
decode_table[ASK_PRIMARY_MASTER] = _decodeAskPrimaryMaster decode_table[ASK_PRIMARY_MASTER] = _decodeAskPrimaryMaster
def _decodeAnswerPrimaryMaster(self): def _decodeAnswerPrimaryMaster(body):
try: try:
primary_uuid, n = unpack('!16sL', self._body[:20]) primary_uuid, n = unpack('!16sL', body[:20])
known_master_list = [] known_master_list = []
for i in xrange(n): for i in xrange(n):
ip_address, port, uuid = unpack('!4sH16s', self._body[20+i*22:42+i*22]) ip_address, port, uuid = unpack('!4sH16s', body[20+i*22:42+i*22])
ip_address = inet_ntoa(ip_address) ip_address = inet_ntoa(ip_address)
known_master_list.append((ip_address, port, uuid)) known_master_list.append((ip_address, port, uuid))
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer primary master') raise PacketMalformedError('invalid answer primary master')
return primary_uuid, known_master_list return primary_uuid, known_master_list
decode_table[ANSWER_PRIMARY_MASTER] = _decodeAnswerPrimaryMaster decode_table[ANSWER_PRIMARY_MASTER] = _decodeAnswerPrimaryMaster
def _decodeAnnouncePrimaryMaster(self): def _decodeAnnouncePrimaryMaster(body):
pass pass
decode_table[ANNOUNCE_PRIMARY_MASTER] = _decodeAnnouncePrimaryMaster decode_table[ANNOUNCE_PRIMARY_MASTER] = _decodeAnnouncePrimaryMaster
def _decodeReelectPrimaryMaster(self): def _decodeReelectPrimaryMaster(body):
pass pass
decode_table[REELECT_PRIMARY_MASTER] = _decodeReelectPrimaryMaster decode_table[REELECT_PRIMARY_MASTER] = _decodeReelectPrimaryMaster
def _decodeNotifyNodeInformation(self): def _decodeNotifyNodeInformation(body):
try: try:
n = unpack('!L', self._body[:4])[0] n = unpack('!L', body[:4])[0]
node_list = [] node_list = []
for i in xrange(n): for i in xrange(n):
r = unpack('!H4sH16sH', self._body[4+i*26:30+i*26]) r = unpack('!H4sH16sH', body[4+i*26:30+i*26])
node_type, ip_address, port, uuid, state = r node_type, ip_address, port, uuid, state = r
ip_address = inet_ntoa(ip_address) ip_address = inet_ntoa(ip_address)
if node_type not in VALID_NODE_TYPE_LIST: if node_type not in VALID_NODE_TYPE_LIST:
...@@ -482,43 +480,43 @@ class Packet(object): ...@@ -482,43 +480,43 @@ class Packet(object):
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer node information') raise PacketMalformedError('invalid answer node information')
return (node_list,) return (node_list,)
decode_table[NOTIFY_NODE_INFORMATION] = _decodeNotifyNodeInformation decode_table[NOTIFY_NODE_INFORMATION] = _decodeNotifyNodeInformation
def _decodeAskLastIDs(self): def _decodeAskLastIDs(body):
pass pass
decode_table[ASK_LAST_IDS] = _decodeAskLastIDs decode_table[ASK_LAST_IDS] = _decodeAskLastIDs
def _decodeAnswerLastIDs(self): def _decodeAnswerLastIDs(body):
try: try:
loid, ltid, lptid = unpack('!8s8s8s', self._body) loid, ltid, lptid = unpack('!8s8s8s', body)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer last ids') raise PacketMalformedError('invalid answer last ids')
return loid, ltid, lptid return loid, ltid, lptid
decode_table[ANSWER_LAST_IDS] = _decodeAnswerLastIDs decode_table[ANSWER_LAST_IDS] = _decodeAnswerLastIDs
def _decodeAskPartitionTable(self): def _decodeAskPartitionTable(body):
try: try:
n = unpack('!L', self._body[:4])[0] n = unpack('!L', body[:4])[0]
offset_list = [] offset_list = []
for i in xrange(n): for i in xrange(n):
offset = unpack('!L', self._body[4+i*4:8+i*4])[0] offset = unpack('!L', body[4+i*4:8+i*4])[0]
offset_list.append(offset) offset_list.append(offset)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid ask partition table') raise PacketMalformedError('invalid ask partition table')
return (offset_list,) return (offset_list,)
decode_table[ASK_PARTITION_TABLE] = _decodeAskPartitionTable decode_table[ASK_PARTITION_TABLE] = _decodeAskPartitionTable
def _decodeAnswerPartitionTable(self): def _decodeAnswerPartitionTable(body):
try: try:
ptid, n = unpack('!8sL', self._body[:12]) ptid, n = unpack('!8sL', body[:12])
index = 12 index = 12
row_list = [] row_list = []
cell_list = [] cell_list = []
for i in xrange(n): for i in xrange(n):
offset, m = unpack('!LL', self._body[index:index+8]) offset, m = unpack('!LL', body[index:index+8])
index += 8 index += 8
for j in xrange(m): for j in xrange(m):
uuid, state = unpack('!16sH', self._body[index:index+18]) uuid, state = unpack('!16sH', body[index:index+18])
index += 18 index += 18
state = partition_cell_states.get(state) state = partition_cell_states.get(state)
cell_list.append((uuid, state)) cell_list.append((uuid, state))
...@@ -527,19 +525,19 @@ class Packet(object): ...@@ -527,19 +525,19 @@ class Packet(object):
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer partition table') raise PacketMalformedError('invalid answer partition table')
return ptid, row_list return ptid, row_list
decode_table[ANSWER_PARTITION_TABLE] = _decodeAnswerPartitionTable decode_table[ANSWER_PARTITION_TABLE] = _decodeAnswerPartitionTable
def _decodeSendPartitionTable(self): def _decodeSendPartitionTable(body):
try: try:
ptid, n = unpack('!8sL', self._body[:12]) ptid, n = unpack('!8sL', body[:12])
index = 12 index = 12
row_list = [] row_list = []
cell_list = [] cell_list = []
for i in xrange(n): for i in xrange(n):
offset, m = unpack('!LL', self._body[index:index+8]) offset, m = unpack('!LL', body[index:index+8])
index += 8 index += 8
for j in xrange(m): for j in xrange(m):
uuid, state = unpack('!16sH', self._body[index:index+18]) uuid, state = unpack('!16sH', body[index:index+18])
index += 18 index += 18
state = partition_cell_states.get(state) state = partition_cell_states.get(state)
cell_list.append((uuid, state)) cell_list.append((uuid, state))
...@@ -548,331 +546,331 @@ class Packet(object): ...@@ -548,331 +546,331 @@ class Packet(object):
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid send partition table') raise PacketMalformedError('invalid send partition table')
return ptid, row_list return ptid, row_list
decode_table[SEND_PARTITION_TABLE] = _decodeSendPartitionTable decode_table[SEND_PARTITION_TABLE] = _decodeSendPartitionTable
def _decodeNotifyPartitionChanges(self): def _decodeNotifyPartitionChanges(body):
try: try:
ptid, n = unpack('!8sL', self._body[:12]) ptid, n = unpack('!8sL', body[:12])
cell_list = [] cell_list = []
for i in xrange(n): for i in xrange(n):
offset, uuid, state = unpack('!L16sH', self._body[12+i*22:34+i*22]) offset, uuid, state = unpack('!L16sH', body[12+i*22:34+i*22])
state = partition_cell_states.get(state) state = partition_cell_states.get(state)
cell_list.append((offset, uuid, state)) cell_list.append((offset, uuid, state))
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid notify partition changes') raise PacketMalformedError('invalid notify partition changes')
return ptid, cell_list return ptid, cell_list
decode_table[NOTIFY_PARTITION_CHANGES] = _decodeNotifyPartitionChanges decode_table[NOTIFY_PARTITION_CHANGES] = _decodeNotifyPartitionChanges
def _decodeStartOperation(self): def _decodeStartOperation(body):
pass pass
decode_table[START_OPERATION] = _decodeStartOperation decode_table[START_OPERATION] = _decodeStartOperation
def _decodeStopOperation(self): def _decodeStopOperation(body):
pass pass
decode_table[STOP_OPERATION] = _decodeStopOperation decode_table[STOP_OPERATION] = _decodeStopOperation
def _decodeAskUnfinishedTransactions(self): def _decodeAskUnfinishedTransactions(body):
pass pass
decode_table[ASK_UNFINISHED_TRANSACTIONS] = _decodeAskUnfinishedTransactions decode_table[ASK_UNFINISHED_TRANSACTIONS] = _decodeAskUnfinishedTransactions
def _decodeAnswerUnfinishedTransactions(self): def _decodeAnswerUnfinishedTransactions(body):
try: try:
n = unpack('!L', self._body[:4])[0] n = unpack('!L', body[:4])[0]
tid_list = [] tid_list = []
for i in xrange(n): for i in xrange(n):
tid = unpack('8s', self._body[4+i*8:12+i*8])[0] tid = unpack('8s', body[4+i*8:12+i*8])[0]
tid_list.append(tid) tid_list.append(tid)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer unfinished transactions') raise PacketMalformedError('invalid answer unfinished transactions')
return (tid_list,) return (tid_list,)
decode_table[ANSWER_UNFINISHED_TRANSACTIONS] = _decodeAnswerUnfinishedTransactions decode_table[ANSWER_UNFINISHED_TRANSACTIONS] = _decodeAnswerUnfinishedTransactions
def _decodeAskObjectPresent(self): def _decodeAskObjectPresent(body):
try: try:
oid, tid = unpack('8s8s', self._body) oid, tid = unpack('8s8s', body)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid ask object present') raise PacketMalformedError('invalid ask object present')
return oid, tid return oid, tid
decode_table[ASK_OBJECT_PRESENT] = _decodeAskObjectPresent decode_table[ASK_OBJECT_PRESENT] = _decodeAskObjectPresent
def _decodeAnswerObjectPresent(self): def _decodeAnswerObjectPresent(body):
try: try:
oid, tid = unpack('8s8s', self._body) oid, tid = unpack('8s8s', body)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer object present') raise PacketMalformedError('invalid answer object present')
return oid, tid return oid, tid
decode_table[ANSWER_OBJECT_PRESENT] = _decodeAnswerObjectPresent decode_table[ANSWER_OBJECT_PRESENT] = _decodeAnswerObjectPresent
def _decodeDeleteTransaction(self): def _decodeDeleteTransaction(body):
try: try:
tid = unpack('8s', self._body)[0] tid = unpack('8s', body)[0]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid delete transaction') raise PacketMalformedError('invalid delete transaction')
return (tid,) return (tid,)
decode_table[DELETE_TRANSACTION] = _decodeDeleteTransaction decode_table[DELETE_TRANSACTION] = _decodeDeleteTransaction
def _decodeCommitTransaction(self): def _decodeCommitTransaction(body):
try: try:
tid = unpack('8s', self._body)[0] tid = unpack('8s', body)[0]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid commit transaction') raise PacketMalformedError('invalid commit transaction')
return (tid,) return (tid,)
decode_table[COMMIT_TRANSACTION] = _decodeCommitTransaction decode_table[COMMIT_TRANSACTION] = _decodeCommitTransaction
def _decodeAskNewTID(self): def _decodeAskNewTID(body):
pass pass
decode_table[ASK_NEW_TID] = _decodeAskNewTID decode_table[ASK_NEW_TID] = _decodeAskNewTID
def _decodeAnswerNewTID(self): def _decodeAnswerNewTID(body):
try: try:
tid = unpack('8s', self._body)[0] tid = unpack('8s', body)[0]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer new tid') raise PacketMalformedError('invalid answer new tid')
return (tid,) return (tid,)
decode_table[ANSWER_NEW_TID] = _decodeAnswerNewTID decode_table[ANSWER_NEW_TID] = _decodeAnswerNewTID
def _decodeAskNewOIDs(self): def _decodeAskNewOIDs(body):
try: try:
num_oids = unpack('!H', self._body)[0] num_oids = unpack('!H', body)[0]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid ask new oids') raise PacketMalformedError('invalid ask new oids')
return (num_oids,) return (num_oids,)
decode_table[ASK_NEW_OIDS] = _decodeAskNewOIDs decode_table[ASK_NEW_OIDS] = _decodeAskNewOIDs
def _decodeAnswerNewOIDs(self): def _decodeAnswerNewOIDs(body):
try: try:
n = unpack('!H', self._body[:2])[0] n = unpack('!H', body[:2])[0]
oid_list = [] oid_list = []
for i in xrange(n): for i in xrange(n):
oid = unpack('8s', self._body[2+i*8:10+i*8])[0] oid = unpack('8s', body[2+i*8:10+i*8])[0]
oid_list.append(oid) oid_list.append(oid)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer new oids') raise PacketMalformedError('invalid answer new oids')
return (oid_list,) return (oid_list,)
decode_table[ANSWER_NEW_OIDS] = _decodeAnswerNewOIDs decode_table[ANSWER_NEW_OIDS] = _decodeAnswerNewOIDs
def _decodeFinishTransaction(self): def _decodeFinishTransaction(body):
try: try:
tid, n = unpack('!8sL', self._body[:12]) tid, n = unpack('!8sL', body[:12])
oid_list = [] oid_list = []
for i in xrange(n): for i in xrange(n):
oid = unpack('8s', self._body[12+i*8:20+i*8])[0] oid = unpack('8s', body[12+i*8:20+i*8])[0]
oid_list.append(oid) oid_list.append(oid)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid finish transaction') raise PacketMalformedError('invalid finish transaction')
return oid_list, tid return oid_list, tid
decode_table[FINISH_TRANSACTION] = _decodeFinishTransaction decode_table[FINISH_TRANSACTION] = _decodeFinishTransaction
def _decodeNotifyTransactionFinished(self): def _decodeNotifyTransactionFinished(body):
try: try:
tid = unpack('8s', self._body)[0] tid = unpack('8s', body)[0]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid notify transactin finished') raise PacketMalformedError('invalid notify transactin finished')
return (tid,) return (tid,)
decode_table[NOTIFY_TRANSACTION_FINISHED] = _decodeNotifyTransactionFinished decode_table[NOTIFY_TRANSACTION_FINISHED] = _decodeNotifyTransactionFinished
def _decodeLockInformation(self): def _decodeLockInformation(body):
try: try:
tid = unpack('8s', self._body)[0] tid = unpack('8s', body)[0]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid lock information') raise PacketMalformedError('invalid lock information')
return (tid,) return (tid,)
decode_table[LOCK_INFORMATION] = _decodeLockInformation decode_table[LOCK_INFORMATION] = _decodeLockInformation
def _decodeNotifyInformationLocked(self): def _decodeNotifyInformationLocked(body):
try: try:
tid = unpack('8s', self._body)[0] tid = unpack('8s', body)[0]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid notify information locked') raise PacketMalformedError('invalid notify information locked')
return (tid,) return (tid,)
decode_table[NOTIFY_INFORMATION_LOCKED] = _decodeNotifyInformationLocked decode_table[NOTIFY_INFORMATION_LOCKED] = _decodeNotifyInformationLocked
def _decodeInvalidateObjects(self): def _decodeInvalidateObjects(body):
try: try:
tid, n = unpack('!8sL', self._body[:12]) tid, n = unpack('!8sL', body[:12])
oid_list = [] oid_list = []
for i in xrange(12, 12 + n * 8, 8): for i in xrange(12, 12 + n * 8, 8):
oid = unpack('8s', self._body[i:i+8])[0] oid = unpack('8s', body[i:i+8])[0]
oid_list.append(oid) oid_list.append(oid)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid finish transaction') raise PacketMalformedError('invalid finish transaction')
return oid_list, tid return oid_list, tid
decode_table[INVALIDATE_OBJECTS] = _decodeInvalidateObjects decode_table[INVALIDATE_OBJECTS] = _decodeInvalidateObjects
def _decodeUnlockInformation(self): def _decodeUnlockInformation(body):
try: try:
tid = unpack('8s', self._body)[0] tid = unpack('8s', body)[0]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid unlock information') raise PacketMalformedError('invalid unlock information')
return (tid,) return (tid,)
decode_table[UNLOCK_INFORMATION] = _decodeUnlockInformation decode_table[UNLOCK_INFORMATION] = _decodeUnlockInformation
def _decodeAbortTransaction(self): def _decodeAbortTransaction(body):
try: try:
tid = unpack('8s', self._body)[0] tid = unpack('8s', body)[0]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid abort transaction') raise PacketMalformedError('invalid abort transaction')
return (tid,) return (tid,)
decode_table[ABORT_TRANSACTION] = _decodeAbortTransaction decode_table[ABORT_TRANSACTION] = _decodeAbortTransaction
def _decodeAskStoreObject(self): def _decodeAskStoreObject(body):
try: try:
oid, serial, tid, compression, checksum, data_len \ oid, serial, tid, compression, checksum, data_len \
= unpack('!8s8s8sBLL', self._body[:33]) = unpack('!8s8s8sBLL', body[:33])
data = self._body[33:] data = body[33:]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid ask store object') raise PacketMalformedError('invalid ask store object')
if data_len != len(data): if data_len != len(data):
raise PacketMalformedError('invalid data size') raise PacketMalformedError('invalid data size')
return oid, serial, compression, checksum, data, tid return oid, serial, compression, checksum, data, tid
decode_table[ASK_STORE_OBJECT] = _decodeAskStoreObject decode_table[ASK_STORE_OBJECT] = _decodeAskStoreObject
def _decodeAnswerStoreObject(self): def _decodeAnswerStoreObject(body):
try: try:
conflicting, oid, serial = unpack('!B8s8s', self._body) conflicting, oid, serial = unpack('!B8s8s', body)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer store object') raise PacketMalformedError('invalid answer store object')
return conflicting, oid, serial return conflicting, oid, serial
decode_table[ANSWER_STORE_OBJECT] = _decodeAnswerStoreObject decode_table[ANSWER_STORE_OBJECT] = _decodeAnswerStoreObject
def _decodeAskStoreTransaction(self): def _decodeAskStoreTransaction(body):
try: try:
tid, oid_len, user_len, desc_len, ext_len \ tid, oid_len, user_len, desc_len, ext_len \
= unpack('!8sLHHH', self._body[:18]) = unpack('!8sLHHH', body[:18])
offset = 18 offset = 18
user = self._body[offset:offset+user_len] user = body[offset:offset+user_len]
offset += user_len offset += user_len
desc = self._body[offset:offset+desc_len] desc = body[offset:offset+desc_len]
offset += desc_len offset += desc_len
ext = self._body[offset:offset+ext_len] ext = body[offset:offset+ext_len]
offset += ext_len offset += ext_len
oid_list = [] oid_list = []
for i in xrange(oid_len): for i in xrange(oid_len):
oid = unpack('8s', self._body[offset:offset+8])[0] oid = unpack('8s', body[offset:offset+8])[0]
offset += 8 offset += 8
oid_list.append(oid) oid_list.append(oid)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid ask store transaction') raise PacketMalformedError('invalid ask store transaction')
return tid, user, desc, ext, oid_list return tid, user, desc, ext, oid_list
decode_table[ASK_STORE_TRANSACTION] = _decodeAskStoreTransaction decode_table[ASK_STORE_TRANSACTION] = _decodeAskStoreTransaction
def _decodeAnswerStoreTransaction(self): def _decodeAnswerStoreTransaction(body):
try: try:
tid = unpack('8s', self._body)[0] tid = unpack('8s', body)[0]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer store transaction') raise PacketMalformedError('invalid answer store transaction')
return (tid,) return (tid,)
decode_table[ANSWER_STORE_TRANSACTION] = _decodeAnswerStoreTransaction decode_table[ANSWER_STORE_TRANSACTION] = _decodeAnswerStoreTransaction
def _decodeAskObject(self): def _decodeAskObject(body):
try: try:
oid, serial, tid = unpack('8s8s8s', self._body) oid, serial, tid = unpack('8s8s8s', body)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid ask object') raise PacketMalformedError('invalid ask object')
return oid, serial, tid return oid, serial, tid
decode_table[ASK_OBJECT] = _decodeAskObject decode_table[ASK_OBJECT] = _decodeAskObject
def _decodeAnswerObject(self): def _decodeAnswerObject(body):
try: try:
oid, serial_start, serial_end, compression, checksum, data_len \ oid, serial_start, serial_end, compression, checksum, data_len \
= unpack('!8s8s8sBLL', self._body[:33]) = unpack('!8s8s8sBLL', body[:33])
data = self._body[33:] data = body[33:]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer object') raise PacketMalformedError('invalid answer object')
if len(data) != data_len: if len(data) != data_len:
raise PacketMalformedError('invalid data size') raise PacketMalformedError('invalid data size')
return oid, serial_start, serial_end, compression, checksum, data return oid, serial_start, serial_end, compression, checksum, data
decode_table[ANSWER_OBJECT] = _decodeAnswerObject decode_table[ANSWER_OBJECT] = _decodeAnswerObject
def _decodeAskTIDs(self): def _decodeAskTIDs(body):
try: try:
first, last, partition = unpack('!QQL', self._body) first, last, partition = unpack('!QQL', body)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid ask tids') raise PacketMalformedError('invalid ask tids')
return first, last, partition return first, last, partition
decode_table[ASK_TIDS] = _decodeAskTIDs decode_table[ASK_TIDS] = _decodeAskTIDs
def _decodeAnswerTIDs(self): def _decodeAnswerTIDs(body):
try: try:
n = unpack('!L', self._body[:4])[0] n = unpack('!L', body[:4])[0]
tid_list = [] tid_list = []
for i in xrange(n): for i in xrange(n):
tid = unpack('8s', self._body[4+i*8:12+i*8])[0] tid = unpack('8s', body[4+i*8:12+i*8])[0]
tid_list.append(tid) tid_list.append(tid)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer tids') raise PacketMalformedError('invalid answer tids')
return (tid_list,) return (tid_list,)
decode_table[ANSWER_TIDS] = _decodeAnswerTIDs decode_table[ANSWER_TIDS] = _decodeAnswerTIDs
def _decodeAskTransactionInformation(self): def _decodeAskTransactionInformation(body):
try: try:
tid = unpack('8s', self._body)[0] tid = unpack('8s', body)[0]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid ask transaction information') raise PacketMalformedError('invalid ask transaction information')
return (tid,) return (tid,)
decode_table[ASK_TRANSACTION_INFORMATION] = _decodeAskTransactionInformation decode_table[ASK_TRANSACTION_INFORMATION] = _decodeAskTransactionInformation
def _decodeAnswerTransactionInformation(self): def _decodeAnswerTransactionInformation(body):
try: try:
tid, user_len, desc_len, ext_len, oid_len \ tid, user_len, desc_len, ext_len, oid_len \
= unpack('!8sHHHL', self._body[:18]) = unpack('!8sHHHL', body[:18])
offset = 18 offset = 18
user = self._body[offset:offset+user_len] user = body[offset:offset+user_len]
offset += user_len offset += user_len
desc = self._body[offset:offset+desc_len] desc = body[offset:offset+desc_len]
offset += desc_len offset += desc_len
ext = self._body[offset:offset+ext_len] ext = body[offset:offset+ext_len]
offset += ext_len offset += ext_len
oid_list = [] oid_list = []
for i in xrange(oid_len): for i in xrange(oid_len):
oid = unpack('8s', self._body[offset+i*8:offset+8+i*8])[0] oid = unpack('8s', body[offset+i*8:offset+8+i*8])[0]
oid_list.append(oid) oid_list.append(oid)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer transaction information') raise PacketMalformedError('invalid answer transaction information')
return tid, user, desc, ext, oid_list return tid, user, desc, ext, oid_list
decode_table[ANSWER_TRANSACTION_INFORMATION] = _decodeAnswerTransactionInformation decode_table[ANSWER_TRANSACTION_INFORMATION] = _decodeAnswerTransactionInformation
def _decodeAskObjectHistory(self): def _decodeAskObjectHistory(body):
try: try:
oid, first, last = unpack('!8sQQ', self._body) oid, first, last = unpack('!8sQQ', body)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid ask object history') raise PacketMalformedError('invalid ask object history')
return oid, first, last return oid, first, last
decode_table[ASK_OBJECT_HISTORY] = _decodeAskObjectHistory decode_table[ASK_OBJECT_HISTORY] = _decodeAskObjectHistory
def _decodeAnswerObjectHistory(self): def _decodeAnswerObjectHistory(body):
try: try:
oid, length = unpack('!8sL', self._body[:12]) oid, length = unpack('!8sL', body[:12])
history_list = [] history_list = []
for i in xrange(12, 12 + length * 12, 12): for i in xrange(12, 12 + length * 12, 12):
serial, size = unpack('!8sL', self._body[i:i+12]) serial, size = unpack('!8sL', body[i:i+12])
history_list.append((serial, size)) history_list.append((serial, size))
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer object history') raise PacketMalformedError('invalid answer object history')
return oid, history_list return oid, history_list
decode_table[ANSWER_OBJECT_HISTORY] = _decodeAnswerObjectHistory decode_table[ANSWER_OBJECT_HISTORY] = _decodeAnswerObjectHistory
def _decodeAskOIDs(self): def _decodeAskOIDs(body):
try: try:
first, last, partition = unpack('!QQL', self._body) first, last, partition = unpack('!QQL', body)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid ask oids') raise PacketMalformedError('invalid ask oids')
return first, last, partition return first, last, partition
decode_table[ASK_OIDS] = _decodeAskOIDs decode_table[ASK_OIDS] = _decodeAskOIDs
def _decodeAnswerOIDs(self): def _decodeAnswerOIDs(body):
try: try:
n = unpack('!L', self._body[:4])[0] n = unpack('!L', body[:4])[0]
oid_list = [] oid_list = []
for i in xrange(n): for i in xrange(n):
oid = unpack('8s', self._body[4+i*8:12+i*8])[0] oid = unpack('8s', body[4+i*8:12+i*8])[0]
oid_list.append(oid) oid_list.append(oid)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer oids') raise PacketMalformedError('invalid answer oids')
return (oid_list,) return (oid_list,)
decode_table[ANSWER_OIDS] = _decodeAnswerOIDs decode_table[ANSWER_OIDS] = _decodeAnswerOIDs
# Packet constructors # Packet encoding
def _error(error_code, error_message): def _error(error_code, error_message):
body = pack('!HL', error_code, len(error_message)) + error_message body = pack('!HL', error_code, len(error_message)) + error_message
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment