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