add ping-pong example

This commit is contained in:
dave 2017-09-17 19:11:53 -07:00
parent a6c4706d0d
commit 036cc36186
2 changed files with 61 additions and 1 deletions

60
examples/ping.py Normal file
View File

@ -0,0 +1,60 @@
from contextlib import closing
from msgbus.client import MsgbusSubClient
from time import time, sleep
def pong(host, port):
"""
Subscribe to the ping channel and send pongs in reply
"""
with closing(MsgbusSubClient(host, port)) as client:
client.sub("ping")
while True:
_, msg = client.recv()
client.pub("pong", msg)
print("pong(): >< {} {}".format(_, msg))
def ping(host, port, message, count=5, interval=1):
"""
Send a ping and wait for the reply. In a loop
"""
with closing(MsgbusSubClient(host, port)) as client:
client.sub("pong")
sleep(1)
while count > 0:
count -= 1
print("ping(): > ping", message)
start = time()
client.pub("ping", message)
_, msg = client.recv()
print("ping(): <", _, msg, "rtt:", round(time() - start, 6), "\n")
sleep(interval)
def main():
import argparse
parser = argparse.ArgumentParser(description="send a message to a msgbus server")
parser.add_argument("-i", "--host", default="127.0.0.1", help="host to connect to")
parser.add_argument("-p", "--port", default=7003, help="port to connect to")
parser.add_argument("-m", "--mode", choices=["ping", "pong"], required=True, help="client mode")
parser.add_argument("--payload", default="hello", help="ping payload")
parser.add_argument("-c", "--count", type=int, default=5, help="how many pings")
parser.add_argument("--interval", type=int, default=1, help="ping interval")
args = parser.parse_args()
if args.mode == "ping":
ping(args.host, args.port, args.payload, args.count, args.interval)
elif args.mode == "pong":
pong(args.host, args.port)
# python3 examples/ping.py -m pong -i 127.0.0.1 -p 7100 &
# python3 examples/ping.py -m ping -i 127.0.0.1 -p 7100
# ping(): > ping hello
# pong(): >< ping hello
# ping(): < pong hello rtt: 0.000666
if __name__ == '__main__':
main()

View File

@ -200,7 +200,7 @@ class MsgBusServer(object):
while self.alive:
for peer_addr in self.seed_peers:
if not self.has_peer(peer_addr):
print("connecting to {}".format(peer_addr))
print("Connecting to {}".format(peer_addr))
self.loop.call_soon(asyncio.ensure_future, self.connect_to_peer(peer_addr))
await asyncio.sleep(1)