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,502 +357,520 @@ class Packet(object): ...@@ -377,502 +357,520 @@ 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):
def _decodeError(self): # logging.debug('parsing %s', dump(msg))
try: if len(msg) < MIN_PACKET_SIZE:
body = self._body return None
code, size = unpack('!HL', body[:6]) msg_id, msg_type, msg_len = unpack('!LHL', msg[:PACKET_HEADER_SIZE])
message = body[6:] try:
except struct.error, msg: msg_type = packet_types[msg_type]
raise PacketMalformedError('invalid error message') except KeyError:
if len(message) != size: raise PacketMalformedError('Unknown packet type')
raise PacketMalformedError('invalid error message size') if msg_len > MAX_PACKET_SIZE:
return code, message raise PacketMalformedError('message too big (%d)' % msg_len)
decode_table[ERROR] = _decodeError if msg_len < MIN_PACKET_SIZE:
raise PacketMalformedError('message too small (%d)' % msg_len)
def _decodePing(self): if len(msg) < msg_len:
pass # Not enough.
decode_table[PING] = _decodePing return None
packet = Packet(msg_type, msg[PACKET_HEADER_SIZE:msg_len])
def _decodePong(self): packet.setId(msg_id)
pass return packet
decode_table[PONG] = _decodePong
# packet decoding
def _decodeRequestNodeIdentification(self): def _decodeError(body):
try: try:
body = self._body body = body
major, minor, node_type, uuid, ip_address, port, size \ code, size = unpack('!HL', body[:6])
= unpack('!LLH16s4sHL', body[:36]) message = body[6:]
except struct.error, msg:
raise PacketMalformedError('invalid error message')
if len(message) != size:
raise PacketMalformedError('invalid error message size')
return code, message
decode_table[ERROR] = _decodeError
def _decodePing(body):
pass
decode_table[PING] = _decodePing
def _decodePong(body):
pass
decode_table[PONG] = _decodePong
def _decodeRequestNodeIdentification(body):
try:
body = body
major, minor, node_type, uuid, ip_address, port, size \
= unpack('!LLH16s4sHL', body[:36])
ip_address = inet_ntoa(ip_address)
name = body[36:]
except struct.error, msg:
raise PacketMalformedError('invalid request node identification')
if size != len(name):
raise PacketMalformedError('invalid name size')
if node_type not in VALID_NODE_TYPE_LIST:
raise PacketMalformedError('invalid node type %d' % node_type)
if (major, minor) != PROTOCOL_VERSION:
raise PacketMalformedError('protocol version mismatch')
return node_type, uuid, ip_address, port, name
decode_table[REQUEST_NODE_IDENTIFICATION] = _decodeRequestNodeIdentification
def _decodeAcceptNodeIdentification(body):
try:
node_type, uuid, ip_address, port, num_partitions, num_replicas, your_uuid \
= unpack('!H16s4sHLL16s', body)
ip_address = inet_ntoa(ip_address)
except struct.error, msg:
raise PacketMalformedError('invalid accept node identification')
if node_type not in VALID_NODE_TYPE_LIST:
raise PacketMalformedError('invalid node type %d' % node_type)
return node_type, uuid, ip_address, port, num_partitions, num_replicas, your_uuid
decode_table[ACCEPT_NODE_IDENTIFICATION] = _decodeAcceptNodeIdentification
def _decodeAskPrimaryMaster(body):
pass
decode_table[ASK_PRIMARY_MASTER] = _decodeAskPrimaryMaster
def _decodeAnswerPrimaryMaster(body):
try:
primary_uuid, n = unpack('!16sL', body[:20])
known_master_list = []
for i in xrange(n):
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)
name = body[36:] known_master_list.append((ip_address, port, uuid))
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid request node identification') raise PacketMalformedError('invalid answer primary master')
if size != len(name): return primary_uuid, known_master_list
raise PacketMalformedError('invalid name size') decode_table[ANSWER_PRIMARY_MASTER] = _decodeAnswerPrimaryMaster
if node_type not in VALID_NODE_TYPE_LIST:
raise PacketMalformedError('invalid node type %d' % node_type) def _decodeAnnouncePrimaryMaster(body):
if (major, minor) != PROTOCOL_VERSION: pass
raise PacketMalformedError('protocol version mismatch') decode_table[ANNOUNCE_PRIMARY_MASTER] = _decodeAnnouncePrimaryMaster
return node_type, uuid, ip_address, port, name
decode_table[REQUEST_NODE_IDENTIFICATION] = _decodeRequestNodeIdentification def _decodeReelectPrimaryMaster(body):
pass
def _decodeAcceptNodeIdentification(self): decode_table[REELECT_PRIMARY_MASTER] = _decodeReelectPrimaryMaster
try:
node_type, uuid, ip_address, port, num_partitions, num_replicas, your_uuid \ def _decodeNotifyNodeInformation(body):
= unpack('!H16s4sHLL16s', self._body) try:
n = unpack('!L', body[:4])[0]
node_list = []
for i in xrange(n):
r = unpack('!H4sH16sH', body[4+i*26:30+i*26])
node_type, ip_address, port, uuid, state = r
ip_address = inet_ntoa(ip_address) ip_address = inet_ntoa(ip_address)
except struct.error, msg: if node_type not in VALID_NODE_TYPE_LIST:
raise PacketMalformedError('invalid accept node identification') raise PacketMalformedError('invalid node type %d' % node_type)
if node_type not in VALID_NODE_TYPE_LIST: state = node_states.get(state)
raise PacketMalformedError('invalid node type %d' % node_type) if state not in VALID_NODE_STATE_LIST:
return node_type, uuid, ip_address, port, num_partitions, num_replicas, your_uuid raise PacketMalformedError('invalid node state %d' % state)
decode_table[ACCEPT_NODE_IDENTIFICATION] = _decodeAcceptNodeIdentification node_list.append((node_type, ip_address, port, uuid, state))
except PacketMalformedError:
def _decodeAskPrimaryMaster(self): raise
pass except struct.error, msg:
decode_table[ASK_PRIMARY_MASTER] = _decodeAskPrimaryMaster raise PacketMalformedError('invalid answer node information')
return (node_list,)
def _decodeAnswerPrimaryMaster(self): decode_table[NOTIFY_NODE_INFORMATION] = _decodeNotifyNodeInformation
try:
primary_uuid, n = unpack('!16sL', self._body[:20]) def _decodeAskLastIDs(body):
known_master_list = [] pass
for i in xrange(n): decode_table[ASK_LAST_IDS] = _decodeAskLastIDs
ip_address, port, uuid = unpack('!4sH16s', self._body[20+i*22:42+i*22])
ip_address = inet_ntoa(ip_address) def _decodeAnswerLastIDs(body):
known_master_list.append((ip_address, port, uuid)) try:
except struct.error, msg: loid, ltid, lptid = unpack('!8s8s8s', body)
raise PacketMalformedError('invalid answer primary master') except struct.error, msg:
return primary_uuid, known_master_list raise PacketMalformedError('invalid answer last ids')
decode_table[ANSWER_PRIMARY_MASTER] = _decodeAnswerPrimaryMaster return loid, ltid, lptid
decode_table[ANSWER_LAST_IDS] = _decodeAnswerLastIDs
def _decodeAnnouncePrimaryMaster(self):
pass def _decodeAskPartitionTable(body):
decode_table[ANNOUNCE_PRIMARY_MASTER] = _decodeAnnouncePrimaryMaster try:
n = unpack('!L', body[:4])[0]
def _decodeReelectPrimaryMaster(self): offset_list = []
pass for i in xrange(n):
decode_table[REELECT_PRIMARY_MASTER] = _decodeReelectPrimaryMaster offset = unpack('!L', body[4+i*4:8+i*4])[0]
offset_list.append(offset)
def _decodeNotifyNodeInformation(self): except struct.error, msg:
try: raise PacketMalformedError('invalid ask partition table')
n = unpack('!L', self._body[:4])[0] return (offset_list,)
node_list = [] decode_table[ASK_PARTITION_TABLE] = _decodeAskPartitionTable
for i in xrange(n):
r = unpack('!H4sH16sH', self._body[4+i*26:30+i*26]) def _decodeAnswerPartitionTable(body):
node_type, ip_address, port, uuid, state = r try:
ip_address = inet_ntoa(ip_address) ptid, n = unpack('!8sL', body[:12])
if node_type not in VALID_NODE_TYPE_LIST: index = 12
raise PacketMalformedError('invalid node type %d' % node_type) row_list = []
state = node_states.get(state) cell_list = []
if state not in VALID_NODE_STATE_LIST: for i in xrange(n):
raise PacketMalformedError('invalid node state %d' % state) offset, m = unpack('!LL', body[index:index+8])
node_list.append((node_type, ip_address, port, uuid, state)) index += 8
except PacketMalformedError: for j in xrange(m):
raise uuid, state = unpack('!16sH', body[index:index+18])
except struct.error, msg: index += 18
raise PacketMalformedError('invalid answer node information')
return (node_list,)
decode_table[NOTIFY_NODE_INFORMATION] = _decodeNotifyNodeInformation
def _decodeAskLastIDs(self):
pass
decode_table[ASK_LAST_IDS] = _decodeAskLastIDs
def _decodeAnswerLastIDs(self):
try:
loid, ltid, lptid = unpack('!8s8s8s', self._body)
except struct.error, msg:
raise PacketMalformedError('invalid answer last ids')
return loid, ltid, lptid
decode_table[ANSWER_LAST_IDS] = _decodeAnswerLastIDs
def _decodeAskPartitionTable(self):
try:
n = unpack('!L', self._body[:4])[0]
offset_list = []
for i in xrange(n):
offset = unpack('!L', self._body[4+i*4:8+i*4])[0]
offset_list.append(offset)
except struct.error, msg:
raise PacketMalformedError('invalid ask partition table')
return (offset_list,)
decode_table[ASK_PARTITION_TABLE] = _decodeAskPartitionTable
def _decodeAnswerPartitionTable(self):
try:
ptid, n = unpack('!8sL', self._body[:12])
index = 12
row_list = []
cell_list = []
for i in xrange(n):
offset, m = unpack('!LL', self._body[index:index+8])
index += 8
for j in xrange(m):
uuid, state = unpack('!16sH', self._body[index:index+18])
index += 18
state = partition_cell_states.get(state)
cell_list.append((uuid, state))
row_list.append((offset, tuple(cell_list)))
del cell_list[:]
except struct.error, msg:
raise PacketMalformedError('invalid answer partition table')
return ptid, row_list
decode_table[ANSWER_PARTITION_TABLE] = _decodeAnswerPartitionTable
def _decodeSendPartitionTable(self):
try:
ptid, n = unpack('!8sL', self._body[:12])
index = 12
row_list = []
cell_list = []
for i in xrange(n):
offset, m = unpack('!LL', self._body[index:index+8])
index += 8
for j in xrange(m):
uuid, state = unpack('!16sH', self._body[index:index+18])
index += 18
state = partition_cell_states.get(state)
cell_list.append((uuid, state))
row_list.append((offset, tuple(cell_list)))
del cell_list[:]
except struct.error, msg:
raise PacketMalformedError('invalid send partition table')
return ptid, row_list
decode_table[SEND_PARTITION_TABLE] = _decodeSendPartitionTable
def _decodeNotifyPartitionChanges(self):
try:
ptid, n = unpack('!8sL', self._body[:12])
cell_list = []
for i in xrange(n):
offset, uuid, state = unpack('!L16sH', self._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((uuid, state))
except struct.error, msg: row_list.append((offset, tuple(cell_list)))
raise PacketMalformedError('invalid notify partition changes') del cell_list[:]
return ptid, cell_list except struct.error, msg:
decode_table[NOTIFY_PARTITION_CHANGES] = _decodeNotifyPartitionChanges raise PacketMalformedError('invalid answer partition table')
return ptid, row_list
def _decodeStartOperation(self): decode_table[ANSWER_PARTITION_TABLE] = _decodeAnswerPartitionTable
pass
decode_table[START_OPERATION] = _decodeStartOperation def _decodeSendPartitionTable(body):
try:
def _decodeStopOperation(self): ptid, n = unpack('!8sL', body[:12])
pass index = 12
decode_table[STOP_OPERATION] = _decodeStopOperation row_list = []
cell_list = []
def _decodeAskUnfinishedTransactions(self): for i in xrange(n):
pass offset, m = unpack('!LL', body[index:index+8])
decode_table[ASK_UNFINISHED_TRANSACTIONS] = _decodeAskUnfinishedTransactions index += 8
for j in xrange(m):
def _decodeAnswerUnfinishedTransactions(self): uuid, state = unpack('!16sH', body[index:index+18])
try: index += 18
n = unpack('!L', self._body[:4])[0] state = partition_cell_states.get(state)
tid_list = [] cell_list.append((uuid, state))
for i in xrange(n): row_list.append((offset, tuple(cell_list)))
tid = unpack('8s', self._body[4+i*8:12+i*8])[0] del cell_list[:]
tid_list.append(tid) except struct.error, msg:
except struct.error, msg: raise PacketMalformedError('invalid send partition table')
raise PacketMalformedError('invalid answer unfinished transactions') return ptid, row_list
return (tid_list,) decode_table[SEND_PARTITION_TABLE] = _decodeSendPartitionTable
decode_table[ANSWER_UNFINISHED_TRANSACTIONS] = _decodeAnswerUnfinishedTransactions
def _decodeNotifyPartitionChanges(body):
def _decodeAskObjectPresent(self): try:
try: ptid, n = unpack('!8sL', body[:12])
oid, tid = unpack('8s8s', self._body) cell_list = []
except struct.error, msg: for i in xrange(n):
raise PacketMalformedError('invalid ask object present') offset, uuid, state = unpack('!L16sH', body[12+i*22:34+i*22])
return oid, tid state = partition_cell_states.get(state)
decode_table[ASK_OBJECT_PRESENT] = _decodeAskObjectPresent cell_list.append((offset, uuid, state))
except struct.error, msg:
def _decodeAnswerObjectPresent(self): raise PacketMalformedError('invalid notify partition changes')
try: return ptid, cell_list
oid, tid = unpack('8s8s', self._body) decode_table[NOTIFY_PARTITION_CHANGES] = _decodeNotifyPartitionChanges
except struct.error, msg:
raise PacketMalformedError('invalid answer object present') def _decodeStartOperation(body):
return oid, tid pass
decode_table[ANSWER_OBJECT_PRESENT] = _decodeAnswerObjectPresent decode_table[START_OPERATION] = _decodeStartOperation
def _decodeDeleteTransaction(self): def _decodeStopOperation(body):
try: pass
tid = unpack('8s', self._body)[0] decode_table[STOP_OPERATION] = _decodeStopOperation
except struct.error, msg:
raise PacketMalformedError('invalid delete transaction') def _decodeAskUnfinishedTransactions(body):
return (tid,) pass
decode_table[DELETE_TRANSACTION] = _decodeDeleteTransaction decode_table[ASK_UNFINISHED_TRANSACTIONS] = _decodeAskUnfinishedTransactions
def _decodeCommitTransaction(self): def _decodeAnswerUnfinishedTransactions(body):
try: try:
tid = unpack('8s', self._body)[0] n = unpack('!L', body[:4])[0]
except struct.error, msg: tid_list = []
raise PacketMalformedError('invalid commit transaction') for i in xrange(n):
return (tid,) tid = unpack('8s', body[4+i*8:12+i*8])[0]
decode_table[COMMIT_TRANSACTION] = _decodeCommitTransaction tid_list.append(tid)
except struct.error, msg:
def _decodeAskNewTID(self): raise PacketMalformedError('invalid answer unfinished transactions')
pass return (tid_list,)
decode_table[ASK_NEW_TID] = _decodeAskNewTID decode_table[ANSWER_UNFINISHED_TRANSACTIONS] = _decodeAnswerUnfinishedTransactions
def _decodeAnswerNewTID(self): def _decodeAskObjectPresent(body):
try: try:
tid = unpack('8s', self._body)[0] oid, tid = unpack('8s8s', body)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer new tid') raise PacketMalformedError('invalid ask object present')
return (tid,) return oid, tid
decode_table[ANSWER_NEW_TID] = _decodeAnswerNewTID decode_table[ASK_OBJECT_PRESENT] = _decodeAskObjectPresent
def _decodeAskNewOIDs(self): def _decodeAnswerObjectPresent(body):
try: try:
num_oids = unpack('!H', self._body)[0] oid, tid = unpack('8s8s', body)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid ask new oids') raise PacketMalformedError('invalid answer object present')
return (num_oids,) return oid, tid
decode_table[ASK_NEW_OIDS] = _decodeAskNewOIDs decode_table[ANSWER_OBJECT_PRESENT] = _decodeAnswerObjectPresent
def _decodeAnswerNewOIDs(self): def _decodeDeleteTransaction(body):
try: try:
n = unpack('!H', self._body[:2])[0] tid = unpack('8s', body)[0]
oid_list = [] except struct.error, msg:
for i in xrange(n): raise PacketMalformedError('invalid delete transaction')
oid = unpack('8s', self._body[2+i*8:10+i*8])[0] return (tid,)
oid_list.append(oid) decode_table[DELETE_TRANSACTION] = _decodeDeleteTransaction
except struct.error, msg:
raise PacketMalformedError('invalid answer new oids') def _decodeCommitTransaction(body):
return (oid_list,) try:
decode_table[ANSWER_NEW_OIDS] = _decodeAnswerNewOIDs tid = unpack('8s', body)[0]
except struct.error, msg:
def _decodeFinishTransaction(self): raise PacketMalformedError('invalid commit transaction')
try: return (tid,)
tid, n = unpack('!8sL', self._body[:12]) decode_table[COMMIT_TRANSACTION] = _decodeCommitTransaction
oid_list = []
for i in xrange(n): def _decodeAskNewTID(body):
oid = unpack('8s', self._body[12+i*8:20+i*8])[0] pass
oid_list.append(oid) decode_table[ASK_NEW_TID] = _decodeAskNewTID
except struct.error, msg:
raise PacketMalformedError('invalid finish transaction') def _decodeAnswerNewTID(body):
return oid_list, tid try:
decode_table[FINISH_TRANSACTION] = _decodeFinishTransaction tid = unpack('8s', body)[0]
except struct.error, msg:
def _decodeNotifyTransactionFinished(self): raise PacketMalformedError('invalid answer new tid')
try: return (tid,)
tid = unpack('8s', self._body)[0] decode_table[ANSWER_NEW_TID] = _decodeAnswerNewTID
except struct.error, msg:
raise PacketMalformedError('invalid notify transactin finished') def _decodeAskNewOIDs(body):
return (tid,) try:
decode_table[NOTIFY_TRANSACTION_FINISHED] = _decodeNotifyTransactionFinished num_oids = unpack('!H', body)[0]
except struct.error, msg:
def _decodeLockInformation(self): raise PacketMalformedError('invalid ask new oids')
try: return (num_oids,)
tid = unpack('8s', self._body)[0] decode_table[ASK_NEW_OIDS] = _decodeAskNewOIDs
except struct.error, msg:
raise PacketMalformedError('invalid lock information') def _decodeAnswerNewOIDs(body):
return (tid,) try:
decode_table[LOCK_INFORMATION] = _decodeLockInformation n = unpack('!H', body[:2])[0]
oid_list = []
def _decodeNotifyInformationLocked(self): for i in xrange(n):
try: oid = unpack('8s', body[2+i*8:10+i*8])[0]
tid = unpack('8s', self._body)[0] oid_list.append(oid)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid notify information locked') raise PacketMalformedError('invalid answer new oids')
return (tid,) return (oid_list,)
decode_table[NOTIFY_INFORMATION_LOCKED] = _decodeNotifyInformationLocked decode_table[ANSWER_NEW_OIDS] = _decodeAnswerNewOIDs
def _decodeInvalidateObjects(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(12, 12 + n * 8, 8): for i in xrange(n):
oid = unpack('8s', self._body[i: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[INVALIDATE_OBJECTS] = _decodeInvalidateObjects decode_table[FINISH_TRANSACTION] = _decodeFinishTransaction
def _decodeUnlockInformation(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 unlock information') raise PacketMalformedError('invalid notify transactin finished')
return (tid,) return (tid,)
decode_table[UNLOCK_INFORMATION] = _decodeUnlockInformation decode_table[NOTIFY_TRANSACTION_FINISHED] = _decodeNotifyTransactionFinished
def _decodeAbortTransaction(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 abort transaction') raise PacketMalformedError('invalid lock information')
return (tid,) return (tid,)
decode_table[ABORT_TRANSACTION] = _decodeAbortTransaction decode_table[LOCK_INFORMATION] = _decodeLockInformation
def _decodeAskStoreObject(self): def _decodeNotifyInformationLocked(body):
try: try:
oid, serial, tid, compression, checksum, data_len \ tid = unpack('8s', body)[0]
= unpack('!8s8s8sBLL', self._body[:33]) except struct.error, msg:
data = self._body[33:] raise PacketMalformedError('invalid notify information locked')
except struct.error, msg: return (tid,)
raise PacketMalformedError('invalid ask store object') decode_table[NOTIFY_INFORMATION_LOCKED] = _decodeNotifyInformationLocked
if data_len != len(data):
raise PacketMalformedError('invalid data size') def _decodeInvalidateObjects(body):
return oid, serial, compression, checksum, data, tid try:
decode_table[ASK_STORE_OBJECT] = _decodeAskStoreObject tid, n = unpack('!8sL', body[:12])
oid_list = []
def _decodeAnswerStoreObject(self): for i in xrange(12, 12 + n * 8, 8):
try: oid = unpack('8s', body[i:i+8])[0]
conflicting, oid, serial = unpack('!B8s8s', self._body) oid_list.append(oid)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid answer store object') raise PacketMalformedError('invalid finish transaction')
return conflicting, oid, serial return oid_list, tid
decode_table[ANSWER_STORE_OBJECT] = _decodeAnswerStoreObject decode_table[INVALIDATE_OBJECTS] = _decodeInvalidateObjects
def _decodeAskStoreTransaction(self): def _decodeUnlockInformation(body):
try: try:
tid, oid_len, user_len, desc_len, ext_len \ tid = unpack('8s', body)[0]
= unpack('!8sLHHH', self._body[:18]) except struct.error, msg:
offset = 18 raise PacketMalformedError('invalid unlock information')
user = self._body[offset:offset+user_len] return (tid,)
offset += user_len decode_table[UNLOCK_INFORMATION] = _decodeUnlockInformation
desc = self._body[offset:offset+desc_len]
offset += desc_len def _decodeAbortTransaction(body):
ext = self._body[offset:offset+ext_len] try:
offset += ext_len tid = unpack('8s', body)[0]
oid_list = [] except struct.error, msg:
for i in xrange(oid_len): raise PacketMalformedError('invalid abort transaction')
oid = unpack('8s', self._body[offset:offset+8])[0] return (tid,)
offset += 8 decode_table[ABORT_TRANSACTION] = _decodeAbortTransaction
oid_list.append(oid)
except struct.error, msg: def _decodeAskStoreObject(body):
raise PacketMalformedError('invalid ask store transaction') try:
return tid, user, desc, ext, oid_list oid, serial, tid, compression, checksum, data_len \
decode_table[ASK_STORE_TRANSACTION] = _decodeAskStoreTransaction = unpack('!8s8s8sBLL', body[:33])
data = body[33:]
def _decodeAnswerStoreTransaction(self): except struct.error, msg:
try: raise PacketMalformedError('invalid ask store object')
tid = unpack('8s', self._body)[0] if data_len != len(data):
except struct.error, msg: raise PacketMalformedError('invalid data size')
raise PacketMalformedError('invalid answer store transaction') return oid, serial, compression, checksum, data, tid
return (tid,) decode_table[ASK_STORE_OBJECT] = _decodeAskStoreObject
decode_table[ANSWER_STORE_TRANSACTION] = _decodeAnswerStoreTransaction
def _decodeAnswerStoreObject(body):
def _decodeAskObject(self): try:
try: conflicting, oid, serial = unpack('!B8s8s', body)
oid, serial, tid = unpack('8s8s8s', self._body) except struct.error, msg:
except struct.error, msg: raise PacketMalformedError('invalid answer store object')
raise PacketMalformedError('invalid ask object') return conflicting, oid, serial
return oid, serial, tid decode_table[ANSWER_STORE_OBJECT] = _decodeAnswerStoreObject
decode_table[ASK_OBJECT] = _decodeAskObject
def _decodeAskStoreTransaction(body):
def _decodeAnswerObject(self): try:
try: tid, oid_len, user_len, desc_len, ext_len \
oid, serial_start, serial_end, compression, checksum, data_len \ = unpack('!8sLHHH', body[:18])
= unpack('!8s8s8sBLL', self._body[:33]) offset = 18
data = self._body[33:] user = body[offset:offset+user_len]
except struct.error, msg: offset += user_len
raise PacketMalformedError('invalid answer object') desc = body[offset:offset+desc_len]
if len(data) != data_len: offset += desc_len
raise PacketMalformedError('invalid data size') ext = body[offset:offset+ext_len]
return oid, serial_start, serial_end, compression, checksum, data offset += ext_len
decode_table[ANSWER_OBJECT] = _decodeAnswerObject oid_list = []
for i in xrange(oid_len):
def _decodeAskTIDs(self): oid = unpack('8s', body[offset:offset+8])[0]
try: offset += 8
first, last, partition = unpack('!QQL', self._body) oid_list.append(oid)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid ask tids') raise PacketMalformedError('invalid ask store transaction')
return first, last, partition return tid, user, desc, ext, oid_list
decode_table[ASK_TIDS] = _decodeAskTIDs decode_table[ASK_STORE_TRANSACTION] = _decodeAskStoreTransaction
def _decodeAnswerTIDs(self): def _decodeAnswerStoreTransaction(body):
try: try:
n = unpack('!L', self._body[:4])[0] tid = unpack('8s', body)[0]
tid_list = [] except struct.error, msg:
for i in xrange(n): raise PacketMalformedError('invalid answer store transaction')
tid = unpack('8s', self._body[4+i*8:12+i*8])[0] return (tid,)
tid_list.append(tid) decode_table[ANSWER_STORE_TRANSACTION] = _decodeAnswerStoreTransaction
except struct.error, msg:
raise PacketMalformedError('invalid answer tids') def _decodeAskObject(body):
return (tid_list,) try:
decode_table[ANSWER_TIDS] = _decodeAnswerTIDs oid, serial, tid = unpack('8s8s8s', body)
except struct.error, msg:
def _decodeAskTransactionInformation(self): raise PacketMalformedError('invalid ask object')
try: return oid, serial, tid
tid = unpack('8s', self._body)[0] decode_table[ASK_OBJECT] = _decodeAskObject
except struct.error, msg:
raise PacketMalformedError('invalid ask transaction information') def _decodeAnswerObject(body):
return (tid,) try:
decode_table[ASK_TRANSACTION_INFORMATION] = _decodeAskTransactionInformation oid, serial_start, serial_end, compression, checksum, data_len \
= unpack('!8s8s8sBLL', body[:33])
def _decodeAnswerTransactionInformation(self): data = body[33:]
try: except struct.error, msg:
tid, user_len, desc_len, ext_len, oid_len \ raise PacketMalformedError('invalid answer object')
= unpack('!8sHHHL', self._body[:18]) if len(data) != data_len:
offset = 18 raise PacketMalformedError('invalid data size')
user = self._body[offset:offset+user_len] return oid, serial_start, serial_end, compression, checksum, data
offset += user_len decode_table[ANSWER_OBJECT] = _decodeAnswerObject
desc = self._body[offset:offset+desc_len]
offset += desc_len def _decodeAskTIDs(body):
ext = self._body[offset:offset+ext_len] try:
offset += ext_len first, last, partition = unpack('!QQL', body)
oid_list = [] except struct.error, msg:
for i in xrange(oid_len): raise PacketMalformedError('invalid ask tids')
oid = unpack('8s', self._body[offset+i*8:offset+8+i*8])[0] return first, last, partition
oid_list.append(oid) decode_table[ASK_TIDS] = _decodeAskTIDs
except struct.error, msg:
raise PacketMalformedError('invalid answer transaction information') def _decodeAnswerTIDs(body):
return tid, user, desc, ext, oid_list try:
decode_table[ANSWER_TRANSACTION_INFORMATION] = _decodeAnswerTransactionInformation n = unpack('!L', body[:4])[0]
tid_list = []
def _decodeAskObjectHistory(self): for i in xrange(n):
try: tid = unpack('8s', body[4+i*8:12+i*8])[0]
oid, first, last = unpack('!8sQQ', self._body) tid_list.append(tid)
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid ask object history') raise PacketMalformedError('invalid answer tids')
return oid, first, last return (tid_list,)
decode_table[ASK_OBJECT_HISTORY] = _decodeAskObjectHistory decode_table[ANSWER_TIDS] = _decodeAnswerTIDs
def _decodeAnswerObjectHistory(self): def _decodeAskTransactionInformation(body):
try: try:
oid, length = unpack('!8sL', self._body[:12]) tid = unpack('8s', body)[0]
history_list = [] except struct.error, msg:
for i in xrange(12, 12 + length * 12, 12): raise PacketMalformedError('invalid ask transaction information')
serial, size = unpack('!8sL', self._body[i:i+12]) return (tid,)
history_list.append((serial, size)) decode_table[ASK_TRANSACTION_INFORMATION] = _decodeAskTransactionInformation
except struct.error, msg:
raise PacketMalformedError('invalid answer object history') def _decodeAnswerTransactionInformation(body):
return oid, history_list try:
decode_table[ANSWER_OBJECT_HISTORY] = _decodeAnswerObjectHistory tid, user_len, desc_len, ext_len, oid_len \
= unpack('!8sHHHL', body[:18])
def _decodeAskOIDs(self): offset = 18
try: user = body[offset:offset+user_len]
first, last, partition = unpack('!QQL', self._body) offset += user_len
except struct.error, msg: desc = body[offset:offset+desc_len]
raise PacketMalformedError('invalid ask oids') offset += desc_len
return first, last, partition ext = body[offset:offset+ext_len]
decode_table[ASK_OIDS] = _decodeAskOIDs offset += ext_len
oid_list = []
def _decodeAnswerOIDs(self): for i in xrange(oid_len):
try: oid = unpack('8s', body[offset+i*8:offset+8+i*8])[0]
n = unpack('!L', self._body[:4])[0] oid_list.append(oid)
oid_list = [] except struct.error, msg:
for i in xrange(n): raise PacketMalformedError('invalid answer transaction information')
oid = unpack('8s', self._body[4+i*8:12+i*8])[0] return tid, user, desc, ext, oid_list
oid_list.append(oid) decode_table[ANSWER_TRANSACTION_INFORMATION] = _decodeAnswerTransactionInformation
except struct.error, msg:
raise PacketMalformedError('invalid answer oids') def _decodeAskObjectHistory(body):
return (oid_list,) try:
decode_table[ANSWER_OIDS] = _decodeAnswerOIDs oid, first, last = unpack('!8sQQ', body)
except struct.error, msg:
# Packet constructors raise PacketMalformedError('invalid ask object history')
return oid, first, last
decode_table[ASK_OBJECT_HISTORY] = _decodeAskObjectHistory
def _decodeAnswerObjectHistory(body):
try:
oid, length = unpack('!8sL', body[:12])
history_list = []
for i in xrange(12, 12 + length * 12, 12):
serial, size = unpack('!8sL', body[i:i+12])
history_list.append((serial, size))
except struct.error, msg:
raise PacketMalformedError('invalid answer object history')
return oid, history_list
decode_table[ANSWER_OBJECT_HISTORY] = _decodeAnswerObjectHistory
def _decodeAskOIDs(body):
try:
first, last, partition = unpack('!QQL', body)
except struct.error, msg:
raise PacketMalformedError('invalid ask oids')
return first, last, partition
decode_table[ASK_OIDS] = _decodeAskOIDs
def _decodeAnswerOIDs(body):
try:
n = unpack('!L', body[:4])[0]
oid_list = []
for i in xrange(n):
oid = unpack('8s', body[4+i*8:12+i*8])[0]
oid_list.append(oid)
except struct.error, msg:
raise PacketMalformedError('invalid answer oids')
return (oid_list,)
decode_table[ANSWER_OIDS] = _decodeAnswerOIDs
# 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