fix(udp): client parse response_payload
This commit is contained in:
parent
253ccdf1ff
commit
d78eb940cd
@ -25,14 +25,16 @@ change_request_opcode: str = "010"
|
|||||||
summary_request_opcode: str = "011"
|
summary_request_opcode: str = "011"
|
||||||
help_requrest_opcode: str = "100"
|
help_requrest_opcode: str = "100"
|
||||||
|
|
||||||
# Res-code
|
# Res-code dict
|
||||||
correct_put_and_change_request_rescode: str = "000"
|
rescode_dict: dict[str, str] = {
|
||||||
correct_get_request_rescode: str = "001"
|
"000": "Put/Change Request Successful",
|
||||||
correct_summary_request_rescode: str = "010"
|
"001": "Get Request Successful",
|
||||||
file_not_error_rescode: str = "011"
|
"010": "Summary Request Successful",
|
||||||
unknown_request_rescode: str = "100"
|
"011": "File Not Found Error",
|
||||||
unsuccessful_change_rescode: str = "101"
|
"100": "Unknown Request",
|
||||||
help_rescode: str = "110"
|
"101": "Change Unsuccessful Error",
|
||||||
|
"110": "Help"
|
||||||
|
}
|
||||||
|
|
||||||
# custome type to represent the hostname(server name) and the server port
|
# custome type to represent the hostname(server name) and the server port
|
||||||
Address = Tuple[str, int]
|
Address = Tuple[str, int]
|
||||||
@ -74,35 +76,35 @@ class UDPClient:
|
|||||||
elif command == "help":
|
elif command == "help":
|
||||||
request_payload: str = help_requrest_opcode + "00000"
|
request_payload: str = help_requrest_opcode + "00000"
|
||||||
print(
|
print(
|
||||||
f"myftp> - {self.mode} - : asking for help from the server"
|
f"myftp> - {self.mode} - Asking for help from the server"
|
||||||
) if self.debug else None
|
) if self.debug else None
|
||||||
|
|
||||||
# get command handling
|
# get command handling
|
||||||
elif get_command_pattern.match(command):
|
elif get_command_pattern.match(command):
|
||||||
_, filename = command.split(" ", 1)
|
_, filename = command.split(" ", 1)
|
||||||
print(
|
print(
|
||||||
f"myftp> - {self.mode} - : getting file {filename} from the server"
|
f"myftp> - {self.mode} - : Getting file {filename} from the server"
|
||||||
) if self.debug else None
|
) if self.debug else None
|
||||||
|
|
||||||
# put command handling
|
# put command handling
|
||||||
elif put_command_pattern.match(command):
|
elif put_command_pattern.match(command):
|
||||||
_, filename = command.split(" ", 1)
|
_, filename = command.split(" ", 1)
|
||||||
print(
|
print(
|
||||||
f"myftp> - {self.mode} - : putting file {filename} into the server"
|
f"myftp> - {self.mode} - : Putting file {filename} into the server"
|
||||||
) if self.debug else None
|
) if self.debug else None
|
||||||
|
|
||||||
# summary command handling
|
# summary command handling
|
||||||
elif summary_command_pattern.match(command):
|
elif summary_command_pattern.match(command):
|
||||||
_, filename = command.split(" ", 1)
|
_, filename = command.split(" ", 1)
|
||||||
print(
|
print(
|
||||||
f"myftp> - {self.mode} - : summary file {filename} from the server"
|
f"myftp> - {self.mode} - : Summary file {filename} from the server"
|
||||||
) if self.debug else None
|
) if self.debug else None
|
||||||
|
|
||||||
# change command handling
|
# change command handling
|
||||||
elif change_command_pattern.match(command):
|
elif change_command_pattern.match(command):
|
||||||
_, old_filename, new_filename = command.split()
|
_, old_filename, new_filename = command.split()
|
||||||
print(
|
print(
|
||||||
f"myftp> - {self.mode} - : changing file named {old_filename} into {new_filename} on the server"
|
f"myftp> - {self.mode} - : Changing file named {old_filename} into {new_filename} on the server"
|
||||||
) if self.debug else None
|
) if self.debug else None
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@ -113,9 +115,9 @@ class UDPClient:
|
|||||||
|
|
||||||
client_socket.sendto(request_payload.encode("utf-8"), (self.server_name, self.server_port))
|
client_socket.sendto(request_payload.encode("utf-8"), (self.server_name, self.server_port))
|
||||||
|
|
||||||
modified_message = client_socket.recv(2048)[1:]
|
response_payload = client_socket.recv(2048)
|
||||||
|
|
||||||
print(modified_message.decode())
|
self.parse_response_payload(response_payload)
|
||||||
|
|
||||||
except ConnectionRefusedError:
|
except ConnectionRefusedError:
|
||||||
print(
|
print(
|
||||||
@ -171,6 +173,32 @@ class UDPClient:
|
|||||||
|
|
||||||
return file_list
|
return file_list
|
||||||
|
|
||||||
|
def parse_response_payload(self,
|
||||||
|
response_payload: bytes):
|
||||||
|
|
||||||
|
# we want to get the first byte as a string i.e "01010010"
|
||||||
|
first_byte: str = bin(response_payload[0])[2:].zfill(8)
|
||||||
|
rescode: str = first_byte[:3]
|
||||||
|
response_data_length: int = int(first_byte[-5:], 2)
|
||||||
|
response_data: bytes = response_payload[1:]
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"myftp> - {self.mode} - First_byte from server response: {first_byte}. Rescode: {rescode}. Data length: {response_data_length}"
|
||||||
|
) if self.debug else None
|
||||||
|
|
||||||
|
try:
|
||||||
|
print(
|
||||||
|
f"myftp> - {self.mode} - Res-code meaning: {rescode_dict[rescode]}"
|
||||||
|
) if self.debug else None
|
||||||
|
except KeyError:
|
||||||
|
print(
|
||||||
|
f"myftp> - {self.mode} - Res-code does not have meaning"
|
||||||
|
)
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"myftp> - {self.mode} - {response_data.decode()}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_address_input() -> Address:
|
def get_address_input() -> Address:
|
||||||
while True:
|
while True:
|
||||||
|
@ -40,7 +40,7 @@ class UDPServer:
|
|||||||
self.server_socket.bind((self.server_name, self.server_port))
|
self.server_socket.bind((self.server_name, self.server_port))
|
||||||
|
|
||||||
print(
|
print(
|
||||||
f"myftp> - {self.mode} - server is ready to receive at {self.server_name}:{self.server_port}"
|
f"myftp> - {self.mode} - Server is ready to receive at {self.server_name}:{self.server_port}"
|
||||||
) if self.debug else None
|
) if self.debug else None
|
||||||
|
|
||||||
shut_down = False
|
shut_down = False
|
||||||
@ -51,12 +51,21 @@ class UDPServer:
|
|||||||
request_payload = message.decode()
|
request_payload = message.decode()
|
||||||
|
|
||||||
print(
|
print(
|
||||||
f"myftp> - {self.mode} - received message from client at {clientAddress}: {request_payload}"
|
f"myftp> - {self.mode} ------------------------------------------------------------------"
|
||||||
|
) if self.debug else None
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"myftp> - {self.mode} - Received message from client at {clientAddress}: {request_payload}"
|
||||||
) if self.debug else None
|
) if self.debug else None
|
||||||
|
|
||||||
# check for connectivity
|
# check for connectivity
|
||||||
if request_payload == "ping":
|
if request_payload == "ping":
|
||||||
self.server_socket.sendto("pong".encode(), clientAddress)
|
self.server_socket.sendto("pong".encode(), clientAddress)
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"myftp> - {self.mode} - pong sent back to client"
|
||||||
|
) if self.debug else None
|
||||||
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# list files available on server
|
# list files available on server
|
||||||
@ -70,8 +79,9 @@ class UDPServer:
|
|||||||
# help request handling
|
# help request handling
|
||||||
elif request_payload == help_requrest_opcode + "00000":
|
elif request_payload == help_requrest_opcode + "00000":
|
||||||
print(
|
print(
|
||||||
f"myftp> - {self.mode} - received help request"
|
f"myftp> - {self.mode} - Client message parsed. Received help request"
|
||||||
) if self.debug else None
|
) if self.debug else None
|
||||||
|
|
||||||
rescode = help_rescode
|
rescode = help_rescode
|
||||||
response_data_string = "get,put,summary,change,help,bye"
|
response_data_string = "get,put,summary,change,help,bye"
|
||||||
|
|
||||||
@ -84,22 +94,24 @@ class UDPServer:
|
|||||||
self.server_socket.sendto(payload, clientAddress)
|
self.server_socket.sendto(payload, clientAddress)
|
||||||
|
|
||||||
print(
|
print(
|
||||||
f"myftp> - {self.mode} - sent message to client at {clientAddress}: {payload}"
|
f"myftp> - {self.mode} - Sent message to client at {clientAddress}: {payload}"
|
||||||
) if self.debug else None
|
) if self.debug else None
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
shut_down = True
|
shut_down = True
|
||||||
self.server_socket.close()
|
self.server_socket.close()
|
||||||
print(f"myftp> - {self.mode} - Server shutting down\n")
|
print(f"myftp> - {self.mode} - Server shutting down")
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
print(f"myftp> - {self.mode} - Closed the server socket\n")
|
print(f"myftp> - {self.mode} - Closed the server socket")
|
||||||
|
|
||||||
# assembling the payload to send back to the client
|
# assembling the payload to send back to the client
|
||||||
def build_res_payload(self,
|
def build_res_payload(self,
|
||||||
rescode: str,
|
rescode: str,
|
||||||
response_data_string: str) -> bytes:
|
response_data_string: str) -> bytes:
|
||||||
|
|
||||||
|
print(f"myftp> - {self.mode} - Assembling response payload to be sent back to the client")
|
||||||
|
|
||||||
bytes_response_data = response_data_string.encode("utf-8")
|
bytes_response_data = response_data_string.encode("utf-8")
|
||||||
|
|
||||||
data_len = len(bytes_response_data)
|
data_len = len(bytes_response_data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user