]> git.sesse.net Git - casparcg/blob - protocol/amcp/AMCPCommand.h
set svn:eol-style native on .h and .cpp files
[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
26 #include <core/consumer/frame_consumer.h>
27 #include <core/video_channel.h>
28
29 #include <boost/algorithm/string.hpp>
30
31 namespace caspar { namespace protocol {
32 namespace amcp {
33         
34         class AMCPCommand
35         {
36                 AMCPCommand(const AMCPCommand&);
37                 AMCPCommand& operator=(const AMCPCommand&);
38         public:
39                 AMCPCommand();
40                 virtual ~AMCPCommand() {}
41                 virtual bool Execute() = 0;
42
43                 virtual bool NeedChannel() = 0;
44                 virtual int GetMinimumParameters() = 0;
45
46                 void SendReply();
47
48                 void AddParameter(const std::wstring& param){_parameters.push_back(param);}
49
50                 void SetClientInfo(IO::ClientInfoPtr& s){pClientInfo_ = s;}
51                 IO::ClientInfoPtr GetClientInfo(){return pClientInfo_;}
52
53                 void SetChannel(const std::shared_ptr<core::video_channel>& pChannel){pChannel_ = pChannel;}
54                 std::shared_ptr<core::video_channel> GetChannel(){return pChannel_;}
55
56                 void SetChannels(const std::vector<spl::shared_ptr<core::video_channel>>& channels){channels_ = channels;}
57                 const std::vector<spl::shared_ptr<core::video_channel>>& GetChannels() { return channels_; }
58
59                 void SetChannelIndex(unsigned int channelIndex){channelIndex_ = channelIndex;}
60                 unsigned int GetChannelIndex(){return channelIndex_;}
61
62                 void SetLayerIntex(int layerIndex){layerIndex_ = layerIndex;}
63                 int GetLayerIndex(int defaultValue = 0) const{return layerIndex_ != -1 ? layerIndex_ : defaultValue;}
64
65                 virtual void Clear();
66
67                 virtual std::wstring print() const = 0;
68
69                 void SetReplyString(const std::wstring& str){replyString_ = str;}
70
71         protected:
72                 std::vector<std::wstring> _parameters;
73                 std::vector<std::wstring> _parameters2;
74
75         private:
76                 unsigned int channelIndex_;
77                 int layerIndex_;
78                 IO::ClientInfoPtr pClientInfo_;
79                 std::shared_ptr<core::video_channel> pChannel_;
80                 std::vector<spl::shared_ptr<core::video_channel>> channels_;
81                 std::wstring replyString_;
82         };
83
84         typedef std::tr1::shared_ptr<AMCPCommand> AMCPCommandPtr;
85
86         template<bool TNeedChannel,int TMinParameters>
87         class AMCPCommandBase : public AMCPCommand
88         {
89         public:
90                 virtual bool Execute()
91                 {
92                         _parameters2 = _parameters;
93                         for(size_t n = 0; n < _parameters.size(); ++n)
94                                 _parameters[n] = boost::to_upper_copy(_parameters[n]);
95                         return (TNeedChannel && !GetChannel()) || _parameters.size() < TMinParameters ? false : DoExecute();
96                 }
97
98                 virtual bool NeedChannel(){return TNeedChannel;}                
99                 virtual int GetMinimumParameters(){return TMinParameters;}
100         protected:
101                 ~AMCPCommandBase(){}
102         private:
103                 virtual bool DoExecute() = 0;
104         };      
105
106 }}}