def __init__(self, name, mydata, content_path=None, **kwargs): self.name = pyndn.Name(name) #not include segment number and version number self.handle = pyndn.NDN() self.exclusions = pyndn.ExclusionFilter() self.mod = kwargs.get("mod", "non-adatpive") self.default_block_size = kwargs.get("default_block_size", DEFAULT_BLOCK_SIZE) self.mydata = mydata s = content_path if s == None: s = os.path.basename(name) if os.path.exists(s): log.info("file %s will be published with name %s " %(s, name)) else: log.error("%s is not exist under %s directory" %(s, "current" if content_path==None else "the")) exit(0) self.file_in = open(s, "rb") self.file_size = os.path.getsize(s)
def __init__(self, name, mydata, content_path=None, **kwargs): self.name = pyndn.Name( name) #not include segment number and version number self.handle = pyndn.NDN() self.exclusions = pyndn.ExclusionFilter() self.mod = kwargs.get("mod", "non-adatpive") self.default_block_size = kwargs.get("default_block_size", DEFAULT_BLOCK_SIZE) self.mydata = mydata s = content_path if s == None: s = os.path.basename(name) if os.path.exists(s): log.info("file %s will be published with name %s " % (s, name)) else: log.error("%s is not exist under %s directory" % (s, "current" if content_path == None else "the")) exit(0) self.file_in = open(s, "rb") self.file_size = os.path.getsize(s)
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 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