2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Nicklas P Andersson
22 #include "../StdAfx.h"
24 #include "AMCPCommandQueue.h"
26 #include <common/lock.h>
28 #include <boost/property_tree/ptree.hpp>
32 namespace caspar { namespace protocol { namespace amcp {
36 tbb::spin_mutex& get_global_mutex()
38 static tbb::spin_mutex mutex;
43 std::map<std::wstring, AMCPCommandQueue*>& get_instances()
45 static std::map<std::wstring, AMCPCommandQueue*> queues;
52 AMCPCommandQueue::AMCPCommandQueue(const std::wstring& name)
53 : executor_(L"AMCPCommandQueue " + name)
55 tbb::spin_mutex::scoped_lock lock(get_global_mutex());
57 get_instances().insert(std::make_pair(name, this));
60 AMCPCommandQueue::~AMCPCommandQueue()
62 tbb::spin_mutex::scoped_lock lock(get_global_mutex());
64 get_instances().erase(executor_.name());
67 void AMCPCommandQueue::AddCommand(AMCPCommand::ptr_type pCurrentCommand)
72 if(executor_.size() > 128)
76 CASPAR_LOG(error) << "AMCP Command Queue Overflow.";
77 CASPAR_LOG(error) << "Failed to execute command:" << pCurrentCommand->print();
78 pCurrentCommand->SetReplyString(L"500 FAILED\r\n");
79 pCurrentCommand->SendReply();
83 CASPAR_LOG_CURRENT_EXCEPTION();
87 executor_.begin_invoke([=]
95 auto print = pCurrentCommand->print();
96 auto params = boost::join(pCurrentCommand->parameters(), L" ");
99 tbb::spin_mutex::scoped_lock lock(running_command_mutex_);
100 running_command_ = true;
101 running_command_name_ = print;
102 running_command_params_ = std::move(params);
103 running_command_since_.restart();
106 if (pCurrentCommand->Execute())
107 CASPAR_LOG(debug) << "Executed command (" << timer.elapsed() << "s): " << print;
109 CASPAR_LOG(warning) << "Failed to execute command: " << print;
111 catch (file_not_found&)
113 CASPAR_LOG(error) << L"File not found. No match found for parameters. Check syntax.";
114 pCurrentCommand->SetReplyString(L"404 " + pCurrentCommand->print() + L" FAILED\r\n");
116 catch (const user_error& e)
118 CASPAR_LOG(error) << *boost::get_error_info<msg_info_t>(e) << ". Check syntax.";
119 pCurrentCommand->SetReplyString(L"403 " + pCurrentCommand->print() + L" FAILED\r\n");
121 catch (std::out_of_range&)
123 CASPAR_LOG(error) << L"Missing parameter. Check syntax.";
124 pCurrentCommand->SetReplyString(L"402 " + pCurrentCommand->print() + L" FAILED\r\n");
128 CASPAR_LOG_CURRENT_EXCEPTION();
129 CASPAR_LOG(warning) << "Failed to execute command:" << pCurrentCommand->print();
130 pCurrentCommand->SetReplyString(L"501 " + pCurrentCommand->print() + L" FAILED\r\n");
133 pCurrentCommand->SendReply();
135 CASPAR_LOG(trace) << "Ready for a new command";
137 tbb::spin_mutex::scoped_lock lock(running_command_mutex_);
138 running_command_ = false;
142 CASPAR_LOG_CURRENT_EXCEPTION();
147 boost::property_tree::wptree AMCPCommandQueue::info() const
149 boost::property_tree::wptree info;
151 auto name = executor_.name();
152 info.add(L"name", name);
153 auto size = executor_.size();
154 info.add(L"queued", std::max(0u, size));
156 bool running_command;
157 std::wstring running_command_name;
158 std::wstring running_command_params;
159 int64_t running_command_elapsed;
161 lock(running_command_mutex_, [&]
163 running_command = running_command_;
167 running_command_name = running_command_name_;
168 running_command_params = running_command_params_;
169 running_command_elapsed = static_cast<int64_t>(
170 running_command_since_.elapsed() * 1000.0);
176 info.add(L"running.command", running_command_name);
177 info.add(L"running.params", running_command_params);
178 info.add(L"running.elapsed", running_command_elapsed);
184 boost::property_tree::wptree AMCPCommandQueue::info_all_queues()
186 boost::property_tree::wptree info;
187 tbb::spin_mutex::scoped_lock lock(get_global_mutex());
189 for (auto& queue : get_instances())
191 info.add_child(L"queues.queue", queue.second->info());