-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_client_class.py
110 lines (94 loc) · 3.33 KB
/
chat_client_class.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import time
import socket
import select
import sys
import json
from chat_utils import *
import client_state_machine as csm # import client_state_machine_student as csm
import threading
class Client:
def __init__(self, args):
self.peer = ''
self.console_input = []
self.state = S_OFFLINE
self.system_msg = ''
self.local_msg = ''
self.peer_msg = ''
self.args = args
def quit(self):
self.socket.shutdown(socket.SHUT_RDWR)
self.socket.close()
def get_name(self):
return self.name
def init_chat(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
svr = SERVER if self.args.d == None else (self.args.d, CHAT_PORT)
self.socket.connect(svr)
self.sm = csm.ClientSM(self.socket)
reading_thread = threading.Thread(target=self.read_input)
reading_thread.daemon = True
reading_thread.start()
def shutdown_chat(self):
return
def send(self, msg):
mysend(self.socket, msg)
def recv(self):
return myrecv(self.socket)
def get_msgs(self):
read, write, error = select.select([self.socket], [], [], 0)
my_msg = ''
peer_msg = []
#peer_code = M_UNDEF for json data, peer_code is redundant
if len(self.console_input) > 0:
my_msg = self.console_input.pop(0)
if self.socket in read:
peer_msg = self.recv()
return my_msg, peer_msg
def output(self):
if len(self.system_msg) > 0:
print(self.system_msg)
self.system_msg = ''
def login(self):
my_msg, peer_msg = self.get_msgs()
if len(my_msg) > 0:
self.name = my_msg
msg = json.dumps({"action":"login", "name":self.name})
self.send(msg)
response = json.loads(self.recv())
if response["status"] == 'ok':
self.state = S_LOGGEDIN
self.sm.set_state(S_LOGGEDIN)
self.sm.set_myname(self.name)
self.print_instructions()
return (True)
elif response["status"] == 'duplicate':
self.system_msg += 'Duplicate username, try again'
return False
else: # fix: dup is only one of the reasons
return(False)
def read_input(self):
while True:
text = sys.stdin.readline()[:-1]
self.console_input.append(text) # no need for lock, append is thread safe
def print_instructions(self):
self.system_msg += menu
def run_chat(self):
self.init_chat()
self.system_msg += 'Welcome to ICS chat\n'
self.system_msg += 'Please enter your name: '
self.output()
while self.login() != True:
self.output()
self.system_msg += 'Welcome, ' + self.get_name() + '!'
self.output()
while self.sm.get_state() != S_OFFLINE:
self.proc()
self.output()
time.sleep(CHAT_WAIT)
self.quit()
#==============================================================================
# main processing loop
#==============================================================================
def proc(self):
my_msg, peer_msg = self.get_msgs()
self.system_msg += self.sm.proc(my_msg, peer_msg)