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 do_receive_content(self, kind, upcallInfo):
     self.handle.setRunTimeout(DEFAULT_RUN_TIMEOUT)
     self.chunkinfo.endT = datetime.datetime.now()
     self.chunkinfo.content_size = len(upcallInfo.ContentObject.content)
     
     
     self.chunkinfo = None
     log.info("receive valid content: %s" %upcallInfo.ContentObject.name)
     log.debug("receive valid content: %s" %upcallInfo.ContentObject)
     
     self.file_out.write(upcallInfo.ContentObject.content)
     fbi = upcallInfo.ContentObject.signedInfo.finalBlockID 
     if  fbi != None:
         if isinstance(fbi, str):
             fbi = pyndn.Name.seg2num(fbi)
             
         log.info("***************final block id: %s, or %s" %(upcallInfo.ContentObject.signedInfo.finalBlockID, fbi))
         #log.debug("final_block_id: %s" %self.mydata.final_block_id)
         #log.debug("final content: %s" %upcallInfo.ContentObject)
         if self.mydata.final_block_id == None:
             self.mydata.final_block_id = int(fbi)
         else:
             assert self.mydata.final_block_id == fbi, \
                 "self.mydata.final_block_id: %s != upcallInfo.ContentObject.signedInfo.finalBlockID: %s" \
                     %(self.mydata.final_block_id, upcallInfo.ContentObject.signedInfo.finalBlockID)
         log.info("mydata.final_block_id: %s, next_seg: %s, next_byte: %s" \
                   %(self.mydata.final_block_id, self.mydata.next_seg, self.mydata.next_byte))
         
         
     self.mydata.next_seg += 1
     self.mydata.next_byte += len(upcallInfo.ContentObject.content)
     self.mydata.total_size += len(upcallInfo.ContentObject.content)
     
     if self.mydata.final_block_id !=None:
         
         if (self.mod == "adaptive" and self.mydata.next_byte > self.mydata.final_block_id) or \
                 (self.mod == "non-adaptive" and  self.mydata.next_seg > self.mydata.final_block_id):
             log.info("**********aready get the final block")
             self.stop()
             return
     else:
         log.debug("final_block_id: %s, next_seg: %s" %(self.mydata.final_block_id, self.mydata.next_seg))
         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
    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