def _enqueue_msg(self, msg, sendor, sendee): msg_type = ffi.getctype(ffi.typeof(msg)) msg_size = ffi.sizeof(msg[0]) new_msg = ffi.cast(ffi.typeof(msg), lib.malloc(msg_size)) ffi.memmove(new_msg, msg, msg_size) if msg_type == 'msg_appendentries_t *': size_of_entries = ffi.sizeof(ffi.getctype('msg_entry_t')) * new_msg.n_entries new_msg.entries = ffi.cast('msg_entry_t*', lib.malloc(size_of_entries)) ffi.memmove(new_msg.entries, msg.entries, size_of_entries) self.messages.append(Message(new_msg, sendor, sendee))
def poll_message(self, msg): msg_type = ffi.getctype(ffi.typeof(msg.data)) if msg_type == 'msg_appendentries_t *': node = lib.raft_get_node(msg.sendee.raft, msg.sendor.id) response = ffi.new('msg_appendentries_response_t*') e = lib.raft_recv_appendentries(msg.sendee.raft, node, msg.data, response) if lib.RAFT_ERR_SHUTDOWN == e: logging.error('Catastrophic') print(msg.sendee.debug_log()) print(msg.sendor.debug_log()) sys.exit(1) elif lib.RAFT_ERR_NEEDS_SNAPSHOT == e: pass # TODO: pretend as if snapshot works else: self.enqueue_msg(response, msg.sendee, msg.sendor) elif msg_type == 'msg_appendentries_response_t *': node = lib.raft_get_node(msg.sendee.raft, msg.sendor.id) lib.raft_recv_appendentries_response(msg.sendee.raft, node, msg.data) elif msg_type == 'msg_requestvote_t *': response = ffi.new('msg_requestvote_response_t*') node = lib.raft_get_node(msg.sendee.raft, msg.sendor.id) lib.raft_recv_requestvote(msg.sendee.raft, node, msg.data, response) self.enqueue_msg(response, msg.sendee, msg.sendor) elif msg_type == 'msg_requestvote_response_t *': node = lib.raft_get_node(msg.sendee.raft, msg.sendor.id) e = lib.raft_recv_requestvote_response(msg.sendee.raft, node, msg.data) if lib.RAFT_ERR_SHUTDOWN == e: msg.sendor.shutdown() else: assert False
def _enqueue_msg(self, msg, sendor, sendee): msg_type = ffi.getctype(ffi.typeof(msg)) msg_size = ffi.sizeof(msg[0]) new_msg = ffi.cast(ffi.typeof(msg), lib.malloc(msg_size)) ffi.memmove(new_msg, msg, msg_size) if msg_type == 'msg_appendentries_t *': new_msg.entries = lib.raft_entry_array_deepcopy( msg.entries, msg.n_entries) self.messages.append(Message(new_msg, sendor, sendee))