1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef DISCOVERY_DNSSD_IMPL_QUERIER_IMPL_H_
6 #define DISCOVERY_DNSSD_IMPL_QUERIER_IMPL_H_
7 
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <unordered_map>
12 #include <vector>
13 
14 #include "absl/hash/hash.h"
15 #include "absl/strings/string_view.h"
16 #include "discovery/dnssd/impl/constants.h"
17 #include "discovery/dnssd/impl/conversion_layer.h"
18 #include "discovery/dnssd/impl/dns_data_graph.h"
19 #include "discovery/dnssd/impl/instance_key.h"
20 #include "discovery/dnssd/impl/service_key.h"
21 #include "discovery/dnssd/public/dns_sd_instance_endpoint.h"
22 #include "discovery/dnssd/public/dns_sd_querier.h"
23 #include "discovery/mdns/mdns_record_changed_callback.h"
24 #include "discovery/mdns/mdns_records.h"
25 #include "discovery/mdns/public/mdns_service.h"
26 
27 namespace openscreen {
28 namespace discovery {
29 
30 class NetworkInterfaceConfig;
31 class ReportingClient;
32 
33 class QuerierImpl : public DnsSdQuerier, public MdnsRecordChangedCallback {
34  public:
35   // |querier|, |task_runner|, and |network_config| must outlive the QuerierImpl
36   // instance constructed.
37   QuerierImpl(MdnsService* querier,
38               TaskRunner* task_runner,
39               ReportingClient* reporting_client,
40               const NetworkInterfaceConfig* network_config);
41   ~QuerierImpl() override;
42 
43   bool IsQueryRunning(const std::string& service) const;
44 
45   // DnsSdQuerier overrides.
46   void StartQuery(const std::string& service, Callback* callback) override;
47   void StopQuery(const std::string& service, Callback* callback) override;
48   void ReinitializeQueries(const std::string& service) override;
49 
50   // MdnsRecordChangedCallback overrides.
51   std::vector<PendingQueryChange> OnRecordChanged(
52       const MdnsRecord& record,
53       RecordChangedEvent event) override;
54 
55  private:
56   friend class QuerierImplTesting;
57 
58   // Applies the provided record change to the underlying |graph_| instance.
59   ErrorOr<std::vector<PendingQueryChange>> ApplyRecordChanges(
60       const MdnsRecord& record,
61       RecordChangedEvent event);
62 
63   // Informs all relevant callbacks of the provided changes.
64   void InvokeChangeCallbacks(std::vector<DnsSdInstanceEndpoint> created,
65                              std::vector<DnsSdInstanceEndpoint> updated,
66                              std::vector<DnsSdInstanceEndpoint> deleted);
67 
68   // Graph of underlying mDNS Record and their associations with each-other.
69   std::unique_ptr<DnsDataGraph> graph_;
70 
71   // Map from the (service, domain) pairs currently being queried for to the
72   // callbacks to call when new InstanceEndpoints are available.
73   std::map<ServiceKey, std::vector<Callback*>> callback_map_;
74 
75   MdnsService* const mdns_querier_;
76   TaskRunner* const task_runner_;
77 
78   ReportingClient* reporting_client_;
79 };
80 
81 }  // namespace discovery
82 }  // namespace openscreen
83 
84 #endif  // DISCOVERY_DNSSD_IMPL_QUERIER_IMPL_H_
85