def _propagateBroadcast(cls, broadcast): assert isinstance(broadcast, BroadCast) prop = broadcast.getNextTargets() payload = broadcast.getMessage() cls.logger.debug("Propagating message %s to %s", broadcast.__class__.__name__, repr(prop)) # Only do something if there is something to do if len(prop) > 0: if len(prop) <= Consts.PROPAGATION_RATE: #in the end, send the actual message for p in prop: mes = copy.deepcopy(payload) mes.targetId = p mes.recipientId = p #if we know the target, send the message if ProbeStorage.isKnownId(p): cls.send(mes) elif ProbeStorage.isKnownId(broadcast.sourceId): #try to avoid breaking the chain during broadcasts #send back the payload to the initial source of the broadcast if we don't know this recipient # (forwarding at initial host will work) mes.recipientId = broadcast.sourceId cls.send(mes) else: mes.recipientId = ProbeStorage.getOtherRandomId() cls.send(mes) else: pRate = Consts.PROPAGATION_RATE # take targets for first hop out of the list sendTo = prop[0:pRate] pt = prop[pRate:] propTargets = [pt[i::pRate] for i in range(pRate)] for i, firstHop in enumerate(sendTo): nextHops = propTargets[i] #copy the sourceId of the original message when chaining m = BroadCast(firstHop, broadcast.sourceId, payload, nextHops) if ProbeStorage.isKnownId(firstHop): cls.send(m) elif ProbeStorage.isKnownId(broadcast.sourceId): m.recipientId = broadcast.sourceId cls.send(m) else: m.recipientId = ProbeStorage.getOtherRandomId() cls.send(m)
def treatMessage(cls, message): """Handles the receptions of a Message (called by the listener) For regular Actions, addTask is called after translation of the message For TesterMessages and TesteeMessages, treatTestMessage is called :param message: The Message instance to treat """ cls.logger.debug("Treating message %s", message.__class__.__name__) assert isinstance(message, Message) # forwarding mechanism if message.targetId != Identification.PROBE_ID: if ProbeStorage.isKnownId(message.targetId): message.recipientId = message.targetId else: #if we already forwarded it before , stop here if message.hash in cls.forwardedMessages: cls.logger.warning( "Throwing message %s in forward because message was previously forwarded.", message.__class__.__name__) return Scheduler.forward() message.recipientId = ProbeStorage.getOtherRandomId() cls.logger.info("Forwarding message %s for %s to id %s", message.__class__.__name__, message.targetId, message.recipientId) cls.forwardedMessages.append(message.hash) Client.send(message) return # handle special class of messages separately if isinstance(message, TestMessage): cls.treatTestMessage(message) elif isinstance(message, WatcherMessage): cls.treatWatcherMessage(message) elif isinstance(message, BroadCast): # broadcast = do required action first and continue broadcast cls.logger.ddebug("Handling Broadcast") try: ActionMan.addTask(MTA.toAction(message.getMessage())) except ActionError: pass # be sure to propagate broadcast if a reasonable error occurs ActionMan.addTask(MTA.toAction(message)) # Client.broadcast(message) else: # handles everything else, including Do messages ActionMan.addTask(MTA.toAction(message))
def treatMessage(cls, message): """Handles the receptions of a Message (called by the listener) For regular Actions, addTask is called after translation of the message For TesterMessages and TesteeMessages, treatTestMessage is called :param message: The Message instance to treat """ cls.logger.debug("Treating message %s", message.__class__.__name__) assert isinstance(message, Message) # forwarding mechanism if message.targetId != Identification.PROBE_ID: if ProbeStorage.isKnownId(message.targetId): message.recipientId = message.targetId else: #if we already forwarded it before , stop here if message.hash in cls.forwardedMessages: cls.logger.warning("Throwing message %s in forward because message was previously forwarded.", message.__class__.__name__) return Scheduler.forward() message.recipientId = ProbeStorage.getOtherRandomId() cls.logger.info("Forwarding message %s for %s to id %s", message.__class__.__name__, message.targetId, message.recipientId) cls.forwardedMessages.append(message.hash) Client.send(message) return # handle special class of messages separately if isinstance(message, TestMessage): cls.treatTestMessage(message) elif isinstance(message, WatcherMessage): cls.treatWatcherMessage(message) elif isinstance(message, BroadCast): # broadcast = do required action first and continue broadcast cls.logger.ddebug("Handling Broadcast") try: ActionMan.addTask(MTA.toAction(message.getMessage())) except ActionError: pass # be sure to propagate broadcast if a reasonable error occurs ActionMan.addTask(MTA.toAction(message)) # Client.broadcast(message) else: # handles everything else, including Do messages ActionMan.addTask(MTA.toAction(message))
def sendMessage(self, message): """Send this message using the Params.PROTOCOL :param message: The message to send """ if not ProbeStorage.isKnownId(message.recipientId): self.logger.warning("The probe %s is not currently known to me, message will not be sent", message.targetId) return self.logger.debug("Sending the message : %s for %s to %s with ip %s", message.__class__.__name__, message.getTarget(), message.recipientId, ProbeStorage.getProbeById(message.recipientId).getIp()) try: Retry.retry(times = Consts.SEND_RETRY, interval = Consts.SEND_RETRY_INTERVAL, failure = ProbeConnectionException, eraise = ProbeConnectionException )(self.sender.send)(message) except ProbeConnectionException as e: raise SendError(e)
def sendMessage(self, message): """Send this message using the Params.PROTOCOL :param message: The message to send """ if not ProbeStorage.isKnownId(message.recipientId): self.logger.warning( "The probe %s is not currently known to me, message will not be sent", message.targetId) return self.logger.debug( "Sending the message : %s for %s to %s with ip %s", message.__class__.__name__, message.getTarget(), message.recipientId, ProbeStorage.getProbeById(message.recipientId).getIp()) try: Retry.retry(times=Consts.SEND_RETRY, interval=Consts.SEND_RETRY_INTERVAL, failure=ProbeConnectionException, eraise=ProbeConnectionException)( self.sender.send)(message) except ProbeConnectionException as e: raise SendError(e)