Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The same node cannot subscribe to messages published by itself #1106

Closed
jiejieTop opened this issue May 16, 2023 · 3 comments
Closed

The same node cannot subscribe to messages published by itself #1106

jiejieTop opened this issue May 16, 2023 · 3 comments

Comments

@jiejieTop
Copy link

Problem Description

The node publishes data and subscribes to this topic in the same node, which will result in the failure to subscribe to the message and fail to enter the callback function. The test example is as follows:

/* ========================= eCAL LICENSE =================================
 *
 * Copyright (C) 2016 - 2019 Continental Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ========================= eCAL LICENSE =================================
*/

#include <ecal/ecal.h>
#include <ecal/msg/protobuf/publisher.h>
#include <ecal/msg/protobuf/subscriber.h>

#include <iostream>

#include "person.pb.h"


void OnPerson(const char* topic_name_, const pb::People::Person& person_, const long long time_, const long long clock_)
{
  std::cout << "------------------------------------------" << std::endl;
  std::cout << " HEAD "                                     << std::endl;
  std::cout << "------------------------------------------" << std::endl;
  std::cout << "topic name   : " << topic_name_             << std::endl;
  std::cout << "topic time   : " << time_                   << std::endl;
  std::cout << "topic clock  : " << clock_                  << std::endl;
  std::cout << "------------------------------------------" << std::endl;
  std::cout << " CONTENT "                                  << std::endl;
  std::cout << "------------------------------------------" << std::endl;
  std::cout << "person id    : " << person_.id()            << std::endl;
  std::cout << "person name  : " << person_.name()          << std::endl;
  std::cout << "person stype : " << person_.stype()         << std::endl;
  std::cout << "person email : " << person_.email()         << std::endl;
  std::cout << "dog.name     : " << person_.dog().name()    << std::endl;
  std::cout << "house.rooms  : " << person_.house().rooms() << std::endl;
  std::cout << "------------------------------------------" << std::endl;
  std::cout                                                 << std::endl;
}


int main(int argc, char **argv)
{
  // initialize eCAL API
  eCAL::Initialize(argc, argv, "person publisher");

  // set process state
  eCAL::Process::SetState(proc_sev_healthy, proc_sev_level1, "I feel good !");

  // create a publisher (topic name "person")
  eCAL::protobuf::CPublisher<pb::People::Person> pub("person");

  // generate a class instance of Person
  pb::People::Person person;


  // create a subscriber (topic name "person")
  eCAL::protobuf::CSubscriber<pb::People::Person> sub("person");

  // add receive callback function (_1 = topic_name, _2 = msg, _3 = time, _4 = clock, _5 = id)
  auto callback = std::bind(OnPerson, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4);
  sub.AddReceiveCallback(callback);

  // enter main loop
  auto cnt = 0;
  while(eCAL::Ok())
  {
    // set person object content
    person.set_id(++cnt);
    person.set_name("Max");
    person.set_stype(pb::People::Person_SType_MALE);
    person.set_email("max@mail.net");
    person.mutable_dog()->set_name("Brandy");
    person.mutable_house()->set_rooms(4);

    // send the person object
    pub.Send(person);

    // // print content
    // std::cout << "person id    : " << person.id()            << std::endl;
    // std::cout << "person name  : " << person.name()          << std::endl;
    // std::cout << "person stype : " << person.stype()         << std::endl;
    // std::cout << "person email : " << person.email()         << std::endl;
    // std::cout << "dog.name     : " << person.dog().name()    << std::endl;
    // std::cout << "house.rooms  : " << person.house().rooms() << std::endl;
    // std::cout                                                << std::endl;

    // sleep 500 ms
    eCAL::Process::SleepMS(500);
  }

  // finalize eCAL API
  eCAL::Finalize();

  return(0);
}

How to reproduce

  1. build samples
  2. run it

How did you get eCAL?

Ubuntu PPA (apt-get)

Environment

  • ecal: 5.11.4
  • os: ubuntu20.04

eCAL System Information

No response

@KerstinKeller
Copy link
Contributor

Hi @jiejieTop,
yes it can subscribe to traffic from the same node, but you have to enable it explicitly (I don't remember why exactly, probably to reduce load..)

  // enable loop back communication in the same thread
  eCAL::Util::EnableLoopback(true);

This function needs to be called after eCAL::Initialize() and you can communicate inside the same process.

@jiejieTop
Copy link
Author

Hi @jiejieTop, yes it can subscribe to traffic from the same node, but you have to enable it explicitly (I don't remember why exactly, probably to reduce load..)

  // enable loop back communication in the same thread
  eCAL::Util::EnableLoopback(true);

This function needs to be called after eCAL::Initialize() and you can communicate inside the same process.

Thanks a lot, this works, but why need to explicitly enable?

@rex-schilasky
Copy link
Contributor

Yes, it's following the same logic like socket communication. Normally you don't want to read your own content. We had scenarios in the past were user used the same topics for incoming and outgoing interfaces in different nodes and expected to get triggered by external publishers only.
Btw. there is another transport layer called INPROC that is default switched off. That one can be used to just call the subscribers callback by the publishers send method. But that layer has some other drawbacks (hard coupling publishers and subscriber single threaded) that finally often decrease overall performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants