comparison vamp-client/CapnpRRClient.h @ 134:3dcf0394971d

Debug output improvements, and make the checker actually attempt to call the descriptor function for known plugin types
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 16 Nov 2016 16:12:42 +0000
parents d04958b5d3ad
children 9768b5ca710a
comparison
equal deleted inserted replaced
133:74a7c2a8d6b6 134:3dcf0394971d
41 #include "PluginStub.h" 41 #include "PluginStub.h"
42 #include "SynchronousTransport.h" 42 #include "SynchronousTransport.h"
43 43
44 #include "vamp-support/AssignedPluginHandleMapper.h" 44 #include "vamp-support/AssignedPluginHandleMapper.h"
45 #include "vamp-capnp/VampnProto.h" 45 #include "vamp-capnp/VampnProto.h"
46
47 #include <sstream>
46 48
47 #include <capnp/serialize.h> 49 #include <capnp/serialize.h>
48 50
49 namespace piper_vamp { 51 namespace piper_vamp {
50 namespace client { 52 namespace client {
78 return words >= expected; 80 return words >= expected;
79 } 81 }
80 }; 82 };
81 83
82 public: 84 public:
83 CapnpRRClient(SynchronousTransport *transport) : //!!! ownership? shared ptr? 85 CapnpRRClient(SynchronousTransport *transport, //!!! ownership? shared ptr?
86 LogCallback *logger) : // logger may be nullptr for cerr
87 m_logger(logger),
84 m_transport(transport), 88 m_transport(transport),
85 m_completenessChecker(new CompletenessChecker) { 89 m_completenessChecker(new CompletenessChecker) {
86 transport->setCompletenessChecker(m_completenessChecker); 90 transport->setCompletenessChecker(m_completenessChecker);
87 } 91 }
88 92
100 104
101 ListResponse 105 ListResponse
102 listPluginData(const ListRequest &req) override { 106 listPluginData(const ListRequest &req) override {
103 107
104 if (!m_transport->isOK()) { 108 if (!m_transport->isOK()) {
109 log("Piper server crashed or failed to start (caller should have checked this)");
105 throw std::runtime_error("Piper server crashed or failed to start"); 110 throw std::runtime_error("Piper server crashed or failed to start");
106 } 111 }
107 112
108 capnp::MallocMessageBuilder message; 113 capnp::MallocMessageBuilder message;
109 piper::RpcRequest::Builder builder = message.initRoot<piper::RpcRequest>(); 114 piper::RpcRequest::Builder builder = message.initRoot<piper::RpcRequest>();
110 VampnProto::buildRpcRequest_List(builder, req); 115 VampnProto::buildRpcRequest_List(builder, req);
111 ReqId id = getId(); 116 ReqId id = getId();
112 builder.getId().setNumber(id); 117 builder.getId().setNumber(id);
113 118
114 auto karr = call(message, true); 119 auto karr = call(message, "list", true);
115 120
116 capnp::FlatArrayMessageReader responseMessage(karr); 121 capnp::FlatArrayMessageReader responseMessage(karr);
117 piper::RpcResponse::Reader reader = responseMessage.getRoot<piper::RpcResponse>(); 122 piper::RpcResponse::Reader reader = responseMessage.getRoot<piper::RpcResponse>();
118 123
119 checkResponseType(reader, piper::RpcResponse::Response::Which::LIST, id); 124 checkResponseType(reader, piper::RpcResponse::Response::Which::LIST, id);
125 130
126 LoadResponse 131 LoadResponse
127 loadPlugin(const LoadRequest &req) override { 132 loadPlugin(const LoadRequest &req) override {
128 133
129 if (!m_transport->isOK()) { 134 if (!m_transport->isOK()) {
135 log("Piper server crashed or failed to start (caller should have checked this)");
130 throw std::runtime_error("Piper server crashed or failed to start"); 136 throw std::runtime_error("Piper server crashed or failed to start");
131 } 137 }
132 138
133 LoadResponse resp; 139 LoadResponse resp;
134 PluginHandleMapper::Handle handle = serverLoad(req.pluginKey, 140 PluginHandleMapper::Handle handle = serverLoad(req.pluginKey,
156 Vamp::Plugin::OutputList 162 Vamp::Plugin::OutputList
157 configure(PluginStub *plugin, 163 configure(PluginStub *plugin,
158 PluginConfiguration config) override { 164 PluginConfiguration config) override {
159 165
160 if (!m_transport->isOK()) { 166 if (!m_transport->isOK()) {
167 log("Piper server crashed or failed to start (caller should have checked this)");
161 throw std::runtime_error("Piper server crashed or failed to start"); 168 throw std::runtime_error("Piper server crashed or failed to start");
162 } 169 }
163 170
164 ConfigurationRequest request; 171 ConfigurationRequest request;
165 request.plugin = plugin; 172 request.plugin = plugin;
170 177
171 VampnProto::buildRpcRequest_Configure(builder, request, m_mapper); 178 VampnProto::buildRpcRequest_Configure(builder, request, m_mapper);
172 ReqId id = getId(); 179 ReqId id = getId();
173 builder.getId().setNumber(id); 180 builder.getId().setNumber(id);
174 181
175 auto karr = call(message, true); 182 auto karr = call(message, "configure", true);
176 183
177 capnp::FlatArrayMessageReader responseMessage(karr); 184 capnp::FlatArrayMessageReader responseMessage(karr);
178 piper::RpcResponse::Reader reader = responseMessage.getRoot<piper::RpcResponse>(); 185 piper::RpcResponse::Reader reader = responseMessage.getRoot<piper::RpcResponse>();
179 186
180 //!!! handle (explicit) error case 187 //!!! handle (explicit) error case
194 process(PluginStub *plugin, 201 process(PluginStub *plugin,
195 std::vector<std::vector<float> > inputBuffers, 202 std::vector<std::vector<float> > inputBuffers,
196 Vamp::RealTime timestamp) override { 203 Vamp::RealTime timestamp) override {
197 204
198 if (!m_transport->isOK()) { 205 if (!m_transport->isOK()) {
206 log("Piper server crashed or failed to start (caller should have checked this)");
199 throw std::runtime_error("Piper server crashed or failed to start"); 207 throw std::runtime_error("Piper server crashed or failed to start");
200 } 208 }
201 209
202 ProcessRequest request; 210 ProcessRequest request;
203 request.plugin = plugin; 211 request.plugin = plugin;
208 piper::RpcRequest::Builder builder = message.initRoot<piper::RpcRequest>(); 216 piper::RpcRequest::Builder builder = message.initRoot<piper::RpcRequest>();
209 VampnProto::buildRpcRequest_Process(builder, request, m_mapper); 217 VampnProto::buildRpcRequest_Process(builder, request, m_mapper);
210 ReqId id = getId(); 218 ReqId id = getId();
211 builder.getId().setNumber(id); 219 builder.getId().setNumber(id);
212 220
213 auto karr = call(message, false); 221 auto karr = call(message, "process", false);
214 222
215 capnp::FlatArrayMessageReader responseMessage(karr); 223 capnp::FlatArrayMessageReader responseMessage(karr);
216 piper::RpcResponse::Reader reader = responseMessage.getRoot<piper::RpcResponse>(); 224 piper::RpcResponse::Reader reader = responseMessage.getRoot<piper::RpcResponse>();
217 225
218 //!!! handle (explicit) error case 226 //!!! handle (explicit) error case
229 237
230 virtual Vamp::Plugin::FeatureSet 238 virtual Vamp::Plugin::FeatureSet
231 finish(PluginStub *plugin) override { 239 finish(PluginStub *plugin) override {
232 240
233 if (!m_transport->isOK()) { 241 if (!m_transport->isOK()) {
242 log("Piper server crashed or failed to start (caller should have checked this)");
234 throw std::runtime_error("Piper server crashed or failed to start"); 243 throw std::runtime_error("Piper server crashed or failed to start");
235 } 244 }
236 245
237 FinishRequest request; 246 FinishRequest request;
238 request.plugin = plugin; 247 request.plugin = plugin;
242 251
243 VampnProto::buildRpcRequest_Finish(builder, request, m_mapper); 252 VampnProto::buildRpcRequest_Finish(builder, request, m_mapper);
244 ReqId id = getId(); 253 ReqId id = getId();
245 builder.getId().setNumber(id); 254 builder.getId().setNumber(id);
246 255
247 auto karr = call(message, true); 256 auto karr = call(message, "finish", true);
248 257
249 capnp::FlatArrayMessageReader responseMessage(karr); 258 capnp::FlatArrayMessageReader responseMessage(karr);
250 piper::RpcResponse::Reader reader = responseMessage.getRoot<piper::RpcResponse>(); 259 piper::RpcResponse::Reader reader = responseMessage.getRoot<piper::RpcResponse>();
251 260
252 //!!! handle (explicit) error case 261 //!!! handle (explicit) error case
269 virtual void 278 virtual void
270 reset(PluginStub *plugin, 279 reset(PluginStub *plugin,
271 PluginConfiguration config) override { 280 PluginConfiguration config) override {
272 281
273 // Reload the plugin on the server side, and configure it as requested 282 // Reload the plugin on the server side, and configure it as requested
274 283
275 if (!m_transport->isOK()) { 284 log("CapnpRRClient: reset() called, plugin will be closed and reloaded");
285
286 if (!m_transport->isOK()) {
287 log("Piper server crashed or failed to start (caller should have checked this)");
276 throw std::runtime_error("Piper server crashed or failed to start"); 288 throw std::runtime_error("Piper server crashed or failed to start");
277 } 289 }
278 290
279 if (m_mapper.havePlugin(plugin)) { 291 if (m_mapper.havePlugin(plugin)) {
280 (void)finish(plugin); // server-side unload 292 (void)finish(plugin); // server-side unload
317 checkResponseType(const piper::RpcResponse::Reader &r, 329 checkResponseType(const piper::RpcResponse::Reader &r,
318 piper::RpcResponse::Response::Which type, 330 piper::RpcResponse::Response::Which type,
319 ReqId id) { 331 ReqId id) {
320 332
321 if (r.getResponse().which() != type) { 333 if (r.getResponse().which() != type) {
322 std::cerr << "checkResponseType: wrong response type (received " 334 std::ostringstream s;
323 << int(r.getResponse().which()) << ", expected " 335 s << "checkResponseType: wrong response type (received "
324 << int(type) << ")" 336 << int(r.getResponse().which()) << ", expected " << int(type) << ")";
325 << std::endl; 337 log(s.str());
326 throw std::runtime_error("Wrong response type"); 338 throw std::runtime_error("Wrong response type");
327 } 339 }
328 if (ReqId(r.getId().getNumber()) != id) { 340 if (ReqId(r.getId().getNumber()) != id) {
329 std::cerr << "checkResponseType: wrong response id (received " 341 std::ostringstream s;
330 << r.getId().getNumber() << ", expected " << id << ")" 342 s << "checkResponseType: wrong response id (received "
331 << std::endl; 343 << r.getId().getNumber() << ", expected " << id << ")";
344 log(s.str());
332 throw std::runtime_error("Wrong response id"); 345 throw std::runtime_error("Wrong response id");
333 } 346 }
334 } 347 }
335 348
336 kj::Array<capnp::word> 349 kj::Array<capnp::word>
337 call(capnp::MallocMessageBuilder &message, bool slow) { 350 call(capnp::MallocMessageBuilder &message, std::string type, bool slow) {
338 auto arr = capnp::messageToFlatArray(message); 351 auto arr = capnp::messageToFlatArray(message);
339 auto responseBuffer = m_transport->call(arr.asChars().begin(), 352 auto responseBuffer = m_transport->call(arr.asChars().begin(),
340 arr.asChars().size(), 353 arr.asChars().size(),
354 type,
341 slow); 355 slow);
342 return toKJArray(responseBuffer); 356 return toKJArray(responseBuffer);
343 } 357 }
344 358
345 PluginHandleMapper::Handle 359 PluginHandleMapper::Handle
357 371
358 VampnProto::buildRpcRequest_Load(builder, request); 372 VampnProto::buildRpcRequest_Load(builder, request);
359 ReqId id = getId(); 373 ReqId id = getId();
360 builder.getId().setNumber(id); 374 builder.getId().setNumber(id);
361 375
362 auto karr = call(message, false); 376 auto karr = call(message, "load", false);
363 377
364 //!!! ... --> will also need some way to kill this process 378 //!!! ... --> will also need some way to kill this process
365 //!!! (from another thread) 379 //!!! (from another thread)
366 380
367 capnp::FlatArrayMessageReader responseMessage(karr); 381 capnp::FlatArrayMessageReader responseMessage(karr);
376 VampnProto::readConfiguration(defaultConfig, lr.getDefaultConfiguration()); 390 VampnProto::readConfiguration(defaultConfig, lr.getDefaultConfiguration());
377 return lr.getHandle(); 391 return lr.getHandle();
378 }; 392 };
379 393
380 private: 394 private:
395 LogCallback *m_logger;
381 SynchronousTransport *m_transport; //!!! I don't own this, but should I? 396 SynchronousTransport *m_transport; //!!! I don't own this, but should I?
382 CompletenessChecker *m_completenessChecker; // I own this 397 CompletenessChecker *m_completenessChecker; // I own this
398
399 void log(std::string message) const {
400 if (m_logger) m_logger->log(message);
401 else std::cerr << message << std::endl;
402 }
383 }; 403 };
384 404
385 } 405 }
386 } 406 }
387 407