Commit 5529b30f authored by Grégory Wisniewski's avatar Grégory Wisniewski

New protocol parser, semantic oriented.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2652 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 6ffb55d5
...@@ -160,7 +160,7 @@ class PrimaryAnswersHandler(AnswerBaseHandler): ...@@ -160,7 +160,7 @@ class PrimaryAnswersHandler(AnswerBaseHandler):
self.app.setHandlerData(ttid) self.app.setHandlerData(ttid)
def answerNewOIDs(self, conn, oid_list): def answerNewOIDs(self, conn, oid_list):
self.app.new_oid_list = oid_list self.app.new_oid_list = list(oid_list)
def answerTransactionFinished(self, conn, _, tid): def answerTransactionFinished(self, conn, _, tid):
self.app.setHandlerData(tid) self.app.setHandlerData(tid)
......
...@@ -133,10 +133,13 @@ class EventHandler(object): ...@@ -133,10 +133,13 @@ class EventHandler(object):
def notify(self, conn, message): def notify(self, conn, message):
neo.lib.logging.info('notification from %r: %s', conn, message) neo.lib.logging.info('notification from %r: %s', conn, message)
def requestIdentification(self, conn, node_type, def requestIdentification(self, conn, node_type, uuid, address, name):
uuid, address, name):
raise UnexpectedPacketError raise UnexpectedPacketError
def _requestIdentification(self, conn, protocol, node_type,
uuid, address, name):
self.requestIdentification(conn, node_type, uuid, address, name)
def acceptIdentification(self, conn, node_type, def acceptIdentification(self, conn, node_type,
uuid, num_partitions, num_replicas, your_uuid): uuid, num_partitions, num_replicas, your_uuid):
raise UnexpectedPacketError raise UnexpectedPacketError
...@@ -428,7 +431,7 @@ class EventHandler(object): ...@@ -428,7 +431,7 @@ class EventHandler(object):
d[Packets.Error] = self.error d[Packets.Error] = self.error
d[Packets.Notify] = self.notify d[Packets.Notify] = self.notify
d[Packets.RequestIdentification] = self.requestIdentification d[Packets.RequestIdentification] = self._requestIdentification
d[Packets.AcceptIdentification] = self.acceptIdentification d[Packets.AcceptIdentification] = self.acceptIdentification
d[Packets.AskPrimary] = self.askPrimary d[Packets.AskPrimary] = self.askPrimary
d[Packets.AnswerPrimary] = self.answerPrimary d[Packets.AnswerPrimary] = self.answerPrimary
......
...@@ -37,8 +37,11 @@ class PacketLogger(object): ...@@ -37,8 +37,11 @@ class PacketLogger(object):
klass = packet.getType() klass = packet.getType()
uuid = dump(conn.getUUID()) uuid = dump(conn.getUUID())
ip, port = conn.getAddress() ip, port = conn.getAddress()
packet_name = packet.__class__.__name__
if packet.isResponse() and packet._request is not None:
packet_name += packet._request.__name__
neo.lib.logging.debug('#0x%08x %-30s %s %s (%s:%d)', packet.getId(), neo.lib.logging.debug('#0x%08x %-30s %s %s (%s:%d)', packet.getId(),
packet.__class__.__name__, direction, uuid, ip, port) packet_name, direction, uuid, ip, port)
# look for custom packet logger # look for custom packet logger
logger = self.packet_dispatch_table.get(klass, None) logger = self.packet_dispatch_table.get(klass, None)
logger = logger and getattr(self, logger.im_func.__name__, None) logger = logger and getattr(self, logger.im_func.__name__, None)
......
This diff is collapsed.
...@@ -22,6 +22,23 @@ from zlib import adler32 ...@@ -22,6 +22,23 @@ from zlib import adler32
from Queue import deque from Queue import deque
from struct import pack, unpack from struct import pack, unpack
try:
from struct import Struct
except ImportError:
import struct
# support for python 2.4
class Struct(object):
def __init__(self, fmt):
self._fmt = fmt
self.size = struct.calcsize(fmt)
def pack(self, *args):
return struct.pack(self._fmt, *args)
def unpack(self, *args):
return struct.unpack(self._fmt, *args)
def u64(s): def u64(s):
return unpack('!Q', s)[0] return unpack('!Q', s)[0]
......
...@@ -70,9 +70,9 @@ class ProtocolTests(NeoUnitTestBase): ...@@ -70,9 +70,9 @@ class ProtocolTests(NeoUnitTestBase):
def test_11_RequestIdentification(self): def test_11_RequestIdentification(self):
uuid = self.getNewUUID() uuid = self.getNewUUID()
p = Packets.RequestIdentification(NodeTypes.CLIENT, uuid, p = Packets.RequestIdentification(NodeTypes.CLIENT,
("127.0.0.1", 9080), "unittest") uuid, ("127.0.0.1", 9080), "unittest")
node, p_uuid, (ip, port), name = p.decode() (plow, phigh), node, p_uuid, (ip, port), name = p.decode()
self.assertEqual(node, NodeTypes.CLIENT) self.assertEqual(node, NodeTypes.CLIENT)
self.assertEqual(p_uuid, uuid) self.assertEqual(p_uuid, uuid)
self.assertEqual(ip, "127.0.0.1") self.assertEqual(ip, "127.0.0.1")
...@@ -148,9 +148,11 @@ class ProtocolTests(NeoUnitTestBase): ...@@ -148,9 +148,11 @@ class ProtocolTests(NeoUnitTestBase):
uuid1 = self.getNewUUID() uuid1 = self.getNewUUID()
uuid2 = self.getNewUUID() uuid2 = self.getNewUUID()
uuid3 = self.getNewUUID() uuid3 = self.getNewUUID()
cell_list = [(0, ((uuid1, CellStates.UP_TO_DATE), (uuid2, CellStates.OUT_OF_DATE))), cell_list = [
(43, ((uuid2, CellStates.OUT_OF_DATE),(uuid3, CellStates.DISCARDED))), (0, [(uuid1, CellStates.UP_TO_DATE), (uuid2, CellStates.OUT_OF_DATE)]),
(124, ((uuid1, CellStates.DISCARDED), (uuid3, CellStates.UP_TO_DATE)))] (43, [(uuid2, CellStates.OUT_OF_DATE), (uuid3, CellStates.DISCARDED)]),
(124, [(uuid1, CellStates.DISCARDED), (uuid3, CellStates.UP_TO_DATE)]),
]
p = Packets.AnswerPartitionTable(ptid, cell_list) p = Packets.AnswerPartitionTable(ptid, cell_list)
pptid, p_cell_list = p.decode() pptid, p_cell_list = p.decode()
self.assertEqual(pptid, ptid) self.assertEqual(pptid, ptid)
...@@ -161,9 +163,11 @@ class ProtocolTests(NeoUnitTestBase): ...@@ -161,9 +163,11 @@ class ProtocolTests(NeoUnitTestBase):
uuid1 = self.getNewUUID() uuid1 = self.getNewUUID()
uuid2 = self.getNewUUID() uuid2 = self.getNewUUID()
uuid3 = self.getNewUUID() uuid3 = self.getNewUUID()
cell_list = [(0, ((uuid1, CellStates.UP_TO_DATE), (uuid2, CellStates.OUT_OF_DATE))), cell_list = [
(43, ((uuid2, CellStates.OUT_OF_DATE),(uuid3, CellStates.DISCARDED))), (0, [(uuid1, CellStates.UP_TO_DATE), (uuid2, CellStates.OUT_OF_DATE)]),
(124, ((uuid1, CellStates.DISCARDED), (uuid3, CellStates.UP_TO_DATE)))] (43, [(uuid2, CellStates.OUT_OF_DATE), (uuid3, CellStates.DISCARDED)]),
(124, [(uuid1, CellStates.DISCARDED), (uuid3, CellStates.UP_TO_DATE)]),
]
p = Packets.AnswerPartitionTable(ptid, cell_list) p = Packets.AnswerPartitionTable(ptid, cell_list)
pptid, p_cell_list = p.decode() pptid, p_cell_list = p.decode()
self.assertEqual(pptid, ptid) self.assertEqual(pptid, ptid)
...@@ -176,8 +180,7 @@ class ProtocolTests(NeoUnitTestBase): ...@@ -176,8 +180,7 @@ class ProtocolTests(NeoUnitTestBase):
cell_list = [(0, uuid1, CellStates.UP_TO_DATE), cell_list = [(0, uuid1, CellStates.UP_TO_DATE),
(43, uuid2, CellStates.OUT_OF_DATE), (43, uuid2, CellStates.OUT_OF_DATE),
(124, uuid1, CellStates.DISCARDED)] (124, uuid1, CellStates.DISCARDED)]
p = Packets.NotifyPartitionChanges(ptid, p = Packets.NotifyPartitionChanges(ptid, cell_list)
cell_list)
pptid, p_cell_list = p.decode() pptid, p_cell_list = p.decode()
self.assertEqual(pptid, ptid) self.assertEqual(pptid, ptid)
self.assertEqual(p_cell_list, cell_list) self.assertEqual(p_cell_list, cell_list)
...@@ -235,7 +238,6 @@ class ProtocolTests(NeoUnitTestBase): ...@@ -235,7 +238,6 @@ class ProtocolTests(NeoUnitTestBase):
ptid = p.decode()[0] ptid = p.decode()[0]
self.assertEqual(ptid, tid) self.assertEqual(ptid, tid)
def test_32_askBeginTransaction(self): def test_32_askBeginTransaction(self):
tid = self.getNextTID() tid = self.getNextTID()
p = Packets.AskBeginTransaction(tid) p = Packets.AskBeginTransaction(tid)
...@@ -370,11 +372,11 @@ class ProtocolTests(NeoUnitTestBase): ...@@ -370,11 +372,11 @@ class ProtocolTests(NeoUnitTestBase):
def test_46_answerStoreObject(self): def test_46_answerStoreObject(self):
oid = self.getNextTID() oid = self.getNextTID()
serial = self.getNextTID() serial = self.getNextTID()
p = Packets.AnswerStoreObject(1, oid, serial) p = Packets.AnswerStoreObject(True, oid, serial)
conflicting, poid, pserial = p.decode() conflicting, poid, pserial = p.decode()
self.assertEqual(oid, poid) self.assertEqual(oid, poid)
self.assertEqual(serial, pserial) self.assertEqual(serial, pserial)
self.assertEqual(conflicting, 1) self.assertTrue(conflicting)
def test_47_askObject(self): def test_47_askObject(self):
oid = self.getNextTID() oid = self.getNextTID()
...@@ -532,7 +534,7 @@ class ProtocolTests(NeoUnitTestBase): ...@@ -532,7 +534,7 @@ class ProtocolTests(NeoUnitTestBase):
def test_AddPendingNodes(self): def test_AddPendingNodes(self):
uuid1, uuid2 = self.getNewUUID(), self.getNewUUID() uuid1, uuid2 = self.getNewUUID(), self.getNewUUID()
p = Packets.AddPendingNodes([uuid1, uuid2]) p = Packets.AddPendingNodes((uuid1, uuid2))
self.assertEqual(p.decode(), ([uuid1, uuid2], )) self.assertEqual(p.decode(), ([uuid1, uuid2], ))
def test_SetNodeState(self): def test_SetNodeState(self):
...@@ -551,7 +553,7 @@ class ProtocolTests(NeoUnitTestBase): ...@@ -551,7 +553,7 @@ class ProtocolTests(NeoUnitTestBase):
self.getNewUUID(), NodeStates.DOWN) self.getNewUUID(), NodeStates.DOWN)
node2 = (NodeTypes.MASTER, ('127.0.0.1', 2000), node2 = (NodeTypes.MASTER, ('127.0.0.1', 2000),
self.getNewUUID(), NodeStates.RUNNING) self.getNewUUID(), NodeStates.RUNNING)
p = Packets.AnswerNodeList([node1, node2]) p = Packets.AnswerNodeList((node1, node2))
self.assertEqual(p.decode(), ([node1, node2], )) self.assertEqual(p.decode(), ([node1, node2], ))
def test_AskPartitionList(self): def test_AskPartitionList(self):
...@@ -564,14 +566,14 @@ class ProtocolTests(NeoUnitTestBase): ...@@ -564,14 +566,14 @@ class ProtocolTests(NeoUnitTestBase):
def test_AnswerPartitionList(self): def test_AnswerPartitionList(self):
ptid = self.getPTID(1) ptid = self.getPTID(1)
row_list = [ row_list = [
(0, ( (0, [
(self.getNewUUID(), CellStates.UP_TO_DATE), (self.getNewUUID(), CellStates.UP_TO_DATE),
(self.getNewUUID(), CellStates.OUT_OF_DATE), (self.getNewUUID(), CellStates.OUT_OF_DATE),
)), ]),
(1, ( (1, [
(self.getNewUUID(), CellStates.FEEDING), (self.getNewUUID(), CellStates.FEEDING),
(self.getNewUUID(), CellStates.DISCARDED), (self.getNewUUID(), CellStates.DISCARDED),
)), ]),
] ]
p = Packets.AnswerPartitionList(ptid, row_list) p = Packets.AnswerPartitionList(ptid, row_list)
self.assertEqual(p.decode(), (ptid, row_list)) self.assertEqual(p.decode(), (ptid, row_list))
......
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