]> git.sesse.net Git - casparcg/blob - protocol/amcp/AMCPCommand.h
2.0.0.2: Restructuring. Moved protocol code (legacy) into separate project.
[casparcg] / 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 "../util/clientinfo.h"\r
23 \r
24 #include <core/consumer/frame_consumer.h>\r
25 #include <core/channel.h>\r
26 \r
27 #include <boost/algorithm/string.hpp>\r
28 \r
29 namespace caspar { namespace protocol {\r
30 namespace amcp {\r
31 \r
32         enum AMCPCommandScheduling\r
33         {\r
34                 Default = 0,\r
35                 AddToQueue,\r
36                 ImmediatelyAndClear\r
37         };\r
38 \r
39         class AMCPCommand\r
40         {\r
41                 AMCPCommand(const AMCPCommand&);\r
42                 AMCPCommand& operator=(const AMCPCommand&);\r
43         public:\r
44                 AMCPCommand();\r
45                 virtual ~AMCPCommand() {}\r
46                 virtual bool Execute() = 0;\r
47 \r
48                 virtual bool NeedChannel() = 0;\r
49                 virtual AMCPCommandScheduling GetDefaultScheduling() = 0;\r
50                 virtual int GetMinimumParameters() = 0;\r
51 \r
52                 void SendReply();\r
53 \r
54                 void AddParameter(const std::wstring& param){_parameters.push_back(param);}\r
55 \r
56                 void SetClientInfo(IO::ClientInfoPtr& s){pClientInfo_ = s;}\r
57                 IO::ClientInfoPtr GetClientInfo(){return pClientInfo_;}\r
58 \r
59                 void SetChannel(const std::shared_ptr<core::channel>& pChannel){pChannel_ = pChannel;}\r
60                 std::shared_ptr<core::channel> GetChannel(){return pChannel_;}\r
61 \r
62                 void SetChannelIndex(unsigned int channelIndex){channelIndex_ = channelIndex;}\r
63                 unsigned int GetChannelIndex(){return channelIndex_;}\r
64 \r
65                 void SetLayerIntex(int layerIndex){layerIndex_ = layerIndex;}\r
66                 int GetLayerIndex(int defaultValue = 0) const{return layerIndex_ != -1 ? layerIndex_ : defaultValue;}\r
67 \r
68                 virtual void Clear();\r
69 \r
70                 AMCPCommandScheduling GetScheduling()\r
71                 {\r
72                         return scheduling_ == Default ? GetDefaultScheduling() : scheduling_;\r
73                 }\r
74 \r
75                 void SetScheduling(AMCPCommandScheduling s){scheduling_ = s;}\r
76 \r
77         protected:\r
78                 void SetReplyString(const std::wstring& str){replyString_ = str;}\r
79                 std::vector<std::wstring> _parameters;\r
80 \r
81         private:\r
82                 unsigned int channelIndex_;\r
83                 int layerIndex_;\r
84                 IO::ClientInfoPtr pClientInfo_;\r
85                 std::shared_ptr<core::channel> pChannel_;\r
86                 AMCPCommandScheduling scheduling_;\r
87                 std::wstring replyString_;\r
88         };\r
89 \r
90         typedef std::tr1::shared_ptr<AMCPCommand> AMCPCommandPtr;\r
91 \r
92         template<bool TNeedChannel, AMCPCommandScheduling TScheduling, int TMinParameters>\r
93         class AMCPCommandBase : public AMCPCommand\r
94         {\r
95         public:\r
96                 virtual bool Execute()\r
97                 {\r
98                         for(size_t n = 0; n < _parameters.size(); ++n)\r
99                                 _parameters[n] = boost::to_upper_copy(_parameters[n]);\r
100                         return (TNeedChannel && !GetChannel()) || _parameters.size() < TMinParameters ? false : DoExecute();\r
101                 }\r
102 \r
103                 virtual bool NeedChannel(){return TNeedChannel;}                \r
104                 virtual AMCPCommandScheduling GetDefaultScheduling(){return TScheduling;}\r
105                 virtual int GetMinimumParameters(){return TMinParameters;}\r
106         protected:\r
107                 ~AMCPCommandBase(){}\r
108         private:\r
109                 virtual bool DoExecute() = 0;\r
110         };      \r
111 \r
112 }}}\r