blob: 681bd3b8ed504d4f473209258bd3072ca4c44058 [file] [log] [blame]
Alex Vakulenkof0f55342015-08-18 15:51:40 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef BUFFET_AVAHI_MDNS_CLIENT_H_
18#define BUFFET_AVAHI_MDNS_CLIENT_H_
19
20#include <map>
21#include <string>
22
Robert Gindacf92c662015-08-20 09:30:11 -070023#include <base/memory/weak_ptr.h>
24#include <dbus/bus.h>
Alex Vakulenkof0f55342015-08-18 15:51:40 -070025
26#include "buffet/mdns_client.h"
27
28namespace buffet {
29
30// Publishes privet service on mDns using Avahi.
31class AvahiMdnsClient : public MdnsClient {
32 public:
Alex Vakulenko7f019f82015-08-21 19:30:36 -070033 explicit AvahiMdnsClient(const scoped_refptr<dbus::Bus>& bus);
Alex Vakulenkof0f55342015-08-18 15:51:40 -070034 ~AvahiMdnsClient() override;
35
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070036 // weave::provider::DnsServiceDiscovery implementation.
Alex Vakulenko35d119a2015-09-10 16:16:27 -070037 void PublishService(const std::string& service_type, uint16_t port,
38 const std::vector<std::string>& txt) override;
Robert Gindacf92c662015-08-20 09:30:11 -070039 void StopPublishing(const std::string& service_type) override;
Alex Vakulenkof0f55342015-08-18 15:51:40 -070040
41 private:
Robert Gindacf92c662015-08-20 09:30:11 -070042 using TxtRecord = std::vector<std::vector<uint8_t>>;
43
44 // States used to track progress of our asynchronous dbus operations.
Alex Vakulenko7f019f82015-08-21 19:30:36 -070045 enum AsyncState {
Robert Gindacf92c662015-08-20 09:30:11 -070046 UNDEF,
47 PENDING,
48 READY,
49 ERROR
Alex Vakulenko7f019f82015-08-21 19:30:36 -070050 };
Robert Gindacf92c662015-08-20 09:30:11 -070051
52 scoped_refptr<dbus::Bus> bus_;
53 dbus::ObjectProxy* avahi_{nullptr};
54 // The avahi interface we use to add/remove mdns services.
55 dbus::ObjectProxy* entry_group_{nullptr};
56
57 // State of our dbus connection to avahi.
58 AsyncState avahi_state_{UNDEF};
59 // State of the group/service publish operation.
60 AsyncState service_state_{UNDEF};
61
Robert Gindacf92c662015-08-20 09:30:11 -070062 std::string service_type_;
63 uint16_t port_{0};
64 TxtRecord txt_;
65
66 // Must be last member to invalidate pointers before actual destruction.
67 base::WeakPtrFactory<AvahiMdnsClient> weak_ptr_factory_{this};
68
69 // Convert a {string:string} text record into something we can send over
70 // dbus.
Alex Vakulenko35d119a2015-09-10 16:16:27 -070071 static TxtRecord GetTxtRecord(const std::vector<std::string>& txt);
Robert Gindacf92c662015-08-20 09:30:11 -070072
73 void ConnectToAvahi();
74 void CreateEntryGroup();
75 void FreeEntryGroup();
76 void CreateService();
Robert Gindab50bf542015-08-24 15:27:26 -070077 void UpdateServiceTxt();
Robert Gindacf92c662015-08-20 09:30:11 -070078
79 void OnAvahiOwnerChanged(const std::string& old_owner,
80 const std::string& new_owner);
81 void OnAvahiStateChanged(int32_t state,
82 const std::string& error);
83 void OnAvahiAvailable(bool avahi_is_on_dbus);
84 void HandleAvahiStateChange(int32_t state);
85 void HandleGroupStateChanged(int32_t state,
86 const std::string& error_message);
Alex Vakulenkof0f55342015-08-18 15:51:40 -070087 DISALLOW_COPY_AND_ASSIGN(AvahiMdnsClient);
88};
89
90} // namespace buffet
91
92#endif // BUFFET_AVAHI_MDNS_CLIENT_H_