async def deliver(self, message: Message): if message.type != MessageType.URB: raise ValueError( 'Uniform Reliable Broadcast handles only messages with type URB' ) if message.body not in self.ack: self.ack[message.body] = set() self.ack[message.body].add(message.sender) if message.body not in self.pending and message.body not in self.delivered: self.pending.add(message.body) message.sender = current_node() await self.beb.broadcast(message)
async def deliver(self, message: Message): if message == None: print('something') if message.type == MessageType.ONAR: m_reg = message.body if m_reg.ts > self.reg.ts: self.reg = m_reg ack_m = Message(MessageType.ACK, current_node(), None) await self.pl.send(ack_m, message.sender, timeout) elif message.type == MessageType.ACK: self.writeset.add(message.sender) else: raise ValueError( 'One-N Atomic Register handles only messages with type ONAR or ACK' )
async def propose(self, val): self.proposals.add(val) for r in range(len(nodes())): uc_m = Message(MessageType.UC, current_node(), Proposal(r, self.proposals)) await self.beb.broadcast(uc_m) while True: working: set = await failure_detector() received_from = set( [m.sender for m in self.messages.get(r, [])]) proposals = [ m.body.proposals for m in self.messages.get(r, []) ] if working.issubset(received_from): self.messages.pop(r) self.proposals = set.union(*proposals) break decision = min(self.proposals) self.proposals.clear() self.messages.clear() return decision
async def broadcast(self, message: Message): beb_m = Message(MessageType.BEB, current_node(), message) calls = [] for node in nodes(): calls.append(self.pl.send(beb_m, node, timeout)) await aio.gather(*calls)
async def write(self, val): message = Message(MessageType.ONAR, current_node(), Register(datetime.now(), val)) await self.beb.broadcast(message) await self.wait_all_ack()
async def read(self): message = Message(MessageType.ONAR, current_node(), self.reg) await self.beb.broadcast(message) await self.wait_all_ack() return self.reg.val
async def broadcast(self, message): m = Message(MessageType.APP, current_node(), message) await self.lower_layer.broadcast(m)
async def run(self): svr = await aio.start_server(self.handle_messages, current_node().addr, current_node().port) return svr.serve_forever()
async def broadcast(self, message: Message): urb_m = Message(MessageType.URB, current_node(), message) self.pending.add(urb_m.body) await self.beb.broadcast(urb_m)