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 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_COMMANDS_CLOUD_COMMAND_PROXY_H_ |
| 6 | #define LIBWEAVE_SRC_COMMANDS_CLOUD_COMMAND_PROXY_H_ |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 7 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 8 | #include <deque> |
| 9 | #include <memory> |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 10 | #include <string> |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 11 | #include <utility> |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 12 | |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 13 | #include <base/macros.h> |
| 14 | #include <base/memory/weak_ptr.h> |
Vitaly Buka | 157b16a | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 15 | #include <base/scoped_observer.h> |
Vitaly Buka | 7b382ac | 2015-08-03 13:50:01 -0700 | [diff] [blame] | 16 | #include <weave/command.h> |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 17 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 18 | #include "src/backoff_entry.h" |
| 19 | #include "src/commands/cloud_command_update_interface.h" |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 20 | #include "src/commands/command_instance.h" |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 21 | #include "src/states/state_change_queue_interface.h" |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 22 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 23 | namespace weave { |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 24 | |
| 25 | class CommandInstance; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 26 | |
| 27 | namespace provider { |
Vitaly Buka | 823fdda | 2015-08-13 00:33:00 -0700 | [diff] [blame] | 28 | class TaskRunner; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 29 | } |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 30 | |
| 31 | // Command proxy which publishes command updates to the cloud. |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 32 | class CloudCommandProxy final : public CommandInstance::Observer { |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 33 | public: |
| 34 | CloudCommandProxy(CommandInstance* command_instance, |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 35 | CloudCommandUpdateInterface* cloud_command_updater, |
| 36 | StateChangeQueueInterface* state_change_queue, |
Vitaly Buka | 0f80f7c | 2015-08-13 00:57:25 -0700 | [diff] [blame] | 37 | std::unique_ptr<BackoffEntry> backoff_entry, |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 38 | provider::TaskRunner* task_runner); |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 39 | ~CloudCommandProxy() override = default; |
| 40 | |
| 41 | // CommandProxyInterface implementation/overloads. |
Vitaly Buka | 375f328 | 2015-10-07 18:34:15 -0700 | [diff] [blame] | 42 | void OnCommandDestroyed() override; |
| 43 | void OnErrorChanged() override; |
| 44 | void OnProgressChanged() override; |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 45 | void OnResultsChanged() override; |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 46 | void OnStateChanged() override; |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 47 | |
| 48 | private: |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 49 | using UpdateID = StateChangeQueueInterface::UpdateID; |
| 50 | using UpdateQueueEntry = |
| 51 | std::pair<UpdateID, std::unique_ptr<base::DictionaryValue>>; |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 52 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 53 | // Puts a command update data into the update queue, and optionally sends an |
| 54 | // asynchronous request to GCD server to update the command resource, if there |
| 55 | // are no pending device status updates. |
| 56 | void QueueCommandUpdate(std::unique_ptr<base::DictionaryValue> patch); |
| 57 | |
| 58 | // Sends an asynchronous request to GCD server to update the command resource, |
| 59 | // if there are no pending device status updates. |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 60 | void SendCommandUpdate(); |
| 61 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 62 | // Retry the last failed command update request to the server. |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 63 | void ResendCommandUpdate(); |
| 64 | |
| 65 | // Callback invoked by the asynchronous PATCH request to the server. |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 66 | void OnUpdateCommandDone(ErrorPtr error); |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 67 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 68 | // Callback invoked by the device state change queue to notify of the |
| 69 | // successful device state update. |update_id| is the ID of the state that |
| 70 | // has been updated on the server. |
| 71 | void OnDeviceStateUpdated(UpdateID update_id); |
| 72 | |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 73 | CommandInstance* command_instance_; |
Alex Vakulenko | fe53f61 | 2015-06-26 09:05:15 -0700 | [diff] [blame] | 74 | CloudCommandUpdateInterface* cloud_command_updater_; |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 75 | StateChangeQueueInterface* state_change_queue_; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 76 | provider::TaskRunner* task_runner_{nullptr}; |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 77 | |
| 78 | // Backoff for SendCommandUpdate() method. |
Vitaly Buka | 0f80f7c | 2015-08-13 00:57:25 -0700 | [diff] [blame] | 79 | std::unique_ptr<BackoffEntry> cloud_backoff_entry_; |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 80 | |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 81 | // Set to true while a pending PATCH request is in flight to the server. |
| 82 | bool command_update_in_progress_{false}; |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 83 | // Update queue with all the command update requests ready to be sent to |
| 84 | // the server. |
| 85 | std::deque<UpdateQueueEntry> update_queue_; |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 86 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 87 | // Callback token from the state change queue for OnDeviceStateUpdated() |
| 88 | // callback for ask the device state change queue to call when the state |
| 89 | // is updated on the server. |
| 90 | StateChangeQueueInterface::Token callback_token_; |
| 91 | |
| 92 | // Last device state update ID that has been sent out to the server |
| 93 | // successfully. |
| 94 | UpdateID last_state_update_id_{0}; |
| 95 | |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 96 | ScopedObserver<CommandInstance, CommandInstance::Observer> observer_{this}; |
Vitaly Buka | 157b16a | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 97 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 98 | base::WeakPtrFactory<CloudCommandProxy> backoff_weak_ptr_factory_{this}; |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 99 | base::WeakPtrFactory<CloudCommandProxy> weak_ptr_factory_{this}; |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 100 | DISALLOW_COPY_AND_ASSIGN(CloudCommandProxy); |
| 101 | }; |
| 102 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 103 | } // namespace weave |
Anton Muhin | 5975552 | 2014-11-05 21:30:12 +0400 | [diff] [blame] | 104 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 105 | #endif // LIBWEAVE_SRC_COMMANDS_CLOUD_COMMAND_PROXY_H_ |