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