Skip to content

Commit

Permalink
Merge pull request #14 from ihmcrobotics/feature/cleanup-transport-setup
Browse files Browse the repository at this point in the history
Improve understandability of participant transport setup.
  • Loading branch information
calvertdw committed May 7, 2024
2 parents 75e074c + 385dae0 commit 933bc50
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions ihmc-ros2-library/src/main/java/us/ihmc/ros2/ROS2NodeInterface.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package us.ihmc.ros2;

import java.net.InetAddress;
import java.util.Arrays;
import java.util.UUID;
import java.util.function.Consumer;

import com.eprosima.xmlschemas.fastrtps_profiles.AddressListType;
import com.eprosima.xmlschemas.fastrtps_profiles.RtpsTransportDescriptorType;

import us.ihmc.log.LogTools;
Expand All @@ -24,45 +24,42 @@ static ParticipantAttributes createParticipantAttributes(int domainId, boolean u
{
ParticipantAttributes participantAttributes = ParticipantAttributes.create().domainId(domainId).discoveryLeaseDuration(Time.Infinite);

boolean restrictedToAddress = false;
if (addressRestriction != null)
// Always override the transport so we're sure what it is
participantAttributes.useBuiltinTransports(false);

// If this is false then shared memory will be disabled
if (useSharedMemory)
{
if (addressRestriction.length > 0)
{
if (addressRestriction[0] != null) // Check for null on the first element, to make sure passing in null works as usual -> no address restrictions
{
participantAttributes.bindToAddressRestrictions(useSharedMemory, Arrays.asList(addressRestriction));
restrictedToAddress = true;
}
}
participantAttributes.addSharedMemoryTransport();
}

// If not restricted to an address, disable shared memory if useSharedMemory is false
if(!restrictedToAddress)
{
if(!useSharedMemory)
{
// Disable default transports
participantAttributes.useBuiltinTransports(false);

// Add custom UDPv4 transport
String transportName = UUID.randomUUID().toString();
RtpsTransportDescriptorType transportDescriptor = new RtpsTransportDescriptorType();
transportDescriptor.setTransportId(transportName);
transportDescriptor.setType("UDPv4");

participantAttributes.addTransport(transportDescriptor);
// Add custom UDPv4 transport
String transportName = UUID.randomUUID().toString();
RtpsTransportDescriptorType transportDescriptor = new RtpsTransportDescriptorType();
transportDescriptor.setTransportId(transportName);
transportDescriptor.setType("UDPv4");

// Apply address restrictions
// Check for null on the first element, to make sure passing in null works as usual -> no address restrictions
if (addressRestriction != null && addressRestriction.length > 0 && addressRestriction[0] != null)
{
AddressListType addressWhitelist = new AddressListType();
for (InetAddress address : addressRestriction)
{
addressWhitelist.getAddress().add(address.getHostAddress());
}
transportDescriptor.setInterfaceWhiteList(addressWhitelist);
}


participantAttributes.addTransport(transportDescriptor);

return participantAttributes;
}

static boolean useSHMFromEnvironment()
{
String disableSharedMemoryTransportEnv = System.getenv("ROS_DISABLE_SHARED_MEMORY_TRANSPORT");
if(disableSharedMemoryTransportEnv == null)
if (disableSharedMemoryTransportEnv == null)
{
return false;
}
Expand Down

0 comments on commit 933bc50

Please sign in to comment.