Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/commands/cloud_command_proxy.h" |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 6 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 7 | #include <base/bind.h> |
Vitaly Buka | 7b382ac | 2015-08-03 13:50:01 -0700 | [diff] [blame] | 8 | #include <weave/enum_to_string.h> |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 9 | #include <weave/provider/task_runner.h> |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 10 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 11 | #include "src/commands/command_instance.h" |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 12 | #include "src/commands/schema_constants.h" |
Vitaly Buka | 375f328 | 2015-10-07 18:34:15 -0700 | [diff] [blame] | 13 | #include "src/utils.h" |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 14 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 15 | namespace weave { |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 16 | |
| 17 | CloudCommandProxy::CloudCommandProxy( |
| 18 | CommandInstance* command_instance, |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 19 | CloudCommandUpdateInterface* cloud_command_updater, |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 20 | ComponentManager* component_manager, |
Vitaly Buka | 0f80f7c | 2015-08-13 00:57:25 -0700 | [diff] [blame] | 21 | std::unique_ptr<BackoffEntry> backoff_entry, |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 22 | provider::TaskRunner* task_runner) |
Alex Vakulenko | fe53f61 | 2015-06-26 09:05:15 -0700 | [diff] [blame] | 23 | : command_instance_{command_instance}, |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 24 | cloud_command_updater_{cloud_command_updater}, |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 25 | component_manager_{component_manager}, |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 26 | task_runner_{task_runner}, |
| 27 | cloud_backoff_entry_{std::move(backoff_entry)} { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 28 | callback_token_ = component_manager_->AddServerStateUpdatedCallback( |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 29 | base::Bind(&CloudCommandProxy::OnDeviceStateUpdated, |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 30 | weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | 157b16a | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 31 | observer_.Add(command_instance); |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 32 | } |
| 33 | |
Vitaly Buka | 375f328 | 2015-10-07 18:34:15 -0700 | [diff] [blame] | 34 | void CloudCommandProxy::OnErrorChanged() { |
| 35 | std::unique_ptr<base::DictionaryValue> patch{new base::DictionaryValue}; |
| 36 | patch->Set(commands::attributes::kCommand_Error, |
| 37 | command_instance_->GetError() |
| 38 | ? ErrorInfoToJson(*command_instance_->GetError()).release() |
| 39 | : base::Value::CreateNullValue().release()); |
| 40 | QueueCommandUpdate(std::move(patch)); |
| 41 | } |
| 42 | |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 43 | void CloudCommandProxy::OnResultsChanged() { |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 44 | std::unique_ptr<base::DictionaryValue> patch{new base::DictionaryValue}; |
Vitaly Buka | 8d8d219 | 2015-07-21 22:25:09 -0700 | [diff] [blame] | 45 | patch->Set(commands::attributes::kCommand_Results, |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 46 | command_instance_->GetResults().CreateDeepCopy()); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 47 | QueueCommandUpdate(std::move(patch)); |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 48 | } |
| 49 | |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 50 | void CloudCommandProxy::OnStateChanged() { |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 51 | std::unique_ptr<base::DictionaryValue> patch{new base::DictionaryValue}; |
| 52 | patch->SetString(commands::attributes::kCommand_State, |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 53 | EnumToString(command_instance_->GetState())); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 54 | QueueCommandUpdate(std::move(patch)); |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 55 | } |
| 56 | |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 57 | void CloudCommandProxy::OnProgressChanged() { |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 58 | std::unique_ptr<base::DictionaryValue> patch{new base::DictionaryValue}; |
Vitaly Buka | 8d8d219 | 2015-07-21 22:25:09 -0700 | [diff] [blame] | 59 | patch->Set(commands::attributes::kCommand_Progress, |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 60 | command_instance_->GetProgress().CreateDeepCopy()); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 61 | QueueCommandUpdate(std::move(patch)); |
| 62 | } |
| 63 | |
Vitaly Buka | c3d4e97 | 2015-07-21 09:55:25 -0700 | [diff] [blame] | 64 | void CloudCommandProxy::OnCommandDestroyed() { |
| 65 | delete this; |
| 66 | } |
| 67 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 68 | void CloudCommandProxy::QueueCommandUpdate( |
| 69 | std::unique_ptr<base::DictionaryValue> patch) { |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 70 | ComponentManager::UpdateID id = component_manager_->GetLastStateChangeId(); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 71 | if (update_queue_.empty() || update_queue_.back().first != id) { |
| 72 | // If queue is currently empty or the device state has changed since the |
| 73 | // last patch request queued, add a new request to the queue. |
| 74 | update_queue_.push_back(std::make_pair(id, std::move(patch))); |
| 75 | } else { |
| 76 | // Device state hasn't changed since the last time this command update |
| 77 | // was queued. We can coalesce the command update patches, unless the |
| 78 | // current request is already in flight to the server. |
| 79 | if (update_queue_.size() == 1 && command_update_in_progress_) { |
| 80 | // Can't update the request which is being sent to the server. |
| 81 | // Queue a new update. |
| 82 | update_queue_.push_back(std::make_pair(id, std::move(patch))); |
| 83 | } else { |
| 84 | // Coalesce the patches. |
| 85 | update_queue_.back().second->MergeDictionary(patch.get()); |
| 86 | } |
| 87 | } |
| 88 | // Send out an update request to the server, if needed. |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 89 | |
| 90 | // Post to accumulate more changes during the current message loop task run. |
| 91 | task_runner_->PostDelayedTask( |
| 92 | FROM_HERE, base::Bind(&CloudCommandProxy::SendCommandUpdate, |
| 93 | backoff_weak_ptr_factory_.GetWeakPtr()), |
| 94 | {}); |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void CloudCommandProxy::SendCommandUpdate() { |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 98 | if (command_update_in_progress_ || update_queue_.empty()) |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 99 | return; |
| 100 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 101 | // Check if we have any pending updates ready to be sent to the server. |
| 102 | // We can only send updates for which the device state at the time the |
| 103 | // requests have been queued were successfully propagated to the server. |
| 104 | // That is, if the pending device state updates that we recorded while the |
| 105 | // command update was queued haven't been acknowledged by the server, we |
| 106 | // will hold the corresponding command updates until the related device state |
| 107 | // has been successfully updated on the server. |
| 108 | if (update_queue_.front().first > last_state_update_id_) |
| 109 | return; |
| 110 | |
| 111 | backoff_weak_ptr_factory_.InvalidateWeakPtrs(); |
| 112 | if (cloud_backoff_entry_->ShouldRejectRequest()) { |
| 113 | VLOG(1) << "Cloud request delayed for " |
| 114 | << cloud_backoff_entry_->GetTimeUntilRelease() |
| 115 | << " due to backoff policy"; |
| 116 | task_runner_->PostDelayedTask( |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 117 | FROM_HERE, base::Bind(&CloudCommandProxy::SendCommandUpdate, |
| 118 | backoff_weak_ptr_factory_.GetWeakPtr()), |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 119 | cloud_backoff_entry_->GetTimeUntilRelease()); |
| 120 | return; |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 123 | // Coalesce any pending updates that were queued prior to the current device |
| 124 | // state known to be propagated to the server successfully. |
| 125 | auto iter = update_queue_.begin(); |
| 126 | auto start = ++iter; |
| 127 | while (iter != update_queue_.end()) { |
| 128 | if (iter->first > last_state_update_id_) |
| 129 | break; |
| 130 | update_queue_.front().first = iter->first; |
| 131 | update_queue_.front().second->MergeDictionary(iter->second.get()); |
| 132 | ++iter; |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 133 | } |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 134 | // Remove all the intermediate items that have been merged into the first |
| 135 | // entry. |
| 136 | update_queue_.erase(start, iter); |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 137 | command_update_in_progress_ = true; |
Alex Vakulenko | fe53f61 | 2015-06-26 09:05:15 -0700 | [diff] [blame] | 138 | cloud_command_updater_->UpdateCommand( |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 139 | command_instance_->GetID(), *update_queue_.front().second, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 140 | base::Bind(&CloudCommandProxy::OnUpdateCommandDone, |
| 141 | weak_ptr_factory_.GetWeakPtr())); |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void CloudCommandProxy::ResendCommandUpdate() { |
| 145 | command_update_in_progress_ = false; |
| 146 | SendCommandUpdate(); |
| 147 | } |
| 148 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 149 | void CloudCommandProxy::OnUpdateCommandDone(ErrorPtr error) { |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 150 | command_update_in_progress_ = false; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 151 | cloud_backoff_entry_->InformOfRequest(!error); |
| 152 | if (!error) { |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 153 | // Remove the succeeded update from the queue. |
| 154 | update_queue_.pop_front(); |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 155 | } |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 156 | // If we have more pending updates, send a new request to the server |
| 157 | // immediately, if possible. |
| 158 | SendCommandUpdate(); |
| 159 | } |
| 160 | |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 161 | void CloudCommandProxy::OnDeviceStateUpdated( |
| 162 | ComponentManager::UpdateID update_id) { |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 163 | last_state_update_id_ = update_id; |
| 164 | // Try to send out any queued command updates that could be performed after |
| 165 | // a device state is updated. |
| 166 | SendCommandUpdate(); |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 167 | } |
| 168 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 169 | } // namespace weave |