Commit 9fa43955 authored by Julien Muchembled's avatar Julien Muchembled

Simplify EventHandler by removing 'error_dispatch_table'

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2688 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 3bfbf89b
...@@ -26,7 +26,6 @@ class EventHandler(object): ...@@ -26,7 +26,6 @@ class EventHandler(object):
def __init__(self, app): def __init__(self, app):
self.app = app self.app = app
self.error_dispatch_table = self.__initErrorDispatchTable()
def __repr__(self): def __repr__(self):
return self.__class__.__name__ return self.__class__.__name__
...@@ -62,7 +61,7 @@ class EventHandler(object): ...@@ -62,7 +61,7 @@ class EventHandler(object):
conn.abort() conn.abort()
self.peerBroken(conn) self.peerBroken(conn)
except BrokenNodeDisallowedError: except BrokenNodeDisallowedError:
conn.answer(Errors.Broken('go away')) conn.answer(Errors.BrokenNode('go away'))
conn.abort() conn.abort()
self.connectionClosed(conn) self.connectionClosed(conn)
except NotReadyError, message: except NotReadyError, message:
...@@ -142,23 +141,10 @@ class EventHandler(object): ...@@ -142,23 +141,10 @@ class EventHandler(object):
def error(self, conn, code, message): def error(self, conn, code, message):
try: try:
method = self.error_dispatch_table[code] getattr(self, Errors[code])(conn, message)
method(conn, message) except (AttributeError, ValueError):
except ValueError:
raise UnexpectedPacketError(message) raise UnexpectedPacketError(message)
def notReady(self, conn, message):
raise UnexpectedPacketError
def oidNotFound(self, conn, message):
raise UnexpectedPacketError
def oidDoesNotExist(self, conn, message):
raise UnexpectedPacketError
def tidNotFound(self, conn, message):
raise UnexpectedPacketError
def protocolError(self, conn, message): def protocolError(self, conn, message):
# the connection should have been closed by the remote peer # the connection should have been closed by the remote peer
neo.lib.logging.error('protocol error: %s' % (message,)) neo.lib.logging.error('protocol error: %s' % (message,))
...@@ -174,21 +160,3 @@ class EventHandler(object): ...@@ -174,21 +160,3 @@ class EventHandler(object):
def ack(self, conn, message): def ack(self, conn, message):
neo.lib.logging.debug("no error message : %s" % (message)) neo.lib.logging.debug("no error message : %s" % (message))
# Fetch tables initialization
def __initErrorDispatchTable(self):
d = {}
d[ErrorCodes.ACK] = self.ack
d[ErrorCodes.NOT_READY] = self.notReady
d[ErrorCodes.OID_NOT_FOUND] = self.oidNotFound
d[ErrorCodes.OID_DOES_NOT_EXIST] = self.oidDoesNotExist
d[ErrorCodes.TID_NOT_FOUND] = self.tidNotFound
d[ErrorCodes.PROTOCOL_ERROR] = self.protocolError
d[ErrorCodes.BROKEN_NODE] = self.brokenNodeDisallowedError
d[ErrorCodes.ALREADY_PENDING] = self.alreadyPendingError
return d
...@@ -27,8 +27,6 @@ class PacketLogger(object): ...@@ -27,8 +27,6 @@ class PacketLogger(object):
""" Logger at packet level (for debugging purpose) """ """ Logger at packet level (for debugging purpose) """
def __init__(self): def __init__(self):
_temp = EventHandler(None)
self.error_dispatch_table = _temp.error_dispatch_table
self.enable(LOGGER_ENABLED) self.enable(LOGGER_ENABLED)
def enable(self, enabled): def enable(self, enabled):
......
...@@ -1535,26 +1535,16 @@ class Packets(dict): ...@@ -1535,26 +1535,16 @@ class Packets(dict):
NotifyTransactionFinished = register( NotifyTransactionFinished = register(
0x003E, NotifyTransactionFinished) 0x003E, NotifyTransactionFinished)
def register_error(code): def Errors():
def wrapper(registry, message=''): registry_dict = {}
return Error(code, message) handler_method_name_dict = {}
return wrapper def register_error(code):
return lambda self, message='': Error(code, message)
class ErrorRegistry(dict): for code, error in ErrorCodes.iteritems():
""" name = ''.join(part.capitalize() for part in str(error).split('_'))
Error packet packet registry registry_dict[name] = register_error(error)
""" handler_method_name_dict[code] = name[0].lower() + name[1:]
def __init__(self): return type('ErrorRegistry', (dict,),
dict.__init__(self) registry_dict)(handler_method_name_dict)
Ack = register_error(ErrorCodes.ACK) Errors = Errors()
ProtocolError = register_error(ErrorCodes.PROTOCOL_ERROR)
TidNotFound = register_error(ErrorCodes.TID_NOT_FOUND)
OidNotFound = register_error(ErrorCodes.OID_NOT_FOUND)
OidDoesNotExist = register_error(ErrorCodes.OID_DOES_NOT_EXIST)
NotReady = register_error(ErrorCodes.NOT_READY)
Broken = register_error(ErrorCodes.BROKEN_NODE)
AlreadyPending = register_error(ErrorCodes.ALREADY_PENDING)
Errors = ErrorRegistry()
...@@ -44,7 +44,7 @@ class ProtocolTests(NeoUnitTestBase): ...@@ -44,7 +44,7 @@ class ProtocolTests(NeoUnitTestBase):
self.assertEqual(error_msg, "wait") self.assertEqual(error_msg, "wait")
def test_06_brokenNodeDisallowedError(self): def test_06_brokenNodeDisallowedError(self):
p = Errors.Broken("broken") p = Errors.BrokenNode("broken")
error_code, error_msg = p.decode() error_code, error_msg = p.decode()
self.assertEqual(error_code, ErrorCodes.BROKEN_NODE) self.assertEqual(error_code, ErrorCodes.BROKEN_NODE)
self.assertEqual(error_msg, "broken") self.assertEqual(error_msg, "broken")
......
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