/
client.py
executable file
路83 lines (69 loc) 路 2.67 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
import sys
import argparse
import asyncio
import functools
import ddcm
parser = argparse.ArgumentParser(description='DDCM Command Line Client')
parser.add_argument('--config', help='config file path')
args = parser.parse_args()
def main():
def handle_input():
command = input()
if command.startswith("ping"):
host, port = command.split(' ')[1].split(':')
asyncio.ensure_future(
service.tcpService.call.ping(ddcm.Remote(
host = host,
port = int(port)
)),
loop = loop
)
if command.startswith("loopping"):
host, port = command.split(' ')[1].split(':')
asyncio.ensure_future(
asyncio.wait([
service.tcpService.call.ping(ddcm.Remote(
host = host,
port = int(port)
)) for i in range(100)
]),
loop = loop
)
if command.startswith("trace route"):
for distance, node in service.route.findNeighbors(service.tcpService.node):
print("%(distance)d %(id)s (%(host)s,%(port)d)" % {
"distance": distance,
"id": node.get_hash_string(),
"host": node.remote.host,
"port": node.remote.port
})
print("READY.")
async def handle_events():
while True:
event = await service.debugQueue.get()
if event["type"] is ddcm.const.kad.event.SERVICE_SHUTDOWN:
break
if event["type"] is ddcm.const.kad.event.HANDLE_PING:
print("Recved PING from %(target)s" % {
"target": event["data"]["remoteNode"].get_hash_string()
})
if event["type"] is ddcm.const.kad.event.HANDLE_PONG_PING:
print("Recved PONG from %(target)s" % {
"target": event["data"]["remoteNode"].get_hash_string()
})
if event["type"] is ddcm.const.kad.event.SEND_PING:
print("PING Sent")
config = ddcm.utils.load_config("config/config" + args.config + ".json" or "config.json")
loop = asyncio.get_event_loop()
loop.set_debug(config['debug']['asyncio']['enabled'])
service = ddcm.Service(config, loop)
loop.run_until_complete(service.start())
if config["client"]["input"]:
loop.add_reader(sys.stdin, handle_input)
try:
loop.run_until_complete(handle_events())
except KeyboardInterrupt:
pass
loop.run_until_complete(service.stop())
main()