Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 1 | /* |
| 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 Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 23 | #include <base/memory/weak_ptr.h> |
| 24 | #include <dbus/bus.h> |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 25 | |
| 26 | #include "buffet/mdns_client.h" |
| 27 | |
| 28 | namespace buffet { |
| 29 | |
| 30 | // Publishes privet service on mDns using Avahi. |
| 31 | class AvahiMdnsClient : public MdnsClient { |
| 32 | public: |
Alex Vakulenko | 7f019f8 | 2015-08-21 19:30:36 -0700 | [diff] [blame] | 33 | explicit AvahiMdnsClient(const scoped_refptr<dbus::Bus>& bus); |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 34 | ~AvahiMdnsClient() override; |
| 35 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame^] | 36 | // weave::provider::DnsServiceDiscovery implementation. |
Alex Vakulenko | 35d119a | 2015-09-10 16:16:27 -0700 | [diff] [blame] | 37 | void PublishService(const std::string& service_type, uint16_t port, |
| 38 | const std::vector<std::string>& txt) override; |
Robert Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 39 | void StopPublishing(const std::string& service_type) override; |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 40 | |
| 41 | private: |
Robert Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 42 | using TxtRecord = std::vector<std::vector<uint8_t>>; |
| 43 | |
| 44 | // States used to track progress of our asynchronous dbus operations. |
Alex Vakulenko | 7f019f8 | 2015-08-21 19:30:36 -0700 | [diff] [blame] | 45 | enum AsyncState { |
Robert Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 46 | UNDEF, |
| 47 | PENDING, |
| 48 | READY, |
| 49 | ERROR |
Alex Vakulenko | 7f019f8 | 2015-08-21 19:30:36 -0700 | [diff] [blame] | 50 | }; |
Robert Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 51 | |
| 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 Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 62 | 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 Vakulenko | 35d119a | 2015-09-10 16:16:27 -0700 | [diff] [blame] | 71 | static TxtRecord GetTxtRecord(const std::vector<std::string>& txt); |
Robert Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 72 | |
| 73 | void ConnectToAvahi(); |
| 74 | void CreateEntryGroup(); |
| 75 | void FreeEntryGroup(); |
| 76 | void CreateService(); |
Robert Ginda | b50bf54 | 2015-08-24 15:27:26 -0700 | [diff] [blame] | 77 | void UpdateServiceTxt(); |
Robert Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 78 | |
| 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 Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 87 | DISALLOW_COPY_AND_ASSIGN(AvahiMdnsClient); |
| 88 | }; |
| 89 | |
| 90 | } // namespace buffet |
| 91 | |
| 92 | #endif // BUFFET_AVAHI_MDNS_CLIENT_H_ |