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 #include "discovery/dnssd/public/dns_sd_instance_endpoint.h"
6
7 #include <algorithm>
8 #include <cctype>
9 #include <utility>
10 #include <vector>
11
12 #include "util/osp_logging.h"
13
14 namespace openscreen {
15 namespace discovery {
16
DnsSdInstanceEndpoint(std::string instance_id,std::string service_id,std::string domain_id,DnsSdTxtRecord txt,NetworkInterfaceIndex network_interface,std::vector<IPEndpoint> endpoints)17 DnsSdInstanceEndpoint::DnsSdInstanceEndpoint(
18 std::string instance_id,
19 std::string service_id,
20 std::string domain_id,
21 DnsSdTxtRecord txt,
22 NetworkInterfaceIndex network_interface,
23 std::vector<IPEndpoint> endpoints)
24 : DnsSdInstanceEndpoint(std::move(instance_id),
25 std::move(service_id),
26 std::move(domain_id),
27 std::move(txt),
28 network_interface,
29 std::move(endpoints),
30 std::vector<Subtype>{}) {}
31
DnsSdInstanceEndpoint(std::string instance_id,std::string service_id,std::string domain_id,DnsSdTxtRecord txt,NetworkInterfaceIndex network_interface,std::vector<IPEndpoint> endpoints,std::vector<Subtype> subtypes)32 DnsSdInstanceEndpoint::DnsSdInstanceEndpoint(
33 std::string instance_id,
34 std::string service_id,
35 std::string domain_id,
36 DnsSdTxtRecord txt,
37 NetworkInterfaceIndex network_interface,
38 std::vector<IPEndpoint> endpoints,
39 std::vector<Subtype> subtypes)
40 : DnsSdInstance(std::move(instance_id),
41 std::move(service_id),
42 std::move(domain_id),
43 std::move(txt),
44 endpoints.empty() ? 0 : endpoints[0].port,
45 std::move(subtypes)),
46 endpoints_(std::move(endpoints)),
47 network_interface_(network_interface) {
48 InitializeEndpoints();
49 }
50
DnsSdInstanceEndpoint(DnsSdInstance instance,NetworkInterfaceIndex network_interface,std::vector<IPEndpoint> endpoints)51 DnsSdInstanceEndpoint::DnsSdInstanceEndpoint(
52 DnsSdInstance instance,
53 NetworkInterfaceIndex network_interface,
54 std::vector<IPEndpoint> endpoints)
55 : DnsSdInstance(std::move(instance)),
56 endpoints_(std::move(endpoints)),
57 network_interface_(network_interface) {
58 InitializeEndpoints();
59 }
60
61 DnsSdInstanceEndpoint::DnsSdInstanceEndpoint(
62 const DnsSdInstanceEndpoint& other) = default;
63
64 DnsSdInstanceEndpoint::DnsSdInstanceEndpoint(DnsSdInstanceEndpoint&& other) =
65 default;
66
67 DnsSdInstanceEndpoint::~DnsSdInstanceEndpoint() = default;
68
69 DnsSdInstanceEndpoint& DnsSdInstanceEndpoint::operator=(
70 const DnsSdInstanceEndpoint& rhs) = default;
71
72 DnsSdInstanceEndpoint& DnsSdInstanceEndpoint::operator=(
73 DnsSdInstanceEndpoint&& rhs) = default;
74
InitializeEndpoints()75 void DnsSdInstanceEndpoint::InitializeEndpoints() {
76 OSP_CHECK(!endpoints_.empty());
77 std::sort(endpoints_.begin(), endpoints_.end());
78 for (const auto& endpoint : endpoints_) {
79 OSP_DCHECK_EQ(endpoint.port, port());
80 addresses_.push_back(endpoint.address);
81 }
82 }
83
operator <(const DnsSdInstanceEndpoint & lhs,const DnsSdInstanceEndpoint & rhs)84 bool operator<(const DnsSdInstanceEndpoint& lhs,
85 const DnsSdInstanceEndpoint& rhs) {
86 if (lhs.network_interface_ != rhs.network_interface_) {
87 return lhs.network_interface_ < rhs.network_interface_;
88 }
89
90 if (lhs.endpoints_.size() != rhs.endpoints_.size()) {
91 return lhs.endpoints_.size() < rhs.endpoints_.size();
92 }
93
94 for (int i = 0; i < static_cast<int>(lhs.endpoints_.size()); i++) {
95 if (lhs.endpoints_[i] != rhs.endpoints_[i]) {
96 return lhs.endpoints_[i] < rhs.endpoints_[i];
97 }
98 }
99
100 return static_cast<const DnsSdInstance&>(lhs) <
101 static_cast<const DnsSdInstance&>(rhs);
102 }
103
104 } // namespace discovery
105 } // namespace openscreen
106