blob: 18ad555a8b2764b4f2d4483da4ca107970889d88 [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Vitaly Buka17b0a8a2015-08-31 19:12:35 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Johan Euphrosine3523fdd2015-10-14 20:46:05 -07005#include "examples/provider/avahi_client.h"
Vitaly Buka17b0a8a2015-08-31 19:12:35 -07006
7#include <cstdlib>
8#include <vector>
9
10#include <avahi-common/error.h>
11
12namespace weave {
13namespace examples {
14
15namespace {
16
17void 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 Euphrosine0b7bb9f2015-09-29 01:11:21 -070024std::string GetId() {
25 return "WEAVE" + std::to_string(gethostid());
26}
27
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070028} // namespace
29
Vitaly Bukabeddc602015-09-24 15:28:03 -070030AvahiClient::AvahiClient() {
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070031 thread_pool_.reset(avahi_threaded_poll_new());
32 CHECK(thread_pool_);
33
Johan Euphrosine1b64c432015-09-25 13:55:00 -070034 LOG(INFO) << "connecting to avahi-daemon";
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070035 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 Euphrosinea1e6c002015-09-18 23:55:10 -070043 CHECK(group_) << avahi_strerror(avahi_client_errno(client_.get()))
44 << ". Check avahi-daemon configuration";
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070045}
46
Vitaly Bukabeddc602015-09-24 15:28:03 -070047AvahiClient::~AvahiClient() {
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070048 if (thread_pool_)
49 avahi_threaded_poll_stop(thread_pool_.get());
50}
51
Vitaly Bukabeddc602015-09-24 15:28:03 -070052void AvahiClient::PublishService(const std::string& service_type,
53 uint16_t port,
54 const std::vector<std::string>& txt) {
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070055 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 Buka17b0a8a2015-08-31 19:12:35 -070062 std::vector<const char*> txt_vector_ptr;
Vitaly Buka3dc2f532015-09-08 18:01:32 -070063 for (const auto& i : txt)
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070064 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 Buka3dc2f532015-09-08 18:01:32 -070071 if (prev_port_ == port && prev_type_ == service_type) {
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070072 ret = avahi_entry_group_update_service_txt_strlst(
73 group_.get(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, {}, GetId().c_str(),
Vitaly Buka3dc2f532015-09-08 18:01:32 -070074 service_type.c_str(), nullptr, txt_list.get());
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070075 CHECK_GE(ret, 0) << avahi_strerror(ret);
76 } else {
77 prev_port_ = port;
Vitaly Buka3dc2f532015-09-08 18:01:32 -070078 prev_type_ = service_type;
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070079
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 Buka3dc2f532015-09-08 18:01:32 -070085 service_type.c_str(), nullptr, nullptr, port, txt_list.get());
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070086 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 Bukabeddc602015-09-24 15:28:03 -070092void AvahiClient::StopPublishing(const std::string& service_name) {
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070093 CHECK(group_);
94 avahi_entry_group_reset(group_.get());
95}
96
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070097} // namespace examples
98} // namespace weave