def receive_message(self, sender_client, message): if message.command == "READ": # return the content of the requested address if FileManager.file_exists(message.network_path): page_content = FileManager.file_read(message.network_path) message.command = "READ_RESULT" message.content = page_content else: # specific message when the page does not exist yet message.command = "NOT_EXISTING" message.content = "" sender_client.send_message(message) elif message.command == "WRITE": # TODO: handle writing errors page_content, change_comment = message.content # add a new entry to the log file filelog = FileChangeLog(message.network_path) filelog.add_log(message.username, change_comment) filelog.save() # add a new version of the file in the history files new_idx = filelog.lastlog_index() oldfilename = message.network_path + ".old" + str(new_idx) FileManager.file_write(oldfilename, page_content) # write the new content at the address FileManager.file_write(message.network_path, page_content) # send a message indicating that writing is done message = NetworkMessage(message.network_path, "WRITE_DONE", "") sender_client.send_message(message) elif message.command == "READ_LOG": if FileManager.file_exists(message.network_path): filelog = FileChangeLog(message.network_path) if filelog != []: message.command = "READ_LOG_RESULT" message.content = filelog.changelog sender_client.send_message(message) else: # TODO : error case not supposed to happen (log should exist when the page exists) pass else: # specific message when the page does not exist yet pass
def read(self): log_path = self.filename + ".log" if FileManager.file_exists(log_path): self.changelog = FileManager.file_read(log_path)