Commit 843f4728 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Use '__call__' instead of '__str__' to encode a packet.

Fix related tests.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@1699 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 5b9c1123
...@@ -365,7 +365,7 @@ class Connection(BaseConnection): ...@@ -365,7 +365,7 @@ class Connection(BaseConnection):
PACKET_LOGGER.dispatch(self, packet, ' to ') PACKET_LOGGER.dispatch(self, packet, ' to ')
try: try:
self.write_buf += str(packet) self.write_buf += packet()
except PacketMalformedError: except PacketMalformedError:
logging.critical('trying to send a too big message') logging.critical('trying to send a too big message')
raise raise
......
...@@ -253,7 +253,8 @@ class Packet(object): ...@@ -253,7 +253,8 @@ class Packet(object):
def getType(self): def getType(self):
return self.__class__ return self.__class__
def __str__(self): def __call__(self):
""" Encode a packet as a string to send it over the network """
content = self._body content = self._body
length = PACKET_HEADER_SIZE + len(content) length = PACKET_HEADER_SIZE + len(content)
return pack('!LHL', self.getId(), self._code, length) + content return pack('!LHL', self.getId(), self._code, length) + content
......
...@@ -393,7 +393,7 @@ class ConnectionTests(NeoTestBase): ...@@ -393,7 +393,7 @@ class ConnectionTests(NeoTestBase):
def test_07_Connection_addPacket(self): def test_07_Connection_addPacket(self):
# no connector # no connector
p = Mock({"__str__" : "testdata"}) p = Mock({"__call__" : "testdata"})
em = Mock() em = Mock()
handler = Mock() handler = Mock()
bc = Connection(em, handler, connector_handler=DoNothingConnector, bc = Connection(em, handler, connector_handler=DoNothingConnector,
...@@ -468,8 +468,7 @@ class ConnectionTests(NeoTestBase): ...@@ -468,8 +468,7 @@ class ConnectionTests(NeoTestBase):
(("127.0.0.1", 2132), self.getNewUUID())) (("127.0.0.1", 2132), self.getNewUUID()))
p = Packets.AnswerPrimary(self.getNewUUID(), master_list) p = Packets.AnswerPrimary(self.getNewUUID(), master_list)
p.setId(1) p.setId(1)
data = str(p) bc.read_buf += p()
bc.read_buf += data
self.assertEqual(len(bc.event_dict), 0) self.assertEqual(len(bc.event_dict), 0)
bc.analyse() bc.analyse()
# check packet decoded # check packet decoded
...@@ -501,8 +500,7 @@ class ConnectionTests(NeoTestBase): ...@@ -501,8 +500,7 @@ class ConnectionTests(NeoTestBase):
(("127.0.0.1", 2132), self.getNewUUID())) (("127.0.0.1", 2132), self.getNewUUID()))
p1 = Packets.AnswerPrimary(self.getNewUUID(), master_list) p1 = Packets.AnswerPrimary(self.getNewUUID(), master_list)
p1.setId(1) p1.setId(1)
data = str(p1) bc.read_buf += p1()
bc.read_buf += data
# packet 2 # packet 2
master_list = ( master_list = (
(("127.0.0.1", 2135), self.getNewUUID()), (("127.0.0.1", 2135), self.getNewUUID()),
...@@ -515,8 +513,7 @@ class ConnectionTests(NeoTestBase): ...@@ -515,8 +513,7 @@ class ConnectionTests(NeoTestBase):
(("127.0.0.1", 2132), self.getNewUUID())) (("127.0.0.1", 2132), self.getNewUUID()))
p2 = Packets.AnswerPrimary( self.getNewUUID(), master_list) p2 = Packets.AnswerPrimary( self.getNewUUID(), master_list)
p2.setId(2) p2.setId(2)
data = str(p2) bc.read_buf += p2()
bc.read_buf += data
self.assertEqual(len(bc.read_buf), len(p1) + len(p2)) self.assertEqual(len(bc.read_buf), len(p1) + len(p2))
self.assertEqual(len(bc.event_dict), 0) self.assertEqual(len(bc.event_dict), 0)
bc.analyse() bc.analyse()
...@@ -570,8 +567,7 @@ class ConnectionTests(NeoTestBase): ...@@ -570,8 +567,7 @@ class ConnectionTests(NeoTestBase):
(("127.0.0.1", 2132), self.getNewUUID())) (("127.0.0.1", 2132), self.getNewUUID()))
p = Packets.AnswerPrimary(self.getNewUUID(), master_list) p = Packets.AnswerPrimary(self.getNewUUID(), master_list)
p.setId(1) p.setId(1)
data = str(p) bc.read_buf += p()
bc.read_buf += data
self.assertEqual(len(bc.event_dict), 0) self.assertEqual(len(bc.event_dict), 0)
bc.expectMessage(1) bc.expectMessage(1)
self.assertEqual(len(bc.event_dict), 1) self.assertEqual(len(bc.event_dict), 1)
...@@ -701,7 +697,7 @@ class ConnectionTests(NeoTestBase): ...@@ -701,7 +697,7 @@ class ConnectionTests(NeoTestBase):
uuid = self.getNewUUID() uuid = self.getNewUUID()
p = Packets.AnswerPrimary(uuid, master_list) p = Packets.AnswerPrimary(uuid, master_list)
p.setId(1) p.setId(1)
return str(p) return p()
DoNothingConnector.receive = receive DoNothingConnector.receive = receive
connector = DoNothingConnector() connector = DoNothingConnector()
bc = Connection(em, handler, connector_handler=DoNothingConnector, bc = Connection(em, handler, connector_handler=DoNothingConnector,
......
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