blob: 80efd708feb308123df98e0e34fc021f8f16d496 [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"
Alex Vakulenkod91d6252015-12-05 17:14:39 -080021#include "src/component_manager.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.
Alex Vakulenkoebfa60b2016-02-01 11:52:30 -080032class CloudCommandProxy : 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,
Alex Vakulenkod91d6252015-12-05 17:14:39 -080036 ComponentManager* component_manager,
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 Vakulenkod91d6252015-12-05 17:14:39 -080049 using UpdateQueueEntry = std::pair<ComponentManager::UpdateID,
50 std::unique_ptr<base::DictionaryValue>>;
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070051 // Puts a command update data into the update queue, and optionally sends an
52 // asynchronous request to GCD server to update the command resource, if there
53 // are no pending device status updates.
54 void QueueCommandUpdate(std::unique_ptr<base::DictionaryValue> patch);
55
56 // Sends an asynchronous request to GCD server to update the command resource,
57 // if there are no pending device status updates.
Alex Vakulenkob211c102015-04-21 11:43:23 -070058 void SendCommandUpdate();
59
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070060 // Retry the last failed command update request to the server.
Alex Vakulenkob211c102015-04-21 11:43:23 -070061 void ResendCommandUpdate();
62
63 // Callback invoked by the asynchronous PATCH request to the server.
Vitaly Buka74763422015-10-11 00:39:52 -070064 void OnUpdateCommandDone(ErrorPtr error);
Alex Vakulenkob211c102015-04-21 11:43:23 -070065
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070066 // Callback invoked by the device state change queue to notify of the
67 // successful device state update. |update_id| is the ID of the state that
68 // has been updated on the server.
Alex Vakulenkod91d6252015-12-05 17:14:39 -080069 void OnDeviceStateUpdated(ComponentManager::UpdateID update_id);
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070070
Anton Muhin59755522014-11-05 21:30:12 +040071 CommandInstance* command_instance_;
Alex Vakulenkofe53f612015-06-26 09:05:15 -070072 CloudCommandUpdateInterface* cloud_command_updater_;
Alex Vakulenkod91d6252015-12-05 17:14:39 -080073 ComponentManager* component_manager_;
Vitaly Buka1e363672015-09-25 14:01:16 -070074 provider::TaskRunner* task_runner_{nullptr};
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070075
76 // Backoff for SendCommandUpdate() method.
Vitaly Buka0f80f7c2015-08-13 00:57:25 -070077 std::unique_ptr<BackoffEntry> cloud_backoff_entry_;
Anton Muhin59755522014-11-05 21:30:12 +040078
Alex Vakulenkob211c102015-04-21 11:43:23 -070079 // Set to true while a pending PATCH request is in flight to the server.
80 bool command_update_in_progress_{false};
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070081 // Update queue with all the command update requests ready to be sent to
82 // the server.
83 std::deque<UpdateQueueEntry> update_queue_;
Alex Vakulenkob211c102015-04-21 11:43:23 -070084
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070085 // Callback token from the state change queue for OnDeviceStateUpdated()
86 // callback for ask the device state change queue to call when the state
87 // is updated on the server.
Alex Vakulenkod91d6252015-12-05 17:14:39 -080088 ComponentManager::Token callback_token_;
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070089
90 // Last device state update ID that has been sent out to the server
91 // successfully.
Alex Vakulenkod91d6252015-12-05 17:14:39 -080092 ComponentManager::UpdateID last_state_update_id_{0};
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070093
Vitaly Bukac6029262015-10-07 09:29:13 -070094 ScopedObserver<CommandInstance, CommandInstance::Observer> observer_{this};
Vitaly Buka157b16a2015-07-31 16:20:48 -070095
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070096 base::WeakPtrFactory<CloudCommandProxy> backoff_weak_ptr_factory_{this};
Alex Vakulenkob211c102015-04-21 11:43:23 -070097 base::WeakPtrFactory<CloudCommandProxy> weak_ptr_factory_{this};
Anton Muhin59755522014-11-05 21:30:12 +040098 DISALLOW_COPY_AND_ASSIGN(CloudCommandProxy);
99};
100
Vitaly Bukab6f015a2015-07-09 14:59:23 -0700101} // namespace weave
Anton Muhin59755522014-11-05 21:30:12 +0400102
Vitaly Buka912b6982015-07-06 11:13:03 -0700103#endif // LIBWEAVE_SRC_COMMANDS_CLOUD_COMMAND_PROXY_H_