]> git.sesse.net Git - casparcg/blob - protocol/clk/CLKProtocolStrategy.cpp
Merge branch 'master' of https://github.com/ronag/Server
[casparcg] / protocol / clk / CLKProtocolStrategy.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://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 #include "clk_commands.h"\r
27 \r
28 #include <modules/flash/producer/cg_producer.h>\r
29 \r
30 #include <string>\r
31 #include <algorithm>\r
32 #include <locale>\r
33 \r
34 namespace caspar { namespace protocol { namespace CLK {\r
35         \r
36 CLKProtocolStrategy::CLKProtocolStrategy(\r
37         const std::vector<safe_ptr<core::video_channel>>& channels) \r
38         : currentState_(ExpectingNewCommand)\r
39 {\r
40         add_command_handlers(command_processor_, channels.at(0));\r
41 }\r
42 \r
43 void CLKProtocolStrategy::reset()\r
44 {\r
45         currentState_ = ExpectingNewCommand;\r
46         currentCommandString_.str(L"");\r
47         command_name_.clear();\r
48         parameters_.clear();\r
49 }\r
50 \r
51 void CLKProtocolStrategy::Parse(const TCHAR* pData, int charCount, IO::ClientInfoPtr pClientInfo) \r
52 {\r
53         for (int index = 0; index < charCount; ++index) \r
54         {\r
55                 TCHAR currentByte = pData[index];\r
56                 \r
57                 if (currentByte < 32)\r
58                         currentCommandString_ << L"<" << static_cast<int>(currentByte) << L">";\r
59                 else\r
60                         currentCommandString_ << currentByte;\r
61 \r
62                 if (currentByte != 0)\r
63                 {\r
64                         switch (currentState_)\r
65                         {\r
66                                 case ExpectingNewCommand:\r
67                                         if (currentByte == 1) \r
68                                                 currentState_ = ExpectingCommand;\r
69                                         //just throw anything else away\r
70                                         break;\r
71                                 case ExpectingCommand:\r
72                                         if (currentByte == 2) \r
73                                                 currentState_ = ExpectingParameter;\r
74                                         else\r
75                                                 command_name_ += currentByte;\r
76                                         break;\r
77                                 case ExpectingParameter:\r
78                                         //allocate new parameter\r
79                                         if (parameters_.size() == 0 || currentByte == 2)\r
80                                                 parameters_.push_back(std::wstring());\r
81 \r
82                                         if (currentByte != 2)\r
83                                         {\r
84                                                 //add the character to end end of the last parameter\r
85                                                 if (currentByte == L'<')\r
86                                                         parameters_.back() += L"&lt;";\r
87                                                 else if (currentByte == L'>')\r
88                                                         parameters_.back() += L"&gt;";\r
89                                                 else if (currentByte == L'\"')\r
90                                                         parameters_.back() += L"&quot;";\r
91                                                 else\r
92                                                         parameters_.back() += currentByte;\r
93                                         }\r
94 \r
95                                         break;\r
96                         }\r
97                 }\r
98                 else \r
99                 {\r
100                         std::transform(\r
101                                 command_name_.begin(), command_name_.end(), \r
102                                 command_name_.begin(), \r
103                                 toupper);\r
104 \r
105                         try\r
106                         {\r
107                                 if (!command_processor_.handle(command_name_, parameters_))\r
108                                 {\r
109                                         CASPAR_LOG(error) << "CLK: Unknown command: " << command_name_;\r
110                                 }\r
111                                 else\r
112                                 {\r
113                                         CASPAR_LOG(debug) << L"CLK: Executed valid command: " \r
114                                                 << currentCommandString_.str();\r
115                                 }\r
116                         } \r
117                         catch (...)\r
118                         {\r
119                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
120                                 CASPAR_LOG(error) << "CLK: Failed to interpret command: " \r
121                                         << currentCommandString_.str();\r
122                         }\r
123 \r
124                         reset();\r
125                 }\r
126         }\r
127 }\r
128 \r
129 }}}\r