eclipse-cyclonedds / cyclonedds

Eclipse Cyclone DDS project
https://projects.eclipse.org/projects/iot.cyclonedds
Other
864 stars 354 forks source link

How could we set a DDS "hello Pub/Sub" between two remote system via IP route in C? #1665

Open giovanniDCK opened 1 year ago

giovanniDCK commented 1 year ago

Do we have example or some documentation on how to set up this DDSI sub/pub example for two system ip connected? according to what I read it seams that I have to create some file and set up the participant on both side not with the DDS_DOMAIN_DEFAULT but I am missing something . the adapted hello example would help me to understand....

eboasson commented 1 year ago

Hi @giovanniDCK, I am afraid I don't quite understand your question. If you have two machines that are on the same network, multicast works, firewalls are not blocking DDS traffic, and Cyclone picks that network, then the default configuration should be perfectly fine. You can use a domain id like 0 in the call to dds_create_participant (or you can pass in DDS_DOMAIN_DEFAULT which then defaults to 0 in the absence of a configuration file).

So what does "Cyclone picks that network" mean? It defaults to using only a single network interface, and it picks it by ranking the network interfaces. Multicast capable, non-loopback interfaces get the highest priority, and so in the case of a machine with a single ethernet or WiFi, it usually just works.

If you have to override this, then you have to provide the configuration file. You can just follow the README, creating an XML file that selects the desired network interface and set the CYCLONEDDS_URI environment variable to that file. Other than that, nothing changes.

Maybe you know all that and you've got it working, but now you need to avoid that configuration file because you're on some embedded platform without a file system? It is possible to do that, but it is not something I would recommend to do if there is no really good reason to. This creates a domain passing in the XML configuration directly, and this goes a step further and side-steps all XML parsing. The latter one is rather unergonomic and not documented very well. You'd have to look at the configuration tables and match it with struct ddsi_config ...

zhangyuran-gg commented 9 months ago

Hi, I want to set up pub/sub on two hosts that are not on the same LAN, what should I do? Looking forward to your reply!

zhangyuran-gg commented 9 months ago

If you have to override this, then you have to provide the configuration file. You can just follow the README, creating an XML file that selects the desired network interface and set the CYCLONEDDS_URI environment variable to that file. Other than that, nothing changes.

I specified the address of the interface export CYCLONEDDS_URI="<CycloneDDS><Domain><General><Interfaces><NetworkInterface address='192.168.0.164'/></Interfaces></General></Domain></CycloneDDS>" but the following error occured. ./HelloworldPublisher 1705821506.919583 [0] Helloworld: 192.168.0.164: does not match an available interface 1705821506.919637 [] Helloworld: dds_create_participant: Error Aborted (core dumped) Why is this?

eboasson commented 8 months ago

Hi @zhangyuran-gg

I want to set up pub/sub on two hosts that are not on the same LAN, what should I do? Looking forward to your reply!

There are two things you typically have to do, one is make sure you use the correct interface (trivial if there is only one), and the second is that they can/will communicate. The first is easy, like this:

<Interfaces><NetworkInterface address='192.168.0.164'/></Interfaces>

instructs it to use a network interface with the address 192.168.0.164. The error message you get suggests that this is not the IP address of the machine you're running it on. You might want to check that.

For the second it is generally best to start in the simplest possible configuration (networking-wise), which is: unicast only. It is not super-friendly to configure that, but it is not very hard either:

<General>
  <Interfaces>...</Interfaces>
  <AllowMulticast>false</AllowMulticast>
</General>
<Discovery>
  <ParticipantIndex>auto</ParticipantIndex>
  <Peers>
    <Peer address="a"/>
    <Peer address="b"/>
    ...
  </Peers>
</Discovery>

should be a useful template. The ParticipantIndex bit is annoying ... and has annoyed a lot of people, too, and then it is even more annoying that it defaults to allowing up to 10 participants on a machine. That latter restriction is something I had been working on fix before the holidays and I am trying to get back to it. But for now, it sometimes needs a bit of handholding.

If you follow this template, then I would expect it to work if the IP packets get routed correctly and there a no firewalls making communication impossible.

zhangyuran-gg commented 8 months ago

@eboasson Thank you for your answer! Now it can work under the LAN, but still not on the WAN. I run HelloworldSubscriber on the cloud server, it has a public IP 120.26.38.125 and a LAN IP 192.168.0.33, and I used the following configuration:

<CycloneDDS>
    <Domain id="102">
        <Discovery>
            <ParticipantIndex>auto</ParticipantIndex>
            <Peers>
                <Peer address="192.168.0.121"/>
            </Peers>
        </Discovery>

        <General>
            <Interfaces><NetworkInterface address='192.168.0.33'/></Interfaces>
            <AllowMulticast>false</AllowMulticast>
        </General>
    </Domain>
</CycloneDDS>

I run HelloworldPublisher on my local host, and its IP is 192.168.0.121, and I used the following configuration:

<CycloneDDS>
    <Domain id="102">
        <Discovery>
            <ParticipantIndex>auto</ParticipantIndex>
            <Peers>
                <Peer address="120.26.38.125"/>
            </Peers>
        </Discovery>

        <General>
            <Interfaces><NetworkInterface address='192.168.0.121'/></Interfaces>
            <AllowMulticast>false</AllowMulticast>
        </General>
    </Domain>
</CycloneDDS>

I am still very confused about the pub/sub under the WAN. Hope to receive your reply.