def __init__(self, recv_queue, send_queues): self.connection = Connection() self.receiver = SecureRpcReceiver(recv_queue, self.connection) self.senders = [] for queue in send_queues: print("Linking senders to: {}".format(queue)) self.senders.append(SecureRpcSender(queue, Connection()))
class ReplicaProtocol: def __init__(self, recv_queue): self.connection = Connection() self.receiver = SecureRpcReceiver(recv_queue, self.connection) def start_receiving(self, callback_app, callback_wr): self.callback_app = callback_app self.callback_wr = callback_wr self.receiver.restart_queue() self.receiver.start_receiving(self.data_read) def data_read(self, reply_to, cor_id, msg): [folder_to_read, file_to_read, data, mode] = json.loads(msg) if mode == APPEND: reply = self.callback_app(folder_to_read, file_to_read, data) self.receiver.reply(cor_id, reply_to, reply) elif mode == WRITE: reply = self.callback_wr(folder_to_read, file_to_read, data) reply = WRITE_OK print("Replying to writer: {}".format(reply)) self.receiver.reply(cor_id, reply_to, reply) print("Replied successfully")
class RequesterProtocol: def __init__(self, recv_queue, state_saver): self.connection = Connection() self.receiver = SecureRpcReceiver(recv_queue, self.connection) self.state_saver = state_saver def start_connection(self, connection_id): self.connection_id = connection_id self.receiver.start_receiving(self.data_read) def data_read(self, reply_to, cor_id, msg): [msg, conn_id] = json.loads(msg) print("Received msg: {}".format(msg)) if msg == RESTART: if self.state_saver.is_duplicated("STATE", conn_id): print("Duplicated message: {}".format(conn_id)) return #Close and it will be raised eternally self.receiver.reply(cor_id, reply_to, RESTART) self.state_saver.save_state( "STATE", conn_id, json.dumps([self.connection_id, "RESTART"])) self.receiver.close() elif msg == REQUEST_PLACES: reply = self.connection_id self.receiver.reply(cor_id, reply_to, reply)
class ReadProtocol: def __init__(self, recv_queue): self.connection = Connection() self.receiver = SecureRpcReceiver(recv_queue, self.connection) def start_receiving(self, callback): self.callback = callback self.receiver.start_receiving(self.data_read) def data_read(self, reply_to, cor_id, msg): [folder_to_read, file_to_read] = json.loads(msg) reply = self.callback(folder_to_read, file_to_read) self.receiver.reply(cor_id, reply_to, reply)
class WriteProtocol: def __init__(self, recv_queue, send_queues): self.connection = Connection() self.receiver = SecureRpcReceiver(recv_queue, self.connection) self.senders = [] for queue in send_queues: print("Linking senders to: {}".format(queue)) self.senders.append(SecureRpcSender(queue, Connection())) def start_receiving(self, callback_app, callback_wr): self.callback_app = callback_app self.callback_wr = callback_wr print("Starting to receive Write calls") self.receiver.start_receiving(self.data_read) def replicate_data(self, folder_to_write, file_to_write, data, mode): pending_ack = len(self.senders) msg = [folder_to_write, file_to_write, data, mode] for sender in self.senders: print("Sending replica to: {}".format(sender)) try: answer = sender.send(json.dumps(msg)) print("Received answer: {}".format(answer)) if answer == WRITE_OK: pending_ack -= 1 print("Received replica from {}".format(sender)) except: pending_ack -= 1 print("Exception in replica") print("Data replicated") if pending_ack == 0: return WRITE_OK return FAILED def data_read(self, reply_to, cor_id, msg): [folder_to_read, file_to_read, data, operation] = json.loads(msg) if operation == APPEND: reply = self.callback_app(folder_to_read, file_to_read, data) self.receiver.reply(cor_id, reply_to, reply) elif operation == WRITE: reply = self.callback_wr(folder_to_read, file_to_read, data) self.receiver.reply(cor_id, reply_to, reply)
def __init__(self, recv_queue): self.connection = Connection() self.receiver = SecureRpcReceiver(recv_queue, self.connection)