]> git.sesse.net Git - casparcg/blob - protocol/amcp/AMCPCommand.h
b73cc85c7e46b869ffff85dbee57f00d91bea7ec
[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
28 #include <boost/algorithm/string.hpp>
29
30 namespace caspar { namespace protocol {
31 namespace amcp {
32         
33         class AMCPCommand
34         {
35                 AMCPCommand& operator=(const AMCPCommand&);
36         protected:
37                 AMCPCommand(const AMCPCommand& rhs) : client_(rhs.client_), parameters_(rhs.parameters_)
38                 {}
39
40         public:
41                 typedef std::shared_ptr<AMCPCommand> ptr_type;
42
43                 explicit AMCPCommand(IO::ClientInfoPtr client) : client_(client) {}
44                 virtual ~AMCPCommand() {}
45
46                 virtual bool Execute() = 0;
47                 virtual int minimum_parameters() = 0;
48
49                 void SendReply();
50
51                 std::vector<std::wstring>& parameters() { return parameters_; }
52
53                 IO::ClientInfoPtr client() { return client_; }
54
55                 virtual std::wstring print() const = 0;
56
57                 void SetReplyString(const std::wstring& str){replyString_ = str;}
58
59         private:
60                 std::vector<std::wstring> parameters_;
61                 IO::ClientInfoPtr client_;
62                 std::wstring replyString_;
63         };
64
65         template<int TMinParameters>
66         class AMCPCommandBase : public AMCPCommand
67         {
68         protected:
69                 explicit AMCPCommandBase(IO::ClientInfoPtr client) : AMCPCommand(client) {}
70                 AMCPCommandBase(const AMCPCommandBase& rhs) : AMCPCommand(rhs) {}
71                 template<int T>
72                 AMCPCommandBase(const AMCPCommandBase<T>& rhs) : AMCPCommand(rhs) {}
73
74                 ~AMCPCommandBase(){}
75         public:
76                 virtual bool Execute()
77                 {
78                         return (parameters().size() < TMinParameters) ? false : DoExecute();
79                 }
80                 virtual int minimum_parameters(){return TMinParameters;}
81         private:
82                 virtual bool DoExecute() = 0;
83         };      
84
85         class AMCPChannelCommand
86         {
87         protected:
88                 AMCPChannelCommand(const channel_context& ctx, unsigned int channel_index, int layer_index) : ctx_(ctx), channel_index_(channel_index), layer_index_(layer_index)
89                 {}
90                 AMCPChannelCommand(const AMCPChannelCommand& rhs) : ctx_(rhs.ctx_), channel_index_(rhs.channel_index_), layer_index_(rhs.layer_index_)
91                 {}
92
93                 spl::shared_ptr<core::video_channel>& channel() { return ctx_.channel; }
94                 spl::shared_ptr<IO::lock_container>& lock_container() { return ctx_.lock; }
95
96                 unsigned int channel_index(){return channel_index_;}
97                 int layer_index(int default = 0) const{return layer_index_ != -1 ? layer_index_ : default; }
98
99         private:
100                 unsigned int channel_index_;
101                 int layer_index_;
102                 channel_context ctx_;
103         };
104
105         class AMCPChannelsAwareCommand
106         {
107         protected:
108                 AMCPChannelsAwareCommand(const std::vector<channel_context>& c) : channels_(c) {}
109                 AMCPChannelsAwareCommand(const AMCPChannelsAwareCommand& rhs) : channels_(rhs.channels_) {}
110
111                 const std::vector<channel_context>& channels() { return channels_; }
112
113         private:
114                 const std::vector<channel_context>& channels_;
115         };
116
117         template<int TMinParameters>
118         class AMCPChannelCommandBase : public AMCPChannelCommand, public AMCPCommandBase<TMinParameters>
119         {
120         public:
121                 AMCPChannelCommandBase(IO::ClientInfoPtr client, const channel_context& channel, unsigned int channel_index, int layer_index) : AMCPChannelCommand(channel, channel_index, layer_index), AMCPCommandBase(client)
122                 {}
123         protected:
124                 AMCPChannelCommandBase(const AMCPChannelCommandBase& rhs) : AMCPChannelCommand(rhs), AMCPCommandBase(rhs)
125                 {}
126                 template<int T>
127                 AMCPChannelCommandBase(const AMCPChannelCommandBase<T>& rhs) : AMCPChannelCommand(rhs), AMCPCommandBase(rhs)
128                 {}
129         };
130 }}}