This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.py
67 lines (54 loc) · 1.8 KB
/
utility.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
import os
import json
def parse_ip_addr():
fmsg = os.popen("ip -o addr | awk '/inet/ {print $2, $3, $4}'").read()
ip_addr_list = fmsg.strip().split("\n")
ip_list = []
for line in ip_addr_list:
cur = line.strip().split(" ")
if cur[1]=="inet":
ip_list.append({"interface": cur[0], "ip": cur[2].split("/")[0]})
return ip_list
def build_id(ip: str, name: str) -> str:
return f"{ip}-{name}"
def client_setup():
interfaces = parse_ip_addr()
print("Please select one of the interfaces:")
for it in range(len(interfaces)):
print(it, "-", interfaces[it])
choice = int(input("Choice: "))
while not 0 <= choice < len(interfaces):
choice = int(input("Please enter a valid range: "))
myip = interfaces[choice]["ip"]
print("Selected ip", myip)
while True:
myname = input("Name: ").strip()
if myname.isalnum():
break
print("Please enter a valid name!")
return myip, myname
# Group chat:
GROUP_CHAT_MESSAGE = {
"type": "group_chat",
"content": None
}
# Private chat:
PRIVATE_CHAT_MESSAGE = {
"type": "private_chat",
"content": None
}
# TODO: Discuss with the team about binding the function in this way.
# This parser for general communication, such as group and private chatting etc.
# and it must be attached by default.
def recv_parser(self, message:str, ip:str):
try:
message: dict = json.loads(message)
except:
print("ERROR: Could not parse the message:", message)
return
if message["type"] == "group_chat":
_from = self.persons[ip]
print(f"(group chat){_from}: {message['content']}")
elif message["type"] == "private_chat":
_from = self.persons[ip]
print(f"(private chat){_from}: {message['content']}")