Commit 3774858d authored by Grégory Wisniewski's avatar Grégory Wisniewski

Factorise struct.error exception handling with a decorator over decoding

functions. Factorise check of enum based values and string decoding. 
Unify tuple unpacking by always using parenthesis.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@663 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 193a31e1
...@@ -453,135 +453,149 @@ def parse(msg): ...@@ -453,135 +453,149 @@ def parse(msg):
packet.setId(msg_id) packet.setId(msg_id)
return packet return packet
# packet decoding def handle_errors(decoder):
def _decodeError(body): """ Decorator to be used on encoding/decoding methods. Intercept struct
(pack/unpack) exceptions and wrap them in PacketMalformedError """
def wrapper(body):
try: try:
body = body return decoder(body)
code, size = unpack('!HL', body[:6])
message = body[6:]
except struct.error, msg: except struct.error, msg:
raise PacketMalformedError('invalid error message') name = decoder.__name__
if len(message) != size: raise PacketMalformedError("%s fail (%s)" % (name, msg))
raise PacketMalformedError('invalid error message size') except PacketMalformedError, msg:
return code, message name = decoder.__name__
raise PacketMalformedError("%s fail (%s)" % (name, msg))
return wrapper
def _checkClusterState(state):
cluster_state = cluster_states.get(state)
if cluster_state is None:
raise PacketMalformedError('invalid node state %d' % state)
return cluster_state
def _checkNodeState(state):
node_state = node_states.get(state)
if node_state is None:
raise PacketMalformedError('invalid node state %d' % state)
return node_state
def _checkNodeType(type):
node_type = node_types.get(type)
if node_type is None:
raise PacketMalformedError('invalide node type %d' % type)
return node_type
def _readString(buffer, name, offset=0):
buffer = buffer[offset:]
(size, ) = unpack('!L', buffer[:4])
string = buffer[4:4+size]
if len(string) != size:
raise PacketMalformedError("can't read string <%s>" % name)
return (string, buffer[offset+size:])
# packet decoding
@handle_errors
def _decodeError(body):
(code, ) = unpack('!H', body[:2])
(message, _) = _readString(body, 'message', offset=2)
return (code, message)
decode_table[ERROR] = _decodeError decode_table[ERROR] = _decodeError
@handle_errors
def _decodePing(body): def _decodePing(body):
pass pass
decode_table[PING] = _decodePing decode_table[PING] = _decodePing
@handle_errors
def _decodePong(body): def _decodePong(body):
pass pass
decode_table[PONG] = _decodePong decode_table[PONG] = _decodePong
@handle_errors
def _decodeRequestNodeIdentification(body): def _decodeRequestNodeIdentification(body):
try: r = unpack('!LLH16s4sH', body[:32])
body = body major, minor, node_type, uuid, ip_address, port = r
major, minor, node_type, uuid, ip_address, port, size \
= unpack('!LLH16s4sHL', body[:36])
ip_address = inet_ntoa(ip_address) ip_address = inet_ntoa(ip_address)
name = body[36:] (name, _) = _readString(body, 'name', offset=32)
except struct.error, msg: node_type = _checkNodeType(node_type)
raise PacketMalformedError('invalid request node identification')
if size != len(name):
raise PacketMalformedError('invalid name size')
node_type = node_types.get(node_type)
if node_type not in VALID_NODE_TYPE_LIST:
raise PacketMalformedError('invalid node type %d' % node_type)
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
@handle_errors
def _decodeAcceptNodeIdentification(body): def _decodeAcceptNodeIdentification(body):
try: r = unpack('!H16s4sHLL16s', body)
node_type, uuid, ip_address, port, num_partitions, num_replicas, your_uuid \ node_type, uuid, ip_address, port, num_partitions, num_replicas, your_uuid = r
= unpack('!H16s4sHLL16s', body)
ip_address = inet_ntoa(ip_address) ip_address = inet_ntoa(ip_address)
except struct.error, msg: node_type = _checkNodeType(node_type)
raise PacketMalformedError('invalid accept node identification') return (node_type, uuid, ip_address, port, num_partitions, num_replicas, your_uuid)
node_type = node_types.get(node_type)
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 decode_table[ACCEPT_NODE_IDENTIFICATION] = _decodeAcceptNodeIdentification
@handle_errors
def _decodeAskPrimaryMaster(body): def _decodeAskPrimaryMaster(body):
pass pass
decode_table[ASK_PRIMARY_MASTER] = _decodeAskPrimaryMaster decode_table[ASK_PRIMARY_MASTER] = _decodeAskPrimaryMaster
@handle_errors
def _decodeAnswerPrimaryMaster(body): def _decodeAnswerPrimaryMaster(body):
try: (primary_uuid, n) = unpack('!16sL', 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', 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: return (primary_uuid, known_master_list)
raise PacketMalformedError('invalid answer primary master')
return primary_uuid, known_master_list
decode_table[ANSWER_PRIMARY_MASTER] = _decodeAnswerPrimaryMaster decode_table[ANSWER_PRIMARY_MASTER] = _decodeAnswerPrimaryMaster
@handle_errors
def _decodeAnnouncePrimaryMaster(body): def _decodeAnnouncePrimaryMaster(body):
pass pass
decode_table[ANNOUNCE_PRIMARY_MASTER] = _decodeAnnouncePrimaryMaster decode_table[ANNOUNCE_PRIMARY_MASTER] = _decodeAnnouncePrimaryMaster
@handle_errors
def _decodeReelectPrimaryMaster(body): def _decodeReelectPrimaryMaster(body):
pass pass
decode_table[REELECT_PRIMARY_MASTER] = _decodeReelectPrimaryMaster decode_table[REELECT_PRIMARY_MASTER] = _decodeReelectPrimaryMaster
@handle_errors
def _decodeNotifyNodeInformation(body): def _decodeNotifyNodeInformation(body):
try: (n,) = unpack('!L', body[:4])
n = unpack('!L', body[:4])[0]
node_list = [] node_list = []
for i in xrange(n): for i in xrange(n):
r = unpack('!H4sH16sH', 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)
node_type = node_types.get(node_type) node_type = _checkNodeType(node_type)
if node_type not in VALID_NODE_TYPE_LIST: state = _checkNodeState(state)
raise PacketMalformedError('invalid node type %d' % node_type)
state = node_states.get(state)
if state not in VALID_NODE_STATE_LIST:
raise PacketMalformedError('invalid node state %d' % state)
node_list.append((node_type, ip_address, port, uuid, state)) node_list.append((node_type, ip_address, port, uuid, state))
except PacketMalformedError:
raise
except struct.error, msg:
raise PacketMalformedError('invalid answer node information')
return (node_list,) return (node_list,)
decode_table[NOTIFY_NODE_INFORMATION] = _decodeNotifyNodeInformation decode_table[NOTIFY_NODE_INFORMATION] = _decodeNotifyNodeInformation
@handle_errors
def _decodeAskLastIDs(body): def _decodeAskLastIDs(body):
pass pass
decode_table[ASK_LAST_IDS] = _decodeAskLastIDs decode_table[ASK_LAST_IDS] = _decodeAskLastIDs
@handle_errors
def _decodeAnswerLastIDs(body): def _decodeAnswerLastIDs(body):
try: return unpack('!8s8s8s', body) # (loid, ltid, lptid)
loid, ltid, lptid = unpack('!8s8s8s', body)
except struct.error, msg:
raise PacketMalformedError('invalid answer last ids')
return loid, ltid, lptid
decode_table[ANSWER_LAST_IDS] = _decodeAnswerLastIDs decode_table[ANSWER_LAST_IDS] = _decodeAnswerLastIDs
@handle_errors
def _decodeAskPartitionTable(body): def _decodeAskPartitionTable(body):
try: (n,) = unpack('!L', body[:4])
n = unpack('!L', body[:4])[0]
offset_list = [] offset_list = []
for i in xrange(n): for i in xrange(n):
offset = unpack('!L', 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:
raise PacketMalformedError('invalid ask partition table')
return (offset_list,) return (offset_list,)
decode_table[ASK_PARTITION_TABLE] = _decodeAskPartitionTable decode_table[ASK_PARTITION_TABLE] = _decodeAskPartitionTable
@handle_errors
def _decodeAnswerPartitionTable(body): def _decodeAnswerPartitionTable(body):
try:
ptid, n = unpack('!8sL', body[:12])
index = 12 index = 12
(ptid, n) = unpack('!8sL', body[:index])
row_list = [] row_list = []
cell_list = [] cell_list = []
for i in xrange(n): for i in xrange(n):
...@@ -594,15 +608,13 @@ def _decodeAnswerPartitionTable(body): ...@@ -594,15 +608,13 @@ def _decodeAnswerPartitionTable(body):
cell_list.append((uuid, state)) cell_list.append((uuid, state))
row_list.append((offset, tuple(cell_list))) row_list.append((offset, tuple(cell_list)))
del cell_list[:] del cell_list[:]
except struct.error, msg: return (ptid, row_list)
raise PacketMalformedError('invalid answer partition table')
return ptid, row_list
decode_table[ANSWER_PARTITION_TABLE] = _decodeAnswerPartitionTable decode_table[ANSWER_PARTITION_TABLE] = _decodeAnswerPartitionTable
@handle_errors
def _decodeSendPartitionTable(body): def _decodeSendPartitionTable(body):
try:
ptid, n = unpack('!8sL', body[:12])
index = 12 index = 12
(ptid, n,) = unpack('!8sL', body[:index])
row_list = [] row_list = []
cell_list = [] cell_list = []
for i in xrange(n): for i in xrange(n):
...@@ -615,345 +627,263 @@ def _decodeSendPartitionTable(body): ...@@ -615,345 +627,263 @@ def _decodeSendPartitionTable(body):
cell_list.append((uuid, state)) cell_list.append((uuid, state))
row_list.append((offset, tuple(cell_list))) row_list.append((offset, tuple(cell_list)))
del cell_list[:] del cell_list[:]
except struct.error, msg: return (ptid, row_list)
raise PacketMalformedError('invalid send partition table')
return ptid, row_list
decode_table[SEND_PARTITION_TABLE] = _decodeSendPartitionTable decode_table[SEND_PARTITION_TABLE] = _decodeSendPartitionTable
@handle_errors
def _decodeNotifyPartitionChanges(body): def _decodeNotifyPartitionChanges(body):
try: (ptid, n) = unpack('!8sL', 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', 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:
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
@handle_errors
def _decodeStartOperation(body): def _decodeStartOperation(body):
pass pass
decode_table[START_OPERATION] = _decodeStartOperation decode_table[START_OPERATION] = _decodeStartOperation
@handle_errors
def _decodeStopOperation(body): def _decodeStopOperation(body):
pass pass
decode_table[STOP_OPERATION] = _decodeStopOperation decode_table[STOP_OPERATION] = _decodeStopOperation
@handle_errors
def _decodeAskUnfinishedTransactions(body): def _decodeAskUnfinishedTransactions(body):
pass pass
decode_table[ASK_UNFINISHED_TRANSACTIONS] = _decodeAskUnfinishedTransactions decode_table[ASK_UNFINISHED_TRANSACTIONS] = _decodeAskUnfinishedTransactions
@handle_errors
def _decodeAnswerUnfinishedTransactions(body): def _decodeAnswerUnfinishedTransactions(body):
try: (n,) = unpack('!L', body[:4])
n = unpack('!L', body[:4])[0]
tid_list = [] tid_list = []
for i in xrange(n): for i in xrange(n):
tid = unpack('8s', 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:
raise PacketMalformedError('invalid answer unfinished transactions')
return (tid_list,) return (tid_list,)
decode_table[ANSWER_UNFINISHED_TRANSACTIONS] = _decodeAnswerUnfinishedTransactions decode_table[ANSWER_UNFINISHED_TRANSACTIONS] = _decodeAnswerUnfinishedTransactions
@handle_errors
def _decodeAskObjectPresent(body): def _decodeAskObjectPresent(body):
try: return unpack('8s8s', body) # oid, tid
oid, tid = unpack('8s8s', body)
except struct.error, msg:
raise PacketMalformedError('invalid ask object present')
return oid, tid
decode_table[ASK_OBJECT_PRESENT] = _decodeAskObjectPresent decode_table[ASK_OBJECT_PRESENT] = _decodeAskObjectPresent
@handle_errors
def _decodeAnswerObjectPresent(body): def _decodeAnswerObjectPresent(body):
try: return unpack('8s8s', body) # oid, tid
oid, tid = unpack('8s8s', body)
except struct.error, msg:
raise PacketMalformedError('invalid answer object present')
return oid, tid
decode_table[ANSWER_OBJECT_PRESENT] = _decodeAnswerObjectPresent decode_table[ANSWER_OBJECT_PRESENT] = _decodeAnswerObjectPresent
@handle_errors
def _decodeDeleteTransaction(body): def _decodeDeleteTransaction(body):
try: return unpack('8s', body) # tid
tid = unpack('8s', body)[0]
except struct.error, msg:
raise PacketMalformedError('invalid delete transaction')
return (tid,)
decode_table[DELETE_TRANSACTION] = _decodeDeleteTransaction decode_table[DELETE_TRANSACTION] = _decodeDeleteTransaction
@handle_errors
def _decodeCommitTransaction(body): def _decodeCommitTransaction(body):
try: return unpack('8s', body) # tid
tid = unpack('8s', body)[0]
except struct.error, msg:
raise PacketMalformedError('invalid commit transaction')
return (tid,)
decode_table[COMMIT_TRANSACTION] = _decodeCommitTransaction decode_table[COMMIT_TRANSACTION] = _decodeCommitTransaction
@handle_errors
def _decodeAskNewTID(body): def _decodeAskNewTID(body):
pass pass
decode_table[ASK_NEW_TID] = _decodeAskNewTID decode_table[ASK_NEW_TID] = _decodeAskNewTID
@handle_errors
def _decodeAnswerNewTID(body): def _decodeAnswerNewTID(body):
try: return unpack('8s', body) # tid
tid = unpack('8s', body)[0]
except struct.error, msg:
raise PacketMalformedError('invalid answer new tid')
return (tid,)
decode_table[ANSWER_NEW_TID] = _decodeAnswerNewTID decode_table[ANSWER_NEW_TID] = _decodeAnswerNewTID
@handle_errors
def _decodeAskNewOIDs(body): def _decodeAskNewOIDs(body):
try: return unpack('!H', body) # num oids
num_oids = unpack('!H', body)[0]
except struct.error, msg:
raise PacketMalformedError('invalid ask new oids')
return (num_oids,)
decode_table[ASK_NEW_OIDS] = _decodeAskNewOIDs decode_table[ASK_NEW_OIDS] = _decodeAskNewOIDs
@handle_errors
def _decodeAnswerNewOIDs(body): def _decodeAnswerNewOIDs(body):
try: (n,) = unpack('!H', body[:2])
n = unpack('!H', body[:2])[0]
oid_list = [] oid_list = []
for i in xrange(n): for i in xrange(n):
oid = unpack('8s', 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:
raise PacketMalformedError('invalid answer new oids')
return (oid_list,) return (oid_list,)
decode_table[ANSWER_NEW_OIDS] = _decodeAnswerNewOIDs decode_table[ANSWER_NEW_OIDS] = _decodeAnswerNewOIDs
@handle_errors
def _decodeFinishTransaction(body): def _decodeFinishTransaction(body):
try: (tid, n) = unpack('!8sL', 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', 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: return (oid_list, tid)
raise PacketMalformedError('invalid finish transaction')
return oid_list, tid
decode_table[FINISH_TRANSACTION] = _decodeFinishTransaction decode_table[FINISH_TRANSACTION] = _decodeFinishTransaction
@handle_errors
def _decodeNotifyTransactionFinished(body): def _decodeNotifyTransactionFinished(body):
try: return unpack('8s', body) # tid
tid = unpack('8s', body)[0]
except struct.error, msg:
raise PacketMalformedError('invalid notify transactin finished')
return (tid,)
decode_table[NOTIFY_TRANSACTION_FINISHED] = _decodeNotifyTransactionFinished decode_table[NOTIFY_TRANSACTION_FINISHED] = _decodeNotifyTransactionFinished
@handle_errors
def _decodeLockInformation(body): def _decodeLockInformation(body):
try: return unpack('8s', body) # tid
tid = unpack('8s', body)[0]
except struct.error, msg:
raise PacketMalformedError('invalid lock information')
return (tid,)
decode_table[LOCK_INFORMATION] = _decodeLockInformation decode_table[LOCK_INFORMATION] = _decodeLockInformation
@handle_errors
def _decodeNotifyInformationLocked(body): def _decodeNotifyInformationLocked(body):
try: return unpack('8s', body) # tid
tid = unpack('8s', body)[0]
except struct.error, msg:
raise PacketMalformedError('invalid notify information locked')
return (tid,)
decode_table[NOTIFY_INFORMATION_LOCKED] = _decodeNotifyInformationLocked decode_table[NOTIFY_INFORMATION_LOCKED] = _decodeNotifyInformationLocked
@handle_errors
def _decodeInvalidateObjects(body): def _decodeInvalidateObjects(body):
try: (tid, n) = unpack('!8sL', 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', 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: return (oid_list, tid)
raise PacketMalformedError('invalid finish transaction')
return oid_list, tid
decode_table[INVALIDATE_OBJECTS] = _decodeInvalidateObjects decode_table[INVALIDATE_OBJECTS] = _decodeInvalidateObjects
@handle_errors
def _decodeUnlockInformation(body): def _decodeUnlockInformation(body):
try: return unpack('8s', body) # tid
tid = unpack('8s', body)[0]
except struct.error, msg:
raise PacketMalformedError('invalid unlock information')
return (tid,)
decode_table[UNLOCK_INFORMATION] = _decodeUnlockInformation decode_table[UNLOCK_INFORMATION] = _decodeUnlockInformation
@handle_errors
def _decodeAbortTransaction(body): def _decodeAbortTransaction(body):
try: return unpack('8s', body) # tid
tid = unpack('8s', body)[0]
except struct.error, msg:
raise PacketMalformedError('invalid abort transaction')
return (tid,)
decode_table[ABORT_TRANSACTION] = _decodeAbortTransaction decode_table[ABORT_TRANSACTION] = _decodeAbortTransaction
@handle_errors
def _decodeAskStoreObject(body): def _decodeAskStoreObject(body):
try: r = unpack('!8s8s8sBL', body[:29])
oid, serial, tid, compression, checksum, data_len \ oid, serial, tid, compression, checksum = r
= unpack('!8s8s8sBLL', body[:33]) (data, _) = _readString(body, 'data', offset=29)
data = body[33:] return (oid, serial, compression, checksum, data, tid)
except struct.error, msg:
raise PacketMalformedError('invalid ask store object')
if data_len != len(data):
raise PacketMalformedError('invalid data size')
return oid, serial, compression, checksum, data, tid
decode_table[ASK_STORE_OBJECT] = _decodeAskStoreObject decode_table[ASK_STORE_OBJECT] = _decodeAskStoreObject
@handle_errors
def _decodeAnswerStoreObject(body): def _decodeAnswerStoreObject(body):
try: return unpack('!B8s8s', body) # conflicting, oid, serial
conflicting, oid, serial = unpack('!B8s8s', body)
except struct.error, msg:
raise PacketMalformedError('invalid answer store object')
return conflicting, oid, serial
decode_table[ANSWER_STORE_OBJECT] = _decodeAnswerStoreObject decode_table[ANSWER_STORE_OBJECT] = _decodeAnswerStoreObject
@handle_errors
def _decodeAskStoreTransaction(body): def _decodeAskStoreTransaction(body):
try: r = unpack('!8sLHHH', body[:18])
tid, oid_len, user_len, desc_len, ext_len \ tid, oid_len, user_len, desc_len, ext_len = r
= unpack('!8sLHHH', body[:18]) body = body[18:]
offset = 18 user = body[:user_len]
user = body[offset:offset+user_len] body = body[user_len:]
offset += user_len desc = body[:desc_len]
desc = body[offset:offset+desc_len] body = body[desc_len:]
offset += desc_len ext = body[:ext_len]
ext = body[offset:offset+ext_len] body = body[ext_len:]
offset += ext_len
oid_list = [] oid_list = []
for i in xrange(oid_len): for i in xrange(oid_len):
oid = unpack('8s', body[offset:offset+8])[0] (oid, ) = unpack('8s', body[:8])
offset += 8 body = body[8:]
oid_list.append(oid) oid_list.append(oid)
except struct.error, msg: return (tid, user, desc, ext, oid_list)
raise PacketMalformedError('invalid ask store transaction')
return tid, user, desc, ext, oid_list
decode_table[ASK_STORE_TRANSACTION] = _decodeAskStoreTransaction decode_table[ASK_STORE_TRANSACTION] = _decodeAskStoreTransaction
@handle_errors
def _decodeAnswerStoreTransaction(body): def _decodeAnswerStoreTransaction(body):
try: return unpack('8s', body) # tid
tid = unpack('8s', body)[0]
except struct.error, msg:
raise PacketMalformedError('invalid answer store transaction')
return (tid,)
decode_table[ANSWER_STORE_TRANSACTION] = _decodeAnswerStoreTransaction decode_table[ANSWER_STORE_TRANSACTION] = _decodeAnswerStoreTransaction
@handle_errors
def _decodeAskObject(body): def _decodeAskObject(body):
try: return unpack('8s8s8s', body) # oid, serial, tid
oid, serial, tid = unpack('8s8s8s', body)
except struct.error, msg:
raise PacketMalformedError('invalid ask object')
return oid, serial, tid
decode_table[ASK_OBJECT] = _decodeAskObject decode_table[ASK_OBJECT] = _decodeAskObject
@handle_errors
def _decodeAnswerObject(body): def _decodeAnswerObject(body):
try: r = unpack('!8s8s8sBL', body[:29])
oid, serial_start, serial_end, compression, checksum, data_len \ oid, serial_start, serial_end, compression, checksum = r
= unpack('!8s8s8sBLL', body[:33]) (data, _) = _readString(body, 'data', offset=29)
data = body[33:] return (oid, serial_start, serial_end, compression, checksum, data)
except struct.error, msg:
raise PacketMalformedError('invalid answer object')
if len(data) != data_len:
raise PacketMalformedError('invalid data size')
return oid, serial_start, serial_end, compression, checksum, data
decode_table[ANSWER_OBJECT] = _decodeAnswerObject decode_table[ANSWER_OBJECT] = _decodeAnswerObject
@handle_errors
def _decodeAskTIDs(body): def _decodeAskTIDs(body):
try: return unpack('!QQL', body) # first, last, partition
first, last, partition = unpack('!QQL', body)
except struct.error, msg:
raise PacketMalformedError('invalid ask tids')
return first, last, partition
decode_table[ASK_TIDS] = _decodeAskTIDs decode_table[ASK_TIDS] = _decodeAskTIDs
@handle_errors
def _decodeAnswerTIDs(body): def _decodeAnswerTIDs(body):
try: (n, ) = unpack('!L', body[:4])
n = unpack('!L', body[:4])[0]
tid_list = [] tid_list = []
for i in xrange(n): for i in xrange(n):
tid = unpack('8s', 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:
raise PacketMalformedError('invalid answer tids')
return (tid_list,) return (tid_list,)
decode_table[ANSWER_TIDS] = _decodeAnswerTIDs decode_table[ANSWER_TIDS] = _decodeAnswerTIDs
@handle_errors
def _decodeAskTransactionInformation(body): def _decodeAskTransactionInformation(body):
try: return unpack('8s', body) # tid
tid = unpack('8s', body)[0]
except struct.error, msg:
raise PacketMalformedError('invalid ask transaction information')
return (tid,)
decode_table[ASK_TRANSACTION_INFORMATION] = _decodeAskTransactionInformation decode_table[ASK_TRANSACTION_INFORMATION] = _decodeAskTransactionInformation
@handle_errors
def _decodeAnswerTransactionInformation(body): def _decodeAnswerTransactionInformation(body):
try: r = unpack('!8sHHHL', body[:18])
tid, user_len, desc_len, ext_len, oid_len \ tid, user_len, desc_len, ext_len, oid_len = r
= unpack('!8sHHHL', body[:18]) body = body[18:]
offset = 18 user = body[:user_len]
user = body[offset:offset+user_len] body = body[user_len:]
offset += user_len desc = body[:desc_len]
desc = body[offset:offset+desc_len] body = body[desc_len:]
offset += desc_len ext = body[:ext_len]
ext = body[offset:offset+ext_len] body = body[ext_len:]
offset += ext_len
oid_list = [] oid_list = []
for i in xrange(oid_len): for i in xrange(oid_len):
oid = unpack('8s', body[offset+i*8:offset+8+i*8])[0] (oid, ) = unpack('8s', body[:8])
body = body[8:]
oid_list.append(oid) oid_list.append(oid)
except struct.error, msg: return (tid, user, desc, ext, oid_list)
raise PacketMalformedError('invalid answer transaction information')
return tid, user, desc, ext, oid_list
decode_table[ANSWER_TRANSACTION_INFORMATION] = _decodeAnswerTransactionInformation decode_table[ANSWER_TRANSACTION_INFORMATION] = _decodeAnswerTransactionInformation
@handle_errors
def _decodeAskObjectHistory(body): def _decodeAskObjectHistory(body):
try: return unpack('!8sQQ', body) # oid, first, last
oid, first, last = unpack('!8sQQ', body)
except struct.error, msg:
raise PacketMalformedError('invalid ask object history')
return oid, first, last
decode_table[ASK_OBJECT_HISTORY] = _decodeAskObjectHistory decode_table[ASK_OBJECT_HISTORY] = _decodeAskObjectHistory
@handle_errors
def _decodeAnswerObjectHistory(body): def _decodeAnswerObjectHistory(body):
try: (oid, length) = unpack('!8sL', 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', 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: return (oid, history_list)
raise PacketMalformedError('invalid answer object history')
return oid, history_list
decode_table[ANSWER_OBJECT_HISTORY] = _decodeAnswerObjectHistory decode_table[ANSWER_OBJECT_HISTORY] = _decodeAnswerObjectHistory
@handle_errors
def _decodeAskOIDs(body): def _decodeAskOIDs(body):
try: return unpack('!QQL', body) # first, last, partition
first, last, partition = unpack('!QQL', body)
except struct.error, msg:
raise PacketMalformedError('invalid ask oids')
return first, last, partition
decode_table[ASK_OIDS] = _decodeAskOIDs decode_table[ASK_OIDS] = _decodeAskOIDs
@handle_errors
def _decodeAnswerOIDs(body): def _decodeAnswerOIDs(body):
try: (n,) = unpack('!L', body[:4])
n = unpack('!L', body[:4])[0]
oid_list = [] oid_list = []
for i in xrange(n): for i in xrange(n):
oid = unpack('8s', 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:
raise PacketMalformedError('invalid answer oids')
return (oid_list,) return (oid_list,)
decode_table[ANSWER_OIDS] = _decodeAnswerOIDs decode_table[ANSWER_OIDS] = _decodeAnswerOIDs
@handle_errors
def _decodeAskPartitionList(body): def _decodeAskPartitionList(body):
try: return unpack('!LL16s', body) # min_offset, max_offset, uuid
min_offset, max_offset, uuid = unpack('!LL16s', body)
except struct.error, msg:
raise PacketMalformedError('invalid ask partition list')
return (min_offset, max_offset, uuid)
decode_table[ASK_PARTITION_LIST] = _decodeAskPartitionList decode_table[ASK_PARTITION_LIST] = _decodeAskPartitionList
@handle_errors
def _decodeAnswerPartitionList(body): def _decodeAnswerPartitionList(body):
try:
ptid, n = unpack('!8sL', body[:12])
index = 12 index = 12
(ptid, n) = unpack('!8sL', body[:index])
row_list = [] row_list = []
cell_list = [] cell_list = []
for i in xrange(n): for i in xrange(n):
...@@ -966,105 +896,70 @@ def _decodeAnswerPartitionList(body): ...@@ -966,105 +896,70 @@ def _decodeAnswerPartitionList(body):
cell_list.append((uuid, state)) cell_list.append((uuid, state))
row_list.append((offset, tuple(cell_list))) row_list.append((offset, tuple(cell_list)))
del cell_list[:] del cell_list[:]
except struct.error, msg: return (ptid, row_list)
raise PacketMalformedError('invalid answer partition list')
return ptid, row_list
decode_table[ANSWER_PARTITION_LIST] = _decodeAnswerPartitionList decode_table[ANSWER_PARTITION_LIST] = _decodeAnswerPartitionList
@handle_errors
def _decodeAskNodeList(body): def _decodeAskNodeList(body):
try: (node_type, ) = unpack('!H', body)
node_type = unpack('!H', body)[0] node_type = _checkNodeType(node_type)
node_type = node_types.get(node_type)
if node_type not in VALID_NODE_TYPE_LIST:
raise PacketMalformedError('invalid node type %d' % node_type)
except struct.error, msg:
raise PacketMalformedError('invalid ask node list')
return (node_type,) return (node_type,)
decode_table[ASK_NODE_LIST] = _decodeAskNodeList decode_table[ASK_NODE_LIST] = _decodeAskNodeList
@handle_errors
def _decodeAnswerNodeList(body): def _decodeAnswerNodeList(body):
try: (n,) = unpack('!L', body[:4])
n = unpack('!L', body[:4])[0]
node_list = [] node_list = []
for i in xrange(n): for i in xrange(n):
r = unpack('!H4sH16sH', 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)
node_type = node_types.get(node_type) node_type = _checkNodeType(node_type)
if node_type not in VALID_NODE_TYPE_LIST: state = checkNodeState(state)
raise PacketMalformedError('invalid node type %d' % node_type)
state = node_states.get(state)
if state not in VALID_NODE_STATE_LIST:
raise PacketMalformedError('invalid node state %d' % state)
node_list.append((node_type, ip_address, port, uuid, state)) node_list.append((node_type, ip_address, port, uuid, state))
except struct.error, msg:
raise PacketMalformedError('invalid answer node information')
return (node_list,) return (node_list,)
decode_table[ANSWER_NODE_LIST] = _decodeAnswerNodeList decode_table[ANSWER_NODE_LIST] = _decodeAnswerNodeList
@handle_errors
def _decodeSetNodeState(body): def _decodeSetNodeState(body):
try: (uuid, state, modify) = unpack('!16sHB', body)
uuid, state, modify = unpack('!16sHB', body) state = checkNodeState(state)
state = node_states.get(state)
if state not in VALID_NODE_STATE_LIST:
raise PacketMalformedError('invalid node state %d' % state)
except struct.error, msg:
raise PacketMalformedError('invalid set node state')
return (uuid, state, modify) return (uuid, state, modify)
decode_table[SET_NODE_STATE] = _decodeSetNodeState decode_table[SET_NODE_STATE] = _decodeSetNodeState
@handle_errors
def _decodeAnswerNodeState(body): def _decodeAnswerNodeState(body):
try: (uuid, state) = unpack('!16sH', body)
uuid, state = unpack('!16sH', body) state = checkNodeState(state)
state = node_states.get(state)
if state not in VALID_NODE_STATE_LIST:
raise PacketMalformedError('invalid node state %d' % state)
except struct.error, msg:
raise PacketMalformedError('invalid answer node state')
return (uuid, state) return (uuid, state)
decode_table[ANSWER_NODE_STATE] = _decodeAnswerNodeState decode_table[ANSWER_NODE_STATE] = _decodeAnswerNodeState
@handle_errors
def _decodeSetClusterState(body): def _decodeSetClusterState(body):
try: (state, ) = unpack('!H', body[:2])
state, len_name = unpack('!HL', body[:6]) (name, _) = _readString(body, 'name', offset=2)
name = body[6:] state = _checkClusterState(state)
if len_name != len(name):
raise PacketMalformedError('invalid name size')
state = cluster_states.get(state)
if state is None:
raise PacketMalformedError('invalid cluster state %d' % state)
except struct.error, msg:
raise PacketMalformedError('invalid set node state')
return (name, state) return (name, state)
decode_table[SET_CLUSTER_STATE] = _decodeSetClusterState decode_table[SET_CLUSTER_STATE] = _decodeSetClusterState
@handle_errors
def _decodeAnswerClusterState(body): def _decodeAnswerClusterState(body):
try:
(state, ) = unpack('!H', body) (state, ) = unpack('!H', body)
state = cluster_states.get(state) state = _checkClusterState(state)
if state is None:
raise PacketMalformedError('invalid cluster state %d' % state)
except struct.error, msg:
raise PacketMalformedError('invalid answer cluster state')
return (state, ) return (state, )
decode_table[ANSWER_CLUSTER_STATE] = _decodeAnswerClusterState decode_table[ANSWER_CLUSTER_STATE] = _decodeAnswerClusterState
@handle_errors
def _decodeAddPendingNodes(body): def _decodeAddPendingNodes(body):
try:
(n, ) = unpack('!H', body[:2]) (n, ) = unpack('!H', body[:2])
uuid_list = [unpack('!16s', body[2+i*16:18+i*16])[0] for i in xrange(n)] uuid_list = [unpack('!16s', body[2+i*16:18+i*16])[0] for i in xrange(n)]
except struct.error, msg:
raise PacketMalformedError('invalid add pending nodes')
return (uuid_list, ) return (uuid_list, )
decode_table[ADD_PENDING_NODES] = _decodeAddPendingNodes decode_table[ADD_PENDING_NODES] = _decodeAddPendingNodes
@handle_errors
def _decodeAnswerNewNodes(body): def _decodeAnswerNewNodes(body):
try:
(n, ) = unpack('!H', body[:2]) (n, ) = unpack('!H', body[:2])
uuid_list = [unpack('!16s', body[2+i*16:18+i*16])[0] for i in xrange(n)] uuid_list = [unpack('!16s', body[2+i*16:18+i*16])[0] for i in xrange(n)]
except struct.error, msg:
raise PacketMalformedError('invalid answer new nodes')
return (uuid_list, ) return (uuid_list, )
decode_table[ANSWER_NEW_NODES] = _decodeAnswerNewNodes decode_table[ANSWER_NEW_NODES] = _decodeAnswerNewNodes
......
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