def upcall(self, kind, upcallInfo): if kind != pyndn.UPCALL_INTEREST: log.warn("get kind: %s" % str(kind)) return pyndn.RESULT_OK co = self.prepare(upcallInfo) self.handle.put(co) log.info("content: %s" % co) return pyndn.RESULT_INTEREST_CONSUMED
def upcall(self, kind, upcallInfo): if kind != pyndn.UPCALL_INTEREST: log.warn("get kind: %s" %str(kind)) return pyndn.RESULT_OK co = self.prepare(upcallInfo) self.handle.put(co) log.info("content: %s" %co) return pyndn.RESULT_INTEREST_CONSUMED
def upcall(self, kind, upcallInfo): if kind == pyndn.UPCALL_FINAL:#handler is about to be deregistered log.info("handler is about to be deregistered") if self.mydata.final_block_id == None: self.handle.setRunTimeout(DEFAULT_RUN_TIMEOUT*10) return pyndn.RESULT_OK if kind == pyndn.UPCALL_INTEREST_TIMED_OUT: self.do_meet_accident(kind, upcallInfo) return pyndn.RESULT_OK if kind in [pyndn.UPCALL_CONTENT_UNVERIFIED, pyndn.UPCALL_CONTENT_BAD]: self.do_meet_accident(kind, upcallInfo) return pyndn.RESULT_OK if kind in [pyndn.UPCALL_INTEREST, pyndn.UPCALL_CONSUMED_INTEREST]: log.warn("unexpected kind: %s" %kind) assert kind == pyndn.UPCALL_CONTENT, "kind: "+str(kind) if self.interest_schema == "exclusion": matched_comps = upcallInfo.matchedComps response_name = upcallInfo.ContentObject.name org_prefix = response_name[:matched_comps] assert org_prefix == self.name, "org_prefix: "+ str(org_prefix)+" , self.name: "+str(self.name) if matched_comps == len(response_name): comp = pyndn.Name([upcallInfo.ContentObject.digest()]) disp_name = pyndn.Name(response_name) else: comp = response_name[matched_comps:matched_comps + 1] disp_name = response_name[:matched_comps + 1] log.info("disp_name: %s, comp: %s" %(disp_name, comp)) self.exclusions.add_name(comp) self.do_receive_content(kind, upcallInfo) #if matched_comps + 1 < len(response_name): # new = Consumer(response_name[:matched_comps+1]) # new.express_interest() return pyndn.RESULT_OK
def prepare(self, upcallInfo): ist = upcallInfo.Interest ist_name = ist.name mod = "non-adaptive" expected_block_size = self.default_block_size chunkid = int(ist_name[-1]) begin_byte = chunkid * self.default_block_size for i in range(len(ist_name)): sub = ist_name[i] if sub == ADAPTIVE_MOD_FLAG: if self.mod != "adaptive": log.error( "Interest ask for adptive mod while server does not support" ) #return None mod = "adaptive" assert i + 1 < len(ist_name), "bad name %s" % (ist_name) if self.mod == "adaptive": expected_block_size = int(ist_name[i + 1]) begin_byte = chunkid break if begin_byte > self.file_size: log.warn( "already reach the final block: begin_byte: %s, file_size: %s" % (begin_byte, self.file_size)) exit(1) self.file_in.seek(begin_byte) data = self.file_in.read(expected_block_size) log.debug("Interest: %s, mod: %s, expected_block_size: %s, begin_byte: %s, chunk_size: %s" \ %(ist.name, mod, expected_block_size, begin_byte, len(data))) final_block_id = None if self.file_in.tell() == self.file_size: final_block_id = chunkid log.debug("data is %s" % (data)) # create a new data packet co = pyndn.ContentObject() # since they want us to use versions and segments append those to our name #co.name = self.name.appendVersion().appendSegment(0) co.name = ist_name # place the content co.content = data si = co.signedInfo key = self.handle.getDefaultKey() # key used to sign data (required by ndnx) si.publisherPublicKeyDigest = key.publicKeyID # how to obtain the key (required by ndn); here we attach the # key to the data (not too secure), we could also provide name # of the key under which it is stored in DER format si.freshnessSeconds = 0 si.keyLocator = pyndn.KeyLocator(key) # data type (not needed, since DATA is the default) si.type = pyndn.CONTENT_DATA # number of the last segment (0 - i.e. this is the only # segment) if final_block_id != None: si.finalBlockID = pyndn.Name.num2seg(final_block_id) # signing the packet co.sign(key) chunkinfo = ChunkInfo(chunkid) chunkinfo.begin_byte = begin_byte chunkinfo.content_size = len(data) chunkinfo.expected_block_size = expected_block_size chunkinfo.retxN = 1 self.mydata.add_chunk_info(chunkinfo) return co
def do_meet_accident(self, kind, upcallInfo): log.warn("accident happen: kind=%s, upcallInfo=%s" %(kind, upcallInfo)) self.handle.setRunTimeout(DEFAULT_RUN_TIMEOUT) self.express_interest()
def prepare(self, upcallInfo): ist = upcallInfo.Interest ist_name = ist.name mod = "non-adaptive" expected_block_size = self.default_block_size chunkid = int(ist_name[-1]) begin_byte = chunkid * self.default_block_size for i in range(len(ist_name)): sub = ist_name[i] if sub == ADAPTIVE_MOD_FLAG: if self.mod != "adaptive": log.error("Interest ask for adptive mod while server does not support") #return None mod = "adaptive" assert i+1 < len(ist_name), "bad name %s" %(ist_name) if self.mod == "adaptive": expected_block_size = int(ist_name[i+1]) begin_byte = chunkid break if begin_byte > self.file_size: log.warn("already reach the final block: begin_byte: %s, file_size: %s" %(begin_byte, self.file_size)) exit(1) self.file_in.seek(begin_byte) data = self.file_in.read(expected_block_size) log.debug("Interest: %s, mod: %s, expected_block_size: %s, begin_byte: %s, chunk_size: %s" \ %(ist.name, mod, expected_block_size, begin_byte, len(data))) final_block_id = None if self.file_in.tell() == self.file_size: final_block_id = chunkid log.debug("data is %s"%(data)) # create a new data packet co = pyndn.ContentObject() # since they want us to use versions and segments append those to our name #co.name = self.name.appendVersion().appendSegment(0) co.name = ist_name # place the content co.content = data si = co.signedInfo key = self.handle.getDefaultKey() # key used to sign data (required by ndnx) si.publisherPublicKeyDigest = key.publicKeyID # how to obtain the key (required by ndn); here we attach the # key to the data (not too secure), we could also provide name # of the key under which it is stored in DER format si.freshnessSeconds = 0 si.keyLocator = pyndn.KeyLocator(key) # data type (not needed, since DATA is the default) si.type = pyndn.CONTENT_DATA # number of the last segment (0 - i.e. this is the only # segment) if final_block_id != None: si.finalBlockID = pyndn.Name.num2seg(final_block_id) # signing the packet co.sign(key) chunkinfo = ChunkInfo(chunkid) chunkinfo.begin_byte = begin_byte chunkinfo.content_size = len(data) chunkinfo.expected_block_size = expected_block_size chunkinfo.retxN = 1 self.mydata.add_chunk_info(chunkinfo) return co