summaryrefslogtreecommitdiff
path: root/chromium/third_party/nearby/src/internal/platform/nsd_service_info.h
blob: d870bd41f0bdf33aafa3ca36dfa129afd3ee4bda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef PLATFORM_BASE_NSD_SERVICE_INFO_H_
#define PLATFORM_BASE_NSD_SERVICE_INFO_H_

#include <string>

#include "absl/container/flat_hash_map.h"

namespace location {
namespace nearby {

// https://developer.android.com/reference/android/net/nsd/NsdServiceInfo.html.
class NsdServiceInfo {
 public:
  static constexpr int kTypeFromServiceIdHashLength = 6;
  static constexpr absl::string_view kNsdTypeFormat{"_%s._tcp."};

  NsdServiceInfo() = default;
  NsdServiceInfo(const NsdServiceInfo&) = default;
  NsdServiceInfo& operator=(const NsdServiceInfo&) = default;
  NsdServiceInfo(NsdServiceInfo&&) = default;
  NsdServiceInfo& operator=(NsdServiceInfo&&) = default;
  ~NsdServiceInfo() = default;

  // Gets the service name.
  std::string GetServiceName() const { return service_name_; }

  // Sets the service name.
  void SetServiceName(std::string service_name) {
    service_name_ = std::move(service_name);
  }

  // Gets the TXTRecord value of the specified TXTRecord key assigned.
  std::string GetTxtRecord(const std::string& txt_record_key) const {
    if (txt_records_.empty()) return {};
    auto record = txt_records_.find(txt_record_key);
    if (record == txt_records_.end()) return {};
    return record->second;
  }

  // Adds the TXTRecord with a pair of key and value.
  void SetTxtRecord(const std::string& txt_record_key,
                    const std::string& txt_record_value) {
    txt_records_.emplace(txt_record_key, txt_record_value);
  }

  // Gets all TXTRecord.
  absl::flat_hash_map<std::string, std::string> GetTxtRecords() const {
    return txt_records_;
  }

  // Sets all TXTRecord.
  void SetTxtRecords(
      absl::flat_hash_map<std::string, std::string>& txt_records) {
    txt_records_ = txt_records;
  }

  // Gets IP Address, which is in byte sequence, in network order.
  std::string GetIPAddress() const { return ip_address_; }

  // Sets IP Address.
  void SetIPAddress(const std::string& ip_address) { ip_address_ = ip_address; }

  // Gets the port number
  int GetPort() const { return port_; }

  // Sets the port number.
  void SetPort(int port) { port_ = port; }

  // Gets the service type.
  std::string GetServiceType() const { return service_type_; }

  // Sets the service type.
  void SetServiceType(const std::string& service_type) {
    service_type_ = service_type;
  }

  bool IsValid() const { return !service_name_.empty(); }

 private:
  std::string service_name_;
  absl::flat_hash_map<std::string, std::string> txt_records_;
  std::string ip_address_;
  int port_;
  std::string service_type_;
};

}  // namespace nearby
}  // namespace location

#endif  // PLATFORM_BASE_NSD_SERVICE_INFO_H_