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): ...@@ -57,51 +57,8 @@ class EventHandler(object):
# XXX: there is an inconsistency between connection* and handle* names. As # 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' # 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): def _packetMalformed(self, conn, packet, message='', *args):
"""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):
"""Called when a packet is malformed.""" """Called when a packet is malformed."""
args = (conn.getAddress()[0], conn.getAddress()[1], message) args = (conn.getAddress()[0], conn.getAddress()[1], message)
if packet is None: if packet is None:
...@@ -117,7 +74,7 @@ class EventHandler(object): ...@@ -117,7 +74,7 @@ class EventHandler(object):
conn.abort() conn.abort()
self.peerBroken(conn) self.peerBroken(conn)
def unexpectedPacket(self, conn, packet, message=None): def _unexpectedPacket(self, conn, packet, message=None):
"""Handle an unexpected packet.""" """Handle an unexpected packet."""
if message is None: if message is None:
message = 'unexpected packet type %s in %s' % (packet.getType(), message = 'unexpected packet type %s in %s' % (packet.getType(),
...@@ -125,46 +82,34 @@ class EventHandler(object): ...@@ -125,46 +82,34 @@ class EventHandler(object):
else: else:
message = 'unexpected packet: %s in %s' % (message, message = 'unexpected packet: %s in %s' % (message,
self.__class__.__name__) self.__class__.__name__)
logging.error('%s', message) logging.error(message)
conn.answer(protocol.protocolError(message), packet.getId()) conn.answer(protocol.protocolError(message), packet.getId())
conn.abort() conn.abort()
self.peerBroken(conn) 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): def dispatch(self, conn, packet):
"""This is a helper method to handle various packet types.""" """This is a helper method to handle various packet types."""
t = packet.getType()
try: try:
try: try:
method = self.packet_dispatch_table[t] method = self.packet_dispatch_table[packet.getType()]
except KeyError: except KeyError:
raise UnexpectedPacketError('no handler found') raise UnexpectedPacketError('no handler found')
args = packet.decode() or () args = packet.decode() or ()
method(conn, packet, *args) method(conn, packet, *args)
except UnexpectedPacketError, e: except UnexpectedPacketError, e:
self.unexpectedPacket(conn, packet, *e.args) self._unexpectedPacket(conn, packet, *e.args)
except PacketMalformedError, e: except PacketMalformedError, e:
self.packetMalformed(conn, packet, *e.args) self._packetMalformed(conn, packet, *e.args)
except BrokenNodeDisallowedError, e: except BrokenNodeDisallowedError:
self.brokenNodeDisallowedError(conn, packet, *e.args) answer_packet = protocol.brokenNodeDisallowedError('go away')
except NotReadyError, e: conn.answer(answer_packet, packet.getId())
self.notReadyError(conn, packet, *e.args) conn.abort()
except ProtocolError, e: except NotReadyError:
self.protocolError(conn, packet, *e.args) 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): def checkClusterName(self, name):
# raise an exception if the fiven name mismatch the current cluster name # raise an exception if the fiven name mismatch the current cluster name
...@@ -172,14 +117,55 @@ class EventHandler(object): ...@@ -172,14 +117,55 @@ class EventHandler(object):
logging.error('reject an alien cluster') logging.error('reject an alien cluster')
raise protocol.ProtocolError('invalid cluster name') raise protocol.ProtocolError('invalid cluster name')
# Packet handlers.
def handleError(self, conn, packet, code, message): # Network level handlers
try:
method = self.error_dispatch_table[code] def packetReceived(self, conn, packet):
method(conn, packet, message) """Called when a packet is received."""
except ValueError: self.dispatch(conn, packet)
raise UnexpectedPacketError(message)
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, def handleRequestNodeIdentification(self, conn, packet, node_type,
uuid, address, name): uuid, address, name):
...@@ -371,8 +357,16 @@ class EventHandler(object): ...@@ -371,8 +357,16 @@ class EventHandler(object):
def handleNotifyLastOID(self, conn, packet, oid): def handleNotifyLastOID(self, conn, packet, oid):
raise UnexpectedPacketError raise UnexpectedPacketError
# Error packet handlers. # 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): def handleNotReady(self, conn, packet, message):
raise UnexpectedPacketError raise UnexpectedPacketError
...@@ -400,6 +394,8 @@ class EventHandler(object): ...@@ -400,6 +394,8 @@ class EventHandler(object):
logging.debug("no error message : %s" % (message)) logging.debug("no error message : %s" % (message))
# Fetch tables initialization
def initPacketDispatchTable(self): def initPacketDispatchTable(self):
d = {} 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