def post(self, recipients, message): if not isinstance(message, BaseMessage): raise ValueError("Expected second argument to be " "f.a.b.BaseMessage, got %r instead" % (type(message), )) recipients = recipient.IRecipients(recipients) if IDialogMessage.providedBy(message): reply_to = message.reply_to if reply_to is None: reply_to = recipient.Recipient(self._queue_name, self._sink.get_shard_id(), self.channel_type) message.reply_to = reply_to elif reply_to.channel != self.channel_type: self.error("Invalid reply_to for messaging backend, " "dropping %d instance(s) of message: %r", len(list(recipients)), message) return defers = [] for recip in recipients: assert recip.channel == self.channel_type, \ "Unexpected channel type" self.log('Sending message to %r', recip) d = self._messaging.publish(recip.key, recip.route, message) defers.append(d) return defer.DeferredList(defers)
def dispatch(self, uri, msg): self.log('Tunnel received message from uri: %s.', uri) self._pending_dispatches += 1 if IDialogMessage.providedBy(msg): recp = msg.reply_to self.log("Message is a dialog message, recipient: %r", recp) if recp not in self._uris or self._uris[recp] != uri: self.debug("Uri not known or doesn't match the recipient, " "registering external route") self._channel.create_external_route('tunnel', recipient=recp, uri=uri) time.call_next(self._dispatch_message, msg)
def post(self, recipients, message): if not isinstance(message, BaseMessage): raise ValueError("Expected second argument to be " "f.a.b.BaseMessage, got %r instead" % (type(message),)) message.message_id = str(uuid.uuid1()) recipients = recipient.IRecipients(recipients) if IDialogMessage.providedBy(message): if message.reply_to is None: message.reply_to = self._get_own_address() for recip in recipients: self.log("Sending message to %r", recip) msg = message.clone() msg.recipient = recip self._messaging.dispatch(msg)
def post(self, recipients, message): if not isinstance(message, BaseMessage): raise ValueError("Expected second argument to be " "f.a.b.BaseMessage, got %r instead" % (type(message), )) if IDialogMessage.providedBy(message): reply_to = message.reply_to if reply_to is None: reply_to = recipient.Recipient(self._sink.get_agent_id(), self._backend.route, self.channel_type) message.reply_to = reply_to elif ((reply_to.type is not RecipientType.agent) or (reply_to.channel != self.channel_type)): self.error("Invalid reply_to for tunneling, " "dropping %d instance(s) of message: %r", len(list(IRecipients(recipients))), message) return return self._backend.post(recipients, message)
def on_message(self, msg): """ When a message with an already known 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 reached again the same shard. This message is necessary, as silently ignoring the incoming bids adds a lot of latency to the nested contracts (it is waiting 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 duplicated message if msg.message_id in self._message_ids: self.log("Throwing away duplicated message %r", msg.get_msg_class()) return False else: self._message_ids.set(msg.message_id, True, msg.expiration_time) # 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", t_id, msg.get_msg_class()) recp = msg.duplication_recipient() if recp: resp = msg.duplication_message() self.post(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._agency_agent._protocols: protocol = self._agency_agent._protocols[recv_id] protocol.on_message(msg) return True # Handle new conversation coming in (interest) # if msg.protocol_id == 'alert': # print self._agency_agent._interests p_type = msg.protocol_type if p_type in self._agency_agent._interests: p_id = msg.protocol_id interest = self._agency_agent._interests[p_type].get(p_id) if interest and interest.schedule_message(msg): return True self.debug("Couldn't find appropriate protocol for message: " "%s", msg.get_msg_class()) return False