cannam@111
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@118
|
2 /*
|
c@118
|
3 Piper C++
|
c@118
|
4
|
c@118
|
5 An API for audio analysis and feature extraction plugins.
|
c@118
|
6
|
c@118
|
7 Centre for Digital Music, Queen Mary, University of London.
|
c@118
|
8 Copyright 2006-2016 Chris Cannam and QMUL.
|
c@118
|
9
|
c@118
|
10 Permission is hereby granted, free of charge, to any person
|
c@118
|
11 obtaining a copy of this software and associated documentation
|
c@118
|
12 files (the "Software"), to deal in the Software without
|
c@118
|
13 restriction, including without limitation the rights to use, copy,
|
c@118
|
14 modify, merge, publish, distribute, sublicense, and/or sell copies
|
c@118
|
15 of the Software, and to permit persons to whom the Software is
|
c@118
|
16 furnished to do so, subject to the following conditions:
|
c@118
|
17
|
c@118
|
18 The above copyright notice and this permission notice shall be
|
c@118
|
19 included in all copies or substantial portions of the Software.
|
c@118
|
20
|
c@118
|
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
c@118
|
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
c@118
|
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
c@118
|
24 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
c@118
|
25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
c@118
|
26 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
c@118
|
27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
c@118
|
28
|
c@118
|
29 Except as contained in this notice, the names of the Centre for
|
c@118
|
30 Digital Music; Queen Mary, University of London; and Chris Cannam
|
c@118
|
31 shall not be used in advertising or otherwise to promote the sale,
|
c@118
|
32 use or other dealings in this Software without prior written
|
c@118
|
33 authorization.
|
c@118
|
34 */
|
cannam@111
|
35
|
c@94
|
36 #ifndef PIPER_PROCESS_QT_TRANSPORT_H
|
c@94
|
37 #define PIPER_PROCESS_QT_TRANSPORT_H
|
c@94
|
38
|
c@94
|
39 #include "SynchronousTransport.h"
|
c@94
|
40
|
c@94
|
41 #include <QProcess>
|
c@94
|
42 #include <QString>
|
c@100
|
43 #include <QMutex>
|
c@94
|
44
|
c@94
|
45 #include <iostream>
|
c@94
|
46
|
c@124
|
47 //#define DEBUG_TRANSPORT 1
|
c@124
|
48
|
c@97
|
49 namespace piper_vamp {
|
c@97
|
50 namespace client {
|
c@94
|
51
|
c@100
|
52 /**
|
c@100
|
53 * A SynchronousTransport implementation that spawns a sub-process
|
c@100
|
54 * using Qt's QProcess abstraction and talks to it via stdin/stdout
|
c@100
|
55 * channels. Calls are completely serialized; the protocol only
|
c@100
|
56 * supports one call in process at a time, and therefore the transport
|
c@125
|
57 * only allows one at a time.
|
c@125
|
58 *
|
c@125
|
59 * This class is thread-safe, but in practice you can only use it from
|
c@125
|
60 * within a single thread, because the underlying QProcess does not
|
c@125
|
61 * support switching threads.
|
c@100
|
62 */
|
c@94
|
63 class ProcessQtTransport : public SynchronousTransport
|
c@94
|
64 {
|
c@94
|
65 public:
|
c@119
|
66 ProcessQtTransport(std::string processName, std::string formatArg) :
|
c@126
|
67 m_completenessChecker(0),
|
c@126
|
68 m_crashed(false) {
|
c@119
|
69
|
c@94
|
70 m_process = new QProcess();
|
c@94
|
71 m_process->setReadChannel(QProcess::StandardOutput);
|
c@94
|
72 m_process->setProcessChannelMode(QProcess::ForwardedErrorChannel);
|
cannam@114
|
73
|
c@119
|
74 m_process->start(QString::fromStdString(processName),
|
c@124
|
75 { QString::fromStdString(formatArg) });
|
cannam@114
|
76
|
c@94
|
77 if (!m_process->waitForStarted()) {
|
cannam@113
|
78 if (m_process->state() == QProcess::NotRunning) {
|
cannam@113
|
79 QProcess::ProcessError err = m_process->error();
|
cannam@113
|
80 if (err == QProcess::FailedToStart) {
|
cannam@113
|
81 std::cerr << "Unable to start server process "
|
cannam@113
|
82 << processName << std::endl;
|
cannam@113
|
83 } else if (err == QProcess::Crashed) {
|
cannam@113
|
84 std::cerr << "Server process " << processName
|
cannam@113
|
85 << " crashed on startup" << std::endl;
|
cannam@113
|
86 } else {
|
cannam@113
|
87 std::cerr << "Server process " << processName
|
cannam@113
|
88 << " failed on startup with error code "
|
cannam@113
|
89 << err << std::endl;
|
cannam@113
|
90 }
|
cannam@113
|
91 delete m_process;
|
cannam@113
|
92 m_process = nullptr;
|
cannam@111
|
93 }
|
c@94
|
94 }
|
c@94
|
95 }
|
c@94
|
96
|
c@94
|
97 ~ProcessQtTransport() {
|
c@94
|
98 if (m_process) {
|
c@94
|
99 if (m_process->state() != QProcess::NotRunning) {
|
c@118
|
100 m_process->closeWriteChannel();
|
c@94
|
101 m_process->waitForFinished(200);
|
c@94
|
102 m_process->close();
|
c@94
|
103 m_process->waitForFinished();
|
c@124
|
104 #ifdef DEBUG_TRANSPORT
|
c@94
|
105 std::cerr << "server exited" << std::endl;
|
c@124
|
106 #endif
|
c@94
|
107 }
|
c@94
|
108 delete m_process;
|
c@94
|
109 }
|
c@94
|
110 }
|
c@94
|
111
|
c@94
|
112 void
|
cannam@111
|
113 setCompletenessChecker(MessageCompletenessChecker *checker) override {
|
c@94
|
114 m_completenessChecker = checker;
|
c@94
|
115 }
|
c@94
|
116
|
c@94
|
117 bool
|
c@94
|
118 isOK() const override {
|
c@126
|
119 return (m_process != nullptr) && !m_crashed;
|
c@94
|
120 }
|
c@94
|
121
|
c@94
|
122 std::vector<char>
|
c@126
|
123 call(const char *ptr, size_t size, bool slow) override {
|
c@94
|
124
|
c@118
|
125 QMutexLocker locker(&m_mutex);
|
c@118
|
126
|
c@94
|
127 if (!m_completenessChecker) {
|
c@94
|
128 throw std::logic_error("No completeness checker set on transport");
|
c@94
|
129 }
|
c@126
|
130 if (!isOK()) {
|
c@126
|
131 throw std::logic_error("Transport is not OK");
|
c@126
|
132 }
|
c@94
|
133
|
c@124
|
134 #ifdef DEBUG_TRANSPORT
|
c@115
|
135 std::cerr << "writing " << size << " bytes to server" << std::endl;
|
c@124
|
136 #endif
|
c@94
|
137 m_process->write(ptr, size);
|
c@126
|
138 m_process->waitForBytesWritten();
|
c@94
|
139
|
c@94
|
140 std::vector<char> buffer;
|
c@94
|
141 bool complete = false;
|
c@94
|
142
|
c@94
|
143 while (!complete) {
|
c@94
|
144
|
c@94
|
145 qint64 byteCount = m_process->bytesAvailable();
|
c@94
|
146
|
c@118
|
147 if (!byteCount) {
|
c@124
|
148 #ifdef DEBUG_TRANSPORT
|
c@118
|
149 std::cerr << "waiting for data from server..." << std::endl;
|
c@124
|
150 #endif
|
c@126
|
151 if (slow) {
|
c@126
|
152 m_process->waitForReadyRead(1000);
|
c@126
|
153 } else {
|
c@126
|
154 #ifdef _WIN32
|
c@126
|
155 // This is most unsatisfactory -- if we give a non-zero
|
c@126
|
156 // arg here, then we end up sleeping way beyond the arrival
|
c@126
|
157 // of the data to read -- can end up using less than 10%
|
c@126
|
158 // CPU during processing which is crazy. So for Windows
|
c@126
|
159 // only, we busy-wait during "fast" calls. It works out
|
c@126
|
160 // much faster in the end. Could do with a simpler native
|
c@126
|
161 // blocking API really.
|
c@126
|
162 m_process->waitForReadyRead(0);
|
c@126
|
163 #else
|
c@126
|
164 m_process->waitForReadyRead(100);
|
c@126
|
165 #endif
|
c@126
|
166 }
|
c@126
|
167 if (m_process->state() == QProcess::NotRunning &&
|
c@126
|
168 // don't give up until we've read all that's been buffered!
|
c@126
|
169 !m_process->bytesAvailable()) {
|
c@115
|
170 QProcess::ProcessError err = m_process->error();
|
c@115
|
171 if (err == QProcess::Crashed) {
|
c@115
|
172 std::cerr << "Server crashed during request" << std::endl;
|
c@115
|
173 } else {
|
c@115
|
174 std::cerr << "Server failed during request with error code "
|
c@115
|
175 << err << std::endl;
|
c@115
|
176 }
|
c@126
|
177 m_crashed = true;
|
c@121
|
178 throw ServerCrashed();
|
c@94
|
179 }
|
c@94
|
180 } else {
|
c@94
|
181 size_t formerSize = buffer.size();
|
c@94
|
182 buffer.resize(formerSize + byteCount);
|
c@94
|
183 m_process->read(buffer.data() + formerSize, byteCount);
|
c@94
|
184 complete = m_completenessChecker->isComplete(buffer);
|
c@94
|
185 }
|
c@94
|
186 }
|
c@94
|
187
|
c@94
|
188 return buffer;
|
c@94
|
189 }
|
c@94
|
190
|
c@94
|
191 private:
|
c@94
|
192 MessageCompletenessChecker *m_completenessChecker; //!!! I don't own this (currently)
|
c@94
|
193 QProcess *m_process; // I own this
|
c@100
|
194 QMutex m_mutex;
|
c@126
|
195 bool m_crashed;
|
c@94
|
196 };
|
c@94
|
197
|
c@94
|
198 }
|
c@94
|
199 }
|
c@94
|
200
|
c@94
|
201 #endif
|