-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
74 lines (67 loc) · 2.36 KB
/
main.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
import os, sys, getopt
from netsim.netinterface import network_interface
import select
from invite import invite
from wait_for_invite import receive_invite
from reset_sqn_table import reset_sqn
from reg_msg_ver import receive
from reg_msg_gen import send
OWN_ADDR = ''
NET_PATH = './netsim/network/'
invite_flag = False
try:
opts, args = getopt.getopt(sys.argv[1:], shortopts='hs:i', longopts=['help', 'self=', 'invite'])
except getopt.GetoptError:
print('Usage: python main.py -s <self address> -i <invite: include for true, else false>')
sys.exit(1)
if len(opts) == 0:
print('Usage: python main.py -s <self address> -i <invite: include for true, else false>')
sys.exit(1)
for opt, arg in opts:
if opt == '-h' or opt == '--help':
print('Usage: python main.py -s <self address> -i <invite: include for true, else false>')
sys.exit(0)
elif opt == '-s' or opt == '--self':
OWN_ADDR = arg
elif opt == '-i' or opt == '--invite':
invite_flag = True
netif = network_interface(NET_PATH, OWN_ADDR)
if invite_flag:
password = input("Please enter your password: ")
INVITEE_LIST = input("Please enter who you are inviting: ")
GROUP_ID = input("Please enter group id: ")
print("")
groupkey = invite(netif, OWN_ADDR, INVITEE_LIST, GROUP_ID, password)
else:
password = input("Please enter your password: ")
groupkey = receive_invite(netif, OWN_ADDR, password)
print("Key exchange complete")
print("")
reset_sqn(OWN_ADDR)
print("")
sys.stdout.write("Begin chatting!\n\n")
sys.stdout.flush()
while True:
ready, _, _ = select.select([sys.stdin], [], [], 0)
if ready:
m = sys.stdin.readline().rstrip('\n')
snd_msg = OWN_ADDR + ": " + m
to_send = send(OWN_ADDR, snd_msg, groupkey, password)
netif.send_msg('S', to_send)
else:
status, rcv_msg = netif.receive_msg(blocking=False)
if status:
plaintext = receive(OWN_ADDR, rcv_msg, groupkey)
if plaintext != None:
print(plaintext)
'''
while True:
status, rcv_msg = netif.receive_msg(blocking=False)
if status:
plaintext = receive(OWN_ADDR, rcv_msg, groupkey)
print("\n" + plaintext + "\n")
else:
snd_msg = OWN_ADDR + ": " + input("Enter new message: ")
to_send = send(OWN_ADDR, snd_msg, groupkey, password)
netif.send_msg('S', to_send)
'''