blob: 10286201ae0b90f1f660896730909acf25524f07 [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#include <weave/mdns.h>
26
27#include "buffet/mdns_client.h"
28
29namespace buffet {
30
31// Publishes privet service on mDns using Avahi.
32class AvahiMdnsClient : public MdnsClient {
33 public:
Robert Gindacf92c662015-08-20 09:30:11 -070034 explicit AvahiMdnsClient(const scoped_refptr<dbus::Bus> &bus);
Alex Vakulenkof0f55342015-08-18 15:51:40 -070035 ~AvahiMdnsClient() override;
36
37 // weave::Mdns implementation.
Robert Gindacf92c662015-08-20 09:30:11 -070038 void PublishService(const std::string& service_type,
Alex Vakulenkof0f55342015-08-18 15:51:40 -070039 uint16_t port,
40 const std::map<std::string, std::string>& txt) override;
Robert Gindacf92c662015-08-20 09:30:11 -070041 void StopPublishing(const std::string& service_type) override;
Alex Vakulenkof0f55342015-08-18 15:51:40 -070042
43 private:
Robert Gindacf92c662015-08-20 09:30:11 -070044 using TxtRecord = std::vector<std::vector<uint8_t>>;
45
46 // States used to track progress of our asynchronous dbus operations.
47 typedef enum {
48 UNDEF,
49 PENDING,
50 READY,
51 ERROR
52 } AsyncState;
53
54 scoped_refptr<dbus::Bus> bus_;
55 dbus::ObjectProxy* avahi_{nullptr};
56 // The avahi interface we use to add/remove mdns services.
57 dbus::ObjectProxy* entry_group_{nullptr};
58
59 // State of our dbus connection to avahi.
60 AsyncState avahi_state_{UNDEF};
61 // State of the group/service publish operation.
62 AsyncState service_state_{UNDEF};
63
64 std::string service_name_;
65 std::string service_type_;
66 uint16_t port_{0};
67 TxtRecord txt_;
68
69 // Must be last member to invalidate pointers before actual destruction.
70 base::WeakPtrFactory<AvahiMdnsClient> weak_ptr_factory_{this};
71
72 // Convert a {string:string} text record into something we can send over
73 // dbus.
74 TxtRecord GetTxtRecord(const std::map<std::string, std::string>& txt);
75
76 void ConnectToAvahi();
77 void CreateEntryGroup();
78 void FreeEntryGroup();
79 void CreateService();
80
81 void OnAvahiOwnerChanged(const std::string& old_owner,
82 const std::string& new_owner);
83 void OnAvahiStateChanged(int32_t state,
84 const std::string& error);
85 void OnAvahiAvailable(bool avahi_is_on_dbus);
86 void HandleAvahiStateChange(int32_t state);
87 void HandleGroupStateChanged(int32_t state,
88 const std::string& error_message);
Alex Vakulenkof0f55342015-08-18 15:51:40 -070089 DISALLOW_COPY_AND_ASSIGN(AvahiMdnsClient);
90};
91
92} // namespace buffet
93
94#endif // BUFFET_AVAHI_MDNS_CLIENT_H_