def _ensure_connected(self): if self._socket is None: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setblocking(1) try: s.connect(("", self.port)) except socket.error as e: with excutils.save_and_reraise_exception(): s.close() if e.errno in (errno.ECONNREFUSED, errno.ENOTCONN, errno.ECONNRESET): # Don't bother with further connections... self.dead = True read_pipe = s.makefile("rb", 0) write_pipe = s.makefile("wb", 0) try: msg = self._do_recv(read_pipe=read_pipe) su.schema_validate(msg, SCHEMAS[CHALLENGE]) if msg != CHALLENGE: raise IOError("Challenge expected not received") else: pieces = _encode_message(self.auth_key, CHALLENGE_RESPONSE, self.identity) self._do_send_and_ack(pieces, write_pipe=write_pipe, read_pipe=read_pipe) except Exception: with excutils.save_and_reraise_exception(): s.close() else: self._socket = s self._read_pipe = read_pipe self._write_pipe = write_pipe
def _do_send_and_ack(self, pieces, write_pipe=None, read_pipe=None): self._do_send(pieces, write_pipe=write_pipe) self._sent += 1 msg = self._do_recv(read_pipe=read_pipe) su.schema_validate(msg, SCHEMAS[ACK]) if msg != ACK: raise IOError("Failed receiving ack for sent" " message %s" % self._metrics['sent'])
def loads(cls, blob): data = misc.decode_json(blob) try: su.schema_validate(data, cls.SCHEMA) except su.ValidationError as e: raise excp.InvalidFormat( "%s data not of the" " expected format: %s" % (cls.__name__, e.message), e) else: return (data['topic'], data['tasks'])
def validate(cls, data): try: su.schema_validate(data, cls.SCHEMA) except su.ValidationError as e: cls_name = reflection.get_class_name(cls, fully_qualified=False) excp.raise_with_cause(excp.InvalidFormat, "%s message response data not of the" " expected format: %s" % (cls_name, e.message), cause=e) else: state = data['state'] if state == FAILURE and 'result' in data: ft.Failure.validate(data['result'])
def validate(cls, data, response): if response: schema = cls.RESPONSE_SCHEMA else: schema = cls.SENDER_SCHEMA try: su.schema_validate(data, schema) except su.ValidationError as e: cls_name = reflection.get_class_name(cls, fully_qualified=False) if response: excp.raise_with_cause(excp.InvalidFormat, "%s message response data not of the" " expected format: %s" % (cls_name, e.message), cause=e) else: excp.raise_with_cause(excp.InvalidFormat, "%s message sender data not of the" " expected format: %s" % (cls_name, e.message), cause=e)
def validate(cls, data): try: su.schema_validate(data, cls.SCHEMA) except su.ValidationError as e: cls_name = reflection.get_class_name(cls, fully_qualified=False) excp.raise_with_cause(excp.InvalidFormat, "%s message response data not of the" " expected format: %s" % (cls_name, e.message), cause=e) else: # Validate all failure dictionaries that *may* be present... failures = [] if 'failures' in data: failures.extend(six.itervalues(data['failures'])) result = data.get('result') if result is not None: result_data_type, result_data = result if result_data_type == 'failure': failures.append(result_data) for fail_data in failures: ft.Failure.validate(fail_data)
def validate(cls, data): """Validate input data matches expected failure ``dict`` format.""" try: su.schema_validate(data, cls.SCHEMA) except su.ValidationError as e: raise exc.InvalidFormat("Failure data not of the" " expected format: %s" % (e.message), e) else: # Ensure that all 'exc_type_names' originate from one of # BASE_EXCEPTIONS, because those are the root exceptions that # python mandates/provides and anything else is invalid... causes = collections.deque([data]) while causes: cause = causes.popleft() root_exc_type = cause['exc_type_names'][-1] if root_exc_type not in cls.BASE_EXCEPTIONS: raise exc.InvalidFormat( "Failure data 'exc_type_names' must" " have an initial exception type that is one" " of %s types: '%s' is not one of those" " types" % (cls.BASE_EXCEPTIONS, root_exc_type)) sub_causes = cause.get('causes') if sub_causes: causes.extend(sub_causes)
def _dispatch(self, from_who, msg_decoder_func): if not self.challenge_responded: msg = msg_decoder_func() su.schema_validate(msg, SCHEMAS[CHALLENGE_RESPONSE]) if msg != CHALLENGE_RESPONSE: raise ChallengeIgnored("Discarding connection from %s" " challenge was not responded to" % self.addr) else: LOG.trace("Peer %s (%s) has passed challenge sequence", self.addr, from_who) self.challenge_responded = True self.tied_to = from_who self._send_ack() else: if self.tied_to != from_who: raise UnknownSender("Sender %s previously identified as %s" " changed there identity to %s after" " challenge sequence" % (self.addr, self.tied_to, from_who)) try: task = self.targets[from_who] except KeyError: raise UnknownSender("Unknown message from %s (%s) not matched" " to any known target" % (self.addr, from_who)) msg = msg_decoder_func() su.schema_validate(msg, SCHEMAS[EVENT]) if LOG.isEnabledFor(logging.TRACE): msg_delay = max(0, time.time() - msg['sent_on']) LOG.trace("Dispatching message from %s (%s) (it took %0.3f" " seconds for it to arrive for processing after" " being sent)", self.addr, from_who, msg_delay) task.notifier.notify(msg['event_type'], msg.get('details')) self._send_ack()