refactor(client+server): simplify set up logic
- Added comments to methods
This commit is contained in:
parent
74c01a613e
commit
9a6cd398a5
@ -198,6 +198,11 @@ class Client:
|
|||||||
self.client_socket.close()
|
self.client_socket.close()
|
||||||
|
|
||||||
def parse_response_payload(self, response_payload: bytes):
|
def parse_response_payload(self, response_payload: bytes):
|
||||||
|
"""
|
||||||
|
Parse response payload for further processing
|
||||||
|
|
||||||
|
response_payload is the the entire packet that was sent from the server
|
||||||
|
"""
|
||||||
first_byte = bytes([response_payload[0]])
|
first_byte = bytes([response_payload[0]])
|
||||||
first_byte_binary = int.from_bytes(first_byte, "big")
|
first_byte_binary = int.from_bytes(first_byte, "big")
|
||||||
rescode = first_byte_binary >> 5
|
rescode = first_byte_binary >> 5
|
||||||
@ -218,6 +223,7 @@ class Client:
|
|||||||
|
|
||||||
# error rescodes
|
# error rescodes
|
||||||
if rescode in [0b011, 0b100, 0b101]:
|
if rescode in [0b011, 0b100, 0b101]:
|
||||||
|
# print to client
|
||||||
print(f"myftp> - {self.protocol} - {rescode_dict[rescode]}")
|
print(f"myftp> - {self.protocol} - {rescode_dict[rescode]}")
|
||||||
|
|
||||||
# successful rescodes
|
# successful rescodes
|
||||||
@ -230,6 +236,7 @@ class Client:
|
|||||||
# get rescode
|
# get rescode
|
||||||
elif rescode == 0b001:
|
elif rescode == 0b001:
|
||||||
self.handle_get_response_from_server(filename_length, response_data)
|
self.handle_get_response_from_server(filename_length, response_data)
|
||||||
|
# summary rescode
|
||||||
elif rescode == 0b010:
|
elif rescode == 0b010:
|
||||||
self.handle_summary_response_from_server(filename_length, response_data)
|
self.handle_summary_response_from_server(filename_length, response_data)
|
||||||
|
|
||||||
@ -264,6 +271,8 @@ class Client:
|
|||||||
self, filename_length: int, response_data: bytes
|
self, filename_length: int, response_data: bytes
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
|
Handle the get response from the server
|
||||||
|
|
||||||
Response_data is
|
Response_data is
|
||||||
File name (filename_length bytes) +
|
File name (filename_length bytes) +
|
||||||
File size (4 bytes) +
|
File size (4 bytes) +
|
||||||
@ -296,6 +305,8 @@ class Client:
|
|||||||
self, filename_length: int, response_data: bytes
|
self, filename_length: int, response_data: bytes
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
|
Handle summary response from server
|
||||||
|
|
||||||
Response_data is
|
Response_data is
|
||||||
File name (filename_length bytes) +
|
File name (filename_length bytes) +
|
||||||
File size (4 bytes) +
|
File size (4 bytes) +
|
||||||
@ -397,26 +408,15 @@ def init():
|
|||||||
user_supplied_address = get_address_input()
|
user_supplied_address = get_address_input()
|
||||||
|
|
||||||
# UDP client selected here
|
# UDP client selected here
|
||||||
if protocol_selection == "2":
|
client = Client(
|
||||||
udp_client = Client(
|
|
||||||
user_supplied_address[0],
|
user_supplied_address[0],
|
||||||
user_supplied_address[1],
|
user_supplied_address[1],
|
||||||
args.directory,
|
args.directory,
|
||||||
args.debug,
|
args.debug,
|
||||||
"UDP",
|
("UDP" if protocol_selection == "2" else "TCP"),
|
||||||
)
|
)
|
||||||
|
|
||||||
udp_client.run()
|
client.run()
|
||||||
else:
|
|
||||||
tcp_client = Client(
|
|
||||||
user_supplied_address[0],
|
|
||||||
user_supplied_address[1],
|
|
||||||
args.directory,
|
|
||||||
args.debug,
|
|
||||||
"TCP",
|
|
||||||
)
|
|
||||||
|
|
||||||
tcp_client.run()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -227,6 +227,9 @@ class Server:
|
|||||||
def process_change_req(
|
def process_change_req(
|
||||||
self, old_filename_length_in_bytes: int, req_payload: bytes
|
self, old_filename_length_in_bytes: int, req_payload: bytes
|
||||||
) -> int:
|
) -> int:
|
||||||
|
"""
|
||||||
|
Process change request from client
|
||||||
|
"""
|
||||||
old_filename = req_payload[:old_filename_length_in_bytes].decode("ascii")
|
old_filename = req_payload[:old_filename_length_in_bytes].decode("ascii")
|
||||||
new_filename_length = int.from_bytes(
|
new_filename_length = int.from_bytes(
|
||||||
req_payload[
|
req_payload[
|
||||||
@ -423,6 +426,7 @@ class Server:
|
|||||||
# we only need the firstbyte
|
# we only need the firstbyte
|
||||||
if filename is None:
|
if filename is None:
|
||||||
second_byte_to_FL_plus_five = None
|
second_byte_to_FL_plus_five = None
|
||||||
|
# second byte and more are needed
|
||||||
else:
|
else:
|
||||||
# get case
|
# get case
|
||||||
second_byte_to_FL_plus_five = (
|
second_byte_to_FL_plus_five = (
|
||||||
@ -435,11 +439,13 @@ class Server:
|
|||||||
f"myftp> - {self.protocol} - First byte assembled for rescode {format(rescode, '03b')}: {bin(int.from_bytes(first_byte, byteorder='big'))[2:]}"
|
f"myftp> - {self.protocol} - First byte assembled for rescode {format(rescode, '03b')}: {bin(int.from_bytes(first_byte, byteorder='big'))[2:]}"
|
||||||
) if self.debug else None
|
) if self.debug else None
|
||||||
|
|
||||||
|
# get/summary case
|
||||||
if second_byte_to_FL_plus_five is not None and response_data is not None:
|
if second_byte_to_FL_plus_five is not None and response_data is not None:
|
||||||
res_payload = first_byte + second_byte_to_FL_plus_five + response_data
|
res_payload = first_byte + second_byte_to_FL_plus_five + response_data
|
||||||
# help case
|
# help case
|
||||||
elif second_byte_to_FL_plus_five is None and response_data is not None:
|
elif second_byte_to_FL_plus_five is None and response_data is not None:
|
||||||
res_payload = first_byte + response_data
|
res_payload = first_byte + response_data
|
||||||
|
# change/put case
|
||||||
else:
|
else:
|
||||||
res_payload = first_byte
|
res_payload = first_byte
|
||||||
|
|
||||||
@ -507,20 +513,16 @@ def init():
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
# UDP client selected here
|
# start the server
|
||||||
if protocol_selection == "2":
|
server = Server(
|
||||||
udp_server = Server(
|
args.ip_addr,
|
||||||
args.ip_addr, args.port_number, args.directory, args.debug, "UDP"
|
args.port_number,
|
||||||
|
args.directory,
|
||||||
|
args.debug,
|
||||||
|
("UDP" if protocol_selection == "2" else "TCP"),
|
||||||
)
|
)
|
||||||
|
|
||||||
udp_server.run()
|
server.run()
|
||||||
|
|
||||||
else:
|
|
||||||
tcp_server = Server(
|
|
||||||
args.ip_addr, args.port_number, args.directory, args.debug, "TCP"
|
|
||||||
)
|
|
||||||
|
|
||||||
tcp_server.run()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user