]> git.sesse.net Git - casparcg/blob - protocol/amcp/AMCPCommand.h
[AMCP] #475 Added special command REQ that can be prepended before any command to...
[casparcg] / protocol / amcp / AMCPCommand.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Nicklas P Andersson
20 */
21
22 #pragma once
23
24 #include "../util/ClientInfo.h"
25 #include "amcp_shared.h"
26 #include <core/consumer/frame_consumer.h>
27 #include <core/producer/frame_producer.h>
28 #include <accelerator/ogl/util/device.h>
29
30 #include <boost/algorithm/string.hpp>
31
32 namespace caspar { namespace protocol {
33 namespace amcp {
34
35         struct command_context
36         {
37                 IO::ClientInfoPtr                                                                               client;
38                 channel_context                                                                                 channel;
39                 int                                                                                                             channel_index;
40                 int                                                                                                             layer_id;
41                 std::vector<channel_context>                                                    channels;
42                 spl::shared_ptr<core::help_repository>                                  help_repo;
43                 spl::shared_ptr<core::media_info_repository>                    media_info_repo;
44                 spl::shared_ptr<core::cg_producer_registry>                             cg_registry;
45                 spl::shared_ptr<core::system_info_provider_repository>  system_info_repo;
46                 std::shared_ptr<core::thumbnail_generator>                              thumb_gen;
47                 spl::shared_ptr<const core::frame_producer_registry>    producer_registry;
48                 spl::shared_ptr<const core::frame_consumer_registry>    consumer_registry;
49                 std::shared_ptr<accelerator::ogl::device>                               ogl_device;
50                 std::promise<bool>&                                                                             shutdown_server_now;
51                 std::vector<std::wstring>                                                               parameters;
52
53                 int layer_index(int default_ = 0) const { return layer_id == -1 ? default_: layer_id; }
54
55                 command_context(
56                                 IO::ClientInfoPtr client,
57                                 channel_context channel,
58                                 int channel_index,
59                                 int layer_id,
60                                 std::vector<channel_context> channels,
61                                 spl::shared_ptr<core::help_repository> help_repo,
62                                 spl::shared_ptr<core::media_info_repository> media_info_repo,
63                                 spl::shared_ptr<core::cg_producer_registry> cg_registry,
64                                 spl::shared_ptr<core::system_info_provider_repository> system_info_repo,
65                                 std::shared_ptr<core::thumbnail_generator> thumb_gen,
66                                 spl::shared_ptr<const core::frame_producer_registry> producer_registry,
67                                 spl::shared_ptr<const core::frame_consumer_registry> consumer_registry,
68                                 std::shared_ptr<accelerator::ogl::device> ogl_device,
69                                 std::promise<bool>& shutdown_server_now)
70                         : client(std::move(client))
71                         , channel(channel)
72                         , channel_index(channel_index)
73                         , layer_id(layer_id)
74                         , channels(std::move(channels))
75                         , help_repo(std::move(help_repo))
76                         , media_info_repo(std::move(media_info_repo))
77                         , cg_registry(std::move(cg_registry))
78                         , system_info_repo(std::move(system_info_repo))
79                         , thumb_gen(std::move(thumb_gen))
80                         , producer_registry(std::move(producer_registry))
81                         , consumer_registry(std::move(consumer_registry))
82                         , ogl_device(std::move(ogl_device))
83                         , shutdown_server_now(shutdown_server_now)
84                 {
85                 }
86         };
87
88         typedef std::function<std::wstring(command_context& args)> amcp_command_func;
89
90         class AMCPCommand
91         {
92         private:
93                 command_context         ctx_;
94                 amcp_command_func       command_;
95                 int                                     min_num_params_;
96                 std::wstring            name_;
97                 std::wstring            replyString_;
98                 std::wstring            request_id_;
99         public:
100                 AMCPCommand(const command_context& ctx, const amcp_command_func& command, int min_num_params, const std::wstring& name)
101                         : ctx_(ctx)
102                         , command_(command)
103                         , min_num_params_(min_num_params)
104                         , name_(name)
105                 {
106                 }
107
108                 typedef std::shared_ptr<AMCPCommand> ptr_type;
109
110                 bool Execute()
111                 {
112                         SetReplyString(command_(ctx_));
113                         return true;
114                 }
115
116                 int minimum_parameters() const
117                 {
118                         return min_num_params_;
119                 }
120
121                 void SendReply()
122                 {
123                         if (replyString_.empty())
124                                 return;
125
126                         ctx_.client->send(std::move(replyString_));
127                 }
128
129                 std::vector<std::wstring>& parameters() { return ctx_.parameters; }
130
131                 IO::ClientInfoPtr client() { return ctx_.client; }
132
133                 std::wstring print() const
134                 {
135                         return name_;
136                 }
137
138                 void set_request_id(std::wstring request_id)
139                 {
140                         request_id_ = std::move(request_id);
141                 }
142
143                 void SetReplyString(const std::wstring& str)
144                 {
145                         if (request_id_.empty())
146                                 replyString_ = str;
147                         else
148                                 replyString_ = L"RES " + request_id_ + L" " + str;
149                 }
150         };
151 }}}