]> git.sesse.net Git - casparcg/blob - protocol/clk/CLKProtocolStrategy.cpp
set svn:eol-style native on .h and .cpp files
[casparcg] / protocol / clk / CLKProtocolStrategy.cpp
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  
23 #include "..\stdafx.h"
24
25 #include "CLKProtocolStrategy.h"
26
27 #include <modules/flash/producer/cg_proxy.h>
28
29 #include <string>
30 #include <sstream>
31 #include <algorithm>
32
33 namespace caspar { namespace protocol { namespace CLK {
34         
35 CLKProtocolStrategy::CLKProtocolStrategy(const std::vector<spl::shared_ptr<core::video_channel>>& channels) 
36         : currentState_(ExpectingNewCommand), bClockLoaded_(false), pChannel_(channels.at(0))
37 {}
38
39 void CLKProtocolStrategy::Parse(const TCHAR* pData, int charCount, IO::ClientInfoPtr pClientInfo) 
40 {
41         for(int index = 0; index < charCount; ++index) 
42         {
43                 if(currentState_ == ExpectingNewCommand)
44                         currentCommandString_.str(TEXT(""));
45
46                 TCHAR currentByte = pData[index];
47                 if(currentByte < 32)
48                         currentCommandString_ << TEXT("<") << (int)currentByte << TEXT(">");
49                 else
50                         currentCommandString_ << currentByte;
51
52                 if(currentByte != 0)
53                 {
54                         switch(currentState_)
55                         {
56                                 case ExpectingNewCommand:
57                                         if(currentByte == 1) 
58                                                 currentState_ = ExpectingCommand;                                       
59                                         //just throw anything else away
60                                         break;
61
62                                 case ExpectingCommand:
63                                         if(currentByte == 2) 
64                                         {
65                                                 if(!currentCommand_.SetCommand()) 
66                                                 {
67                                                         CASPAR_LOG(error) << "CLK: Failed to interpret command";
68                                                         currentState_ = ExpectingNewCommand;
69                                                         currentCommand_.Clear();
70                                                 }
71                                                 else 
72                                                         currentState_ = ExpectingClockID;                                               
73                                         }
74                                         else
75                                                 currentCommand_.commandString_ += currentByte;
76                                         break;
77
78                                 case ExpectingClockID:
79                                         if(currentByte == 2)
80                                                 currentState_ = currentCommand_.NeedsTime() ? ExpectingTime : ExpectingParameter;
81                                         else
82                                                 currentCommand_.clockID_ = currentByte - TCHAR('0');
83                                         break;
84
85                                 case ExpectingTime:
86                                         if(currentByte == 2)
87                                                 currentState_ = ExpectingParameter;
88                                         else
89                                                 currentCommand_.time_ += currentByte;
90                                         break;
91
92                                 case ExpectingParameter:
93                                         //allocate new parameter
94                                         if(currentCommand_.parameters_.size() == 0 || currentByte == 2)
95                                                 currentCommand_.parameters_.push_back(std::wstring());
96
97                                         //add the character to end end of the last parameter
98                                         if(currentByte == TEXT('<'))
99                                                 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT("&lt;");
100                                         else if(currentByte == TEXT('>'))
101                                                 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT("&gt;");
102                                         else if(currentByte == TEXT('\"'))
103                                                 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT("&quot;");
104                                         else
105                                                 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += currentByte;
106
107                                         break;
108                         }
109                 }
110                 else 
111                 {
112                         if(currentState_ == ExpectingCommand)
113                         {
114                                 if(!currentCommand_.SetCommand())
115                                         CASPAR_LOG(error) << "CLK: Failed to interpret command";
116                         }
117
118                         if(currentCommand_.command_ == CLKCommand::CLKReset) 
119                         {
120                                 pChannel_->stage().clear(flash::cg_proxy::DEFAULT_LAYER);
121                                 bClockLoaded_ = false;
122                                 
123                                 CASPAR_LOG(info) << L"CLK: Recieved and executed reset-command";
124                         }
125                         else if(currentCommand_.command_ != CLKCommand::CLKInvalidCommand)
126                         {
127                                 if(!bClockLoaded_) 
128                                 {
129                                         flash::create_cg_proxy(pChannel_).add(0, TEXT("hawrysklocka/clock.ft"), true, TEXT(""), currentCommand_.GetData());
130                                         bClockLoaded_ = true;
131                                 }
132                                 else 
133                                         flash::create_cg_proxy(pChannel_).update(0, currentCommand_.GetData());
134                                 
135                                 CASPAR_LOG(debug) << L"CLK: Clockdata sent: " << currentCommand_.GetData();
136                                 CASPAR_LOG(debug) << L"CLK: Executed valid command: " << currentCommandString_.str();
137                         }
138
139                         currentState_ = ExpectingNewCommand;
140                         currentCommand_.Clear();
141                 }
142         }
143 }
144
145 }       //namespace CLK
146 }}      //namespace caspar