Commit 499646f4 authored by Vincent Pelletier's avatar Vincent Pelletier

Change the way PING/PONG are handled:

- PONG is no longer a response (its type value is lower than 0x8000), because it's actually just a "still alive" notification, which happens on demand.
- PING & PONG don't reach the handler level any more, but are directly handled at connection level. There is no point in making them flexible, as a PING really just test that connection is established.
This fixes client's unability to send ping, because of MTClientConnection.ask prototype being incompatible with ClientConnection.ask.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@899 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent b0c3abbe
...@@ -240,7 +240,14 @@ class Connection(BaseConnection): ...@@ -240,7 +240,14 @@ class Connection(BaseConnection):
pass pass
try: try:
self._queue.append(packet) packet_type = packet.getType()
if packet_type == protocol.PING:
# Send a pong notification
self.answer(protocol.pong(), packet)
elif packet_type != protocol.PONG:
# Skip PONG packets, its only purpose is to drop IdleEvent
# generated upong ping.
self._queue.append(packet)
finally: finally:
self.read_buf = self.read_buf[len(packet):] self.read_buf = self.read_buf[len(packet):]
...@@ -400,6 +407,14 @@ class Connection(BaseConnection): ...@@ -400,6 +407,14 @@ class Connection(BaseConnection):
packet.setId(msg_id) packet.setId(msg_id)
self._addPacket(packet) self._addPacket(packet)
def ping(self, timeout=5):
""" Send a ping and expect to receive a pong notification """
packet = protocol.ping()
packet.setId(msg_id)
msg_id = self._getNextId()
self.expectMessage(msg_id, timeout, 0)
self._addPacket(packet)
def isServerConnection(self): def isServerConnection(self):
raise NotImplementedError raise NotImplementedError
......
...@@ -80,8 +80,7 @@ class IdleEvent(object): ...@@ -80,8 +80,7 @@ class IdleEvent(object):
# it to be discarded. # it to be discarded.
self._additional_timeout -= 5 self._additional_timeout -= 5
conn.expectMessage(self._id, 5, self._additional_timeout) conn.expectMessage(self._id, 5, self._additional_timeout)
# Start a keep-alive packet. conn.ping()
conn.ask(protocol.ping(), 5, 0)
else: else:
conn.expectMessage(self._id, self._additional_timeout, 0) conn.expectMessage(self._id, self._additional_timeout, 0)
return True return True
......
...@@ -23,7 +23,7 @@ from neo.protocol import PacketMalformedError, UnexpectedPacketError, \ ...@@ -23,7 +23,7 @@ from neo.protocol import PacketMalformedError, UnexpectedPacketError, \
from neo.connection import ServerConnection from neo.connection import ServerConnection
from protocol import ERROR, REQUEST_NODE_IDENTIFICATION, ACCEPT_NODE_IDENTIFICATION, \ from protocol import ERROR, REQUEST_NODE_IDENTIFICATION, ACCEPT_NODE_IDENTIFICATION, \
PING, PONG, ASK_PRIMARY_MASTER, ANSWER_PRIMARY_MASTER, ANNOUNCE_PRIMARY_MASTER, \ ASK_PRIMARY_MASTER, ANSWER_PRIMARY_MASTER, ANNOUNCE_PRIMARY_MASTER, \
REELECT_PRIMARY_MASTER, NOTIFY_NODE_INFORMATION, START_OPERATION, \ REELECT_PRIMARY_MASTER, NOTIFY_NODE_INFORMATION, START_OPERATION, \
STOP_OPERATION, ASK_LAST_IDS, ANSWER_LAST_IDS, ASK_PARTITION_TABLE, \ STOP_OPERATION, ASK_LAST_IDS, ANSWER_LAST_IDS, ASK_PARTITION_TABLE, \
ANSWER_PARTITION_TABLE, SEND_PARTITION_TABLE, NOTIFY_PARTITION_CHANGES, \ ANSWER_PARTITION_TABLE, SEND_PARTITION_TABLE, NOTIFY_PARTITION_CHANGES, \
...@@ -177,13 +177,6 @@ class EventHandler(object): ...@@ -177,13 +177,6 @@ class EventHandler(object):
uuid, address, num_partitions, num_replicas, your_uuid): uuid, address, num_partitions, num_replicas, your_uuid):
raise UnexpectedPacketError raise UnexpectedPacketError
def handlePing(self, conn, packet):
logging.debug('got a ping packet; am I overloaded?')
conn.answer(protocol.pong(), packet)
def handlePong(self, conn, packet):
pass
def handleAskPrimaryMaster(self, conn, packet): def handleAskPrimaryMaster(self, conn, packet):
raise UnexpectedPacketError raise UnexpectedPacketError
...@@ -393,8 +386,6 @@ class EventHandler(object): ...@@ -393,8 +386,6 @@ class EventHandler(object):
d[ERROR] = self.handleError d[ERROR] = self.handleError
d[REQUEST_NODE_IDENTIFICATION] = self.handleRequestNodeIdentification d[REQUEST_NODE_IDENTIFICATION] = self.handleRequestNodeIdentification
d[ACCEPT_NODE_IDENTIFICATION] = self.handleAcceptNodeIdentification d[ACCEPT_NODE_IDENTIFICATION] = self.handleAcceptNodeIdentification
d[PING] = self.handlePing
d[PONG] = self.handlePong
d[ASK_PRIMARY_MASTER] = self.handleAskPrimaryMaster d[ASK_PRIMARY_MASTER] = self.handleAskPrimaryMaster
d[ANSWER_PRIMARY_MASTER] = self.handleAnswerPrimaryMaster d[ANSWER_PRIMARY_MASTER] = self.handleAnswerPrimaryMaster
d[ANNOUNCE_PRIMARY_MASTER] = self.handleAnnouncePrimaryMaster d[ANNOUNCE_PRIMARY_MASTER] = self.handleAnnouncePrimaryMaster
......
...@@ -101,9 +101,6 @@ packet_types = Enum({ ...@@ -101,9 +101,6 @@ packet_types = Enum({
# Check if a peer is still alive. Any -> Any. # Check if a peer is still alive. Any -> Any.
'PING': 0x0001, 'PING': 0x0001,
# Notify being alive. Any -> Any.
'PONG': 0x8001,
# Request a node identification. This must be the first packet for any connection. # Request a node identification. This must be the first packet for any connection.
# Any -> Any. # Any -> Any.
'REQUEST_NODE_IDENTIFICATION': 0x0002, 'REQUEST_NODE_IDENTIFICATION': 0x0002,
...@@ -302,6 +299,8 @@ packet_types = Enum({ ...@@ -302,6 +299,8 @@ packet_types = Enum({
# Answer state of the cluster # Answer state of the cluster
'ANSWER_CLUSTER_STATE': 0x8028, 'ANSWER_CLUSTER_STATE': 0x8028,
# Notify being alive. Any -> Any.
'PONG': 0x0029,
}) })
# Error codes. # Error codes.
......
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