2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
21 #include "..\stdafx.h"
\r
23 #include "CLKProtocolStrategy.h"
\r
25 #include <core/producer\flash\cg_producer.h>
\r
29 #include <algorithm>
\r
31 namespace caspar { namespace protocol { namespace CLK {
\r
33 CLKProtocolStrategy::CLKProtocolStrategy(const std::vector<safe_ptr<core::channel>>& channels)
\r
34 : currentState_(ExpectingNewCommand), bClockLoaded_(false), pChannel_(channels.at(0))
\r
37 void CLKProtocolStrategy::Parse(const TCHAR* pData, int charCount, IO::ClientInfoPtr pClientInfo)
\r
39 for(int index = 0; index < charCount; ++index)
\r
41 if(currentState_ == ExpectingNewCommand)
\r
42 currentCommandString_.str(TEXT(""));
\r
44 TCHAR currentByte = pData[index];
\r
45 if(currentByte < 32)
\r
46 currentCommandString_ << TEXT("<") << (int)currentByte << TEXT(">");
\r
48 currentCommandString_ << currentByte;
\r
50 if(currentByte != 0)
\r
52 switch(currentState_)
\r
54 case ExpectingNewCommand:
\r
55 if(currentByte == 1)
\r
56 currentState_ = ExpectingCommand;
\r
57 //just throw anything else away
\r
60 case ExpectingCommand:
\r
61 if(currentByte == 2)
\r
63 if(!currentCommand_.SetCommand())
\r
65 CASPAR_LOG(error) << "CLK: Failed to interpret command";
\r
66 currentState_ = ExpectingNewCommand;
\r
67 currentCommand_.Clear();
\r
70 currentState_ = ExpectingClockID;
\r
73 currentCommand_.commandString_ += currentByte;
\r
76 case ExpectingClockID:
\r
77 if(currentByte == 2)
\r
78 currentState_ = currentCommand_.NeedsTime() ? ExpectingTime : ExpectingParameter;
\r
80 currentCommand_.clockID_ = currentByte - TCHAR('0');
\r
84 if(currentByte == 2)
\r
85 currentState_ = ExpectingParameter;
\r
87 currentCommand_.time_ += currentByte;
\r
90 case ExpectingParameter:
\r
91 //allocate new parameter
\r
92 if(currentCommand_.parameters_.size() == 0 || currentByte == 2)
\r
93 currentCommand_.parameters_.push_back(std::wstring());
\r
95 //add the character to end end of the last parameter
\r
96 if(currentByte == TEXT('<'))
\r
97 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT("<");
\r
98 else if(currentByte == TEXT('>'))
\r
99 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT(">");
\r
100 else if(currentByte == TEXT('\"'))
\r
101 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT(""");
\r
103 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += currentByte;
\r
110 if(currentState_ == ExpectingCommand)
\r
112 if(!currentCommand_.SetCommand())
\r
113 CASPAR_LOG(error) << "CLK: Failed to interpret command";
\r
116 if(currentCommand_.command_ == CLKCommand::CLKReset)
\r
118 core::flash::get_default_cg_producer(pChannel_)->clear();
\r
119 bClockLoaded_ = false;
\r
121 CASPAR_LOG(info) << L"CLK: Recieved and executed reset-command";
\r
123 else if(currentCommand_.command_ != CLKCommand::CLKInvalidCommand)
\r
125 if(!bClockLoaded_)
\r
127 core::flash::get_default_cg_producer(pChannel_)->add(0, TEXT("hawrysklocka/clock"), true, TEXT(""), currentCommand_.GetData());
\r
128 bClockLoaded_ = true;
\r
131 core::flash::get_default_cg_producer(pChannel_)->update(0, currentCommand_.GetData());
\r
133 CASPAR_LOG(debug) << L"CLK: Clockdata sent: " << currentCommand_.GetData();
\r
134 CASPAR_LOG(debug) << L"CLK: Executed valid command: " << currentCommandString_.str();
\r
137 currentState_ = ExpectingNewCommand;
\r
138 currentCommand_.Clear();
\r
144 }} //namespace caspar