blob: 42908764ab7802265b062f693ca594fe49da190e [file] [log] [blame]
Qingsi Wang09619332018-09-12 22:51:55 -07001/*
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
Mirko Bonadei4a427422019-03-01 15:30:43 +010018#include "rtc_base/async_invoker.h"
19#include "rtc_base/ip_address.h"
20#include "rtc_base/location.h"
Qingsi Wang09619332018-09-12 22:51:55 -070021#include "rtc_base/mdns_responder_interface.h"
Mirko Bonadei4a427422019-03-01 15:30:43 +010022#include "rtc_base/thread.h"
Qingsi Wang09619332018-09-12 22:51:55 -070023
24namespace webrtc {
25
Qingsi Wang7852d292018-10-31 11:17:07 -070026class FakeMdnsResponder : public MdnsResponderInterface {
Qingsi Wang09619332018-09-12 22:51:55 -070027 public:
Qingsi Wang3ea7b832018-11-06 17:51:02 -080028 explicit FakeMdnsResponder(rtc::Thread* thread) : thread_(thread) {}
Qingsi Wang7852d292018-10-31 11:17:07 -070029 ~FakeMdnsResponder() = default;
Qingsi Wang09619332018-09-12 22:51:55 -070030
31 void CreateNameForAddress(const rtc::IPAddress& addr,
32 NameCreatedCallback callback) override {
33 std::string name;
34 if (addr_name_map_.find(addr) != addr_name_map_.end()) {
35 name = addr_name_map_[addr];
36 } else {
37 name = std::to_string(next_available_id_++) + ".local";
38 addr_name_map_[addr] = name;
39 }
Qingsi Wang3ea7b832018-11-06 17:51:02 -080040 invoker_.AsyncInvoke<void>(
41 RTC_FROM_HERE, thread_,
42 [callback, addr, name]() { callback(addr, name); });
Qingsi Wang09619332018-09-12 22:51:55 -070043 }
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 Wang3ea7b832018-11-06 17:51:02 -080050 bool result = it != addr_name_map_.end();
51 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, thread_,
52 [callback, result]() { callback(result); });
Qingsi Wang09619332018-09-12 22:51:55 -070053 }
54
Qingsi Wang1dac6d82018-12-12 15:28:47 -080055 rtc::IPAddress GetMappedAddressForName(const std::string& name) const {
56 for (const auto& addr_name_pair : addr_name_map_) {
57 if (addr_name_pair.second == name) {
58 return addr_name_pair.first;
59 }
60 }
61 return rtc::IPAddress();
62 }
63
Qingsi Wang09619332018-09-12 22:51:55 -070064 private:
65 uint32_t next_available_id_ = 0;
66 std::map<rtc::IPAddress, std::string> addr_name_map_;
Qingsi Wang3ea7b832018-11-06 17:51:02 -080067 rtc::Thread* thread_;
68 rtc::AsyncInvoker invoker_;
Qingsi Wang09619332018-09-12 22:51:55 -070069};
70
71} // namespace webrtc
72
73#endif // RTC_BASE_FAKE_MDNS_RESPONDER_H_