Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [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_COMMAND_QUEUE_H_ |
| 6 | #define LIBWEAVE_SRC_COMMANDS_COMMAND_QUEUE_H_ |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 10 | #include <queue> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 11 | #include <string> |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 12 | #include <utility> |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 13 | #include <vector> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 14 | |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 15 | #include <base/callback.h> |
Alex Vakulenko | 132617a | 2014-09-04 08:59:43 -0700 | [diff] [blame] | 16 | #include <base/macros.h> |
Vitaly Buka | 0d50107 | 2015-08-18 18:09:46 -0700 | [diff] [blame] | 17 | #include <base/time/time.h> |
Vitaly Buka | a8ece8f | 2015-10-05 13:30:23 -0700 | [diff] [blame] | 18 | #include <weave/device.h> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 19 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 20 | #include "src/commands/command_instance.h" |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 21 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 22 | namespace weave { |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 23 | |
| 24 | class CommandQueue final { |
| 25 | public: |
| 26 | CommandQueue() = default; |
| 27 | |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 28 | // TODO: Remove AddCommandAddedCallback and AddCommandRemovedCallback. |
| 29 | using CommandCallback = base::Callback<void(Command* command)>; |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 30 | |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 31 | // Adds notifications callback for a new command is added to the queue. |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 32 | void AddCommandAddedCallback(const CommandCallback& callback); |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 33 | |
| 34 | // Adds notifications callback for a command is removed from the queue. |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 35 | void AddCommandRemovedCallback(const CommandCallback& callback); |
| 36 | |
| 37 | void AddCommandHandler(const std::string& command_name, |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 38 | const Device::CommandHandlerCallback& callback); |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 39 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 40 | // 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 Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 47 | // 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 Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 50 | |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 51 | // 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 Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 54 | |
| 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 Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 58 | CommandInstance* Find(const std::string& id) const; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 59 | |
| 60 | private: |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 61 | friend class CommandQueueTest; |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 62 | |
| 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 Vakulenko | 534a312 | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 75 | // Overridden value to be returned from Now(). |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 76 | base::Time test_now_; |
| 77 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 78 | // ID-to-CommandInstance map. |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 79 | std::map<std::string, std::shared_ptr<CommandInstance>> map_; |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 80 | |
| 81 | // Queue of commands to be removed. |
| 82 | std::queue<std::pair<base::Time, std::string>> remove_queue_; |
| 83 | |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 84 | using CallbackList = std::vector<CommandCallback>; |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 85 | CallbackList on_command_added_; |
| 86 | CallbackList on_command_removed_; |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 87 | std::map<std::string, Device::CommandHandlerCallback> command_callbacks_; |
| 88 | Device::CommandHandlerCallback default_command_callback_; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 89 | |
| 90 | DISALLOW_COPY_AND_ASSIGN(CommandQueue); |
| 91 | }; |
| 92 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 93 | } // namespace weave |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 94 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 95 | #endif // LIBWEAVE_SRC_COMMANDS_COMMAND_QUEUE_H_ |