Commit c4863a33 authored by Julien Muchembled's avatar Julien Muchembled

Simplify EventHandler by removing 'packet_dispatch_table'

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2685 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 7b76e88b
This diff is collapsed.
......@@ -28,7 +28,6 @@ class PacketLogger(object):
def __init__(self):
_temp = EventHandler(None)
self.packet_dispatch_table = _temp.packet_dispatch_table
self.error_dispatch_table = _temp.error_dispatch_table
self.enable(LOGGER_ENABLED)
......@@ -38,7 +37,6 @@ class PacketLogger(object):
def _dispatch(self, conn, packet, direction):
"""This is a helper method to handle various packet types."""
# default log message
klass = packet.getType()
uuid = dump(conn.getUUID())
ip, port = conn.getAddress()
packet_name = packet.__class__.__name__
......@@ -47,8 +45,7 @@ class PacketLogger(object):
neo.lib.logging.debug('#0x%08x %-30s %s %s (%s:%d)', packet.getId(),
packet_name, direction, uuid, ip, port)
# look for custom packet logger
logger = self.packet_dispatch_table.get(klass, None)
logger = logger and getattr(self, logger.im_func.__name__, None)
logger = getattr(self, packet.handler_method_name, None)
if logger is None:
return
# enhanced log
......
......@@ -18,7 +18,6 @@
import socket
import sys
import traceback
from types import ClassType
from socket import inet_ntoa, inet_aton
from cStringIO import StringIO
from struct import Struct
......@@ -673,6 +672,9 @@ class RequestIdentification(Packet):
args.insert(0, PROTOCOL_VERSION)
super(RequestIdentification, self).__init__(*args, **kw)
def decode(self):
return super(RequestIdentification, self).decode()[1:]
class PrimaryMaster(Packet):
"""
Ask a current primary master node. This must be the second message when
......@@ -1355,7 +1357,7 @@ def register(code, request, ignore_when_closed=None):
if answer in (Error, None):
return request
# build a class for the answer
answer = ClassType('Answer%s' % (request.__name__, ), (Packet, ), {})
answer = type('Answer%s' % (request.__name__, ), (Packet, ), {})
answer._fmt = request._answer
# compute the answer code
code = code | RESPONSE_MASK
......@@ -1384,14 +1386,16 @@ class ParserState(object):
def clear(self):
self.payload = None
class PacketRegistry(dict):
class Packets(dict):
"""
Packet registry that check packet code unicity and provide an index
"""
def __init__(self):
dict.__init__(self)
# load packet classes
self.update(StaticRegistry)
def __metaclass__(name, base, d):
for k, v in d.iteritems():
if isinstance(v, type) and issubclass(v, Packet):
v.handler_method_name = k[0].lower() + k[1:]
# this builds a "singleton"
return type('PacketRegistry', base, d)(StaticRegistry)
def parse(self, buf, state_container):
state = state_container.get()
......@@ -1531,9 +1535,6 @@ class PacketRegistry(dict):
NotifyTransactionFinished = register(
0x003E, NotifyTransactionFinished)
# build a "singleton"
Packets = PacketRegistry()
def register_error(code):
def wrapper(registry, message=''):
return Error(code, message)
......
......@@ -373,6 +373,7 @@ class ConnectionTests(NeoUnitTestBase):
def test_07_Connection_addPacket(self):
# new packet
p = Mock({"encode" : "testdata"})
p.handler_method_name = 'testmethod'
bc = self._makeConnection()
self._checkWriteBuf(bc, '')
bc._addPacket(p)
......
......@@ -28,22 +28,17 @@ class HandlerTests(NeoUnitTestBase):
NeoUnitTestBase.setUp(self)
app = Mock()
self.handler = EventHandler(app)
self.fake_type = 'FAKE_PACKET_TYPE'
def setFakeMethod(self, method):
self.handler.packet_dispatch_table[self.fake_type] = method
self.handler.fake_method = method
def getFakePacket(self):
return Mock({
'getType': self.fake_type,
p = Mock({
'decode': (),
'__repr__': 'Fake Packet',
})
def checkFakeCalled(self):
method = self.handler.packet_dispatch_table[self.fake_type]
calls = method.getNamedCalls('__call__')
self.assertEquals(len(calls), 1)
p.handler_method_name = 'fake_method'
return p
def test_dispatch(self):
conn = self.getFakeConnection()
......
......@@ -73,7 +73,7 @@ class ProtocolTests(NeoUnitTestBase):
uuid = self.getNewUUID()
p = Packets.RequestIdentification(NodeTypes.CLIENT,
uuid, (self.local_ip, 9080), "unittest")
(plow, phigh), node, p_uuid, (ip, port), name = p.decode()
node, p_uuid, (ip, port), name = p.decode()
self.assertEqual(node, NodeTypes.CLIENT)
self.assertEqual(p_uuid, uuid)
self.assertEqual(ip, self.local_ip)
......@@ -85,7 +85,7 @@ class ProtocolTests(NeoUnitTestBase):
self.local_ip = IP_VERSION_FORMAT_DICT[socket.AF_INET6]
p = Packets.RequestIdentification(NodeTypes.CLIENT,
uuid, (self.local_ip, 9080), "unittest")
(plow, phigh), node, p_uuid, (ip, port), name = p.decode()
node, p_uuid, (ip, port), name = p.decode()
self.assertEqual(node, NodeTypes.CLIENT)
self.assertEqual(p_uuid, uuid)
self.assertEqual(ip, self.local_ip)
......
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