Commit 79c6f0c0 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Add PacketMalformedError exception that inherit from ProtocolError to replace

the last one with decoding errors. New protocol exceptions will follows in next
commits.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@498 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 746fed5b
......@@ -21,7 +21,7 @@ import sys
import traceback
from neo import protocol
from neo.protocol import Packet, ProtocolError
from neo.protocol import Packet, PacketMalformedError
from neo.event import IdleEvent
from neo.connector import ConnectorException, ConnectorTryAgainException, \
ConnectorInProgressException, ConnectorConnectionRefusedException
......@@ -209,8 +209,8 @@ class Connection(BaseConnection):
while 1:
try:
packet = Packet.parse(self.read_buf)
except ProtocolError, m:
self.handler.packetMalformed(self, *m)
except PacketMalformedError, (packet, msg):
self.handler.packetMalformed(self, packet, msg)
return
if packet is None:
......@@ -286,9 +286,11 @@ class Connection(BaseConnection):
packet.getType(), dump(self.uuid), *self.getAddress())
try:
self.write_buf += packet.encode()
except ProtocolError, m:
except PacketMalformedError, m:
logging.critical('trying to send a too big message')
return self.addPacket(protocol.internalError(m[0]))
# XXX: we should assert that the internalError packet has a size
# lower than MAX_PACKET_SIZE
return self.notify(protocol.internalError(m))
# If this is the first time, enable polling for writing.
if self.write_buf:
......
......@@ -18,7 +18,7 @@
import logging
from neo import protocol
from neo.protocol import Packet, ProtocolError
from neo.protocol import Packet, PacketMalformedError
from neo.connection import ServerConnection
from protocol import ERROR, REQUEST_NODE_IDENTIFICATION, ACCEPT_NODE_IDENTIFICATION, \
......@@ -100,7 +100,7 @@ class EventHandler(object):
method(conn, packet, *args)
except (KeyError, ValueError):
self.handleUnexpectedPacket(conn, packet)
except ProtocolError, m:
except PacketMalformedError, m:
self.packetMalformed(conn, packet, m[1])
def handleUnexpectedPacket(self, conn, packet, message = None):
......
......@@ -319,6 +319,7 @@ UUID_NAMESPACES = {
}
class ProtocolError(Exception): pass
class PacketMalformedError(ProtocolError): pass
class Packet(object):
"""A packet."""
......@@ -336,13 +337,12 @@ class Packet(object):
try:
msg_type = packet_types[msg_type]
except KeyError:
raise ProtocolError(cls(msg_id, msg_type),
'Unknown packet type')
raise PacketMalformedError(cls(msg_id, msg_type), 'Unknown packet type')
if msg_len > MAX_PACKET_SIZE:
raise ProtocolError(cls(msg_id, msg_type),
raise PacketMalformedError(cls(msg_id, msg_type),
'message too big (%d)' % msg_len)
if msg_len < MIN_PACKET_SIZE:
raise ProtocolError(cls(msg_id, msg_type),
raise PacketMalformedError(cls(msg_id, msg_type),
'message too small (%d)' % msg_len)
if len(msg) < msg_len:
# Not enough.
......@@ -375,9 +375,8 @@ class Packet(object):
def encode(self):
msg = pack('!LHL', self._id, self._type, PACKET_HEADER_SIZE + len(self._body)) + self._body
if len(msg) > MAX_PACKET_SIZE:
raise ProtocolError('message too big (%d)' % len(msg))
raise PacketMalformedError(self, 'message too big (%d)' % len(msg))
return msg
__str__ = encode
......@@ -386,7 +385,7 @@ class Packet(object):
try:
method = self.decode_table[self._type]
except KeyError:
raise ProtocolError(self, 'unknown message type 0x%x' % self._type)
raise PacketMalformedError(self, 'unknown message type 0x%x' % self._type)
return method(self)
decode_table = {}
......@@ -397,9 +396,9 @@ class Packet(object):
code, size = unpack('!HL', body[:6])
message = body[6:]
except struct.error, msg:
raise ProtocolError(self, 'invalid error message')
raise PacketMalformedError(self, 'invalid error message')
if len(message) != size:
raise ProtocolError(self, 'invalid error message size')
raise PacketMalformedError(self, 'invalid error message size')
return code, message
decode_table[ERROR] = _decodeError
......@@ -419,13 +418,13 @@ class Packet(object):
ip_address = inet_ntoa(ip_address)
name = body[36:]
except struct.error, msg:
raise ProtocolError(self, 'invalid request node identification')
raise PacketMalformedError(self, 'invalid request node identification')
if size != len(name):
raise ProtocolError(self, 'invalid name size')
raise PacketMalformedError(self, 'invalid name size')
if node_type not in VALID_NODE_TYPE_LIST:
raise ProtocolError(self, 'invalid node type %d' % node_type)
raise PacketMalformedError(self, 'invalid node type %d' % node_type)
if (major, minor) != PROTOCOL_VERSION:
raise ProtocolError(self, 'protocol version mismatch')
raise PacketMalformedError(self, 'protocol version mismatch')
return node_type, uuid, ip_address, port, name
decode_table[REQUEST_NODE_IDENTIFICATION] = _decodeRequestNodeIdentification
......@@ -435,9 +434,9 @@ class Packet(object):
= unpack('!H16s4sHLL16s', self._body)
ip_address = inet_ntoa(ip_address)
except struct.error, msg:
raise ProtocolError(self, 'invalid accept node identification')
raise PacketMalformedError(self, 'invalid accept node identification')
if node_type not in VALID_NODE_TYPE_LIST:
raise ProtocolError(self, 'invalid node type %d' % node_type)
raise PacketMalformedError(self, '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
......@@ -454,7 +453,7 @@ class Packet(object):
ip_address = inet_ntoa(ip_address)
known_master_list.append((ip_address, port, uuid))
except struct.error, msg:
raise ProtocolError(self, 'invalid answer primary master')
raise PacketMalformedError(self, 'invalid answer primary master')
return primary_uuid, known_master_list
decode_table[ANSWER_PRIMARY_MASTER] = _decodeAnswerPrimaryMaster
......@@ -475,15 +474,15 @@ class Packet(object):
node_type, ip_address, port, uuid, state = r
ip_address = inet_ntoa(ip_address)
if node_type not in VALID_NODE_TYPE_LIST:
raise ProtocolError(self, 'invalid node type %d' % node_type)
raise PacketMalformedError(self, 'invalid node type %d' % node_type)
state = node_states.get(state)
if state not in VALID_NODE_STATE_LIST:
raise ProtocolError(self, 'invalid node state %d' % state)
raise PacketMalformedError(self, 'invalid node state %d' % state)
node_list.append((node_type, ip_address, port, uuid, state))
except ProtocolError:
except PacketMalformedError:
raise
except struct.error, msg:
raise ProtocolError(self, 'invalid answer node information')
raise PacketMalformedError(self, 'invalid answer node information')
return (node_list,)
decode_table[NOTIFY_NODE_INFORMATION] = _decodeNotifyNodeInformation
......@@ -495,7 +494,7 @@ class Packet(object):
try:
loid, ltid, lptid = unpack('!8s8s8s', self._body)
except struct.error, msg:
raise ProtocolError(self, 'invalid answer last ids')
raise PacketMalformedError(self, 'invalid answer last ids')
return loid, ltid, lptid
decode_table[ANSWER_LAST_IDS] = _decodeAnswerLastIDs
......@@ -507,7 +506,7 @@ class Packet(object):
offset = unpack('!L', self._body[4+i*4:8+i*4])[0]
offset_list.append(offset)
except struct.error, msg:
raise ProtocolError(self, 'invalid ask partition table')
raise PacketMalformedError(self, 'invalid ask partition table')
return (offset_list,)
decode_table[ASK_PARTITION_TABLE] = _decodeAskPartitionTable
......@@ -528,7 +527,7 @@ class Packet(object):
row_list.append((offset, tuple(cell_list)))
del cell_list[:]
except struct.error, msg:
raise ProtocolError(self, 'invalid answer partition table')
raise PacketMalformedError(self, 'invalid answer partition table')
return ptid, row_list
decode_table[ANSWER_PARTITION_TABLE] = _decodeAnswerPartitionTable
......@@ -549,7 +548,7 @@ class Packet(object):
row_list.append((offset, tuple(cell_list)))
del cell_list[:]
except struct.error, msg:
raise ProtocolError(self, 'invalid send partition table')
raise PacketMalformedError(self, 'invalid send partition table')
return ptid, row_list
decode_table[SEND_PARTITION_TABLE] = _decodeSendPartitionTable
......@@ -562,7 +561,7 @@ class Packet(object):
state = partition_cell_states.get(state)
cell_list.append((offset, uuid, state))
except struct.error, msg:
raise ProtocolError(self, 'invalid notify partition changes')
raise PacketMalformedError(self, 'invalid notify partition changes')
return ptid, cell_list
decode_table[NOTIFY_PARTITION_CHANGES] = _decodeNotifyPartitionChanges
......@@ -586,7 +585,7 @@ class Packet(object):
tid = unpack('8s', self._body[4+i*8:12+i*8])[0]
tid_list.append(tid)
except struct.error, msg:
raise ProtocolError(self, 'invalid answer unfinished transactions')
raise PacketMalformedError(self, 'invalid answer unfinished transactions')
return (tid_list,)
decode_table[ANSWER_UNFINISHED_TRANSACTIONS] = _decodeAnswerUnfinishedTransactions
......@@ -594,7 +593,7 @@ class Packet(object):
try:
oid, tid = unpack('8s8s', self._body)
except struct.error, msg:
raise ProtocolError(self, 'invalid ask object present')
raise PacketMalformedError(self, 'invalid ask object present')
return oid, tid
decode_table[ASK_OBJECT_PRESENT] = _decodeAskObjectPresent
......@@ -602,7 +601,7 @@ class Packet(object):
try:
oid, tid = unpack('8s8s', self._body)
except struct.error, msg:
raise ProtocolError(self, 'invalid answer object present')
raise PacketMalformedError(self, 'invalid answer object present')
return oid, tid
decode_table[ANSWER_OBJECT_PRESENT] = _decodeAnswerObjectPresent
......@@ -610,7 +609,7 @@ class Packet(object):
try:
tid = unpack('8s', self._body)[0]
except struct.error, msg:
raise ProtocolError(self, 'invalid delete transaction')
raise PacketMalformedError(self, 'invalid delete transaction')
return (tid,)
decode_table[DELETE_TRANSACTION] = _decodeDeleteTransaction
......@@ -618,7 +617,7 @@ class Packet(object):
try:
tid = unpack('8s', self._body)[0]
except struct.error, msg:
raise ProtocolError(self, 'invalid commit transaction')
raise PacketMalformedError(self, 'invalid commit transaction')
return (tid,)
decode_table[COMMIT_TRANSACTION] = _decodeCommitTransaction
......@@ -630,7 +629,7 @@ class Packet(object):
try:
tid = unpack('8s', self._body)[0]
except struct.error, msg:
raise ProtocolError(self, 'invalid answer new tid')
raise PacketMalformedError(self, 'invalid answer new tid')
return (tid,)
decode_table[ANSWER_NEW_TID] = _decodeAnswerNewTID
......@@ -638,7 +637,7 @@ class Packet(object):
try:
num_oids = unpack('!H', self._body)[0]
except struct.error, msg:
raise ProtocolError(self, 'invalid ask new oids')
raise PacketMalformedError(self, 'invalid ask new oids')
return (num_oids,)
decode_table[ASK_NEW_OIDS] = _decodeAskNewOIDs
......@@ -650,7 +649,7 @@ class Packet(object):
oid = unpack('8s', self._body[2+i*8:10+i*8])[0]
oid_list.append(oid)
except struct.error, msg:
raise ProtocolError(self, 'invalid answer new oids')
raise PacketMalformedError(self, 'invalid answer new oids')
return (oid_list,)
decode_table[ANSWER_NEW_OIDS] = _decodeAnswerNewOIDs
......@@ -662,7 +661,7 @@ class Packet(object):
oid = unpack('8s', self._body[12+i*8:20+i*8])[0]
oid_list.append(oid)
except struct.error, msg:
raise ProtocolError(self, 'invalid finish transaction')
raise PacketMalformedError(self, 'invalid finish transaction')
return oid_list, tid
decode_table[FINISH_TRANSACTION] = _decodeFinishTransaction
......@@ -670,7 +669,7 @@ class Packet(object):
try:
tid = unpack('8s', self._body)[0]
except struct.error, msg:
raise ProtocolError(self, 'invalid notify transactin finished')
raise PacketMalformedError(self, 'invalid notify transactin finished')
return (tid,)
decode_table[NOTIFY_TRANSACTION_FINISHED] = _decodeNotifyTransactionFinished
......@@ -678,7 +677,7 @@ class Packet(object):
try:
tid = unpack('8s', self._body)[0]
except struct.error, msg:
raise ProtocolError(self, 'invalid lock information')
raise PacketMalformedError(self, 'invalid lock information')
return (tid,)
decode_table[LOCK_INFORMATION] = _decodeLockInformation
......@@ -686,7 +685,7 @@ class Packet(object):
try:
tid = unpack('8s', self._body)[0]
except struct.error, msg:
raise ProtocolError(self, 'invalid notify information locked')
raise PacketMalformedError(self, 'invalid notify information locked')
return (tid,)
decode_table[NOTIFY_INFORMATION_LOCKED] = _decodeNotifyInformationLocked
......@@ -698,7 +697,7 @@ class Packet(object):
oid = unpack('8s', self._body[i:i+8])[0]
oid_list.append(oid)
except struct.error, msg:
raise ProtocolError(self, 'invalid finish transaction')
raise PacketMalformedError(self, 'invalid finish transaction')
return oid_list, tid
decode_table[INVALIDATE_OBJECTS] = _decodeInvalidateObjects
......@@ -706,7 +705,7 @@ class Packet(object):
try:
tid = unpack('8s', self._body)[0]
except struct.error, msg:
raise ProtocolError(self, 'invalid unlock information')
raise PacketMalformedError(self, 'invalid unlock information')
return (tid,)
decode_table[UNLOCK_INFORMATION] = _decodeUnlockInformation
......@@ -714,7 +713,7 @@ class Packet(object):
try:
tid = unpack('8s', self._body)[0]
except struct.error, msg:
raise ProtocolError(self, 'invalid abort transaction')
raise PacketMalformedError(self, 'invalid abort transaction')
return (tid,)
decode_table[ABORT_TRANSACTION] = _decodeAbortTransaction
......@@ -724,9 +723,9 @@ class Packet(object):
= unpack('!8s8s8sBLL', self._body[:33])
data = self._body[33:]
except struct.error, msg:
raise ProtocolError(self, 'invalid ask store object')
raise PacketMalformedError(self, 'invalid ask store object')
if data_len != len(data):
raise ProtocolError(self, 'invalid data size')
raise PacketMalformedError(self, 'invalid data size')
return oid, serial, compression, checksum, data, tid
decode_table[ASK_STORE_OBJECT] = _decodeAskStoreObject
......@@ -734,7 +733,7 @@ class Packet(object):
try:
conflicting, oid, serial = unpack('!B8s8s', self._body)
except struct.error, msg:
raise ProtocolError(self, 'invalid answer store object')
raise PacketMalformedError(self, 'invalid answer store object')
return conflicting, oid, serial
decode_table[ANSWER_STORE_OBJECT] = _decodeAnswerStoreObject
......@@ -755,7 +754,7 @@ class Packet(object):
offset += 8
oid_list.append(oid)
except struct.error, msg:
raise ProtocolError(self, 'invalid ask store transaction')
raise PacketMalformedError(self, 'invalid ask store transaction')
return tid, user, desc, ext, oid_list
decode_table[ASK_STORE_TRANSACTION] = _decodeAskStoreTransaction
......@@ -763,7 +762,7 @@ class Packet(object):
try:
tid = unpack('8s', self._body)[0]
except struct.error, msg:
raise ProtocolError(self, 'invalid answer store transaction')
raise PacketMalformedError(self, 'invalid answer store transaction')
return (tid,)
decode_table[ANSWER_STORE_TRANSACTION] = _decodeAnswerStoreTransaction
......@@ -771,7 +770,7 @@ class Packet(object):
try:
oid, serial, tid = unpack('8s8s8s', self._body)
except struct.error, msg:
raise ProtocolError(self, 'invalid ask object')
raise PacketMalformedError(self, 'invalid ask object')
return oid, serial, tid
decode_table[ASK_OBJECT] = _decodeAskObject
......@@ -781,9 +780,9 @@ class Packet(object):
= unpack('!8s8s8sBLL', self._body[:33])
data = self._body[33:]
except struct.error, msg:
raise ProtocolError(self, 'invalid answer object')
raise PacketMalformedError(self, 'invalid answer object')
if len(data) != data_len:
raise ProtocolError(self, 'invalid data size')
raise PacketMalformedError(self, 'invalid data size')
return oid, serial_start, serial_end, compression, checksum, data
decode_table[ANSWER_OBJECT] = _decodeAnswerObject
......@@ -791,7 +790,7 @@ class Packet(object):
try:
first, last, partition = unpack('!QQL', self._body)
except struct.error, msg:
raise ProtocolError(self, 'invalid ask tids')
raise PacketMalformedError(self, 'invalid ask tids')
return first, last, partition
decode_table[ASK_TIDS] = _decodeAskTIDs
......@@ -803,7 +802,7 @@ class Packet(object):
tid = unpack('8s', self._body[4+i*8:12+i*8])[0]
tid_list.append(tid)
except struct.error, msg:
raise ProtocolError(self, 'invalid answer tids')
raise PacketMalformedError(self, 'invalid answer tids')
return (tid_list,)
decode_table[ANSWER_TIDS] = _decodeAnswerTIDs
......@@ -811,7 +810,7 @@ class Packet(object):
try:
tid = unpack('8s', self._body)[0]
except struct.error, msg:
raise ProtocolError(self, 'invalid ask transaction information')
raise PacketMalformedError(self, 'invalid ask transaction information')
return (tid,)
decode_table[ASK_TRANSACTION_INFORMATION] = _decodeAskTransactionInformation
......@@ -831,7 +830,7 @@ class Packet(object):
oid = unpack('8s', self._body[offset+i*8:offset+8+i*8])[0]
oid_list.append(oid)
except struct.error, msg:
raise ProtocolError(self, 'invalid answer transaction information')
raise PacketMalformedError(self, 'invalid answer transaction information')
return tid, user, desc, ext, oid_list
decode_table[ANSWER_TRANSACTION_INFORMATION] = _decodeAnswerTransactionInformation
......@@ -839,7 +838,7 @@ class Packet(object):
try:
oid, first, last = unpack('!8sQQ', self._body)
except struct.error, msg:
raise ProtocolError(self, 'invalid ask object history')
raise PacketMalformedError(self, 'invalid ask object history')
return oid, first, last
decode_table[ASK_OBJECT_HISTORY] = _decodeAskObjectHistory
......@@ -851,7 +850,7 @@ class Packet(object):
serial, size = unpack('!8sL', self._body[i:i+12])
history_list.append((serial, size))
except struct.error, msg:
raise ProtocolError(self, 'invalid answer object history')
raise PacketMalformedError(self, 'invalid answer object history')
return oid, history_list
decode_table[ANSWER_OBJECT_HISTORY] = _decodeAnswerObjectHistory
......@@ -859,7 +858,7 @@ class Packet(object):
try:
first, last, partition = unpack('!QQL', self._body)
except struct.error, msg:
raise ProtocolError(self, 'invalid ask oids')
raise PacketMalformedError(self, 'invalid ask oids')
return first, last, partition
decode_table[ASK_OIDS] = _decodeAskOIDs
......@@ -871,7 +870,7 @@ class Packet(object):
oid = unpack('8s', self._body[4+i*8:12+i*8])[0]
oid_list.append(oid)
except struct.error, msg:
raise ProtocolError(self, 'invalid answer oids')
raise PacketMalformedError(self, 'invalid answer oids')
return (oid_list,)
decode_table[ANSWER_OIDS] = _decodeAnswerOIDs
......
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