On Wed, Sep 23, 2009 at 5:07 PM, Michael Hanselmann <[email protected]> wrote:
> 2009/9/23 Guido Trotter <[email protected]>:
>> --- a/lib/confd/client.py
>> +++ b/lib/confd/client.py
>> +class ConfdFilterCallback:
>> + def _LogFilter(self, salt, new_reply, old_reply):
>> + if not self._logger:
>> + return
>> +
>> + if new_reply.serial > old_reply.serial:
>> + self._logger.debug("Filtering confirming answer, with newer"
>> + - " serial for query %s" % salt)
>
> TypeError: unsupported operand type(s) for -: 'str' and 'str'
>
>> + elif new_reply.serial == old_reply.serial:
>> + if new_reply.answer != old_reply.answer:
>> + self._logger.debug("Got incoherent answers for query %s"
>> + " (serial: %s)" % (salt, new_reply.serial))
>> + else:
>> + self._logger.debug("Filtering confirming answer, with same"
>> + " serial for query %s" % salt)
>> + else:
>> + self._logger.debug("Filtering outdated answer for query %s"
>> + " serial: (%d < %d)" % (salt, old_reply.serial,
>> + new_reply.serial))
>> +
>> + def _HandleExpire(self, up):
>> + if salt in self._answers:
>> + del self._answers[salt]
>> +
>> + def _HandleReply(self, up):
>> + filter_upcall = False
>> + salt = up.salt
>> + if salt not in self._answers:
>> + self._answers[salt] = up.server_reply
>> + elif up.server_reply.serial > self._answers[salt].serial:
>> + old_answer = self._answers[salt]
>> + self._answers[salt] = up.server_reply
>> + if up.server_reply.answer == old_answer.answer:
>> + filter_upcall = True
>> + self._LogFilter(self, salt, up.server_reply, old_answer)
>> + else:
>> + filter_upcall = True
>> + self._LogFilter(self, salt, up.server_reply, self._answers[salt])
>> +
>> + return filter_upcall
>> +
>> + def __call__(self, up):
>> + filter_upcall = False
>> + if up.type == UPCALL_REPLY:
>> + filter_upcall = self._HandleReply(up)
>> + elif up.type == UPCALL_EXPIRE:
>> + self._HandleExpire(up)
>
> I think a lookup table might make sense here.
>
> try:
> fn = self._handlers[up.type]
> except KeyError:
> raise Exception("Unknown …")
>
> return fn(up)
>
>> + if not filter_upcall:
>> + self._callback(up)
>
Interdiff: (but I think the new _HandleReply looks so ugly):
diff --git a/lib/confd/client.py b/lib/confd/client.py
index cca2ecd..ca4fca9 100644
--- a/lib/confd/client.py
+++ b/lib/confd/client.py
@@ -299,7 +299,7 @@ class ConfdFilterCallback:
if new_reply.serial > old_reply.serial:
self._logger.debug("Filtering confirming answer, with newer"
- - " serial for query %s" % salt)
+ " serial for query %s" % salt)
elif new_reply.serial == old_reply.serial:
if new_reply.answer != old_reply.answer:
self._logger.warning("Got incoherent answers for query %s"
@@ -317,21 +317,29 @@ class ConfdFilterCallback:
del self._answers[salt]
def _HandleReply(self, up):
- filter_upcall = False
+ """Handle a server reply
+
+ @rtype: boolean
+ @return: True, if the reply should be filtered, False if it should be
+ passed on
+ """
salt = up.salt
if salt not in self._answers:
self._answers[salt] = up.server_reply
+ return False
elif up.server_reply.serial > self._answers[salt].serial:
old_answer = self._answers[salt]
self._answers[salt] = up.server_reply
if up.server_reply.answer == old_answer.answer:
- filter_upcall = True
self._LogFilter(salt, up.server_reply, old_answer)
+ return True
+ return False
else:
- filter_upcall = True
self._LogFilter(salt, up.server_reply, self._answers[salt])
+ return True
+
+ return False
- return filter_upcall
def __call__(self, up):
"""Filtering callback