buffet: Add state change queue
When device state properties are updated, compile the change
notifications in StateChangeQueue object so they can be pulled
by Cloud Server adaptor and batch-update the device state on
the server.
When StateManager receives property updates, it notifies the
StateChangeQueue object of the changes via StateChangeQueueInterface.
The changes are kept in the queue along with the their time stamps
until they are pulled by Cloud Server adaper (using StateChangeQueue
method). At this point, the adapter would notify the server of
recorded device state changes and the StateChangeQueue is cleared,
ready to record new state updates.
BUG=chromium:415364
TEST=FEATURES=test emerge-link buffet
Change-Id: Ie99e2ada39aaf0164e08699d65153abfc5235a2f
Reviewed-on: https://chromium-review.googlesource.com/226014
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Christopher Wiley <wiley@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/states/state_change_queue.h b/buffet/states/state_change_queue.h
new file mode 100644
index 0000000..a5cde95
--- /dev/null
+++ b/buffet/states/state_change_queue.h
@@ -0,0 +1,45 @@
+// Copyright 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BUFFET_STATES_STATE_CHANGE_QUEUE_H_
+#define BUFFET_STATES_STATE_CHANGE_QUEUE_H_
+
+#include <vector>
+
+#include <base/macros.h>
+#include <base/threading/thread_checker.h>
+
+#include "buffet/states/state_change_queue_interface.h"
+
+namespace buffet {
+
+// An object to record and retrieve device state change notification events.
+class StateChangeQueue : public StateChangeQueueInterface {
+ public:
+ explicit StateChangeQueue(size_t max_queue_size);
+
+ // Overrides from StateChangeQueueInterface.
+ bool IsEmpty() const override { return state_changes_.empty(); }
+ bool NotifyPropertiesUpdated(const StateChange& change) override;
+ std::vector<StateChange> GetAndClearRecordedStateChanges() override;
+
+ private:
+ // To make sure we do not call NotifyPropertiesUpdated() and
+ // GetAndClearRecordedStateChanges() on different threads, |thread_checker_|
+ // is here to help us with verifying the single-threaded operation.
+ base::ThreadChecker thread_checker_;
+
+ // Maximum queue size. If it is full, the oldest state update records are
+ // merged together until the queue size is within the size limit.
+ size_t max_queue_size_;
+
+ // Accumulated list of device state change notifications.
+ std::vector<StateChange> state_changes_;
+
+ DISALLOW_COPY_AND_ASSIGN(StateChangeQueue);
+};
+
+} // namespace buffet
+
+#endif // BUFFET_STATES_STATE_CHANGE_QUEUE_H_