Commit 486b0858 authored by Simon Spannagel's avatar Simon Spannagel
Browse files

Messenger: avoid self-dipatch: never send a message to the module itself.

parent 1a9ae602
Loading
Loading
Loading
Loading
+11 −8
Original line number Diff line number Diff line
@@ -32,12 +32,15 @@ Messenger::~Messenger() {
}
#endif

// Check if the detectors match for the message and the delegate
static bool check_send(BaseMessage* message, BaseDelegate* delegate) {
// Check if the detectors match for the message and the delegate and that we don't have self-dispatch
static bool check_send(Module* source, BaseMessage* message, BaseDelegate* delegate) {
    if(delegate->getDetector() != nullptr &&
       (message->getDetector() == nullptr || delegate->getDetector()->getName() != message->getDetector()->getName())) {
        return false;
    }
    if(delegate->getUniqueName() == source->getUniqueName()) {
        return false;
    }
    return true;
}

@@ -55,25 +58,25 @@ bool Messenger::hasReceiver(Module* source, const std::shared_ptr<BaseMessage>&

    // Check if a normal specific listener exists
    for(auto& delegate : delegates_[type_idx][name]) {
        if(check_send(message.get(), delegate.get())) {
        if(check_send(source, message.get(), delegate.get())) {
            return true;
        }
    }
    // Check if a normal generic listener exists
    for(auto& delegate : delegates_[type_idx]["*"]) {
        if(check_send(message.get(), delegate.get())) {
        if(check_send(source, message.get(), delegate.get())) {
            return true;
        }
    }
    // Check if a base message specific listener exists
    for(auto& delegate : delegates_[typeid(BaseMessage)][name]) {
        if(check_send(message.get(), delegate.get())) {
        if(check_send(source, message.get(), delegate.get())) {
            return true;
        }
    }
    // Check if a base message generic listener exists
    for(auto& delegate : delegates_[typeid(BaseMessage)]["*"]) {
        if(check_send(message.get(), delegate.get())) {
        if(check_send(source, message.get(), delegate.get())) {
            return true;
        }
    }
@@ -178,7 +181,7 @@ bool LocalMessenger::dispatchMessage(Module* source,
        if(msg_name_iterator != msg_type_iterator->second.end()) {
            // Send messages only to their specific listeners
            for(const auto& delegate : msg_name_iterator->second) {
                if(check_send(message.get(), delegate.get())) {
                if(check_send(source, message.get(), delegate.get())) {
                    LOG(TRACE) << "Sending message " << allpix::demangle(type_idx.name()) << " from "
                               << source->getUniqueName() << " to " << delegate->getUniqueName();
                    // Construct BaseMessage where message should be stored
@@ -198,7 +201,7 @@ bool LocalMessenger::dispatchMessage(Module* source,
        const auto msg_name_iterator = base_msg_type_iterator->second.find(id);
        if(msg_name_iterator != base_msg_type_iterator->second.end()) {
            for(const auto& delegate : msg_name_iterator->second) {
                if(check_send(message.get(), delegate.get())) {
                if(check_send(source, message.get(), delegate.get())) {
                    LOG(TRACE) << "Sending message " << allpix::demangle(type_idx.name()) << " from "
                               << source->getUniqueName() << " to generic listener " << delegate->getUniqueName();
                    auto& dest = messages_[delegate->getUniqueName()][typeid(BaseMessage)];