def publish_request(self, ta, service_id, generation, service_props, ttl): try: service = self.sd.publish(service_id, generation, service_props, ttl, self.client_id) if not service.has_prev_generation(): self.debug( "Published new service with id %x, generation %d, " "props %s and TTL %d s." % (service_id, generation, props.to_str(service_props), ttl), LogCategory.CORE) else: log_msg = "Re-published service with id %x. " \ "Generation %d -> %d." \ % (service_id, service.had_generation(), service.generation()) if service.was_orphan(): log_msg += " Replacing orphan." if service.props() != service.had_props(): log_msg += " Properties changed from %s to %s." \ % (props.to_str(service.had_props()), props.to_str(service.props())) if service.ttl() != service.had_ttl(): log_msg += " TTL changed from %d to %d s." \ % (service.had_ttl(), service.ttl()) if service.client_id() != service.had_client_id(): log_msg += " Owner is changed from %x to %x." \ % (service.had_client_id(), service.client_id()) self.debug(log_msg, LogCategory.CORE) yield ta.complete() except sd.PermissionError as e: self.warning( "Permission error while publishing service %x: " "%s." % (service_id, e), LogCategory.SECURITY) yield ta.fail(fail_reason=proto.FAIL_REASON_PERMISSION_DENIED) except sd.ResourceError as e: self.warning( "Resource error while publishing service %x: " "%s." % (service_id, e), LogCategory.SECURITY) yield ta.fail(fail_reason=proto.FAIL_REASON_INSUFFICIENT_RESOURCES) except sd.GenerationError as e: self.warning( "Error while re-publishing service %x: %s." % (service_id, e), LogCategory.CORE) yield ta.fail(fail_reason=proto.FAIL_REASON_OLD_GENERATION) except sd.SameGenerationButDifferentError as e: self.warning( "Error while re-publishing service %x: %s." % (service_id, e), LogCategory.CORE) yield ta.fail( fail_reason=proto.FAIL_REASON_SAME_GENERATION_BUT_DIFFERENT)
def subscription_triggered(self, sub_id, match_type, service): subscription = self.server.sd.get_subscription(sub_id) if subscription.filter is not None: filter_s = "with filter %s" % subscription.filter else: filter_s = "without filter" if match_type == sd.MatchType.DISAPPEARED: service_props = service.had_props() else: service_props = service.props() self.debug( "Subscription id %d %s received %s event by " "service id %x with properties %s." % (sub_id, filter_s, match_type.name, service.service_id, props.to_str(service_props)), LogCategory.CORE) proto_match_type = getattr(proto, "MATCH_TYPE_%s" % match_type.name) ta = self.sub_tas[sub_id] if match_type == sd.MatchType.DISAPPEARED: self.respond(ta.notify(proto_match_type, service.service_id)) else: self.respond( ta.notify(proto_match_type, service.service_id, generation=service.generation(), service_props=service.props(), ttl=service.ttl(), client_id=service.client_id(), orphan_since=service.orphan_since()))
def publish_request(self, ta, service_id, generation, service_props, ttl): try: service = self.sd.publish(service_id, generation, service_props, ttl, self.client_id) if service.before is None: self.debug( "Published new service with id %x, generation %d, " "props %s and TTL %d s." % (service_id, generation, props.to_str(service_props), ttl)) else: log_msg = "Re-published service with id %x. " \ "Generation %d -> %d." \ % (service_id, service.before.generation, service.generation) if service.before.is_orphan(): log_msg += " Replacing orphan." if service.props != service.before.props: log_msg += " Properties changed from %s to %s." \ % (props.to_str(service.before.props), props.to_str(service.props)) if service.ttl != service.before.ttl: log_msg += " TTL changed from %d to %d s." \ % (service.before.ttl, service.ttl) if service.client_id != service.before.client_id: log_msg += " Owner is changed from %x to %x." \ % (service.before.client_id, service.client_id) self.debug(log_msg) yield ta.complete() except sd.PermissionError as e: self.warning("Permission error while publishing service %x: " "%s." % (service_id, e)) yield ta.fail(fail_reason=proto.FAIL_REASON_PERMISSION_DENIED) except sd.ResourceError as e: self.warning("Resource error while publishing service %x: " "%s." % (service_id, e)) yield ta.fail(fail_reason=proto.FAIL_REASON_INSUFFICIENT_RESOURCES) except sd.GenerationError as e: self.warning("Error while re-publishing service %x: %s." % (service_id, e)) yield ta.fail(fail_reason=proto.FAIL_REASON_OLD_GENERATION)
def unpublish_request(self, ta, service_id): try: service_props = self.sd.get_service(service_id).props self.sd.unpublish(service_id, self.client_id) self.debug("Unpublished service %s with service id %x." % (props.to_str(service_props), service_id)) yield ta.complete() except sd.PermissionError as e: self.warning("Permission error while trying to unpublish service " "id %x: %s." % (service_id, e)) reason = proto.FAIL_REASON_PERMISSION_DENIED yield ta.fail(fail_reason=reason) except sd.NotFoundError: self.warning("Attempted to unpublish non-existent service " "id %d." % service_id) reason = proto.FAIL_REASON_NON_EXISTENT_SERVICE_ID yield ta.fail(fail_reason=reason)