]> git.sesse.net Git - casparcg/blob - protocol/clk/CLKProtocolStrategy.cpp
7f14171deedd48a324f3f4199fca5b9a4c492ae9
[casparcg] / protocol / clk / CLKProtocolStrategy.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\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
10 *\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
15 *\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
18 *\r
19 * Author: Nicklas P Andersson\r
20 */\r
21 \r
22  \r
23 #include "..\stdafx.h"\r
24 \r
25 #include "CLKProtocolStrategy.h"\r
26 \r
27 #include <modules/flash/producer/cg_proxy.h>\r
28 \r
29 #include <string>\r
30 #include <sstream>\r
31 #include <algorithm>\r
32 \r
33 namespace caspar { namespace protocol { namespace CLK {\r
34         \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
37 {}\r
38 \r
39 void CLKProtocolStrategy::Parse(const TCHAR* pData, int charCount, IO::ClientInfoPtr pClientInfo) \r
40 {\r
41         for(int index = 0; index < charCount; ++index) \r
42         {\r
43                 if(currentState_ == ExpectingNewCommand)\r
44                         currentCommandString_.str(TEXT(""));\r
45 \r
46                 TCHAR currentByte = pData[index];\r
47                 if(currentByte < 32)\r
48                         currentCommandString_ << TEXT("<") << (int)currentByte << TEXT(">");\r
49                 else\r
50                         currentCommandString_ << currentByte;\r
51 \r
52                 if(currentByte != 0)\r
53                 {\r
54                         switch(currentState_)\r
55                         {\r
56                                 case ExpectingNewCommand:\r
57                                         if(currentByte == 1) \r
58                                                 currentState_ = ExpectingCommand;                                       \r
59                                         //just throw anything else away\r
60                                         break;\r
61 \r
62                                 case ExpectingCommand:\r
63                                         if(currentByte == 2) \r
64                                         {\r
65                                                 if(!currentCommand_.SetCommand()) \r
66                                                 {\r
67                                                         CASPAR_LOG(error) << "CLK: Failed to interpret command";\r
68                                                         currentState_ = ExpectingNewCommand;\r
69                                                         currentCommand_.Clear();\r
70                                                 }\r
71                                                 else \r
72                                                         currentState_ = ExpectingClockID;                                               \r
73                                         }\r
74                                         else\r
75                                                 currentCommand_.commandString_ += currentByte;\r
76                                         break;\r
77 \r
78                                 case ExpectingClockID:\r
79                                         if(currentByte == 2)\r
80                                                 currentState_ = currentCommand_.NeedsTime() ? ExpectingTime : ExpectingParameter;\r
81                                         else\r
82                                                 currentCommand_.clockID_ = currentByte - TCHAR('0');\r
83                                         break;\r
84 \r
85                                 case ExpectingTime:\r
86                                         if(currentByte == 2)\r
87                                                 currentState_ = ExpectingParameter;\r
88                                         else\r
89                                                 currentCommand_.time_ += currentByte;\r
90                                         break;\r
91 \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
96 \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("&lt;");\r
100                                         else if(currentByte == TEXT('>'))\r
101                                                 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT("&gt;");\r
102                                         else if(currentByte == TEXT('\"'))\r
103                                                 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT("&quot;");\r
104                                         else\r
105                                                 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += currentByte;\r
106 \r
107                                         break;\r
108                         }\r
109                 }\r
110                 else \r
111                 {\r
112                         if(currentState_ == ExpectingCommand)\r
113                         {\r
114                                 if(!currentCommand_.SetCommand())\r
115                                         CASPAR_LOG(error) << "CLK: Failed to interpret command";\r
116                         }\r
117 \r
118                         if(currentCommand_.command_ == CLKCommand::CLKReset) \r
119                         {\r
120                                 pChannel_->stage().clear(flash::cg_proxy::DEFAULT_LAYER);\r
121                                 bClockLoaded_ = false;\r
122                                 \r
123                                 CASPAR_LOG(info) << L"CLK: Recieved and executed reset-command";\r
124                         }\r
125                         else if(currentCommand_.command_ != CLKCommand::CLKInvalidCommand)\r
126                         {\r
127                                 if(!bClockLoaded_) \r
128                                 {\r
129                                         flash::create_cg_proxy(pChannel_).add(0, TEXT("hawrysklocka/clock.ft"), true, TEXT(""), currentCommand_.GetData());\r
130                                         bClockLoaded_ = true;\r
131                                 }\r
132                                 else \r
133                                         flash::create_cg_proxy(pChannel_).update(0, currentCommand_.GetData());\r
134                                 \r
135                                 CASPAR_LOG(debug) << L"CLK: Clockdata sent: " << currentCommand_.GetData();\r
136                                 CASPAR_LOG(debug) << L"CLK: Executed valid command: " << currentCommandString_.str();\r
137                         }\r
138 \r
139                         currentState_ = ExpectingNewCommand;\r
140                         currentCommand_.Clear();\r
141                 }\r
142         }\r
143 }\r
144 \r
145 }       //namespace CLK\r
146 }}      //namespace caspar