Skip to content

Commit

Permalink
fixed: computer name contains dash which is not allowed in node names…
Browse files Browse the repository at this point in the history
…, issue #3
  • Loading branch information
atiderko committed Oct 14, 2024
1 parent cf79906 commit dedca58
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion fkie_mas_daemon/scripts/mas-remote-node.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from fkie_mas_pylib.system.host import is_local
from fkie_mas_pylib.system.host import get_hostname
from fkie_mas_pylib.system.host import get_ros_hostname
from fkie_mas_pylib.system.host import ros_host_suffix
from fkie_mas_pylib.system.url import get_port
from fkie_mas_pylib.system.supervised_popen import SupervisedPopen

Expand Down Expand Up @@ -326,7 +327,7 @@ def run_ROS2_node(package: str, executable: str, name: str, args: List[str], pre
'''

# get namespace and basename from name
namer = name.replace('{HOST}', socket.gethostname())
namer = name.replace('{HOST}', ros_host_suffix())
arg_ns = names.namespace(
namer, with_sep_suffix=False, raise_err_on_none=False)
arg_name = names.basename(namer)
Expand Down
17 changes: 16 additions & 1 deletion fkie_mas_discovery/src/eprosima_pariticipant_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <chrono>
#include <map>
#include <mutex>
#include <regex>
#include <string>
#include <vector>
#include <sstream>
Expand Down Expand Up @@ -268,8 +269,22 @@ int main(int argc, char *argv[])
rclcpp::init(argc, argv);
char hostname_chars[HOST_NAME_MAX];
gethostname(hostname_chars, HOST_NAME_MAX);
std::string hostname(hostname_chars);
// remove domain suffix
std::regex const IP4_PATTERN{"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"};
std::smatch m;
if (!std::regex_match(hostname, m, IP4_PATTERN)) {
std::size_t found = hostname.find('.');
if (found != std::string::npos) {
hostname = hostname.substr(0, found);
}
}
// replace dots and - characters in the node name
hostname = std::regex_replace(hostname, std::regex("\\."), "_");
hostname = std::regex_replace(hostname, std::regex("-"), "_");
// std::string ros_distro = getEnvironmentVariable("ROS_DISTRO");
std::string node_name = "_discovery_" + std::string(hostname_chars);

std::string node_name = "_discovery_" + hostname;
auto listener = std::make_shared<CustomParticipantListener>(node_name, "/mas");
RCLCPP_INFO(listener->get_logger(), "started");
rclcpp::spin(listener);
Expand Down
2 changes: 1 addition & 1 deletion fkie_mas_pylib/fkie_mas_pylib/system/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def ros_host_suffix(hostname: str='') -> str:
if not addr:
addr = get_host_name()
addr = subdomain(addr)
addr = addr.replace('.', '_')
addr = addr.replace('.', '_').replace('-', '_')
return addr


Expand Down

0 comments on commit dedca58

Please sign in to comment.