blob: 74ed86f8e2d8dd58586724c3433ff7b712f356ef [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -07002// 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_COMMAND_QUEUE_H_
6#define LIBWEAVE_SRC_COMMANDS_COMMAND_QUEUE_H_
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -07007
8#include <map>
9#include <memory>
Vitaly Buka2a9b30f2015-04-01 10:51:59 -070010#include <queue>
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070011#include <string>
Vitaly Buka2a9b30f2015-04-01 10:51:59 -070012#include <utility>
Vitaly Bukaae0f3a12015-05-11 16:27:30 -070013#include <vector>
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070014
Vitaly Bukaae0f3a12015-05-11 16:27:30 -070015#include <base/callback.h>
Alex Vakulenko132617a2014-09-04 08:59:43 -070016#include <base/macros.h>
Vitaly Buka0d501072015-08-18 18:09:46 -070017#include <base/time/time.h>
Vitaly Bukaa8ece8f2015-10-05 13:30:23 -070018#include <weave/device.h>
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070019
Stefan Sauer2d16dfa2015-09-25 17:08:35 +020020#include "src/commands/command_instance.h"
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070021
Vitaly Bukab6f015a2015-07-09 14:59:23 -070022namespace weave {
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070023
24class CommandQueue final {
25 public:
26 CommandQueue() = default;
27
Vitaly Bukac6029262015-10-07 09:29:13 -070028 // TODO: Remove AddCommandAddedCallback and AddCommandRemovedCallback.
29 using CommandCallback = base::Callback<void(Command* command)>;
Vitaly Buka695a5fb2015-10-06 16:26:08 -070030
Vitaly Bukaae0f3a12015-05-11 16:27:30 -070031 // Adds notifications callback for a new command is added to the queue.
Vitaly Buka695a5fb2015-10-06 16:26:08 -070032 void AddCommandAddedCallback(const CommandCallback& callback);
Vitaly Bukaae0f3a12015-05-11 16:27:30 -070033
34 // Adds notifications callback for a command is removed from the queue.
Vitaly Buka695a5fb2015-10-06 16:26:08 -070035 void AddCommandRemovedCallback(const CommandCallback& callback);
36
37 void AddCommandHandler(const std::string& command_name,
Vitaly Bukac6029262015-10-07 09:29:13 -070038 const Device::CommandHandlerCallback& callback);
Alex Vakulenko515b42b2014-08-07 15:46:31 -070039
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070040 // Checks if the command queue is empty.
41 bool IsEmpty() const { return map_.empty(); }
42
43 // Returns the number of commands in the queue.
44 size_t GetCount() const { return map_.size(); }
45
46 // Adds a new command to the queue. Each command in the queue has a unique
Anton Muhin5191e812014-10-30 17:49:48 +040047 // ID that identifies that command instance in this queue.
48 // One shouldn't attempt to add a command with the same ID.
49 void Add(std::unique_ptr<CommandInstance> instance);
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070050
Vitaly Buka2a9b30f2015-04-01 10:51:59 -070051 // Selects command identified by |id| ready for removal. Command will actually
52 // be removed after some time.
53 void DelayedRemove(const std::string& id);
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070054
55 // Finds a command instance in the queue by the instance |id|. Returns
56 // nullptr if the command with the given |id| is not found. The returned
57 // pointer should not be persisted for a long period of time.
Alex Vakulenkofedc4872014-08-20 12:38:43 -070058 CommandInstance* Find(const std::string& id) const;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070059
60 private:
Vitaly Buka2a9b30f2015-04-01 10:51:59 -070061 friend class CommandQueueTest;
Vitaly Buka2a9b30f2015-04-01 10:51:59 -070062
63 // Removes a command identified by |id| from the queue.
64 bool Remove(const std::string& id);
65
66 // Removes old commands selected with DelayedRemove.
67 void Cleanup();
68
69 // Overrides CommandQueue::Now() for tests.
70 void SetNowForTest(base::Time now);
71
72 // Returns current time.
73 base::Time Now() const;
74
Alex Vakulenko534a3122015-05-22 15:48:53 -070075 // Overridden value to be returned from Now().
Vitaly Buka2a9b30f2015-04-01 10:51:59 -070076 base::Time test_now_;
77
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070078 // ID-to-CommandInstance map.
Vitaly Bukac6029262015-10-07 09:29:13 -070079 std::map<std::string, std::shared_ptr<CommandInstance>> map_;
Vitaly Buka2a9b30f2015-04-01 10:51:59 -070080
81 // Queue of commands to be removed.
82 std::queue<std::pair<base::Time, std::string>> remove_queue_;
83
Vitaly Buka695a5fb2015-10-06 16:26:08 -070084 using CallbackList = std::vector<CommandCallback>;
Vitaly Bukaae0f3a12015-05-11 16:27:30 -070085 CallbackList on_command_added_;
86 CallbackList on_command_removed_;
Vitaly Bukac6029262015-10-07 09:29:13 -070087 std::map<std::string, Device::CommandHandlerCallback> command_callbacks_;
88 Device::CommandHandlerCallback default_command_callback_;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070089
90 DISALLOW_COPY_AND_ASSIGN(CommandQueue);
91};
92
Vitaly Bukab6f015a2015-07-09 14:59:23 -070093} // namespace weave
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070094
Vitaly Buka912b6982015-07-06 11:13:03 -070095#endif // LIBWEAVE_SRC_COMMANDS_COMMAND_QUEUE_H_