blob: e689cacc3c2d75ac3991436f470e66f91462183c [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
Alex Vakulenko0022b752015-10-02 11:09:59 -070062 std::string service_name_;
Robert Gindacf92c662015-08-20 09:30:11 -070063 std::string service_type_;
64 uint16_t port_{0};
65 TxtRecord txt_;
66
67 // Must be last member to invalidate pointers before actual destruction.
68 base::WeakPtrFactory<AvahiMdnsClient> weak_ptr_factory_{this};
69
70 // Convert a {string:string} text record into something we can send over
71 // dbus.
Alex Vakulenko35d119a2015-09-10 16:16:27 -070072 static TxtRecord GetTxtRecord(const std::vector<std::string>& txt);
Robert Gindacf92c662015-08-20 09:30:11 -070073
74 void ConnectToAvahi();
75 void CreateEntryGroup();
76 void FreeEntryGroup();
77 void CreateService();
Robert Gindab50bf542015-08-24 15:27:26 -070078 void UpdateServiceTxt();
Robert Gindacf92c662015-08-20 09:30:11 -070079
80 void OnAvahiOwnerChanged(const std::string& old_owner,
81 const std::string& new_owner);
82 void OnAvahiStateChanged(int32_t state,
83 const std::string& error);
84 void OnAvahiAvailable(bool avahi_is_on_dbus);
85 void HandleAvahiStateChange(int32_t state);
86 void HandleGroupStateChanged(int32_t state,
87 const std::string& error_message);
Alex Vakulenkof0f55342015-08-18 15:51:40 -070088 DISALLOW_COPY_AND_ASSIGN(AvahiMdnsClient);
89};
90
91} // namespace buffet
92
93#endif // BUFFET_AVAHI_MDNS_CLIENT_H_