Vitaly Buka | cad20f0 | 2015-10-16 17:27:15 -0700 | [diff] [blame^] | 1 | // Copyright 2015 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 14 | |
| 15 | #include "buffet/peerd_client.h" |
| 16 | |
| 17 | #include <map> |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 18 | #include <vector> |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 19 | |
| 20 | #include <base/message_loop/message_loop.h> |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 21 | #include <brillo/errors/error.h> |
| 22 | #include <brillo/strings/string_utils.h> |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 23 | |
| 24 | using org::chromium::peerd::PeerProxy; |
| 25 | |
| 26 | namespace buffet { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | // Commit changes only if no update request happened during the timeout. |
| 31 | // Usually updates happen in batches, so we don't want to flood network with |
| 32 | // updates relevant for a short amount of time. |
| 33 | const int kCommitTimeoutSeconds = 1; |
| 34 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 35 | void OnError(const std::string& operation, brillo::Error* error) { |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 36 | LOG(ERROR) << operation << " failed:" << error->GetMessage(); |
| 37 | } |
| 38 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 39 | const char kExpectedServiceType[] = "_privet._tcp"; |
| 40 | const char kServiceName[] = "privet"; |
| 41 | |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 42 | } // namespace |
| 43 | |
| 44 | PeerdClient::PeerdClient(const scoped_refptr<dbus::Bus>& bus) |
| 45 | : peerd_object_manager_proxy_{bus} { |
| 46 | peerd_object_manager_proxy_.SetManagerAddedCallback( |
| 47 | base::Bind(&PeerdClient::OnPeerdOnline, weak_ptr_factory_.GetWeakPtr())); |
| 48 | peerd_object_manager_proxy_.SetManagerRemovedCallback( |
| 49 | base::Bind(&PeerdClient::OnPeerdOffline, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | PeerdClient::~PeerdClient() { |
| 53 | RemoveService(); |
| 54 | } |
| 55 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 56 | void PeerdClient::PublishService(const std::string& service_type, |
| 57 | uint16_t port, |
| 58 | const std::vector<std::string>& txt) { |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 59 | // Only one service supported. |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 60 | CHECK_EQ(service_type, kExpectedServiceType); |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 61 | port_ = port; |
| 62 | txt_ = txt; |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 63 | Update(); |
| 64 | } |
| 65 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 66 | void PeerdClient::StopPublishing(const std::string& service_type) { |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 67 | // Only one service supported. |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 68 | CHECK_EQ(service_type, kExpectedServiceType); |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 69 | port_ = 0; |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 70 | Update(); |
| 71 | } |
| 72 | |
| 73 | void PeerdClient::Update() { |
| 74 | // Abort pending updates, and wait for more changes. |
| 75 | restart_weak_ptr_factory_.InvalidateWeakPtrs(); |
| 76 | base::MessageLoop::current()->PostDelayedTask( |
| 77 | FROM_HERE, base::Bind(&PeerdClient::UpdateImpl, |
| 78 | restart_weak_ptr_factory_.GetWeakPtr()), |
| 79 | base::TimeDelta::FromSeconds(kCommitTimeoutSeconds)); |
| 80 | } |
| 81 | |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 82 | void PeerdClient::OnPeerdOnline( |
| 83 | org::chromium::peerd::ManagerProxy* manager_proxy) { |
| 84 | peerd_manager_proxy_ = manager_proxy; |
| 85 | VLOG(1) << "Peerd manager is online at '" |
| 86 | << manager_proxy->GetObjectPath().value() << "'."; |
| 87 | Update(); |
| 88 | } |
| 89 | |
| 90 | void PeerdClient::OnPeerdOffline(const dbus::ObjectPath& object_path) { |
| 91 | peerd_manager_proxy_ = nullptr; |
| 92 | VLOG(1) << "Peerd manager is now offline."; |
| 93 | } |
| 94 | |
| 95 | void PeerdClient::ExposeService() { |
| 96 | // Do nothing if peerd hasn't started yet. |
| 97 | if (peerd_manager_proxy_ == nullptr) |
| 98 | return; |
| 99 | VLOG(1) << "Starting peerd advertising."; |
| 100 | CHECK_NE(port_, 0); |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 101 | CHECK(!txt_.empty()); |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 102 | std::map<std::string, brillo::Any> mdns_options{ |
| 103 | {"port", brillo::Any{port_}}, |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 106 | std::map<std::string, std::string> txt; |
| 107 | for (const auto& record : txt_) { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 108 | auto name_value = brillo::string_utils::SplitAtFirst(record, "="); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 109 | CHECK(!name_value.second.empty()); |
| 110 | txt.emplace(std::move(name_value)); |
| 111 | } |
| 112 | |
| 113 | published_ = true; |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 114 | peerd_manager_proxy_->ExposeServiceAsync( |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 115 | kServiceName, txt, {{"mdns", mdns_options}}, base::Closure(), |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 116 | base::Bind(&OnError, "ExposeService")); |
| 117 | } |
| 118 | |
| 119 | void PeerdClient::RemoveService() { |
| 120 | if (peerd_manager_proxy_ == nullptr) |
| 121 | return; |
| 122 | |
| 123 | VLOG(1) << "Stopping peerd advertising."; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 124 | if (published_) { |
| 125 | published_ = false; |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 126 | peerd_manager_proxy_->RemoveExposedServiceAsync( |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 127 | kServiceName, base::Closure(), base::Bind(&OnError, "RemoveService")); |
Vitaly Buka | 7042c58 | 2015-07-30 17:02:14 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | void PeerdClient::UpdateImpl() { |
| 132 | if (port_ == 0) |
| 133 | return RemoveService(); |
| 134 | ExposeService(); |
| 135 | } |
| 136 | |
| 137 | } // namespace buffet |