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