Commit def8b1ad authored by Grégory Wisniewski's avatar Grégory Wisniewski

Add decorators that implements common patterns used in handlers methods.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@503 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 0d1ac173
......@@ -20,7 +20,7 @@ import logging
from neo.handler import EventHandler
from neo.connection import MTClientConnection
from neo import protocol
from neo.protocol import Packet, UnexpectedPacketError, \
from neo.protocol import Packet, \
MASTER_NODE_TYPE, STORAGE_NODE_TYPE, CLIENT_NODE_TYPE, \
INVALID_UUID, RUNNING_STATE, TEMPORARILY_DOWN_STATE, \
BROKEN_STATE, FEEDING_STATE, DISCARDED_STATE
......@@ -29,6 +29,7 @@ from neo.pt import PartitionTable
from neo.client.exception import NEOStorageError
from neo.exception import ElectionFailure
from neo.util import dump
from neo.handler import identification_required, restrict_node_types
from ZODB.TimeStamp import TimeStamp
from ZODB.utils import p64
......@@ -153,11 +154,9 @@ class PrimaryBoostrapEventHandler(BaseClientEventHandler):
finally:
conn.unlock()
@identification_required
def handleAnswerPrimaryMaster(self, conn, packet, primary_uuid, known_master_list):
uuid = conn.getUUID()
if uuid is None:
raise UnexpectedPacketError
app = self.app
node = app.nm.getNodeByUUID(uuid)
# This must be sent only by master node
......@@ -195,11 +194,9 @@ class PrimaryBoostrapEventHandler(BaseClientEventHandler):
# Whatever the situation is, I trust this master.
app.primary_master_node = primary_node
@identification_required
def handleNotifyNodeInformation(self, conn, packet, node_list):
uuid = conn.getUUID()
if uuid is None:
raise UnexpectedPacketError
app = self.app
nm = app.nm
node = nm.getNodeByUUID(uuid)
......@@ -239,6 +236,7 @@ class PrimaryBoostrapEventHandler(BaseClientEventHandler):
n.setState(state)
@identification_required
def handleSendPartitionTable(self, conn, packet, ptid, row_list):
# This handler is in PrimaryBoostrapEventHandler, since this
# basicaly is an answer to askPrimaryMaster.
......@@ -253,9 +251,6 @@ class PrimaryBoostrapEventHandler(BaseClientEventHandler):
# notifyNodeInformation is valid as asynchrounous event, but
# sendPartitionTable is only triggered after askPrimaryMaster.
uuid = conn.getUUID()
if uuid is None:
raise UnexpectedPacketError
app = self.app
nm = app.nm
pt = app.pt
......
......@@ -40,6 +40,58 @@ from protocol import ERROR, REQUEST_NODE_IDENTIFICATION, ACCEPT_NODE_IDENTIFICAT
PROTOCOL_ERROR_CODE, TIMEOUT_ERROR_CODE, BROKEN_NODE_DISALLOWED_CODE, \
INTERNAL_ERROR_CODE
# Some decorators useful to avoid duplication of patterns in handlers
# FIXME: they may be applied on generic handler
def identification_required(handler):
""" Raise UnexpectedPacketError if the identification has not succeed """
def wrapper(self, conn, packet, *args, **kwargs):
# check if node identification succeed
if conn.getUUID() is None:
raise UnexpectedPacketError
# identified, call the handler
handler(self, conn, packet, *args, **kwargs)
return wrapper
def restrict_node_types(*node_types):
""" Raise UnexpectedPacketError if the node type is node in the supplied
list, if the uuid is None or if the node is not known. This decorator
should be applied after identification_required """
def inner(handler):
def wrapper(self, conn, packet, *args, **kwargs):
# check if node type is allowed
uuid = conn.getUUID()
if uuid is None:
raise UnexpectedPacketError
node = self.app.nm.getNodeByUUID(uuid)
if node is None:
raise UnexpectedPacketError
if node.getNodeType() not in node_types:
raise UnexpectedPacketError
# all is ok, call the handler
handler(self, conn, packet, *args, **kwargs)
return wrapper
return inner
def client_connection_required(handler):
""" Raise UnexpectedPacketError if the packet comes from a client connection """
def wrapper(self, conn, packet, *args, **kwargs):
if conn.isServerConnection():
raise UnexpectedPacketError
# it's a client connection, call the handler
handler(self, conn, packet, *args, **kwargs)
return wrapper
def server_connection_required(handler):
""" Raise UnexpectedPacketError if the packet comes from a server connection """
def wrapper(self, conn, packet, *args, **kwargs):
if not conn.isServerConnection():
raise UnexpectedPacketError
# it's a server connection, call the handler
handler(self, conn, packet, *args, **kwargs)
return wrapper
class EventHandler(object):
"""This class handles events."""
def __init__(self):
......
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