-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaclient.py
242 lines (196 loc) · 6.78 KB
/
maclient.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
from socket import *
import threading
import os
from maclasses import MaUser
# ----------------------------------------------------------- constants
null = 'null'
seperator = ';'
# ----------------------------------------------------------- data
current_user = null
# ----------------------------------------------------------- exit page
def exit_page():
print('bye bye')
os._exit(0)
# ----------------------------------------------------------- chat page
def chat_page_view(chat_index):
print()
print('CHAT PAGE')
current_chat = current_user.chats[chat_index]
for i in range(len(current_chat.messages)):
messagei = current_chat.messages[i]
print(str(messagei.hour) + ' - ' + messagei.sender_name + ': ' + messagei.content)
def chat_page_controller(command, chat_index):
msg = str(chat_index) + seperator + command
client_socket.send(msg.encode())
def chat_page(chat_index):
global current_user
while True:
client_socket.send('update_user'.encode())
chat_page_view(chat_index)
command = input(current_user.username + ': ')
if command == 'exit':
exit_page()
elif command == 'back':
client_socket.send('message_like'.encode())
break
elif command == '':
client_socket.send('message_like'.encode())
continue
chat_page_controller(command, chat_index)
# ----------------------------------------------------------- main page
def main_page_view():
global current_user
print()
print('MAIN PAGE')
print('username: ' + current_user.username + ', ' + 'isBusy: ' + str(current_user.isBusy))
print()
print('- toggle is_busy (toggle)')
print('- new group (newgroup;<group_name>;<username1>;<username2> ...)')
print('- send to all (all)')
print()
print('chats:')
for i in range(len(current_user.chats)):
print('-', current_user.chats[i].name)
print()
def main_page_controller(command):
global current_user
if command == 'toggle':
client_socket.send('toggle_busy'.encode())
elif command[:len('newgroup')] == 'newgroup':
client_socket.send(command.encode())
else:
client_socket.send('message_like'.encode())
for i in range(len(current_user.chats)):
if command == current_user.chats[i].name:
chat_page(i)
break
else:
print('chat not found?!')
def main_page():
global current_user
client_socket.send('update_user'.encode())
print()
print('getting data from server...')
data = client_socket.recv(1000000)
print(data.decode())
current_user = MaUser.from_json(data.decode())
receive_thread = threading.Thread(target=receive_messages)
receive_thread.start()
while True:
client_socket.send('update_user'.encode())
main_page_view()
try:
command = input('> ')
if command == 'exit':
exit_page()
result = main_page_controller(command)
except Exception as e:
print('error:', e)
break
print()
receive_thread.join()
# ----------------------------------------------------------- receiving thread
def receive_messages_controller(command):
global current_user
ins = command.split(seperator)
if ins[0] == 'user_data':
current_user = MaUser.from_json(ins[1])
elif ins[0] == 'notif':
print('notif:', ins[1])
def receive_messages():
# this thread is called just when user signed in successfully
while True:
try:
data = client_socket.recv(1024)
if not data:
break
message = data.decode()
# print('notif:', message)
receive_messages_controller(message)
except Exception as e:
print('error:', e)
break
# ----------------------------------------------------------- sign up page
def sign_in_page_controller(command):
print('server response: ' + command)
if 'successfull' in command:
main_page()
def sign_in_page():
while True:
print()
print('SIGN IN PAGE')
username = input('username: ')
if username == 'exit':
exit_page()
elif username == 'back':
break
password = input('password: ')
if password == 'exit':
exit_page()
elif password == 'back':
break
msg = 'signin' + seperator + username + seperator + password
client_socket.send(msg.encode())
msg = client_socket.recv(1024)
msg = msg.decode()
sign_in_page_controller(msg)
# ----------------------------------------------------------- sign up page
def sign_up_page_controller(command):
print('server response: ' + command)
if 'successfull' in command:
main_page()
def sign_up_page():
while True:
print()
print('SIGN UP PAGE')
username = input('username: ')
if username == 'exit':
exit_page()
elif username == 'back':
break
password = input('password: ')
if password == 'exit':
exit_page()
elif password == 'back':
break
msg = 'signup' + seperator + username + seperator + password
client_socket.send(msg.encode())
msg = client_socket.recv(1024)
msg = msg.decode()
sign_up_page_controller(msg)
# ----------------------------------------------------------- first page
def first_page_view():
print()
print('FIRST PAGE')
print('1. sign in (signin)')
print('2. sign up (signup)')
def first_page_controller(command):
if command == '1' or command == 'signin':
sign_in_page()
elif command == '2' or command == 'signup':
sign_up_page()
else:
print('command not found?!')
def first_page():
while True:
first_page_view()
try:
command = input('> ')
if command == 'exit':
exit_page()
elif command == 'back':
break
result = first_page_controller(command)
except Exception as e:
print('error:', e)
break
# ----------------------------------------------------------- main
if __name__ == '__main__':
server_ip = '127.0.0.1'
server_port = 12_042
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect((server_ip, server_port))
send_thread = threading.Thread(target=first_page)
send_thread.start()
send_thread.join()
client_socket.close()