Skip to content

Commit

Permalink
lcm: Add ability to set the URL on construction
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnimmer-tri committed Jul 19, 2018
1 parent f7ea7f9 commit 7f9be04
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lcm/drake_lcm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }

Expand All @@ -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<double>) {
DRAKE_THROW_UNLESS(!channel.empty());
Expand Down
16 changes: 16 additions & 0 deletions lcm/drake_lcm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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<double> time_sec) override;

Expand All @@ -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<LcmReceiveThread> receive_thread_{nullptr};
std::list<HandlerFunction> handlers_;
Expand Down
9 changes: 9 additions & 0 deletions lcm/test/drake_lcm_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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

0 comments on commit 7f9be04

Please sign in to comment.