This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudpclient.py
228 lines (175 loc) · 7.57 KB
/
udpclient.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
import json
import random
import socket
import sys
import threading
from cmd import Cmd
from typing import Union
# Create a UDP socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Technically, this is not necessary for client but recvfrom() will complain without it.
client.bind(('localhost', random.randint(8000, 9000)))
class MBSClientShell(Cmd):
prompt = ''
intro = '\nWelcome to the CSNETWK Message Board System.\nType /help or /? to list commands.\n\nTo exit the program, enter /quit.\n'
server_address = ()
# Normally, recvfrom() is expected to be after sendto().
# However, because we may receive messages at any time, not just after sending data, we need to run it in a separate thread.
# That's because recvfrom() is blocking. If we run it in the main thread, the program will not be able to accept user input.
def _receive(self):
while True:
# Note: Modifying outside variables in this thread may not be thread-safe.
# print('waiting to receive message')
data, address = client.recvfrom(1024)
# print('received %s bytes from %s' % (len(data), address))
# print(data.decode())
# Expect JSON
response = json.loads(data.decode())
# print(response)
# Error and information
if response['command'] == 'error':
print(f"Error: {response['message']}")
continue
if response['command'] == 'info':
print(response['message'])
continue
# Process receive chain of the commands
if response['command'] == 'msg':
print(f"[From {response['handle']}]: {response['message']}")
elif response['command'] == 'all':
print(f"{response['handle']}: {response['message']}")
def validate_command(self, command_args: str, required_arg_count: int) -> Union[bool, list]:
split = command_args.split(maxsplit=1)
if not split:
print("Error: No arguments passed in command")
return False
elif len(split) != required_arg_count:
print("Error: Invalid number of arguments")
return False
return split
def precmd(self, line: str) -> str:
if line:
if line[0] == '/':
line = line[1:]
else:
print("Error: Command must start with '/'")
line = ''
return super().precmd(line)
def emptyline(self) -> None:
# https://docs.python.org/3/library/cmd.html#cmd.Cmd.emptyline
# Do not repeat last command when user presses enter with no input
pass
def do_join(self, arg: str) -> None:
""" Join a Message Board Server\n Syntax: /join <ip> <port>"""
# Basic error checking
args = self.validate_command(arg, 2)
if not args:
return
# Command specific error checking
if self.server_address:
print("Error: Already connected to server")
return
try:
self.server_address = (args[0], int(args[1]))
except ValueError:
print("Error: Invalid port number")
return
request = json.dumps({'command': 'join'})
client.sendto(request.encode(), self.server_address)
try:
data, _ = client.recvfrom(1024)
except ConnectionResetError:
self.server_address = ()
print("Error: Connection to the Message Board Server has failed! Please check IP Address and Port Number.")
return
# print('Connection to the Message Board Server is successful!')
response = json.loads(data.decode())
info = response.get('message')
print(info)
t = threading.Thread(target=self._receive)
t.start()
def do_leave(self, arg: None) -> None:
""" Leave the Message Board Server\n Syntax: /leave"""
# Command specific error checking
if not self.server_address:
print("Error: Not connected to a server.")
return
# Send data
request = json.dumps({'command': 'leave'})
client.sendto(request.encode(), self.server_address)
self.server_address = ()
# TODO: Stop the thread
def do_register(self, arg: str) -> None:
""" Register a handle with the Message Board Server\n Syntax: /register <handle>"""
# Basic error checking
if not arg:
print("Error: No handle/alias passed in command")
return
# Command specific error checking
if not self.server_address:
print("Error: Not connected to server. Use '/join <ip> <port>'")
return
# Send data
request = json.dumps({'command': 'register', 'handle': arg})
client.sendto(request.encode(), self.server_address)
def do_list(self, arg: None) -> None:
""" List all handles registered with the Message Board Server\n Syntax: /list"""
# Command specific error checking
if not self.server_address:
print("Error: Not connected to server. Use '/join <ip> <port>'")
return
# Send data
request = json.dumps({'command': 'list'})
client.sendto(request.encode(), self.server_address)
def do_msg(self, arg: str) -> None:
""" Send a message to a specific handle\n Syntax: /msg <handle> <message>"""
# Basic error checking
args = self.validate_command(arg, 2)
if not args:
return
# Command specific error checking
if not self.server_address:
# This being the 2nd error check is okay
print("Error: Not connected to server. Use '/join <ip> <port>'")
return
dest_handle, message = args[0], args[1]
# Send data
request = json.dumps({'command': 'msg', 'handle': dest_handle, 'message': message})
client.sendto(request.encode(), self.server_address)
# print(f"[To {dest_handle}]: {message}") # handled in receive() thread
def do_all(self, arg: str) -> None:
""" Send a message to all clients (incl. unregistered ones)\n Syntax: /all <message>"""
# Basic error checking
if not arg:
print("Error: No message passed in command")
return
# Command specific error checking
if not self.server_address:
# This being the 2nd error check is okay
print("Error: Not connected to server. Use '/join <ip> <port>'")
return
message = arg
# Send data
request = json.dumps({'command': 'all', 'message': message})
client.sendto(request.encode(), self.server_address)
def do_help(self, arg: str) -> bool | None:
if not arg:
names = self.get_names()
names.sort()
for name in names:
if name[:3] == 'do_':
if getattr(self, name).__doc__:
print(f"\n{name[3:]}\n{getattr(self, name).__doc__}")
print()
else:
return super().do_help(arg)
# This is necessary because CTRL+C will not interrupt recvfrom() at least on Windows.
def do_quit(self, arg: None) -> None: # This is necessary
# close socket
client.close()
# TODO: gracefully handle thread exit
# exit program
sys.exit()
# t = threading.Thread(target=receive)
# t.start()
MBSClientShell().cmdloop()