Skip to content

Commit e0e5f06

Browse files
committed
Define LOOPBACK_INTERFACE constant instead of helper function
1 parent 8327bf5 commit e0e5f06

9 files changed

Lines changed: 20 additions & 27 deletions

File tree

src/shared/constants.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ static const std::unordered_map<int, std::string> ROBOT_MULTICAST_CHANNELS = {
2222
{12, "ff02::c3d0:42d2:bb12"}, {13, "ff02::c3d0:42d2:bb13"},
2323
{14, "ff02::c3d0:42d2:bb14"}, {15, "ff02::c3d0:42d2:bb15"}};
2424

25+
// the name of the loopback network interface, which differs across platforms
26+
#ifdef __APPLE__
27+
static const std::string LOOPBACK_INTERFACE = "lo0";
28+
#else
29+
static const std::string LOOPBACK_INTERFACE = "lo";
30+
#endif
31+
2532
// PlotJuggler's default host and port
2633
static const std::string PLOTJUGGLER_GUI_DEFAULT_HOST = "ff02::c3d0:42d2:aaaa";
2734
static const short unsigned int PLOTJUGGLER_GUI_DEFAULT_PORT = 9870;

src/software/logger/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ cc_library(
146146
deps = [
147147
"//proto:visualization_cc_proto",
148148
"//shared:constants",
149-
"//software/networking/udp:network_utils",
150149
"//software/networking/udp:threaded_udp_sender",
151150
"@g3log",
152151
"@protobuf//:json_util",

src/software/logger/plotjuggler_sink.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <string>
55

66
#include "proto/visualization.pb.h"
7+
#include "shared/constants.h"
78
#include "software/logger/custom_logging_levels.h"
8-
#include "software/networking/udp/network_utils.h"
99
#include "software/networking/udp/threaded_udp_sender.h"
1010

1111

@@ -21,7 +21,7 @@ class PlotJugglerSink
2121
*
2222
* @param interface The interface to send Plotjuggler UDP packets on
2323
*/
24-
PlotJugglerSink(const std::string& interface = getLoopbackInterfaceName());
24+
PlotJugglerSink(const std::string& interface = LOOPBACK_INTERFACE);
2525

2626
~PlotJugglerSink() = default;
2727

src/software/networking/udp/BUILD

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ cc_test(
1717
],
1818
deps = [
1919
":network_utils",
20+
"//shared:constants",
2021
"//shared/test_util:tbots_gtest_main",
2122
],
2223
)
@@ -67,8 +68,8 @@ cc_test(
6768
"threaded_proto_udp_listener_test.cpp",
6869
],
6970
deps = [
70-
":network_utils",
7171
":threaded_proto_udp_listener",
72+
"//shared:constants",
7273
"//shared/test_util:tbots_gtest_main",
7374
"@protobuf//:empty_cc_proto",
7475
],
@@ -90,8 +91,8 @@ cc_test(
9091
"threaded_proto_udp_sender_test.cpp",
9192
],
9293
deps = [
93-
":network_utils",
9494
":threaded_proto_udp_sender",
95+
"//shared:constants",
9596
"//shared/test_util:tbots_gtest_main",
9697
"@protobuf//:empty_cc_proto",
9798
],

src/software/networking/udp/network_utils.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22

33
#include <arpa/inet.h>
44

5-
std::string getLoopbackInterfaceName()
6-
{
7-
#ifdef __APPLE__
8-
return "lo0";
9-
#else
10-
return "lo";
11-
#endif
12-
}
13-
145
std::optional<std::string> getLocalIp(const std::string& interface, bool ipv4)
156
{
167
struct ifaddrs* ifAddrStruct = nullptr;

src/software/networking/udp/network_utils.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55
#include <optional>
66
#include <string>
77

8-
/**
9-
* Get the name of the loopback network interface for the current platform.
10-
*
11-
* @return the loopback interface name for the current platform
12-
*/
13-
std::string getLoopbackInterfaceName();
14-
158
/**
169
* Given an interface, get the IP address associated with that interface
1710
*

src/software/networking/udp/network_utils_test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
#include <gtest/gtest.h>
44

5+
#include "shared/constants.h"
6+
57
TEST(NetworkUtilsTest, getLocalIpValidInterface)
68
{
7-
std::string interface = getLoopbackInterfaceName();
9+
std::string interface = LOOPBACK_INTERFACE;
810
std::optional<std::string> local_ip = getLocalIp(interface, true);
911
EXPECT_TRUE(local_ip);
1012
EXPECT_EQ(local_ip.value(), "127.0.0.1");

src/software/networking/udp/threaded_proto_udp_listener_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#include <gtest/gtest.h>
44

55
#include "google/protobuf/empty.pb.h"
6+
#include "shared/constants.h"
67
#include "software/networking/tbots_network_exception.h"
7-
#include "software/networking/udp/network_utils.h"
88

99
TEST(ThreadedProtoUdpListenerTest, error_finding_local_ip_address)
1010
{
@@ -19,12 +19,12 @@ TEST(ThreadedProtoUdpListenerTest, error_creating_socket)
1919
// This will always fail because it requires root privileges to open this port
2020
EXPECT_THROW(
2121
ThreadedProtoUdpListener<google::protobuf::Empty>(
22-
"224.5.23.1", 1023, getLoopbackInterfaceName(), [](const auto&) {}, true),
22+
"224.5.23.1", 1023, LOOPBACK_INTERFACE, [](const auto&) {}, true),
2323
TbotsNetworkException);
2424
}
2525

2626
TEST(ThreadedProtoUdpListenerTest, no_error_creating_socket)
2727
{
2828
ThreadedProtoUdpListener<google::protobuf::Empty>(
29-
"224.5.23.0", 40000, getLoopbackInterfaceName(), [](const auto&) {}, true);
29+
"224.5.23.0", 40000, LOOPBACK_INTERFACE, [](const auto&) {}, true);
3030
}

src/software/networking/udp/threaded_proto_udp_sender_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#include <gtest/gtest.h>
44

55
#include "google/protobuf/empty.pb.h"
6+
#include "shared/constants.h"
67
#include "software/networking/tbots_network_exception.h"
7-
#include "software/networking/udp/network_utils.h"
88

99
TEST(ThreadedProtoUdpSenderTest, error_finding_local_ip_address)
1010
{
@@ -16,5 +16,5 @@ TEST(ThreadedProtoUdpSenderTest, error_finding_local_ip_address)
1616
TEST(ThreadedProtoUdpSenderTest, no_error_creating_socket)
1717
{
1818
ThreadedProtoUdpSender<google::protobuf::Empty>("224.5.23.1", 40000,
19-
getLoopbackInterfaceName(), true);
19+
LOOPBACK_INTERFACE, true);
2020
}

0 commit comments

Comments
 (0)