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 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/commands/command_queue.h" |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 6 | |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 7 | #include <base/bind.h> |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 8 | #include <base/time/time.h> |
| 9 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 10 | namespace weave { |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 11 | |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 12 | namespace { |
| 13 | const int kRemoveCommandDelayMin = 5; |
Alex Vakulenko | 88f55d8 | 2015-12-03 15:30:27 -0800 | [diff] [blame] | 14 | |
| 15 | std::string GetCommandHandlerKey(const std::string& component_path, |
| 16 | const std::string& command_name) { |
| 17 | return component_path + ":" + command_name; |
| 18 | } |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 19 | } |
| 20 | |
Alex Vakulenko | 98d1fee | 2016-02-01 12:25:21 -0800 | [diff] [blame] | 21 | CommandQueue::CommandQueue(provider::TaskRunner* task_runner, |
| 22 | base::Clock* clock) |
| 23 | : task_runner_{task_runner}, clock_{clock} {} |
| 24 | |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 25 | void CommandQueue::AddCommandAddedCallback(const CommandCallback& callback) { |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 26 | on_command_added_.push_back(callback); |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 27 | // Send all pre-existed commands. |
| 28 | for (const auto& command : map_) |
| 29 | callback.Run(command.second.get()); |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 32 | void CommandQueue::AddCommandRemovedCallback(const CommandCallback& callback) { |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 33 | on_command_removed_.push_back(callback); |
| 34 | } |
| 35 | |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 36 | void CommandQueue::AddCommandHandler( |
Alex Vakulenko | 88f55d8 | 2015-12-03 15:30:27 -0800 | [diff] [blame] | 37 | const std::string& component_path, |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 38 | const std::string& command_name, |
| 39 | const Device::CommandHandlerCallback& callback) { |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 40 | if (!command_name.empty()) { |
| 41 | CHECK(default_command_callback_.is_null()) |
| 42 | << "Commands specific handler are not allowed after default one"; |
| 43 | |
| 44 | for (const auto& command : map_) { |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 45 | if (command.second->GetState() == Command::State::kQueued && |
Alex Vakulenko | 88f55d8 | 2015-12-03 15:30:27 -0800 | [diff] [blame] | 46 | command.second->GetName() == command_name && |
| 47 | command.second->GetComponent() == component_path) { |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 48 | callback.Run(command.second); |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Alex Vakulenko | 88f55d8 | 2015-12-03 15:30:27 -0800 | [diff] [blame] | 52 | std::string key = GetCommandHandlerKey(component_path, command_name); |
| 53 | CHECK(command_callbacks_.insert(std::make_pair(key, callback)).second) |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 54 | << command_name << " already has handler"; |
| 55 | |
| 56 | } else { |
Alex Vakulenko | 88f55d8 | 2015-12-03 15:30:27 -0800 | [diff] [blame] | 57 | CHECK(component_path.empty()) |
| 58 | << "Default handler must not be component-specific"; |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 59 | for (const auto& command : map_) { |
Alex Vakulenko | 88f55d8 | 2015-12-03 15:30:27 -0800 | [diff] [blame] | 60 | std::string key = GetCommandHandlerKey(command.second->GetComponent(), |
| 61 | command.second->GetName()); |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 62 | if (command.second->GetState() == Command::State::kQueued && |
Alex Vakulenko | 88f55d8 | 2015-12-03 15:30:27 -0800 | [diff] [blame] | 63 | command_callbacks_.find(key) == command_callbacks_.end()) { |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 64 | callback.Run(command.second); |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | CHECK(default_command_callback_.is_null()) << "Already has default handler"; |
| 69 | default_command_callback_ = callback; |
| 70 | } |
| 71 | } |
| 72 | |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 73 | void CommandQueue::Add(std::unique_ptr<CommandInstance> instance) { |
| 74 | std::string id = instance->GetID(); |
| 75 | LOG_IF(FATAL, id.empty()) << "Command has no ID"; |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 76 | instance->AttachToQueue(this); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 77 | auto pair = map_.insert(std::make_pair(id, std::move(instance))); |
| 78 | LOG_IF(FATAL, !pair.second) << "Command with ID '" << id |
| 79 | << "' is already in the queue"; |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 80 | for (const auto& cb : on_command_added_) |
| 81 | cb.Run(pair.first->second.get()); |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 82 | |
Alex Vakulenko | 88f55d8 | 2015-12-03 15:30:27 -0800 | [diff] [blame] | 83 | std::string key = GetCommandHandlerKey(pair.first->second->GetComponent(), |
| 84 | pair.first->second->GetName()); |
| 85 | auto it_handler = command_callbacks_.find(key); |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 86 | |
| 87 | if (it_handler != command_callbacks_.end()) |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 88 | it_handler->second.Run(pair.first->second); |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 89 | else if (!default_command_callback_.is_null()) |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 90 | default_command_callback_.Run(pair.first->second); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Alex Vakulenko | 329ad80 | 2016-02-01 12:11:30 -0800 | [diff] [blame] | 93 | void CommandQueue::RemoveLater(const std::string& id) { |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 94 | auto p = map_.find(id); |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 95 | if (p == map_.end()) |
| 96 | return; |
Alex Vakulenko | 98d1fee | 2016-02-01 12:25:21 -0800 | [diff] [blame] | 97 | auto remove_delay = base::TimeDelta::FromMinutes(kRemoveCommandDelayMin); |
| 98 | remove_queue_.push(std::make_pair(clock_->Now() + remove_delay, id)); |
| 99 | if (remove_queue_.size() == 1) { |
| 100 | // The queue was empty, this is the first command to be removed, schedule |
| 101 | // a clean-up task. |
| 102 | ScheduleCleanup(remove_delay); |
| 103 | } |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | bool CommandQueue::Remove(const std::string& id) { |
| 107 | auto p = map_.find(id); |
| 108 | if (p == map_.end()) |
| 109 | return false; |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 110 | std::shared_ptr<CommandInstance> instance = p->second; |
| 111 | instance->DetachFromQueue(); |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 112 | map_.erase(p); |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 113 | for (const auto& cb : on_command_removed_) |
| 114 | cb.Run(instance.get()); |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
Alex Vakulenko | 98d1fee | 2016-02-01 12:25:21 -0800 | [diff] [blame] | 118 | void CommandQueue::Cleanup(const base::Time& cutoff_time) { |
| 119 | while (!remove_queue_.empty() && remove_queue_.top().first <= cutoff_time) { |
| 120 | Remove(remove_queue_.top().second); |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 121 | remove_queue_.pop(); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 122 | } |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Alex Vakulenko | 98d1fee | 2016-02-01 12:25:21 -0800 | [diff] [blame] | 125 | void CommandQueue::ScheduleCleanup(base::TimeDelta delay) { |
| 126 | task_runner_->PostDelayedTask( |
Vitaly Buka | 5e94dc8 | 2016-03-01 13:03:01 -0800 | [diff] [blame] | 127 | FROM_HERE, base::Bind(&CommandQueue::PerformScheduledCleanup, |
| 128 | weak_ptr_factory_.GetWeakPtr()), |
Alex Vakulenko | 98d1fee | 2016-02-01 12:25:21 -0800 | [diff] [blame] | 129 | delay); |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Alex Vakulenko | 98d1fee | 2016-02-01 12:25:21 -0800 | [diff] [blame] | 132 | void CommandQueue::PerformScheduledCleanup() { |
| 133 | base::Time now = clock_->Now(); |
| 134 | Cleanup(now); |
| 135 | if (!remove_queue_.empty()) |
| 136 | ScheduleCleanup(remove_queue_.top().first - now); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 139 | CommandInstance* CommandQueue::Find(const std::string& id) const { |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 140 | auto p = map_.find(id); |
| 141 | return (p != map_.end()) ? p->second.get() : nullptr; |
| 142 | } |
| 143 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 144 | } // namespace weave |