def express_interest(self): if self.chunkinfo == None:#a new chunk requested self.chunkinfo = ChunkInfo(self.mydata.next_seg) self.chunkinfo.begin_byte = self.mydata.next_byte self.chunkinfo.expected_chunk_size = self.mydata.estimated_optimal_size or self.default_block_size self.chunkinfo.beginT = datetime.datetime.now() self.mydata.add_chunk_info(self.chunkinfo) self.chunkinfo.retxN += 1 selector = pyndn.Interest() selector.answerOriginKind = True name = self.name chunkid = self.mydata.next_seg if self.mod == "adaptive": #byte index as chunkid name = self.name.append(ADAPTIVE_MOD_FLAG).append(str(self.chunkinfo.expected_chunk_size)) chunkid = self.mydata.next_byte if self.interest_schema == "exclusion": #do not use this selector = pyndn.Interest(exclude = self.exclusions) selector.answerOriginKind = True elif self.interest_schema == "id":#the default one name = pyndn.Name(name.components) name = name.append(chunkid) elif self.interest_schema == "segment": name = pyndn.Name(name.components) name = name.appendSegment(chunkid) self.handle.expressInterest(name, self, selector) #log.info("express interest %s, exclusions: %s" %(str(self.name), self.exclusions)); log.debug("interest=%s " %name)
def requestSegment(self, segmentNumber): # if self.pendingSegmentCount < self.pendingSegmentMax: self.pendingSegments.add(segmentNumber) segmentName = pyndn.Name(self.redirect.getName()) segmentName.appendSegment(segmentNumber) interest = pyndn.Interest(segmentName) print("Sent segment interest '{}'".format(interestToString(interest))) self.face.expressInterest(interest, self.receiveSegment, self.timeoutSegment)
def request(self,req,callback,content= None, mustbefresh= False): baseName = ndn.Name(req) interest = ndn.Interest(ndn.Name(baseName)) interest.setMustBeFresh(mustbefresh) self.callback = callback if content != None: interest.setContent(content) self.face.expressInterest(interest, self._onData, self._onTimeout) self.loop.run_forever()
def sendIntermediate(self, index): name = self.interest.getName() name = name.getSubName(0, name.size() - 1).append("INTERMEDIATE").append(str(index)).append("NFN") interest = pyndn.Interest(name) # print("Sending intermediate interest '{}'.".format(interestToString(interest))) # self.face.expressInterest(interest, self.receiveIntermediate, self.timeoutIntermediate) request = SimpleRequest(self.face, interest, self.receiveIntermediate, self.timeoutIntermediate) self.childRequests.append(request) request.send()
def receiveData(self, interest, data): content = data.getContent().toRawStr() redirectPrefix = "redirect:" if content.startswith(redirectPrefix): name = content[len(redirectPrefix):] self.redirect = pyndn.Interest(pyndn.Name(name)) print("Received redirect for interest '{}' -> '{}':\n{}" .format(interestToString(interest), interestToString(self.redirect), data.getContent())) self.requestNextSegment() else: print("Received data for interest '{}':\n{}".format(interestToString(interest), urllib.unquote(content))) self.data = data self.onData(self)
def send_chunk_entities_interest(chunk_peer, x, y): # TODO: Bigger timeout (tree times the current one) name = pyndn.Name(chunk_peer.prefix) \ .append('chunk') \ .append(str(x)).append(str(y)) \ .append('entities') logger.debug('Sending ChunkEntitiesInterest to %s', name.toUri()) interest = pyndn.Interest(name) # About three times a normal timeout as two timeout may occur before # the chunk is created. interest.setInterestLifetimeMilliseconds(30000) mngt.context.face.expressInterest( interest, mngt.on_chunk_entities_data, mngt.on_chunk_entities_timeout)
def send_fetch_entity_interest(peer, uid): logger.debug('Sending FetchEntityInterest for entity %d to peer %d', uid, peer.uid) name = pyndn.Name(peer.prefix) \ .append('entity') \ .append(str(uid)) \ .append('fetch') interest = pyndn.Interest(name) # About three times a normal timeout as two timeout may occur before # the chunk is created. interest.setInterestLifetimeMilliseconds(30000) mngt.context.face.expressInterest( interest, mngt.on_fetch_entity_data, mngt.on_fetch_entity_timeout)
def build_interest(self, latest): if self.start_with_latest: latest = True self.start_with_latest = False excl = pyndn.ExclusionFilter() excl.add_any() excl.add_name(pyndn.Name([self.latest_version])) # expected result should be between those two names excl.add_name(pyndn.Name([self.last_version_marker])) excl.add_any() interest = pyndn.Interest(name=self.base_name, exclude=excl, \ minSuffixComponents=3, maxSuffixComponents=3) interest.childSelector = 1 if latest else 0 return interest
def get_candidates(): commandInterestName = ndn.Name() commandInterestName.wireDecode( # ndn.Blob(buffer(base64.b64decode(request.form['commandInterest'])))) ndn.Blob(base64.b64decode(request.form['commandInterest']))) site_prefix = ndn.Name() site_prefix.wireDecode(commandInterestName[-3].getValue().toBuffer()) timestamp = commandInterestName[-4] signature = ndn.WireFormat.getDefaultWireFormat( ).decodeSignatureInfoAndValue( commandInterestName[-2].getValue().toBuffer(), commandInterestName[-1].getValue().toBuffer()) keyLocator = signature.getKeyLocator().getKeyName() operator = mongo.db.operators.find_one( {'site_prefix': site_prefix.toUri()}) if operator == None: abort(403) try: keyChain = KeyChain( policyManager=OperatorVerifyPolicyManager(operator)) def onVerified(interest): pass def onVerifyFailed(interest): raise RuntimeError("Operator verification failed") keyChain.verifyInterest(ndn.Interest(commandInterestName), onVerified, onVerifyFailed, stepCount=1) except Exception as e: print("ERROR: %s" % e) abort(403) # Will get here if verification succeeds requests = mongo.db.requests.find({'operator_id': str(operator['_id'])}) output = [] for req in requests: output.append(req) return json.dumps(output, default=json_util.default)
def _requestNext(self): interest = ndn.Interest( ndn.Name(self.baseName).appendSequenceNumber(self.currentSeqNo)) interest.setMustBeFresh(True) self.face.expressInterest(interest, self._onData, self._onTimeout) self.currentSeqNo += 1
import pyndn import pyndn._pyndn as _pyndn k = pyndn.NDN.getDefaultKey() kl = pyndn.KeyLocator(k) i = pyndn.Interest() i.name = pyndn.Name('/chat') i.minSuffixComponents = 3 i.maxSuffixComponents = 3 i.childSelector = 1 co = pyndn.ContentObject() co.name = pyndn.Name('/chat/%FD%04%E6%93.%18K/%00') co.content = "number 0" co.signedInfo.publisherPublicKeyDigest = k.publicKeyID co.signedInfo.finalBlockID = b'\x00' co.sign(k) print(str(co)) co2 = _pyndn.ContentObject_obj_from_ndn(co.ndn_data) print(str(co2)) print(str(i)) print(co.matchesInterest(i))
def express_my_interest(self): templ = pyndn.Interest(exclude=self.exclusions) self.handle.expressInterest(self.root, self, templ)
def sendKeepalive(self): name = self.interest.getName() name = name.getSubName(0, name.size() - 1).append("ALIVE").append("NFN") interest = pyndn.Interest(name) print("Sending keepalive interest '{}'.".format(interestToString(interest))) self.face.expressInterest(interest, self.receiveKeepalive, self.timeoutKeepalive)