]> git.sesse.net Git - casparcg/blob - protocol/amcp/AMCPCommand.h
0eb9a2855bff2297c9d4629aa4e09e1b6c858744
[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         public:
99                 AMCPCommand(const command_context& ctx, const amcp_command_func& command, int min_num_params, const std::wstring& name)
100                         : ctx_(ctx)
101                         , command_(command)
102                         , min_num_params_(min_num_params)
103                         , name_(name)
104                 {
105                 }
106
107                 typedef std::shared_ptr<AMCPCommand> ptr_type;
108
109                 bool Execute()
110                 {
111                         SetReplyString(command_(ctx_));
112                         return true;
113                 }
114
115                 int minimum_parameters() const
116                 {
117                         return min_num_params_;
118                 }
119
120                 void SendReply()
121                 {
122                         if (replyString_.empty())
123                                 return;
124
125                         ctx_.client->send(std::move(replyString_));
126                 }
127
128                 std::vector<std::wstring>& parameters() { return ctx_.parameters; }
129
130                 IO::ClientInfoPtr client() { return ctx_.client; }
131
132                 std::wstring print() const
133                 {
134                         return name_;
135                 }
136
137                 void SetReplyString(const std::wstring& str)
138                 {
139                         replyString_ = str;
140                 }
141         };
142 }}}