-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
271 lines (207 loc) · 9.97 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
import socket, json, time
from multiprocessing import Process
from tkinter import *
from threading import *
class Client:
def __init__(self, master):
self.handle = None
self.socket = None
self.server_address_port = None
self.handle = None
# thread for server responses
self.t = None
self.is_active_thread = False
self.connected = False
# GUI
self.root = master
self.create_gui()
self.gui_print("***** Welcome to Message Board System *****")
self.gui_print("type \"/?\" for the commands")
def send(self, message):
# Send message to server
client_message = json.dumps(message) # convert message to JSON
self.socket.sendto(client_message.encode(), self.server_address_port)
def server_message(self):
return self.socket.recv(1024).decode('ascii')
def listen(self):
try:
while self.is_active_thread:
encoded_message, *_ = self.socket.recvfrom(1024)
response = json.loads(encoded_message.decode('ascii'))
if "prefix" in response and "type" in response:
if response["type"] == "CONFIRM_CONNECTION":
self.connected = True
elif response["type"] == "CLOSE_CONNECTION":
self.connected = False
elif response["type"] == "CONFIRM_HANDLE":
self.register(response['prefix'])
elif response["type"] == "RECEIVED_MESSAGE":
self.gui_print(text=response['prefix'], style="receiver", linebreak=False)
elif response["type"] == "SENT_MESSAGE":
self.gui_print(text=response['prefix'], style="sender", linebreak=False)
elif response["type"] == "BROADCAST_MESSAGE":
self.gui_print(text=response['prefix'], style="broadcast", linebreak=False)
self.gui_print(response['message'])
elif "type" in response:
if response["type"] == "ERROR":
self.gui_print(text=response['message'], style='error')
else:
self.gui_print(response['message'])
except:
pass
def connect(self, socket_address):
# Connect to server
self.server_address_port = socket_address
self.socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
self.is_active_thread = True
self.t = Thread(target=self.listen)
self.t.start()
def disconnect(self):
# Disconnect from server
self.handle = None
self.connected = False
self.is_active_thread = False
self.t = None
self.socket.close()
def register(self, handle):
# Registers user's handle. Assumes the handle is unique. Check with server first before calling this function.
self.handle = handle
#GUI PART
def create_gui(self):
self.root.title("Message Board System")
self.root.geometry('400x500')
#header
self.head_label = Label(root, bg="white", text=" Welcome to Message Board System ", font=("Arial", 13), pady=10)
self.head_label.place(relwidth=1)
#create chat window
self.chatwindow = Text(root, bg= "beige", width= 50, height= 8)
self.chatwindow.place(x= 15, y= 50, height= 330, width= 370)
self.chatwindow.config(state=DISABLED)
#create message area
self.messageWindow = Text(root, bd= 3, bg= "beige", width= 30, height= 4)
self.messageWindow.place (x= 15, y= 400, height= 88, width= 260)
#button for sending
self.button = Button(root, text = "Enter", bg= "white", activebackground = "light blue", font=("Arial", 13), width = 5, height = 5, command=self.exec)
self.button.place(x= 285, y= 400, height= 88, width= 100)
#scroll bar
self.scroll = Scrollbar(root, command=self.chatwindow.yview())
self.scroll.place(x= 375, y=50, height= 330)
#listen to enter key for commands
self.root.bind('<Return>', self.exec)
# self.win.protocol("WM_DELETE_WINDOW", self.stop)
def gui_print(self, text='', style='', linebreak=True):
# existing styles
self.chatwindow.tag_config('sender', foreground="blue")
self.chatwindow.tag_config('receiver', foreground="red")
self.chatwindow.tag_config('broadcast', foreground="green")
self.chatwindow.tag_config('error', foreground="red")
self.chatwindow.config(state=NORMAL) # allow editing
if style == '':
self.chatwindow.insert(END, text + ('\n\n' if linebreak else ''))
else:
self.chatwindow.insert(END, text + ('\n\n' if linebreak else ''), style)
self.chatwindow.config(state=DISABLED) # set to read-only
# auto scroll to bottom
self.chatwindow.see(END)
def show_error(self, errorMessage):
self.gui_print(text=errorMessage, style="error")
def exec(self, evt=None):
user_input = self.messageWindow.get("1.0",'end-1c')
if user_input.strip(): # check if input box is blank
command, *params = user_input.split()
# reset input field
self.messageWindow.delete(1.0, END)
if command == '/join':
# Connect to the server application
# Syntax: /join <server_ip_add> <port>
try:
server_ip_add, port = params
if not self.connected:
try:
self.connect( (server_ip_add, int(port)) )
self.send({"command": "join"})
time.sleep(2)
if not self.connected:
raise Exception()
except Exception as e:
print(e)
self.show_error("Error: Connection to the Message Board Server has failed! Please check IP Address and Port Number.")
except Exception as e:
print(e)
self.show_error("Error: Command parameters do not match or is not allowed.")
elif command == '/leave':
# Disconnect to the server application
if len(params) == 0:
if self.connected:
self.send({"command": "leave"})
time.sleep(2)
if not self.connected:
self.disconnect()
else:
self.show_error("Error: Disconnection failed. Please connect to the server first.")
else:
self.show_error("Error: Command parameters do not match or is not allowed.")
elif command == '/register':
# Register a unique handle or alias
# Syntax: /register <handle>
try:
[handle] = params
if self.handle == None:
try:
self.send({"command": "register", "handle": handle})
except Exception as e:
print(e)
self.show_error("Error: Connection to the Message Board Server has failed! Please check IP Address and Port Number.")
else:
self.show_error("Error: You have already registered.")
except:
self.show_error("Error: Command parameters do not match or is not allowed.")
elif command == '/all':
# Send message to all
# Syntax: /all <message>
message = ' '.join(params)
if len(message) > 0:
try:
self.send({"command": "all", "message": message})
except Exception as e:
print(e)
self.show_error("Error: Connection to the Message Board Server has failed! Please check IP Address and Port Number.")
else:
self.show_error("Error: Command parameters do not match or is not allowed.")
elif command == '/msg':
# Send direct message to a single handle
# Syntax: /msg <handle> <message>
try:
handle = params[0]
message = ' '.join(params[1:])
if len(message) > 0:
try:
self.send({"command": "msg", "handle": handle, "message": message})
except Exception as e:
print(e)
self.show_error("Error: Connection to the Message Board Server has failed! Please check IP Address and Port Number.")
else:
raise Exception()
except:
self.show_error("Error: Command parameters do not match or is not allowed.")
elif command == '/?':
# Request command help to output all Input, Syntax commands for references
self.gui_print("******** Commands for references ********\n" + \
"\t /join <server_ip_add> <port>\n" + \
"\t /leave \n" + \
"\t /register <handle> \n" + \
"\t /all <message> \n" + \
"\t /msg <handle> <message> """)
else:
self.show_error("Error: Command not found.")
if __name__ == "__main__":
root = Tk()
client = Client(root)
root.send
try:
root.mainloop()
except KeyboardInterrupt:
# Disconnect client before exiting program
if client.socket != None:
client.send({"command": "leave"})
client.disconnect()