2022-02-02 23:08:25 +01:00
|
|
|
from net.UnicastConnection import *
|
|
|
|
from model.Protocol import *
|
|
|
|
import traceback
|
|
|
|
import os.path
|
2022-02-02 01:43:38 +01:00
|
|
|
|
2022-03-08 16:30:16 +01:00
|
|
|
|
2022-02-02 23:08:25 +01:00
|
|
|
class AppController:
|
2022-02-02 01:43:38 +01:00
|
|
|
def __init__(self, parser):
|
|
|
|
self.parser = parser
|
2022-02-02 23:08:25 +01:00
|
|
|
self.connection = UnicastConnection()
|
2022-02-02 01:43:38 +01:00
|
|
|
self.window_controller = None
|
|
|
|
|
2022-02-02 23:08:25 +01:00
|
|
|
def ssl_ca_path(self, ca_path):
|
|
|
|
self.connection.set_ca_path(ca_path)
|
|
|
|
|
2022-02-02 01:43:38 +01:00
|
|
|
def set_window_controller(self, window_controller):
|
|
|
|
self.window_controller = window_controller
|
|
|
|
|
2022-02-02 23:08:25 +01:00
|
|
|
def on_sign(self, host, port, login, passw, tls, register=False):
|
|
|
|
print("[AppController::on_sign]")
|
|
|
|
try:
|
|
|
|
if not self.connection.is_connected:
|
2022-03-08 16:30:16 +01:00
|
|
|
print("[AppController::on_sign] connecting ... calling UnicastConnection::connect")
|
|
|
|
self.connect(host, port, tls)
|
2022-02-02 23:08:25 +01:00
|
|
|
if not register:
|
|
|
|
message = self.parser.build_SIGNIN(login, passw)
|
|
|
|
else:
|
|
|
|
message = self.parser.build_SIGNUP(login, passw)
|
|
|
|
self.connection.send_message(message)
|
|
|
|
line = self.connection.read_line()
|
|
|
|
message_id = self.parser.parse(line, False)
|
|
|
|
if message_id == Protocol.PARSE_SIGNOK:
|
|
|
|
self.window_controller.switch_connected_mode()
|
|
|
|
self.show_message("Connection done", False)
|
2022-02-26 18:26:12 +01:00
|
|
|
self.on_filelist()
|
2022-02-02 23:08:25 +01:00
|
|
|
elif message_id == Protocol.PARSE_SIGNERROR:
|
|
|
|
self.show_message("Error during SIGN_IN / SIGN_UP", True)
|
2022-02-26 18:26:12 +01:00
|
|
|
self.connection.close()
|
2022-02-02 23:08:25 +01:00
|
|
|
except Exception as ex:
|
|
|
|
print(ex)
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
def get_filename_size_from_item(self, item):
|
|
|
|
tokens = item.split('!')
|
|
|
|
if len(tokens) == 2:
|
2022-03-08 16:30:16 +01:00
|
|
|
return (tokens[0], tokens[1])
|
2022-02-02 23:08:25 +01:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def on_filelist(self):
|
|
|
|
print("[AppController::on_filelist]")
|
|
|
|
try:
|
|
|
|
if self.connection.is_connected:
|
|
|
|
message = self.parser.build_FILELIST()
|
|
|
|
self.connection.send_message(message)
|
|
|
|
line = self.connection.read_line()
|
|
|
|
message_id = self.parser.parse(line, False)
|
|
|
|
if message_id == Protocol.PARSE_FILES:
|
|
|
|
tokens = self.parser.parse_FILES(line)
|
|
|
|
if len(tokens) == 1:
|
|
|
|
self.window_controller.clear_all_file_on_window()
|
|
|
|
allfiles = tokens[0].split()
|
|
|
|
for f in allfiles:
|
|
|
|
(filename, filesize) = self.get_filename_size_from_item(f)
|
|
|
|
self.window_controller.add_file_on_window([filename, filesize])
|
|
|
|
except Exception as ex:
|
|
|
|
print(ex)
|
|
|
|
traceback.print_exc()
|
|
|
|
|
2022-02-02 01:43:38 +01:00
|
|
|
def on_quit(self):
|
2022-02-02 23:08:25 +01:00
|
|
|
if self.connection.is_connected:
|
|
|
|
self.connection.send_message(self.parser.build_SIGNOUT())
|
|
|
|
|
|
|
|
def on_fileupload(self, filepath):
|
|
|
|
if os.path.isfile(filepath):
|
|
|
|
size = os.path.getsize(filepath)
|
|
|
|
print(f"[AppController::on_fileupload] file={filepath}; size={size}")
|
|
|
|
message = self.parser.build_SAVEFILE(os.path.basename(filepath), size)
|
|
|
|
self.connection.send_message(message)
|
|
|
|
self.connection.send_file(filepath)
|
|
|
|
line = self.connection.read_line()
|
|
|
|
message_id = self.parser.parse(line, False)
|
|
|
|
if message_id == Protocol.PARSE_SAVEFILEOK:
|
|
|
|
self.show_message("File saved", False)
|
|
|
|
self.on_filelist()
|
|
|
|
elif message_id == Protocol.PARSE_SAVEFILEERROR:
|
|
|
|
self.show_message("Error during saving file !", True)
|
|
|
|
else:
|
|
|
|
self.show_message(f"Error: file {filepath} not found !", True)
|
|
|
|
|
|
|
|
def on_filedownload(self, filename, savepath):
|
|
|
|
if os.path.isdir(savepath):
|
|
|
|
print(f"[AppController::on_filedownload] file={filename}; path={savepath}")
|
|
|
|
message = self.parser.build_GETFILE(filename)
|
|
|
|
self.connection.send_message(message)
|
|
|
|
line = self.connection.read_line()
|
|
|
|
message_id = self.parser.parse(line, False)
|
|
|
|
if message_id == Protocol.PARSE_GETFILEOK:
|
|
|
|
tokens = self.parser.parse_GETFILEOK(line)
|
|
|
|
if len(tokens) == 2:
|
|
|
|
self.connection.receive_file(tokens[0], savepath, int(tokens[1]))
|
|
|
|
self.show_message("File downloaded !", False)
|
|
|
|
else:
|
|
|
|
self.show_message(f"Bad file information\n{tokens}")
|
|
|
|
elif message_id == Protocol.PARSE_GETFILEERROR:
|
|
|
|
self.show_message(f"Cannot download the file", True)
|
|
|
|
else:
|
|
|
|
self.show_message(f"Error: directory {savepath} not found !", True)
|
|
|
|
|
|
|
|
def on_filedelete(self, filename):
|
|
|
|
print(f"[AppController::on_filedelete] file={filename}")
|
|
|
|
message = self.parser.build_REMOVEFILE(filename)
|
|
|
|
self.connection.send_message(message)
|
|
|
|
line = self.connection.read_line()
|
|
|
|
message_id = self.parser.parse(line, False)
|
|
|
|
if message_id == Protocol.PARSE_REMOVEFILEOK:
|
|
|
|
self.show_message("File deleted successfully !", False)
|
|
|
|
self.on_filelist()
|
|
|
|
elif message_id == Protocol.PARSE_REMOVEFILEERROR:
|
|
|
|
self.show_message("Error while erasing file.", True)
|
|
|
|
|
2022-03-08 16:30:16 +01:00
|
|
|
def show_message(self, message, is_error):
|
2022-02-02 23:08:25 +01:00
|
|
|
self.window_controller.show_message(message, is_error)
|
|
|
|
|
|
|
|
def connect(self, host, port, tls):
|
|
|
|
try:
|
|
|
|
self.connection.connect(host, port, tls)
|
|
|
|
if tls and UnicastConnection.STRICT_SSL_VALIDATION:
|
|
|
|
self.show_message(self.connection.ssl_info, False)
|
|
|
|
elif tls and not UnicastConnection.STRICT_SSL_VALIDATION:
|
|
|
|
self.show_message("SSL/TLS enabled, but certificate NOT checked !", False)
|
|
|
|
else:
|
|
|
|
self.show_message("No SSL connection", False)
|
|
|
|
|
|
|
|
except Exception as ex:
|
|
|
|
self.show_message(f"Error during connection !\n{ex}", True)
|
|
|
|
self.connection = UnicastConnection()
|