def generate_log(self, msg_obj: Message): """ Generates logs based on incoming messages. """ date = str(datetime.now()) cmd = msg_obj.get_cmd() src = msg_obj.get_src() dst = msg_obj.get_dst() info = f"Log: {date} -> cmd:{cmd}, src:{src}, dst: {dst}" msg = { "method": "addLineLog", "params": { "info": info }, } return Message.format("info", msg_obj.get_src(), "FILE_MAN", msg).replace("\\", "")
def __execute(self, request): request_obj = Message(request) #send request log request_log = self.__kernel.generate_log(request_obj) self.__file_man_socket.send(request_log.encode()) print("< request log response: " + self.__file_man_socket.recv( self.__BUFFER_SIZE).decode('UTF-8').replace('\n', '')) cmd = request_obj.get_cmd() dst = request_obj.get_dst() response = "" if cmd == "info": if dst == "APP": self.__app_socket.send(request.encode()) response = self.__app_socket.recv( self.__BUFFER_SIZE).decode('UTF-8').replace('\n', '') elif dst == "FILE_MAN": self.__file_man_socket.send(request.encode()) response = self.__file_man_socket.recv( self.__BUFFER_SIZE).decode('UTF-8').replace('\n', '') else: response_msg = { "body": f"error: no such destination {dst} for command {cmd}" } response = Message.format("send", "KERNEL", request_obj.get_src(), response_msg) elif cmd == "stop": if dst == "KERNEL": #closing FILE_MAN request = Message.format("stop", "KERNEL", "FILE_MAN", {"body": "Close. BYE"}) self.__file_man_socket.send(request.encode()) response = self.__file_man_socket.recv( self.__BUFFER_SIZE).decode('UTF-8').replace('\n', '') print(f"< {response}") #closing APP request = Message.format("stop", "KERNEL", "APP", {"body": "Close. BYE"}) self.__app_socket.send(request.encode()) response = self.__app_socket.recv( self.__BUFFER_SIZE).decode('UTF-8').replace('\n', '') print(f"< {response}") #send response to close GUI response = Message.format("stop", "KERNEL", "GUI", {"body": "Close. BYE"}) self.__app_socket.close() self.__file_man_socket.close() else: response_msg = { "body": f"error: you cannot stop single modules" } response = Message.format("send", "KERNEL", request_obj.get_src(), response_msg) else: response_msg = {"body": f"error: no such command {cmd}"} response = Message.format("send", "KERNEL", request_obj.get_src(), response_msg) body = request_obj.get_msg() method = body['method'] # it can be closed if the request cmd is 'stop' if not self.__file_man_socket._closed and method != "readLogFile": print(f"< {self.__address}: {response}") #Send response log response_obj = Message(response) response_log = self.__kernel.generate_log(response_obj) self.__file_man_socket.send(response_log.encode()) print("< response log response: " + self.__file_man_socket.recv( self.__BUFFER_SIZE).decode('UTF-8').replace('\n', '')) return response