Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef RTC_BASE_FAKE_MDNS_RESPONDER_H_ |
| 12 | #define RTC_BASE_FAKE_MDNS_RESPONDER_H_ |
| 13 | |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | |
Ali Tofigh | d389078 | 2022-04-29 12:02:22 | [diff] [blame] | 18 | #include "absl/strings/string_view.h" |
Mirko Bonadei | 4a42742 | 2019-03-01 14:30:43 | [diff] [blame] | 19 | #include "rtc_base/ip_address.h" |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 20 | #include "rtc_base/mdns_responder_interface.h" |
Mirko Bonadei | 4a42742 | 2019-03-01 14:30:43 | [diff] [blame] | 21 | #include "rtc_base/thread.h" |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
Niels Möller | c964d80 | 2021-03-25 08:31:26 | [diff] [blame] | 25 | // This class posts tasks on the given `thread` to invoke callbacks. It's the |
| 26 | // callback's responsibility to be aware of potential destruction of state it |
| 27 | // depends on, e.g., using WeakPtrFactory or PendingTaskSafetyFlag. |
Qingsi Wang | 7852d29 | 2018-10-31 18:17:07 | [diff] [blame] | 28 | class FakeMdnsResponder : public MdnsResponderInterface { |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 29 | public: |
Qingsi Wang | 3ea7b83 | 2018-11-07 01:51:02 | [diff] [blame] | 30 | explicit FakeMdnsResponder(rtc::Thread* thread) : thread_(thread) {} |
Qingsi Wang | 7852d29 | 2018-10-31 18:17:07 | [diff] [blame] | 31 | ~FakeMdnsResponder() = default; |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 32 | |
| 33 | void CreateNameForAddress(const rtc::IPAddress& addr, |
| 34 | NameCreatedCallback callback) override { |
| 35 | std::string name; |
| 36 | if (addr_name_map_.find(addr) != addr_name_map_.end()) { |
| 37 | name = addr_name_map_[addr]; |
| 38 | } else { |
| 39 | name = std::to_string(next_available_id_++) + ".local"; |
| 40 | addr_name_map_[addr] = name; |
| 41 | } |
Danil Chapovalov | 5286dcf | 2022-07-18 15:04:56 | [diff] [blame] | 42 | thread_->PostTask([callback, addr, name]() { callback(addr, name); }); |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 43 | } |
| 44 | void RemoveNameForAddress(const rtc::IPAddress& addr, |
| 45 | NameRemovedCallback callback) override { |
| 46 | auto it = addr_name_map_.find(addr); |
| 47 | if (it != addr_name_map_.end()) { |
| 48 | addr_name_map_.erase(it); |
| 49 | } |
Qingsi Wang | 3ea7b83 | 2018-11-07 01:51:02 | [diff] [blame] | 50 | bool result = it != addr_name_map_.end(); |
Danil Chapovalov | 5286dcf | 2022-07-18 15:04:56 | [diff] [blame] | 51 | thread_->PostTask([callback, result]() { callback(result); }); |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 52 | } |
| 53 | |
Ali Tofigh | d389078 | 2022-04-29 12:02:22 | [diff] [blame] | 54 | rtc::IPAddress GetMappedAddressForName(absl::string_view name) const { |
Qingsi Wang | 1dac6d8 | 2018-12-12 23:28:47 | [diff] [blame] | 55 | for (const auto& addr_name_pair : addr_name_map_) { |
| 56 | if (addr_name_pair.second == name) { |
| 57 | return addr_name_pair.first; |
| 58 | } |
| 59 | } |
| 60 | return rtc::IPAddress(); |
| 61 | } |
| 62 | |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 63 | private: |
| 64 | uint32_t next_available_id_ = 0; |
| 65 | std::map<rtc::IPAddress, std::string> addr_name_map_; |
Niels Möller | c964d80 | 2021-03-25 08:31:26 | [diff] [blame] | 66 | rtc::Thread* const thread_; |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | } // namespace webrtc |
| 70 | |
| 71 | #endif // RTC_BASE_FAKE_MDNS_RESPONDER_H_ |