]> git.sesse.net Git - casparcg/blobdiff - protocol/amcp/AMCPCommand.h
[AMCP] #475 Added special command REQ that can be prepended before any command to...
[casparcg] / protocol / amcp / AMCPCommand.h
index ad081fdacb21305729c74b4934f41b0735c69cf8..7c6d56af797a3468746c928a1515177e95d23c2b 100644 (file)
-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\r
-* This file is part of CasparCG (www.casparcg.com).\r
-*\r
-* CasparCG is free software: you can redistribute it and/or modify\r
-* it under the terms of the GNU General Public License as published by\r
-* the Free Software Foundation, either version 3 of the License, or\r
-* (at your option) any later version.\r
-*\r
-* CasparCG is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-* GNU General Public License for more details.\r
-*\r
-* You should have received a copy of the GNU General Public License\r
-* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-* Author: Nicklas P Andersson\r
-*/\r
-\r
-#pragma once\r
-\r
-#include "../util/clientinfo.h"\r
-\r
-#include <core/consumer/frame_consumer.h>\r
-#include <core/video_channel.h>\r
-\r
-#include <boost/algorithm/string.hpp>\r
-\r
-namespace caspar { namespace protocol {\r
-namespace amcp {\r
-\r
-       enum AMCPCommandScheduling\r
-       {\r
-               Default = 0,\r
-               AddToQueue,\r
-               ImmediatelyAndClear\r
-       };\r
-\r
-       class AMCPCommand\r
-       {\r
-               AMCPCommand(const AMCPCommand&);\r
-               AMCPCommand& operator=(const AMCPCommand&);\r
-       public:\r
-               AMCPCommand();\r
-               virtual ~AMCPCommand() {}\r
-               virtual bool Execute() = 0;\r
-\r
-               virtual bool NeedChannel() = 0;\r
-               virtual AMCPCommandScheduling GetDefaultScheduling() = 0;\r
-               virtual int GetMinimumParameters() = 0;\r
-\r
-               void SendReply();\r
-\r
-               void AddParameter(const std::wstring& param){_parameters.push_back(param);}\r
-\r
-               void SetClientInfo(IO::ClientInfoPtr& s){pClientInfo_ = s;}\r
-               IO::ClientInfoPtr GetClientInfo(){return pClientInfo_;}\r
-\r
-               void SetChannel(const std::shared_ptr<core::video_channel>& pChannel){pChannel_ = pChannel;}\r
-               std::shared_ptr<core::video_channel> GetChannel(){return pChannel_;}\r
-\r
-               void SetChannels(const std::vector<safe_ptr<core::video_channel>>& channels){channels_ = channels;}\r
-               const std::vector<safe_ptr<core::video_channel>>& GetChannels() { return channels_; }\r
-\r
-               void SetChannelIndex(unsigned int channelIndex){channelIndex_ = channelIndex;}\r
-               unsigned int GetChannelIndex(){return channelIndex_;}\r
-\r
-               void SetLayerIntex(int layerIndex){layerIndex_ = layerIndex;}\r
-               int GetLayerIndex(int defaultValue = 0) const{return layerIndex_ != -1 ? layerIndex_ : defaultValue;}\r
-\r
-               virtual void Clear();\r
-\r
-               AMCPCommandScheduling GetScheduling()\r
-               {\r
-                       return scheduling_ == Default ? GetDefaultScheduling() : scheduling_;\r
-               }\r
-\r
-               virtual std::wstring print() const = 0;\r
-\r
-               void SetScheduling(AMCPCommandScheduling s){scheduling_ = s;}\r
-\r
-       protected:\r
-               void SetReplyString(const std::wstring& str){replyString_ = str;}\r
-               std::vector<std::wstring> _parameters;\r
-               std::vector<std::wstring> _parameters2;\r
-\r
-       private:\r
-               unsigned int channelIndex_;\r
-               int layerIndex_;\r
-               IO::ClientInfoPtr pClientInfo_;\r
-               std::shared_ptr<core::video_channel> pChannel_;\r
-               std::vector<safe_ptr<core::video_channel>> channels_;\r
-               AMCPCommandScheduling scheduling_;\r
-               std::wstring replyString_;\r
-       };\r
-\r
-       typedef std::tr1::shared_ptr<AMCPCommand> AMCPCommandPtr;\r
-\r
-       template<bool TNeedChannel, AMCPCommandScheduling TScheduling, int TMinParameters>\r
-       class AMCPCommandBase : public AMCPCommand\r
-       {\r
-       public:\r
-               virtual bool Execute()\r
-               {\r
-                       _parameters2 = _parameters;\r
-                       for(size_t n = 0; n < _parameters.size(); ++n)\r
-                               _parameters[n] = boost::to_upper_copy(_parameters[n]);\r
-                       return (TNeedChannel && !GetChannel()) || _parameters.size() < TMinParameters ? false : DoExecute();\r
-               }\r
-\r
-               virtual bool NeedChannel(){return TNeedChannel;}                \r
-               virtual AMCPCommandScheduling GetDefaultScheduling(){return TScheduling;}\r
-               virtual int GetMinimumParameters(){return TMinParameters;}\r
-       protected:\r
-               ~AMCPCommandBase(){}\r
-       private:\r
-               virtual bool DoExecute() = 0;\r
-       };      \r
-\r
-}}}\r
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Nicklas P Andersson
+*/
+
+#pragma once
+
+#include "../util/ClientInfo.h"
+#include "amcp_shared.h"
+#include <core/consumer/frame_consumer.h>
+#include <core/producer/frame_producer.h>
+#include <accelerator/ogl/util/device.h>
+
+#include <boost/algorithm/string.hpp>
+
+namespace caspar { namespace protocol {
+namespace amcp {
+
+       struct command_context
+       {
+               IO::ClientInfoPtr                                                                               client;
+               channel_context                                                                                 channel;
+               int                                                                                                             channel_index;
+               int                                                                                                             layer_id;
+               std::vector<channel_context>                                                    channels;
+               spl::shared_ptr<core::help_repository>                                  help_repo;
+               spl::shared_ptr<core::media_info_repository>                    media_info_repo;
+               spl::shared_ptr<core::cg_producer_registry>                             cg_registry;
+               spl::shared_ptr<core::system_info_provider_repository>  system_info_repo;
+               std::shared_ptr<core::thumbnail_generator>                              thumb_gen;
+               spl::shared_ptr<const core::frame_producer_registry>    producer_registry;
+               spl::shared_ptr<const core::frame_consumer_registry>    consumer_registry;
+               std::shared_ptr<accelerator::ogl::device>                               ogl_device;
+               std::promise<bool>&                                                                             shutdown_server_now;
+               std::vector<std::wstring>                                                               parameters;
+
+               int layer_index(int default_ = 0) const { return layer_id == -1 ? default_: layer_id; }
+
+               command_context(
+                               IO::ClientInfoPtr client,
+                               channel_context channel,
+                               int channel_index,
+                               int layer_id,
+                               std::vector<channel_context> channels,
+                               spl::shared_ptr<core::help_repository> help_repo,
+                               spl::shared_ptr<core::media_info_repository> media_info_repo,
+                               spl::shared_ptr<core::cg_producer_registry> cg_registry,
+                               spl::shared_ptr<core::system_info_provider_repository> system_info_repo,
+                               std::shared_ptr<core::thumbnail_generator> thumb_gen,
+                               spl::shared_ptr<const core::frame_producer_registry> producer_registry,
+                               spl::shared_ptr<const core::frame_consumer_registry> consumer_registry,
+                               std::shared_ptr<accelerator::ogl::device> ogl_device,
+                               std::promise<bool>& shutdown_server_now)
+                       : client(std::move(client))
+                       , channel(channel)
+                       , channel_index(channel_index)
+                       , layer_id(layer_id)
+                       , channels(std::move(channels))
+                       , help_repo(std::move(help_repo))
+                       , media_info_repo(std::move(media_info_repo))
+                       , cg_registry(std::move(cg_registry))
+                       , system_info_repo(std::move(system_info_repo))
+                       , thumb_gen(std::move(thumb_gen))
+                       , producer_registry(std::move(producer_registry))
+                       , consumer_registry(std::move(consumer_registry))
+                       , ogl_device(std::move(ogl_device))
+                       , shutdown_server_now(shutdown_server_now)
+               {
+               }
+       };
+
+       typedef std::function<std::wstring(command_context& args)> amcp_command_func;
+
+       class AMCPCommand
+       {
+       private:
+               command_context         ctx_;
+               amcp_command_func       command_;
+               int                                     min_num_params_;
+               std::wstring            name_;
+               std::wstring            replyString_;
+               std::wstring            request_id_;
+       public:
+               AMCPCommand(const command_context& ctx, const amcp_command_func& command, int min_num_params, const std::wstring& name)
+                       : ctx_(ctx)
+                       , command_(command)
+                       , min_num_params_(min_num_params)
+                       , name_(name)
+               {
+               }
+
+               typedef std::shared_ptr<AMCPCommand> ptr_type;
+
+               bool Execute()
+               {
+                       SetReplyString(command_(ctx_));
+                       return true;
+               }
+
+               int minimum_parameters() const
+               {
+                       return min_num_params_;
+               }
+
+               void SendReply()
+               {
+                       if (replyString_.empty())
+                               return;
+
+                       ctx_.client->send(std::move(replyString_));
+               }
+
+               std::vector<std::wstring>& parameters() { return ctx_.parameters; }
+
+               IO::ClientInfoPtr client() { return ctx_.client; }
+
+               std::wstring print() const
+               {
+                       return name_;
+               }
+
+               void set_request_id(std::wstring request_id)
+               {
+                       request_id_ = std::move(request_id);
+               }
+
+               void SetReplyString(const std::wstring& str)
+               {
+                       if (request_id_.empty())
+                               replyString_ = str;
+                       else
+                               replyString_ = L"RES " + request_id_ + L" " + str;
+               }
+       };
+}}}