async def getStatePckValue(self, source:Name, nopck:int) -> Optional[StateVector]: name = source + self.statePrefix + [Component.from_number(nopck, Component.TYPE_SEQUENCE_NUM)] try: logging.info(f'MSBalancer: balancing from {Name.to_str(name)}') data_name, meta_info, content = await self.app.express_interest( name, must_be_fresh=True, can_be_prefix=True, lifetime=1000) if bytes(content) == b'': return None return StateVector(bytes(content)) except (InterestNack, InterestTimeout, InterestCanceled, ValidationFailure) as e: return None
def on_interest(name: FormalName, param: InterestParam, _app_param: Optional[BinaryStr]): logging.info(f'>> I: {Name.to_str(name)}, {param}') request = Name.to_str(name).split("/") print("handle Interest Name", Name.to_str(name)) if request[-2] == "metadata": print("handle Meta data") # content = json.dumps(list(pred_frame_buffer)).encode() # content = str(current_I_frame).encode() content = Name.to_str( name + [Component.from_number(current_I_frame, 0)]).encode() name = name app.put_data(name, content=content, freshness_period=300) logging.info("handle to name " + Name.to_str(name)) elif request[-3] == "frame": interest_frame_num = int(request[-1]) if interest_frame_num in frame_buffer_dict: content = frame_buffer_dict[interest_frame_num] app.put_data(name + [b'\x08\x02\x00\x00'], content=content, freshness_period=2000, final_block_id=Component.from_segment(0)) print( f'handle interest: publish pending interest' + Name.to_str(name) + "------------/" + str(interest_frame_num) + "length: ", len(content)) else: interest_buffer.append([interest_frame_num, name]) else: print("handle Request missing ", Name.to_str(name)) while len(interest_buffer) > 0 and len( frame_buffer) > 0 and frame_buffer[-1] >= interest_buffer[0][0]: pendingInterest = interest_buffer.popleft() pendingFN = pendingInterest[0] pendingName = pendingInterest[1] if pendingFN in frame_buffer_dict: content = frame_buffer_dict[pendingFN] app.put_data(pendingName + [b'\x08\x02\x00\x00'], content=content, freshness_period=2000, final_block_id=Component.from_segment(0)) print( f'handle interest: publish pending interest' + Name.to_str(pendingName) + "------------/" + str(pendingFN) + "length: ", len(content))
async def main(app: NDNApp) -> int: try: source = Component.from_number(6, Component.TYPE_SEQUENCE_NUM) print(source) print(Component.to_number(source)) """ name = Name.from_str("/b")*2928 interest = make_interest(name, InterestParam(can_be_prefix=True, must_be_fresh=True, lifetime=6000)) print(f'size: {len(interest)}') _, _, _, pkt = await app.express_interest(interest) data_name, meta_info, content, sig_ptrs = parse_data(pkt) content = content if content else bytes("NONE") print(f'CONTENT: {bytes(content).decode()}') print(f'CONTENT-TYPE: {meta_info.content_type}') """ except (InterestNack, InterestTimeout, InterestCanceled, ValidationFailure) as e: print(f'Nacked with reason={e.reason}') finally: app.shutdown()
async def sendPropInterest(self, source: Name, pckno: int) -> Optional[StateVector]: name: Name = source + self.groupPrefix + [ Component.from_str("prop") ] + [Component.from_number(pckno, Component.TYPE_SEQUENCE_NUM)] try: SVSyncLogger.info( f'SVSyncBalancer: balancing by {Name.to_str(name)}') _, _, _, pkt = await self.app.express_interest( name, need_raw_packet=True, must_be_fresh=True, can_be_prefix=True, lifetime=self.PROP_INTEREST_LIFETIME) int_name, meta_info, content, sig_ptrs = parse_data(pkt) isValidated = await self.secOptions.validate(int_name, sig_ptrs) return StateVector(bytes( content)) if bytes(content) != b'' and isValidated else None except (InterestNack, InterestTimeout, InterestCanceled, ValidationFailure): SVSyncLogger.info( f'SVSyncBalancer: failed to balance from {Name.to_str(name)}') return None