-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect.py
More file actions
81 lines (69 loc) · 3.01 KB
/
connect.py
File metadata and controls
81 lines (69 loc) · 3.01 KB
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
import argparse
import threading
import time
from p2c import PeerToClient, _build_rfc_store_from_rfcs
from p2s import PeerToServer, _load_peer_details
def start_upload_server(peer_num, search_dirs):
ip, port, rfcs = _load_peer_details(peer_num)
rfc_store = _build_rfc_store_from_rfcs(rfcs, search_dirs or ['resources', 'rfc_files', 'downloads'])
upload_server = PeerToClient(ip, port, rfc_store=rfc_store)
t = threading.Thread(target=upload_server.spin_up, daemon=True)
t.start()
return upload_server, t, ip, port, rfcs
def register_rfcs(client, rfcs):
for rfc_number, rfc_title in rfcs:
resp = client.send_add_request(rfc_number, rfc_title)
print(f"ADD RFC {rfc_number} => {resp.strip()}")
def interactive_shell(peer_num, server_ip, server_port, search_dirs):
upload_server, upload_thread, ip, port, rfcs = start_upload_server(peer_num, search_dirs)
print(f"Upload server running for peer{peer_num} at {ip}:{port}")
client = PeerToServer(ip, port, server_ip, server_port)
client.get_connection_to_server()
register_rfcs(client, rfcs)
print("Type: list | lookup <rfc> | download <rfc> | exit")
while True:
try:
cmd = input("connect> ").strip()
except (EOFError, KeyboardInterrupt):
cmd = "exit"
if not cmd:
continue
parts = cmd.split()
op = parts[0].lower()
if op == "list":
print(client.send_list_request())
elif op == "lookup" and len(parts) > 1:
print(client.send_lookup_request(parts[1]))
elif op == "download" and len(parts) > 1:
rnum = parts[1]
path, chosen = client.download_rfc(rnum, re_register=False)
if path and chosen:
print(f"Downloaded RFC {rnum} to {path} from {chosen[0]}:{chosen[1]}")
else:
print(f"RFC {rnum} not found or download failed.")
elif op in ("exit", "quit"):
try:
print(client.send_leave_request())
except Exception:
pass
try:
client.close_connection_to_server()
except Exception:
pass
try:
upload_server.shutdown()
except Exception:
pass
break
else:
print("Commands: list | lookup <rfc> | download <rfc> | exit")
def main():
parser = argparse.ArgumentParser(description="Interactive peer connector: register, serve, and download RFCs.")
parser.add_argument("--peer", type=int, required=True, help="Peer number from peer_details/peer<N>.txt")
parser.add_argument("--server-ip", default="127.0.0.1")
parser.add_argument("--server-port", type=int, default=7734)
parser.add_argument("--resources", nargs='*', help="Search dirs for RFC text files (default: resources rfc_files downloads)")
args = parser.parse_args()
interactive_shell(args.peer, args.server_ip, args.server_port, args.resources)
if __name__ == "__main__":
main()