-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
292 lines (195 loc) · 8.91 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from PyQt5 import QtWidgets, QtCore, QtGui
from QtChatApp import Ui_ChatApp
from login import Ui_MainWindow
import encryption
import socket
import sys
import threading
import time
#predefined key for encryption
key = b'\xdb6\xc7r\xb3#\xbe"\x9c~C\xd4\xbe\xb8\x8f\xfa\xb6\x01\xe92+U\xde\\\xd7\xc5m\xba\xe2\r\xda\xe9'
HEADER = 64
PORT = 1023
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNCET"
WARNING_MESSAGE = 'Username is already taken'
status = ""
connected = True
#old_user = 0
score = 0
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
class login_window(QtWidgets.QMainWindow):
def __init__(self):
super(login_window, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.connectButton.clicked.connect(lambda: connect_click_action(self))
def get_name(self):
return self.ui.name.text()
def get_gender(self):
if (self.ui.femaleButton.isChecked()):
return self.ui.femaleButton.text()
elif (self.ui.maleButton.isChecked()):
return self.ui.maleButton.text()
def get_age(self):
return self.ui.age.text()
def connect_click_action(win):
global gender, age, name
gender = win.get_gender()
age = win.get_age()
name = win.get_name()
# We use a temporary connection to check if our entered username is taken or not.
# Also used to check if the connectivity to a host is proper or not.
# temp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# temp_socket.connect(ADDR)
send('username:' + name)
except:
QtWidgets.QMessageBox.warning(win, "Connection Invalid", "Could not connect to this host and address.")
return
# Checks if the entered username is already taken by the another client.
# temp_socket.close()
win.close()
chat = chat_window()
chat.show()
class chat_window(QtWidgets.QMainWindow):
def __init__(self):
super(chat_window, self).__init__()
self.ui = Ui_ChatApp()
self.ui.setupUi(self)
self.idle_status = False
# Calls send_from_lineEdit method on 'send_button' click.
self.ui.result.setReadOnly(True)
self.diagnosisQuestions = [ "Do you currently live with a family member who has depression?", "Have parents, other relatives, or maybe even friends accused you of being “irritated,” “nasty,” or “always in a bad mood?",
"Does life seem pointless?","Does it seem like it’s impossible to concentrate?","Have you withdrawn from your friends and family?","Have you noticed a sudden change in your weight?",
"Do you often oversleep? Do you think you get too little sleep (and may have insomnia)?", "Do you have aches and pains?",
"Have your grades dropped? If you’re involved in extracurricular activities, have you stopped participating or has your performance declined?","Have you thought of suicide?",""]
self.questionIndex = 1 # * Number of the current question in the bot
self.userMessage= ""
self.display_question(self.diagnosisQuestions[0])
self.ui.yesButton.clicked.connect(self.yesHandling)
self.ui.noButton.clicked.connect(self.noHandling)
self.yesCounter = 0
self.timer = QtCore.QTimer(self)
# * update the timer every tenth second
self.timer.start(100)
# * adding action to timer
self.timer.timeout.connect(self.check_timeout)
self.thread = threading.Thread(target=self.getStatus)
self.thread.start()
# funtion that handle yes answers then prepare the next question to be displayed
def yesHandling(self):
self.yesCounter = self.yesCounter +1
message_sent = self.diagnosisQuestions[self.questionIndex]
self.userMessage = "Yes"
#print the user's message in the server's terminal
# send(self.userMessage)
# display user's message in the chat
self.display_user_message(self.userMessage)
if(self.questionIndex == 10):
# if the user finished answering all the questions then he gets the final result
self.getResult(0)
return
else:
self.questionIndex = self.questionIndex + 1
self.display_question(message_sent)
#same as yesHandling
def noHandling(self):
message_sent = self.diagnosisQuestions[self.questionIndex]
self.userMessage = "No"
#send(self.userMessage)
self.display_user_message(self.userMessage)
if(self.questionIndex == 10):
self.getResult(0)
return
else:
self.questionIndex = self.questionIndex + 1
self.display_question(message_sent)
def display_question(self, message):
self.ui.messagesList.addItem( ( "Question : " + str(message)))
def display_user_message(self, message):
#print user's message in the client's terminal
print(message)
self.ui.messagesList.addItem(( "Answer :" + message + "\n"))
# prints the result of the test in the text box
def getResult(self,old_user):
self.ui.yesButton.hide()
self.ui.noButton.hide()
myscore = score
if not old_user:
user_data = []
# get data >> all converted to strings
user_data.append(name) #get name
user_data.append(gender) #get gender
user_data.append(age) #get age
user_data.append(str(self.yesCounter))
send(','.join(user_data))
myscore = self.yesCounter
if (myscore < 5): #normal
self.ui.result.insert("You're fine. Keep your head up!")
elif (myscore < 8): #depressed
self.ui.result.insert("A little depressed. Try to take care of your self more!")
else: #severe depression
self.ui.result.insert("You need to go to therapy")
#recieves from the server if the connection is idle or not
def getStatus(self):
global connected
while connected:
try:
try:
#length of the header of the status message
msgLength = int(client.recv(HEADER).decode(FORMAT))
# print(msgLength)
# message if the socket tried to recieve just before closing it
except ValueError:
print("[FINISHED] Connection ended due to achieving required result ...")
connected = False
# recieve status from the server
server_message = client.recv(msgLength)
# decrypt and decode server's message
self.server_message = encryption.decrypt(key,server_message).decode(FORMAT)
# print(self.server_message)
# If status message is DISCONNECT_MESSAGE, disconnect the client
if self.server_message == DISCONNECT_MESSAGE:
self.idle_status = True
connected = False
elif WARNING_MESSAGE in self.server_message:
# QtWidgets.QMessageBox.warning( self, "ERROR LOGIN", f"[ERROR] {WARNING_MESSAGE}")
# connected = False
# print(self.server_message)
old_user = 1
score = self.server_message[len(WARNING_MESSAGE):]
self.getResult(old_user)
# print(score)
# If the server was closed
except ConnectionAbortedError:
print("[EXITING] The GUI was closed ... ")
self.client.close()
print("[EXITING] Exiting recieving thread .. ")
sys.exit()
#function to check if the client exceeds 40 secs without sending a message
def check_timeout(self):
if self.idle_status:
QtWidgets.QMessageBox.warning( self, "Connection Lost", "[IDLE] Connection was closed please open the UI again")
sys.exit()
# encrypt text and send it to server
def send(message):
message = encryption.encrypt(key,message)
message_length = len(message)
send_length = str(message_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
client.send(send_length) #send message len
client.send(message) #send encrypted and encoded message
def main():
app = QtWidgets.QApplication(sys.argv)
login_app = QtWidgets.QApplication(sys.argv)
app.aboutToQuit.connect(lambda : send(DISCONNECT_MESSAGE))
# chat = chat_window()
login = login_window()
login.show()
login_app.exec_()
if __name__ == "__main__":
main()