def on_message(self, msg): ''' When a message with an already knwon traversal_id is received, we try to build a duplication message and send it in to a protocol dependent recipient. This is used in contracts traversing the graph, when the contract has rereached the same shard. This message is necessary, as silently ignoring the incoming bids adds a lot of latency to the nested contracts (it is waitng to receive message from all the recipients). ''' self.log('Received message: %r', msg) # Check if it isn't expired message time_left = time.left(msg.expiration_time) if time_left < 0: self.log('Throwing away expired message. Time left: %s, ' 'msg_class: %r', time_left, msg.get_msg_class()) return False # Check for known traversal ids: if IFirstMessage.providedBy(msg): t_id = msg.traversal_id if t_id is None: self.warning( "Received corrupted message. The traversal_id is None ! " "Message: %r", msg) return False if t_id in self._traversal_ids: self.log('Throwing away already known traversal id %r, ' 'msg_class: %r', msg.get_msg_class(), t_id) recp = msg.duplication_recipient() if recp: resp = msg.duplication_message() self.send_msg(recp, resp) return False else: self._traversal_ids.set(t_id, True, msg.expiration_time) # Handle registered dialog if IDialogMessage.providedBy(msg): recv_id = msg.receiver_id if recv_id is not None and recv_id in self._protocols: protocol = self._protocols[recv_id] protocol.on_message(msg) return True # Handle new conversation coming in (interest) p_type = msg.protocol_type if p_type in self._interests: p_id = msg.protocol_id interest = self._interests[p_type].get(p_id) if interest and interest.schedule_message(msg): return True self.warning("Couldn't find appropriate protocol for message: " "%s", msg.get_msg_class()) return False
def register_interest(self, agent_factory, *args, **kwargs): agent_factory = IInterest(agent_factory) if not IFirstMessage.implementedBy(agent_factory.initiator): raise TypeError( "%r.initiator expected to implemented IFirstMessage. Got %r" %\ (agent_factory, agent_factory.initiator, )) p_type = agent_factory.protocol_type p_id = agent_factory.protocol_id if p_type not in self._interests: self._interests[p_type] = dict() if p_id in self._interests[p_type]: self.error('Already interested in %s.%s protocol', p_type, p_id) return False interest_factory = IAgencyInterestInternalFactory(agent_factory) interest = interest_factory(self, *args, **kwargs) self._interests[p_type][p_id] = interest self.debug('Registered interest in %s.%s protocol', p_type, p_id) return interest