2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Nicklas P Andersson
23 #include "..\stdafx.h"
25 #include "CLKProtocolStrategy.h"
27 #include <modules/flash/producer/cg_proxy.h>
33 namespace caspar { namespace protocol { namespace CLK {
35 CLKProtocolStrategy::CLKProtocolStrategy(const std::vector<spl::shared_ptr<core::video_channel>>& channels)
36 : currentState_(ExpectingNewCommand), bClockLoaded_(false), pChannel_(channels.at(0))
39 void CLKProtocolStrategy::Parse(const TCHAR* pData, int charCount, IO::ClientInfoPtr pClientInfo)
41 for(int index = 0; index < charCount; ++index)
43 if(currentState_ == ExpectingNewCommand)
44 currentCommandString_.str(TEXT(""));
46 TCHAR currentByte = pData[index];
48 currentCommandString_ << TEXT("<") << (int)currentByte << TEXT(">");
50 currentCommandString_ << currentByte;
56 case ExpectingNewCommand:
58 currentState_ = ExpectingCommand;
59 //just throw anything else away
62 case ExpectingCommand:
65 if(!currentCommand_.SetCommand())
67 CASPAR_LOG(error) << "CLK: Failed to interpret command";
68 currentState_ = ExpectingNewCommand;
69 currentCommand_.Clear();
72 currentState_ = ExpectingClockID;
75 currentCommand_.commandString_ += currentByte;
78 case ExpectingClockID:
80 currentState_ = currentCommand_.NeedsTime() ? ExpectingTime : ExpectingParameter;
82 currentCommand_.clockID_ = currentByte - TCHAR('0');
87 currentState_ = ExpectingParameter;
89 currentCommand_.time_ += currentByte;
92 case ExpectingParameter:
93 //allocate new parameter
94 if(currentCommand_.parameters_.size() == 0 || currentByte == 2)
95 currentCommand_.parameters_.push_back(std::wstring());
97 //add the character to end end of the last parameter
98 if(currentByte == TEXT('<'))
99 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT("<");
100 else if(currentByte == TEXT('>'))
101 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT(">");
102 else if(currentByte == TEXT('\"'))
103 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT(""");
105 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += currentByte;
112 if(currentState_ == ExpectingCommand)
114 if(!currentCommand_.SetCommand())
115 CASPAR_LOG(error) << "CLK: Failed to interpret command";
118 if(currentCommand_.command_ == CLKCommand::CLKReset)
120 pChannel_->stage().clear(flash::cg_proxy::DEFAULT_LAYER);
121 bClockLoaded_ = false;
123 CASPAR_LOG(info) << L"CLK: Recieved and executed reset-command";
125 else if(currentCommand_.command_ != CLKCommand::CLKInvalidCommand)
129 flash::create_cg_proxy(pChannel_).add(0, TEXT("hawrysklocka/clock.ft"), true, TEXT(""), currentCommand_.GetData());
130 bClockLoaded_ = true;
133 flash::create_cg_proxy(pChannel_).update(0, currentCommand_.GetData());
135 CASPAR_LOG(debug) << L"CLK: Clockdata sent: " << currentCommand_.GetData();
136 CASPAR_LOG(debug) << L"CLK: Executed valid command: " << currentCommandString_.str();
139 currentState_ = ExpectingNewCommand;
140 currentCommand_.Clear();
146 }} //namespace caspar