async def pub2_tests(app, names, seq): client = Pubsub(app, id='/pub1') for name in names: try: await client.pubadv(name, redefine=False) except (InterestNack, InterestTimeout): print(f'Broker unreachable or timeout') sys.exit() while True: n_items = random.randint(2, 5) try: data = ["data_" + str(n) for n in range(seq, seq + n_items)] success = True for name in names: await asyncio.sleep(0.3) success = success and await client.pubdata2( name, data, seq, seq + n_items - 1) if success: print(f'Published to {names}[{seq}~{seq+n_items-1}]') else: print(f'Failed to publish {names}[{seq}~{seq+n_items-1}]') except (InterestNack, InterestTimeout): print(f'Broker unreachable or interest timeout') await asyncio.sleep(1) except Exception as e: print(f'{type(e).__name__} {str(e)}') finally: seq += n_items await asyncio.sleep(0.5)
async def sub_test(app, names, seq): client = Pubsub(app) num_names = len(names) while True: matched = [] try: for name in names: _name = name matched += await client.subtopic(name) if len(matched) == 0: # No matches print(f"No matches found for {names}") await asyncio.sleep(1.0) continue except (InterestNack, InterestTimeout): print(f'Broker unreachable or interest for {_name} timeout') await asyncio.sleep(1.0) continue # Some matches found rn_name, dataname = matched[random.randint(0, len(matched)-1)] found = False count = 3 # Try 3 times while count > 0: try: data = await client.subdata(rn_name, dataname, seq, lifetime=15000) found = True print(f"{dataname}[{seq}] is {data}") break except (InterestNack, InterestTimeout): count -= 1 if not found: print(f"{dataname}[{seq}] not found") # seq += random.randint(1,10) seq += 1 await asyncio.sleep(0.5)
async def ps_demo(app): # Make a Pubsub client client = Pubsub(app) # Publish some data await publish(client, '/b/new_world', 'Utopia or Distopia-move', 4) await publish(client, '/b/no/where', 'Nowhere fast-move', 3) # Find matches to a topic values = await subscribe(client, "/b/#") print(values) app.shutdown()
async def ps_demo(app): # Make a Pubsub client # client = Pubsub(app, scope=TopicScope.LOCAL) client = Pubsub(app) # Advertise some names for which publisher will provide data await pub_pub(client, '/pa/pub/will/publish', prefix='/pub-1') await pub_pub(client, '/pa/see/what/will/happen') pub_data = '{0:%Y/%m/%d %H:%M:%S}'.format(datetime.datetime.now()) await publish(client, '/pa/see/what/will/happen', pub_data) await publish(client, '/pa/pub/will/publish', pub_data) print(await client.subtopic('/pa/pub/+/publish')) print(await client.subtopic('/pa/#')) app.shutdown()
async def validation_test(app, names): client = Pubsub(app) for name in names: await client.pubadv(name, redefine=True) await client.pubdata(name, "Data-" + str(random.randint(1,100))) print("[Without service token]") matches = await client.subtopic("/test/#") for rn_name, dataname in matches: print(f"{dataname}@{rn_name}") print() print("[With valid service token]") matches = await client.subtopic("/test/#", servicetoken="hasta la vista") for rn_name, dataname in matches: print(f"{dataname}@{rn_name}") sys.exit()
async def pub_tests(app, names, seq): client = Pubsub(app) for name in names: try: await client.pubadv(name, redefine=False) except (InterestNack, InterestTimeout): print('Broker unreachable or interest timeout') sys.exit() while True: try: for name in names: data = '{0:%Y/%m/%d %H:%M:%S}'.format(datetime.datetime.now()) await client.pubdata(name, data, seq) print(f'Published {data} to {name}[{seq}]') seq += 1 except (InterestNack, InterestTimeout): print('Broker unreachable or interest timeout') await asyncio.sleep(1) except Exception as e: print(f'{type(e).__name__} {str(e)}') finally: pass await asyncio.sleep(0.5)
async def ps_demo(app): # Make a Pubsub client client = Pubsub(app) # Publish some data await publish(client, '/a/psdcnv2', 'PSDCNv2') await publish(client, '/a/psdcnv2', 'Rocks!') await publish(client, '/a/python-ndn', 'Thanks!') await publish(client, '/b/new_world', 'Utopia or Distopia') await publish(client, '/b/no/where', 'Nowhere fast') await publish(client, '/a/psdcnv2', 'Rocks!') pub_data = '{0:%Y/%m/%d %H:%M:%S}'.format(datetime.datetime.now()) await publish(client, '/c/old/internet', pub_data) # Find matches to a topic values = await subscribe(client, "/a/+"); print(values) values = await subscribe(client, "/b/#", exclude="/b/no"); print(values) values = await subscribe(client, "/c/+/#"); print(values) # Publish some more data await publish(client, '/b/b_pictures', 'Nowhere to hide') # Find matches again to other topics values = await subscribe(client, "/a/+"); print(values) values = await subscribe(client, "/b/#", exclude="/b/new_world"); print(values) values = await subscribe(client, "/c/+/#"); print(values) app.shutdown()
if choose == '': continue # ignore empty command if choice is not None: if batch: print(">", choice, *args) sys.stdout.flush() # Fire the command print() sys.stdout.flush() await command_map[choice][1](ps) else: print(f"Unknown command! '{choose}'") except (InterestNack, InterestTimeout): print(f"** Broker unreachable or timeout") except Exception as e: print(f"** {type(e).__name__}: {str(e)}") finally: pass print() sys.stdout.flush() if __name__ == '__main__': if len(sys.argv) > 1: batch = True reader = open(sys.argv[1], 'r') app = NDNApp() ps = Pubsub(app) load_config('psdcnv2.config') app.run_forever(after_start=choices())