Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Johan Euphrosine | 3523fdd | 2015-10-14 20:46:05 -0700 | [diff] [blame] | 5 | #include "examples/provider/avahi_client.h" |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 6 | |
| 7 | #include <cstdlib> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <avahi-common/error.h> |
| 11 | |
| 12 | namespace weave { |
| 13 | namespace examples { |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | void GroupCallback(AvahiEntryGroup* g, |
| 18 | AvahiEntryGroupState state, |
| 19 | AVAHI_GCC_UNUSED void* userdata) { |
| 20 | CHECK_NE(state, AVAHI_ENTRY_GROUP_COLLISION); |
| 21 | CHECK_NE(state, AVAHI_ENTRY_GROUP_FAILURE); |
| 22 | } |
| 23 | |
Johan Euphrosine | 0b7bb9f | 2015-09-29 01:11:21 -0700 | [diff] [blame] | 24 | std::string GetId() { |
| 25 | return "WEAVE" + std::to_string(gethostid()); |
| 26 | } |
| 27 | |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 28 | } // namespace |
| 29 | |
Vitaly Buka | beddc60 | 2015-09-24 15:28:03 -0700 | [diff] [blame] | 30 | AvahiClient::AvahiClient() { |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 31 | thread_pool_.reset(avahi_threaded_poll_new()); |
| 32 | CHECK(thread_pool_); |
| 33 | |
Johan Euphrosine | 1b64c43 | 2015-09-25 13:55:00 -0700 | [diff] [blame] | 34 | LOG(INFO) << "connecting to avahi-daemon"; |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 35 | int ret = 0; |
| 36 | client_.reset(avahi_client_new(avahi_threaded_poll_get(thread_pool_.get()), |
| 37 | {}, nullptr, this, &ret)); |
| 38 | CHECK(client_) << avahi_strerror(ret); |
| 39 | |
| 40 | avahi_threaded_poll_start(thread_pool_.get()); |
| 41 | |
| 42 | group_.reset(avahi_entry_group_new(client_.get(), GroupCallback, nullptr)); |
Johan Euphrosine | a1e6c00 | 2015-09-18 23:55:10 -0700 | [diff] [blame] | 43 | CHECK(group_) << avahi_strerror(avahi_client_errno(client_.get())) |
| 44 | << ". Check avahi-daemon configuration"; |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Vitaly Buka | beddc60 | 2015-09-24 15:28:03 -0700 | [diff] [blame] | 47 | AvahiClient::~AvahiClient() { |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 48 | if (thread_pool_) |
| 49 | avahi_threaded_poll_stop(thread_pool_.get()); |
| 50 | } |
| 51 | |
Vitaly Buka | beddc60 | 2015-09-24 15:28:03 -0700 | [diff] [blame] | 52 | void AvahiClient::PublishService(const std::string& service_type, |
| 53 | uint16_t port, |
| 54 | const std::vector<std::string>& txt) { |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 55 | LOG(INFO) << "Publishing service"; |
| 56 | CHECK(group_); |
| 57 | |
| 58 | // Create txt record. |
| 59 | std::unique_ptr<AvahiStringList, decltype(&avahi_string_list_free)> txt_list{ |
| 60 | nullptr, &avahi_string_list_free}; |
| 61 | if (!txt.empty()) { |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 62 | std::vector<const char*> txt_vector_ptr; |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 63 | for (const auto& i : txt) |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 64 | txt_vector_ptr.push_back(i.c_str()); |
| 65 | txt_list.reset(avahi_string_list_new_from_array(txt_vector_ptr.data(), |
| 66 | txt_vector_ptr.size())); |
| 67 | CHECK(txt_list); |
| 68 | } |
| 69 | |
| 70 | int ret = 0; |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 71 | if (prev_port_ == port && prev_type_ == service_type) { |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 72 | ret = avahi_entry_group_update_service_txt_strlst( |
| 73 | group_.get(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, {}, GetId().c_str(), |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 74 | service_type.c_str(), nullptr, txt_list.get()); |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 75 | CHECK_GE(ret, 0) << avahi_strerror(ret); |
| 76 | } else { |
| 77 | prev_port_ = port; |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 78 | prev_type_ = service_type; |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 79 | |
| 80 | avahi_entry_group_reset(group_.get()); |
| 81 | CHECK(avahi_entry_group_is_empty(group_.get())); |
| 82 | |
| 83 | ret = avahi_entry_group_add_service_strlst( |
| 84 | group_.get(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, {}, GetId().c_str(), |
Vitaly Buka | 3dc2f53 | 2015-09-08 18:01:32 -0700 | [diff] [blame] | 85 | service_type.c_str(), nullptr, nullptr, port, txt_list.get()); |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 86 | CHECK_GE(ret, 0) << avahi_strerror(ret); |
| 87 | ret = avahi_entry_group_commit(group_.get()); |
| 88 | CHECK_GE(ret, 0) << avahi_strerror(ret); |
| 89 | } |
| 90 | } |
| 91 | |
Vitaly Buka | beddc60 | 2015-09-24 15:28:03 -0700 | [diff] [blame] | 92 | void AvahiClient::StopPublishing(const std::string& service_name) { |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 93 | CHECK(group_); |
| 94 | avahi_entry_group_reset(group_.get()); |
| 95 | } |
| 96 | |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 97 | } // namespace examples |
| 98 | } // namespace weave |