]> git.sesse.net Git - casparcg/blob - protocol/amcp/AMCPCommandQueue.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / protocol / amcp / AMCPCommandQueue.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20  \r
21 #include "..\stdafx.h"\r
22 \r
23 #include "AMCPCommandQueue.h"\r
24 \r
25 namespace caspar { namespace protocol { namespace amcp {\r
26         \r
27 AMCPCommandQueue::AMCPCommandQueue() : newCommandEvent_(FALSE, FALSE) \r
28 {}\r
29 \r
30 AMCPCommandQueue::~AMCPCommandQueue() \r
31 {\r
32         Stop();\r
33 }\r
34 \r
35 bool AMCPCommandQueue::Start() \r
36 {\r
37         if(commandPump_.IsRunning())\r
38                 return false;\r
39 \r
40         return commandPump_.Start(this);\r
41 }\r
42 \r
43 void AMCPCommandQueue::Stop() \r
44 {\r
45         commandPump_.Stop();\r
46 }\r
47 \r
48 void AMCPCommandQueue::AddCommand(AMCPCommandPtr pNewCommand)\r
49 {\r
50         {\r
51                 tbb::mutex::scoped_lock lock(mutex_);\r
52 \r
53                 if(pNewCommand->GetScheduling() == ImmediatelyAndClear) {\r
54                         //Clears the queue, objects are deleted automatically\r
55                         commands_.clear();\r
56 \r
57                         commands_.push_back(pNewCommand);\r
58                         CASPAR_LOG(info) << "Cleared queue and added command";\r
59                 }\r
60                 else {\r
61                         commands_.push_back(pNewCommand);\r
62                         CASPAR_LOG(info) << "Added command to end of queue";\r
63                 }\r
64         }\r
65 \r
66         SetEvent(newCommandEvent_);\r
67 }\r
68 \r
69 void AMCPCommandQueue::Run(HANDLE stopEvent)\r
70 {\r
71         bool logTemporarilyBadState = true;\r
72         AMCPCommandPtr pCurrentCommand;\r
73 \r
74         CASPAR_LOG(info) << "AMCP CommandPump started";\r
75 \r
76         while(WaitForSingleObject(stopEvent, 0) != WAIT_OBJECT_0)\r
77         {\r
78                 DWORD waitResult = WaitForSingleObject(newCommandEvent_, 50);\r
79                 if(waitResult == WAIT_OBJECT_0) \r
80                 {\r
81                         tbb::mutex::scoped_lock lock(mutex_);\r
82 \r
83                         if(commands_.size() > 0)\r
84                         {\r
85                                 CASPAR_LOG(debug) << "Found " << commands_.size() << " commands in queue";\r
86 \r
87                                 AMCPCommandPtr pNextCommand = commands_.front();\r
88 \r
89                                 if(pCurrentCommand == 0 || pNextCommand->GetScheduling() == ImmediatelyAndClear) {\r
90                                         pCurrentCommand = pNextCommand;\r
91                                         commands_.pop_front();\r
92                                 }\r
93                         }\r
94                 }\r
95 \r
96                 if(pCurrentCommand != 0) \r
97                 {\r
98                         try\r
99                         {\r
100                                 if(pCurrentCommand->Execute()) \r
101                                         CASPAR_LOG(info) << "Executed command: " << pCurrentCommand->print();\r
102                                 else \r
103                                         CASPAR_LOG(info) << "Failed to execute command: " << pCurrentCommand->print();\r
104                         }\r
105                         catch(...)\r
106                         {\r
107                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
108                                 CASPAR_LOG(info) << "Failed to execute command:" << pCurrentCommand->print();\r
109                         }\r
110                                 \r
111                         pCurrentCommand->SendReply();\r
112                         pCurrentCommand.reset();\r
113 \r
114                         newCommandEvent_.Set();\r
115                         logTemporarilyBadState = true;\r
116 \r
117                         CASPAR_LOG(info) << "Ready for a new command";\r
118                 }\r
119         }\r
120 \r
121         CASPAR_LOG(info) << "CommandPump ended";\r
122 }\r
123 \r
124 bool AMCPCommandQueue::OnUnhandledException(const std::exception& ex) throw() \r
125 {\r
126         bool bDoRestart = true;\r
127 \r
128         try \r
129         {\r
130                 CASPAR_LOG(fatal) << "UNHANDLED EXCEPTION in commandqueue. Message: " << ex.what();\r
131         }\r
132         catch(...)\r
133         {\r
134                 bDoRestart = false;\r
135         }\r
136 \r
137         return bDoRestart;\r
138 }\r
139 \r
140 }}}