]> git.sesse.net Git - casparcg/blob - core/protocol/amcp/AMCPCommand.h
7e1d914a0ef242d3fd2cb609dc1140c1854f8794
[casparcg] / core / protocol / amcp / AMCPCommand.h
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 #pragma once\r
21 \r
22 #include "../../../common/io/clientinfo.h"\r
23 #include "../../consumer/frame_consumer.h"\r
24 #include <boost/algorithm/string.hpp>\r
25 #include "../../renderer/render_device.h"\r
26 \r
27 namespace caspar {\r
28 namespace amcp {\r
29 \r
30         enum AMCPCommandScheduling\r
31         {\r
32                 Default = 0,\r
33                 AddToQueue,\r
34                 ImmediatelyAndClear\r
35         };\r
36 \r
37         class AMCPCommand\r
38         {\r
39                 AMCPCommand(const AMCPCommand&);\r
40                 AMCPCommand& operator=(const AMCPCommand&);\r
41         public:\r
42                 AMCPCommand();\r
43                 virtual ~AMCPCommand() {}\r
44                 virtual bool Execute() = 0;\r
45 \r
46                 virtual bool NeedChannel() = 0;\r
47                 virtual AMCPCommandScheduling GetDefaultScheduling() = 0;\r
48                 virtual int GetMinimumParameters() = 0;\r
49 \r
50                 void SendReply();\r
51 \r
52                 void AddParameter(const std::wstring& param){_parameters.push_back(param);}\r
53 \r
54                 void SetClientInfo(caspar::IO::ClientInfoPtr& s){pClientInfo_ = s;}\r
55                 caspar::IO::ClientInfoPtr GetClientInfo(){return pClientInfo_;}\r
56 \r
57                 void SetChannel(const renderer::render_device_ptr& pChannel){pChannel_ = pChannel;}\r
58                 renderer::render_device_ptr GetChannel(){return pChannel_;}\r
59 \r
60                 void SetChannelIndex(unsigned int channelIndex){channelIndex_ = channelIndex;}\r
61                 unsigned int GetChannelIndex(){return channelIndex_;}\r
62 \r
63                 void SetLayerIntex(int layerIndex){layerIndex_ = layerIndex;}\r
64                 int GetLayerIndex(int defaultValue = 0) const{return layerIndex_ != -1 ? layerIndex_ : defaultValue;}\r
65 \r
66                 virtual void Clear();\r
67 \r
68                 AMCPCommandScheduling GetScheduling()\r
69                 {\r
70                         return scheduling_ == Default ? GetDefaultScheduling() : scheduling_;\r
71                 }\r
72 \r
73                 void SetScheduling(AMCPCommandScheduling s){scheduling_ = s;}\r
74 \r
75         protected:\r
76                 void SetReplyString(const std::wstring& str){replyString_ = str;}\r
77                 std::vector<std::wstring> _parameters;\r
78 \r
79         private:\r
80                 unsigned int channelIndex_;\r
81                 int layerIndex_;\r
82                 caspar::IO::ClientInfoPtr pClientInfo_;\r
83                 renderer::render_device_ptr pChannel_;\r
84                 AMCPCommandScheduling scheduling_;\r
85                 std::wstring replyString_;\r
86         };\r
87 \r
88         typedef std::tr1::shared_ptr<AMCPCommand> AMCPCommandPtr;\r
89 \r
90         template<bool TNeedChannel, AMCPCommandScheduling TScheduling, int TMinParameters>\r
91         class AMCPCommandBase : public AMCPCommand\r
92         {\r
93         public:\r
94                 virtual bool Execute()\r
95                 {\r
96                         for(size_t n = 0; n < _parameters.size(); ++n)\r
97                                 _parameters[n] = boost::to_upper_copy(_parameters[n]);\r
98                         return (TNeedChannel && !GetChannel()) || _parameters.size() < TMinParameters ? false : DoExecute();\r
99                 }\r
100 \r
101                 virtual bool NeedChannel(){return TNeedChannel;}                \r
102                 virtual AMCPCommandScheduling GetDefaultScheduling(){return TScheduling;}\r
103                 virtual int GetMinimumParameters(){return TMinParameters;}\r
104         protected:\r
105                 ~AMCPCommandBase(){}\r
106         private:\r
107                 virtual bool DoExecute() = 0;\r
108         };      \r
109 \r
110 }}\r