Commit 6b121f40 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Reorder handler.py module, switch some methods to private level, group handlers

to help navigation through the code.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@1147 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 10b108f7
......@@ -57,51 +57,8 @@ class EventHandler(object):
# XXX: there is an inconsistency between connection* and handle* names. As
# we are in an hander, I think that's redondant to prefix with 'handle'
def connectionStarted(self, conn):
"""Called when a connection is started."""
logging.debug('connection started for %s:%d', *(conn.getAddress()))
def connectionCompleted(self, conn):
"""Called when a connection is completed."""
logging.debug('connection completed for %s:%d', *(conn.getAddress()))
def connectionFailed(self, conn):
"""Called when a connection failed."""
logging.debug('connection failed for %s:%d', *(conn.getAddress()))
def connectionAccepted(self, conn, connector, addr):
"""Called when a connection is accepted."""
logging.debug('connection accepted from %s:%d', *addr)
new_conn = ServerConnection(conn.getEventManager(), conn.getHandler(),
connector = connector, addr = addr)
# A request for a node identification should arrive.
new_conn.expectMessage(timeout = 10, additional_timeout = 0)
def handleConnectionLost(self, conn, new_state):
""" this is a method to override in sub-handlers when there is no need
to make distinction from the kind event that closed the connection """
pass
def timeoutExpired(self, conn):
"""Called when a timeout event occurs."""
logging.debug('timeout expired for %s:%d', *(conn.getAddress()))
self.handleConnectionLost(conn, protocol.TEMPORARILY_DOWN_STATE)
def connectionClosed(self, conn):
"""Called when a connection is closed by the peer."""
logging.debug('connection closed for %s:%d', *(conn.getAddress()))
self.handleConnectionLost(conn, protocol.TEMPORARILY_DOWN_STATE)
def peerBroken(self, conn):
"""Called when a peer is broken."""
logging.error('%s:%d is broken', *(conn.getAddress()))
self.handleConnectionLost(conn, protocol.BROKEN_STATE)
def packetReceived(self, conn, packet):
"""Called when a packet is received."""
self.dispatch(conn, packet)
def packetMalformed(self, conn, packet, message='', *args):
def _packetMalformed(self, conn, packet, message='', *args):
"""Called when a packet is malformed."""
args = (conn.getAddress()[0], conn.getAddress()[1], message)
if packet is None:
......@@ -117,7 +74,7 @@ class EventHandler(object):
conn.abort()
self.peerBroken(conn)
def unexpectedPacket(self, conn, packet, message=None):
def _unexpectedPacket(self, conn, packet, message=None):
"""Handle an unexpected packet."""
if message is None:
message = 'unexpected packet type %s in %s' % (packet.getType(),
......@@ -125,46 +82,34 @@ class EventHandler(object):
else:
message = 'unexpected packet: %s in %s' % (message,
self.__class__.__name__)
logging.error('%s', message)
logging.error(message)
conn.answer(protocol.protocolError(message), packet.getId())
conn.abort()
self.peerBroken(conn)
def brokenNodeDisallowedError(self, conn, packet, *args):
""" Called when a broken node send packets """
conn.answer(protocol.brokenNodeDisallowedError('go away'), packet.getId())
conn.abort()
def notReadyError(self, conn, packet, *args):
""" Called when the node is not ready """
conn.answer(protocol.notReady('retry later'), packet.getId())
conn.abort()
def protocolError(self, conn, packet, message='', *args):
""" Called for any other protocol error """
conn.answer(protocol.protocolError(message), packet.getId())
conn.abort()
def dispatch(self, conn, packet):
"""This is a helper method to handle various packet types."""
t = packet.getType()
try:
try:
method = self.packet_dispatch_table[t]
method = self.packet_dispatch_table[packet.getType()]
except KeyError:
raise UnexpectedPacketError('no handler found')
args = packet.decode() or ()
method(conn, packet, *args)
except UnexpectedPacketError, e:
self.unexpectedPacket(conn, packet, *e.args)
self._unexpectedPacket(conn, packet, *e.args)
except PacketMalformedError, e:
self.packetMalformed(conn, packet, *e.args)
except BrokenNodeDisallowedError, e:
self.brokenNodeDisallowedError(conn, packet, *e.args)
except NotReadyError, e:
self.notReadyError(conn, packet, *e.args)
except ProtocolError, e:
self.protocolError(conn, packet, *e.args)
self._packetMalformed(conn, packet, *e.args)
except BrokenNodeDisallowedError:
answer_packet = protocol.brokenNodeDisallowedError('go away')
conn.answer(answer_packet, packet.getId())
conn.abort()
except NotReadyError:
conn.answer(protocol.notReady('retry later'), packet.getId())
conn.abort()
except ProtocolError, message:
conn.answer(protocol.protocolError(message), packet.getId())
conn.abort()
def checkClusterName(self, name):
# raise an exception if the fiven name mismatch the current cluster name
......@@ -172,14 +117,55 @@ class EventHandler(object):
logging.error('reject an alien cluster')
raise protocol.ProtocolError('invalid cluster name')
# Packet handlers.
def handleError(self, conn, packet, code, message):
try:
method = self.error_dispatch_table[code]
method(conn, packet, message)
except ValueError:
raise UnexpectedPacketError(message)
# Network level handlers
def packetReceived(self, conn, packet):
"""Called when a packet is received."""
self.dispatch(conn, packet)
def connectionStarted(self, conn):
"""Called when a connection is started."""
logging.debug('connection started for %s:%d', *(conn.getAddress()))
def connectionCompleted(self, conn):
"""Called when a connection is completed."""
logging.debug('connection completed for %s:%d', *(conn.getAddress()))
def connectionFailed(self, conn):
"""Called when a connection failed."""
logging.debug('connection failed for %s:%d', *(conn.getAddress()))
def connectionAccepted(self, conn, connector, addr):
"""Called when a connection is accepted."""
logging.debug('connection accepted from %s:%d', *addr)
new_conn = ServerConnection(conn.getEventManager(), conn.getHandler(),
connector=connector, addr=addr)
# A request for a node identification should arrive.
new_conn.expectMessage(timeout = 10, additional_timeout = 0)
def timeoutExpired(self, conn):
"""Called when a timeout event occurs."""
logging.debug('timeout expired for %s:%d', *(conn.getAddress()))
self.handleConnectionLost(conn, protocol.TEMPORARILY_DOWN_STATE)
def connectionClosed(self, conn):
"""Called when a connection is closed by the peer."""
logging.debug('connection closed for %s:%d', *(conn.getAddress()))
self.handleConnectionLost(conn, protocol.TEMPORARILY_DOWN_STATE)
def peerBroken(self, conn):
"""Called when a peer is broken."""
logging.error('%s:%d is broken', *(conn.getAddress()))
self.handleConnectionLost(conn, protocol.BROKEN_STATE)
def handleConnectionLost(self, conn, new_state):
""" this is a method to override in sub-handlers when there is no need
to make distinction from the kind event that closed the connection """
pass
# Packet handlers.
def handleRequestNodeIdentification(self, conn, packet, node_type,
uuid, address, name):
......@@ -371,8 +357,16 @@ class EventHandler(object):
def handleNotifyLastOID(self, conn, packet, oid):
raise UnexpectedPacketError
# Error packet handlers.
def handleError(self, conn, packet, code, message):
try:
method = self.error_dispatch_table[code]
method(conn, packet, message)
except ValueError:
raise UnexpectedPacketError(message)
def handleNotReady(self, conn, packet, message):
raise UnexpectedPacketError
......@@ -400,6 +394,8 @@ class EventHandler(object):
logging.debug("no error message : %s" % (message))
# Fetch tables initialization
def initPacketDispatchTable(self):
d = {}
......
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