blob: ee6358f8e1cfbb9731c23749496bff7e4037c84d [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Anton Muhin59755522014-11-05 21:30:12 +04002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Vitaly Buka912b6982015-07-06 11:13:03 -07005#ifndef LIBWEAVE_SRC_COMMANDS_CLOUD_COMMAND_PROXY_H_
6#define LIBWEAVE_SRC_COMMANDS_CLOUD_COMMAND_PROXY_H_
Anton Muhin59755522014-11-05 21:30:12 +04007
Alex Vakulenkobe4254b2015-06-26 11:34:03 -07008#include <deque>
9#include <memory>
Anton Muhin59755522014-11-05 21:30:12 +040010#include <string>
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070011#include <utility>
Anton Muhin59755522014-11-05 21:30:12 +040012
Alex Vakulenkob211c102015-04-21 11:43:23 -070013#include <base/macros.h>
14#include <base/memory/weak_ptr.h>
Vitaly Buka157b16a2015-07-31 16:20:48 -070015#include <base/scoped_observer.h>
Vitaly Buka7b382ac2015-08-03 13:50:01 -070016#include <weave/command.h>
Alex Vakulenkob211c102015-04-21 11:43:23 -070017
Stefan Sauer2d16dfa2015-09-25 17:08:35 +020018#include "src/backoff_entry.h"
19#include "src/commands/cloud_command_update_interface.h"
Vitaly Bukac6029262015-10-07 09:29:13 -070020#include "src/commands/command_instance.h"
Stefan Sauer2d16dfa2015-09-25 17:08:35 +020021#include "src/states/state_change_queue_interface.h"
Anton Muhin59755522014-11-05 21:30:12 +040022
Vitaly Bukab6f015a2015-07-09 14:59:23 -070023namespace weave {
Anton Muhin59755522014-11-05 21:30:12 +040024
25class CommandInstance;
Vitaly Buka1e363672015-09-25 14:01:16 -070026
27namespace provider {
Vitaly Buka823fdda2015-08-13 00:33:00 -070028class TaskRunner;
Vitaly Buka1e363672015-09-25 14:01:16 -070029}
Anton Muhin59755522014-11-05 21:30:12 +040030
31// Command proxy which publishes command updates to the cloud.
Vitaly Bukac6029262015-10-07 09:29:13 -070032class CloudCommandProxy final : public CommandInstance::Observer {
Anton Muhin59755522014-11-05 21:30:12 +040033 public:
34 CloudCommandProxy(CommandInstance* command_instance,
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070035 CloudCommandUpdateInterface* cloud_command_updater,
36 StateChangeQueueInterface* state_change_queue,
Vitaly Buka0f80f7c2015-08-13 00:57:25 -070037 std::unique_ptr<BackoffEntry> backoff_entry,
Vitaly Buka1e363672015-09-25 14:01:16 -070038 provider::TaskRunner* task_runner);
Anton Muhin59755522014-11-05 21:30:12 +040039 ~CloudCommandProxy() override = default;
40
41 // CommandProxyInterface implementation/overloads.
Vitaly Buka375f3282015-10-07 18:34:15 -070042 void OnCommandDestroyed() override;
43 void OnErrorChanged() override;
44 void OnProgressChanged() override;
Alex Vakulenkob211c102015-04-21 11:43:23 -070045 void OnResultsChanged() override;
Vitaly Buka0209da42015-10-08 00:07:18 -070046 void OnStateChanged() override;
Anton Muhin59755522014-11-05 21:30:12 +040047
48 private:
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070049 using UpdateID = StateChangeQueueInterface::UpdateID;
50 using UpdateQueueEntry =
51 std::pair<UpdateID, std::unique_ptr<base::DictionaryValue>>;
Alex Vakulenkob211c102015-04-21 11:43:23 -070052
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070053 // 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 Vakulenkob211c102015-04-21 11:43:23 -070060 void SendCommandUpdate();
61
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070062 // Retry the last failed command update request to the server.
Alex Vakulenkob211c102015-04-21 11:43:23 -070063 void ResendCommandUpdate();
64
65 // Callback invoked by the asynchronous PATCH request to the server.
Vitaly Buka74763422015-10-11 00:39:52 -070066 void OnUpdateCommandDone(ErrorPtr error);
Alex Vakulenkob211c102015-04-21 11:43:23 -070067
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070068 // 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 Muhin59755522014-11-05 21:30:12 +040073 CommandInstance* command_instance_;
Alex Vakulenkofe53f612015-06-26 09:05:15 -070074 CloudCommandUpdateInterface* cloud_command_updater_;
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070075 StateChangeQueueInterface* state_change_queue_;
Vitaly Buka1e363672015-09-25 14:01:16 -070076 provider::TaskRunner* task_runner_{nullptr};
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070077
78 // Backoff for SendCommandUpdate() method.
Vitaly Buka0f80f7c2015-08-13 00:57:25 -070079 std::unique_ptr<BackoffEntry> cloud_backoff_entry_;
Anton Muhin59755522014-11-05 21:30:12 +040080
Alex Vakulenkob211c102015-04-21 11:43:23 -070081 // Set to true while a pending PATCH request is in flight to the server.
82 bool command_update_in_progress_{false};
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070083 // Update queue with all the command update requests ready to be sent to
84 // the server.
85 std::deque<UpdateQueueEntry> update_queue_;
Alex Vakulenkob211c102015-04-21 11:43:23 -070086
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070087 // 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 Bukac6029262015-10-07 09:29:13 -070096 ScopedObserver<CommandInstance, CommandInstance::Observer> observer_{this};
Vitaly Buka157b16a2015-07-31 16:20:48 -070097
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070098 base::WeakPtrFactory<CloudCommandProxy> backoff_weak_ptr_factory_{this};
Alex Vakulenkob211c102015-04-21 11:43:23 -070099 base::WeakPtrFactory<CloudCommandProxy> weak_ptr_factory_{this};
Anton Muhin59755522014-11-05 21:30:12 +0400100 DISALLOW_COPY_AND_ASSIGN(CloudCommandProxy);
101};
102
Vitaly Bukab6f015a2015-07-09 14:59:23 -0700103} // namespace weave
Anton Muhin59755522014-11-05 21:30:12 +0400104
Vitaly Buka912b6982015-07-06 11:13:03 -0700105#endif // LIBWEAVE_SRC_COMMANDS_CLOUD_COMMAND_PROXY_H_