2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
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
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
19 * Author: Nicklas P Andersson
\r
23 #include "..\stdafx.h"
\r
25 #include "CLKProtocolStrategy.h"
\r
26 #include "clk_commands.h"
\r
28 #include <modules/flash/producer/cg_producer.h>
\r
31 #include <algorithm>
\r
34 namespace caspar { namespace protocol { namespace CLK {
\r
36 CLKProtocolStrategy::CLKProtocolStrategy(
\r
37 const std::vector<safe_ptr<core::video_channel>>& channels)
\r
38 : currentState_(ExpectingNewCommand)
\r
40 add_command_handlers(command_processor_, channels.at(0));
\r
43 void CLKProtocolStrategy::reset()
\r
45 currentState_ = ExpectingNewCommand;
\r
46 currentCommandString_.str(L"");
\r
47 command_name_.clear();
\r
48 parameters_.clear();
\r
51 void CLKProtocolStrategy::Parse(const TCHAR* pData, int charCount, IO::ClientInfoPtr pClientInfo)
\r
53 for (int index = 0; index < charCount; ++index)
\r
55 TCHAR currentByte = pData[index];
\r
57 if (currentByte < 32)
\r
58 currentCommandString_ << L"<" << static_cast<int>(currentByte) << L">";
\r
60 currentCommandString_ << currentByte;
\r
62 if (currentByte != 0)
\r
64 switch (currentState_)
\r
66 case ExpectingNewCommand:
\r
67 if (currentByte == 1)
\r
68 currentState_ = ExpectingCommand;
\r
69 //just throw anything else away
\r
71 case ExpectingCommand:
\r
72 if (currentByte == 2)
\r
73 currentState_ = ExpectingParameter;
\r
75 command_name_ += currentByte;
\r
77 case ExpectingParameter:
\r
78 //allocate new parameter
\r
79 if (parameters_.size() == 0 || currentByte == 2)
\r
80 parameters_.push_back(std::wstring());
\r
82 if (currentByte != 2)
\r
84 //add the character to end end of the last parameter
\r
85 if (currentByte == L'<')
\r
86 parameters_.back() += L"<";
\r
87 else if (currentByte == L'>')
\r
88 parameters_.back() += L">";
\r
89 else if (currentByte == L'\"')
\r
90 parameters_.back() += L""";
\r
92 parameters_.back() += currentByte;
\r
101 command_name_.begin(), command_name_.end(),
\r
102 command_name_.begin(),
\r
107 if (!command_processor_.handle(command_name_, parameters_))
\r
109 CASPAR_LOG(error) << "CLK: Unknown command: " << command_name_;
\r
113 CASPAR_LOG(debug) << L"CLK: Executed valid command: "
\r
114 << currentCommandString_.str();
\r
119 CASPAR_LOG_CURRENT_EXCEPTION();
\r
120 CASPAR_LOG(error) << "CLK: Failed to interpret command: "
\r
121 << currentCommandString_.str();
\r