diff --git a/lcm/drake_lcm.cc b/lcm/drake_lcm.cc index 5e2a2a27a945..a909a7ddd825 100644 --- a/lcm/drake_lcm.cc +++ b/lcm/drake_lcm.cc @@ -21,7 +21,11 @@ void Callback(const ::lcm::ReceiveBuffer* buffer, } // namespace -DrakeLcm::DrakeLcm() {} +DrakeLcm::DrakeLcm() : DrakeLcm(std::string{}) {} + +DrakeLcm::DrakeLcm(std::string lcm_url) + : requested_lcm_url_(std::move(lcm_url)), + lcm_(requested_lcm_url_) {} DrakeLcm::~DrakeLcm() { receive_thread_.reset(); } @@ -39,6 +43,10 @@ void DrakeLcm::StopReceiveThread() { ::lcm::LCM* DrakeLcm::get_lcm_instance() { return &lcm_; } +std::string DrakeLcm::get_requested_lcm_url() const { + return requested_lcm_url_; +} + void DrakeLcm::Publish(const std::string& channel, const void* data, int data_size, optional) { DRAKE_THROW_UNLESS(!channel.empty()); diff --git a/lcm/drake_lcm.h b/lcm/drake_lcm.h index cb74c2b603c7..e3f28a4cb363 100644 --- a/lcm/drake_lcm.h +++ b/lcm/drake_lcm.h @@ -21,8 +21,18 @@ class DrakeLcm : public DrakeLcmInterface { public: DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(DrakeLcm); + /** + * Constructs using LCM's default URL (either the default hard-coded URL, or + * else LCM_DEFAULT_URL environment variable if it is set). + */ DrakeLcm(); + /** + * Constructs using the given URL. If empty, it will use the default URL as + * per the no-argument constructor. + */ + explicit DrakeLcm(std::string lcm_url); + /** * A destructor that forces the receive thread to be stopped. */ @@ -63,6 +73,11 @@ class DrakeLcm : public DrakeLcmInterface { */ ::lcm::LCM* get_lcm_instance(); + /** + * Returns the LCM URL passed into the constructor; this can be empty. + */ + std::string get_requested_lcm_url() const; + void Publish(const std::string& channel, const void* data, int data_size, optional time_sec) override; @@ -74,6 +89,7 @@ class DrakeLcm : public DrakeLcmInterface { #pragma GCC diagnostic pop // pop -Wdeprecated-declarations private: + std::string requested_lcm_url_; ::lcm::LCM lcm_; std::unique_ptr receive_thread_{nullptr}; std::list handlers_; diff --git a/lcm/test/drake_lcm_test.cc b/lcm/test/drake_lcm_test.cc index d65c2848dcfd..6badbd4fd2fe 100644 --- a/lcm/test/drake_lcm_test.cc +++ b/lcm/test/drake_lcm_test.cc @@ -225,6 +225,7 @@ TEST_F(DrakeLcmTest, SubscribeTest) { TEST_F(DrakeLcmTest, EmptyChannelTest) { DrakeLcm dut; + EXPECT_EQ(dut.get_requested_lcm_url(), ""); MessageHandler handler; EXPECT_THROW(dut.Subscribe("", &handler), std::exception); @@ -233,6 +234,14 @@ TEST_F(DrakeLcmTest, EmptyChannelTest) { EXPECT_THROW(Publish(&dut, "", message), std::exception); } + +TEST_F(DrakeLcmTest, UrlTest) { + const std::string custom_url = "udpm://239.255.66.66:6666?ttl=0"; + const DrakeLcm dut(custom_url); + + EXPECT_EQ(dut.get_requested_lcm_url(), custom_url); +} + } // namespace } // namespace lcm } // namespace drake