def _sendKeyInterest(self, interest, timeSlot, onEncryptedKeys, onError): """ Send an interest with the given name through the face with callbacks to _handleCoveringKey, _handleTimeout and _handleNetworkNack. :param Interest interest: The interest to send. :param float timeSlot: The time slot, passed to _handleCoveringKey, _handleTimeout and _handleNetworkNack. :param onEncryptedKeys: The OnEncryptedKeys callback, passed to _handleCoveringKey, _handleTimeout and _handleNetworkNack. :type onEncryptedKeys: function object :param onError: This calls onError(errorCode, message) for an error. :type onError: function object """ def onKey(interest, data): self._handleCoveringKey(interest, data, timeSlot, onEncryptedKeys, onError) def onTimeout(interest): self._handleTimeout(interest, timeSlot, onEncryptedKeys, onError) def onNetworkNack(interest, networkNack): self._handleNetworkNack(interest, networkNack, timeSlot, onEncryptedKeys, onError) if self._keyRetrievalLink.getDelegations().size() == 0: # We can use the supplied interest without copying. request = interest else: # Copy the supplied interest and add the Link. request = Interest(interest) # This will use a cached encoding if available. request.setLinkWireEncoding(self._keyRetrievalLink.wireEncode()) self._face.expressInterest(request, onKey, onTimeout, onNetworkNack)
def _sendInterest(self, interest, nRetrials, link, onVerified, onError): """ Express the interest, call verifyData for the fetched Data packet and call onVerified if verify succeeds. If verify fails, call onError(EncryptError.ErrorCode.Validation, "verifyData failed"). If the interest times out, re-express nRetrials times. If the interest times out nRetrials times, or for a network Nack, call onError(EncryptError.ErrorCode.DataRetrievalFailure, interest.getName().toUri()). :param Interest interest: The Interest to express. :param int nRetrials: The number of retrials left after a timeout. :param Link link: The Link object to use in the Interest. This does not make a copy of the Link object. If the Link object's getDelegations().size() is zero, don't use it. :param onVerified: When the fetched Data packet validation succeeds, this calls onVerified(data). :type onVerified: function object :param onError: This calls onError(errorCode, message) for an error, where errorCode is from EncryptError.ErrorCode and message is a str. :type onError: function object """ # Prepare the callback functions. def onData(contentInterest, contentData): # The Interest has no selectors, so assume the library correctly # matched with the Data name before calling onData. try: self._keyChain.verifyData( contentData, onVerified, lambda d, reason: Consumer._callOnError( onError, EncryptError.ErrorCode.Validation, "verifyData failed. Reason: " + reason)) except Exception as ex: try: onError(EncryptError.ErrorCode.General, "verifyData error: " + repr(ex)) except: logging.exception("Error in onError") def onNetworkNack(interest, networkNack): # We have run out of options. Report a retrieval failure. try: onError(EncryptError.ErrorCode.DataRetrievalFailure, interest.getName().toUri()) except: logging.exception("Error in onError") def onTimeout(interest): if nRetrials > 0: self._sendInterest(interest, nRetrials - 1, link, onVerified, onError) else: onNetworkNack(interest, NetworkNack()) if link.getDelegations().size() == 0: # We can use the supplied interest without copying. request = interest else: # Copy the supplied interest and add the Link. request = Interest(interest) # This will use a cached encoding if available. request.setLinkWireEncoding(link.wireEncode()) try: self._face.expressInterest(request, onData, onTimeout, onNetworkNack) except Exception as ex: try: onError(EncryptError.ErrorCode.General, "expressInterest error: " + repr(ex)) except: logging.exception("Error in onError")