Revision 04cdf663 lib/confd/client.py

b/lib/confd/client.py
432 432

  
433 433
    if not filter_upcall:
434 434
      self._callback(up)
435

  
436

  
437
class ConfdCountingCallback:
438
  """Callback that calls another callback, and counts the answers
439

  
440
  """
441
  def __init__(self, callback, logger=None):
442
    """Constructor for ConfdCountingCallback
443

  
444
    @type callback: f(L{ConfdUpcallPayload})
445
    @param callback: function to call when getting answers
446
    @type logger: logging.Logger
447
    @param logger: optional logger for internal conditions
448

  
449
    """
450
    if not callable(callback):
451
      raise errors.ProgrammerError("callback must be callable")
452

  
453
    self._callback = callback
454
    self._logger = logger
455
    # answers contains a dict of salt -> count
456
    self._answers = {}
457

  
458
  def RegisterQuery(self, salt):
459
    if salt in self._answers:
460
      raise errors.ProgrammerError("query already registered")
461
    self._answers[salt] = 0
462

  
463
  def AllAnswered(self):
464
    """Have all the registered queries received at least an answer?
465

  
466
    """
467
    return utils.all(self._answers.values())
468

  
469
  def _HandleExpire(self, up):
470
    # if we have no answer we have received none, before the expiration.
471
    if up.salt in self._answers:
472
      del self._answers[up.salt]
473

  
474
  def _HandleReply(self, up):
475
    """Handle a single confd reply, and decide whether to filter it.
476

  
477
    @rtype: boolean
478
    @return: True if the reply should be filtered, False if it should be passed
479
             on to the up-callback
480

  
481
    """
482
    if up.salt in self._answers:
483
      self._answers[up.salt] += 1
484

  
485
  def __call__(self, up):
486
    """Filtering callback
487

  
488
    @type up: L{ConfdUpcallPayload}
489
    @param up: upper callback
490

  
491
    """
492
    if up.type == UPCALL_REPLY:
493
      self._HandleReply(up)
494
    elif up.type == UPCALL_EXPIRE:
495
      self._HandleExpire(up)
496
    self._callback(up)

Also available in: Unified diff