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 | |
Casey Dahlin | 494b724 | 2015-12-14 11:42:47 -0800 | [diff] [blame] | 23 | #include <avahi-client/client.h> |
| 24 | #include <avahi-client/publish.h> |
| 25 | #include <avahi-common/thread-watch.h> |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 26 | |
| 27 | #include "buffet/mdns_client.h" |
| 28 | |
| 29 | namespace buffet { |
| 30 | |
| 31 | // Publishes privet service on mDns using Avahi. |
| 32 | class AvahiMdnsClient : public MdnsClient { |
| 33 | public: |
Casey Dahlin | 494b724 | 2015-12-14 11:42:47 -0800 | [diff] [blame] | 34 | explicit AvahiMdnsClient(); |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 35 | ~AvahiMdnsClient() override; |
| 36 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 37 | // weave::provider::DnsServiceDiscovery implementation. |
Alex Vakulenko | 35d119a | 2015-09-10 16:16:27 -0700 | [diff] [blame] | 38 | void PublishService(const std::string& service_type, uint16_t port, |
| 39 | const std::vector<std::string>& txt) override; |
Robert Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 40 | void StopPublishing(const std::string& service_type) override; |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 41 | |
| 42 | private: |
Alex Vakulenko | 3dae2c9 | 2015-12-17 11:21:09 -0800 | [diff] [blame] | 43 | static void OnAvahiClientStateUpdate(AvahiClient* s, |
| 44 | AvahiClientState state, |
| 45 | void* userdata); |
| 46 | void RepublishService(); |
| 47 | |
Casey Dahlin | 494b724 | 2015-12-14 11:42:47 -0800 | [diff] [blame] | 48 | uint16_t prev_port_{0}; |
Alex Vakulenko | 3dae2c9 | 2015-12-17 11:21:09 -0800 | [diff] [blame] | 49 | std::string prev_service_type_; |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 50 | std::string service_name_; |
Alex Vakulenko | 3dae2c9 | 2015-12-17 11:21:09 -0800 | [diff] [blame] | 51 | std::vector<std::string> txt_records_; |
Casey Dahlin | 494b724 | 2015-12-14 11:42:47 -0800 | [diff] [blame] | 52 | std::unique_ptr<AvahiThreadedPoll, decltype(&avahi_threaded_poll_free)> |
| 53 | thread_pool_{nullptr, &avahi_threaded_poll_free}; |
| 54 | std::unique_ptr< ::AvahiClient, decltype(&avahi_client_free)> client_{ |
| 55 | nullptr, &avahi_client_free}; |
| 56 | std::unique_ptr<AvahiEntryGroup, decltype(&avahi_entry_group_free)> group_{ |
| 57 | nullptr, &avahi_entry_group_free}; |
Robert Ginda | cf92c66 | 2015-08-20 09:30:11 -0700 | [diff] [blame] | 58 | |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 59 | DISALLOW_COPY_AND_ASSIGN(AvahiMdnsClient); |
| 60 | }; |
| 61 | |
| 62 | } // namespace buffet |
| 63 | |
| 64 | #endif // BUFFET_AVAHI_MDNS_CLIENT_H_ |