comparison vamp-client/CapnpClient.h @ 95:b6ac26b72b59

Implement list, use request-response classes in loader
author Chris Cannam <c.cannam@qmul.ac.uk>
date Thu, 13 Oct 2016 14:31:10 +0100
parents a660dca988f8
children
comparison
equal deleted inserted replaced
94:a660dca988f8 95:b6ac26b72b59
51 //!!! obviously, factor out all repetitive guff 51 //!!! obviously, factor out all repetitive guff
52 52
53 //!!! list and load are supposed to be called by application code, 53 //!!! list and load are supposed to be called by application code,
54 //!!! but the rest are only supposed to be called by the plugin -- 54 //!!! but the rest are only supposed to be called by the plugin --
55 //!!! sort out the api here 55 //!!! sort out the api here
56 56
57 Vamp::Plugin * 57 // Loader methods:
58 load(std::string key, float inputSampleRate, int adapterFlags) { 58
59 59 Vamp::HostExt::ListResponse
60 if (!m_transport->isOK()) { 60 listPluginData() override {
61 throw std::runtime_error("Piper server failed to start"); 61
62 } 62 if (!m_transport->isOK()) {
63 63 throw std::runtime_error("Piper server failed to start");
64 Vamp::HostExt::PluginStaticData psd; 64 }
65 Vamp::HostExt::PluginConfiguration defaultConfig; 65
66 PluginHandleMapper::Handle handle = 66 capnp::MallocMessageBuilder message;
67 serverLoad(key, inputSampleRate, adapterFlags, psd, defaultConfig); 67 RpcRequest::Builder builder = message.initRoot<RpcRequest>();
68 VampnProto::buildRpcRequest_List(builder);
69 ReqId id = getId();
70 builder.getId().setNumber(id);
71
72 //!!! pure boilerplate:
73 auto arr = capnp::messageToFlatArray(message);
74 auto responseBuffer = m_transport->call(arr.asChars().begin(),
75 arr.asChars().size());
76 auto karr = toKJArray(responseBuffer);
77 capnp::FlatArrayMessageReader responseMessage(karr);
78 RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>();
79
80 checkResponseType(reader, RpcResponse::Response::Which::LIST, id);
81
82 Vamp::HostExt::ListResponse lr;
83 VampnProto::readListResponse(lr, reader.getResponse().getList());
84 return lr;
85 }
86
87 Vamp::HostExt::LoadResponse
88 loadPlugin(const Vamp::HostExt::LoadRequest &req) override {
89
90 if (!m_transport->isOK()) {
91 throw std::runtime_error("Piper server failed to start");
92 }
93
94 Vamp::HostExt::LoadResponse resp;
95 PluginHandleMapper::Handle handle = serverLoad(req.pluginKey,
96 req.inputSampleRate,
97 req.adapterFlags,
98 resp.staticData,
99 resp.defaultConfiguration);
68 100
69 Vamp::Plugin *plugin = new PluginStub(this, 101 Vamp::Plugin *plugin = new PluginStub(this,
70 key, 102 req.pluginKey,
71 inputSampleRate, 103 req.inputSampleRate,
72 adapterFlags, 104 req.adapterFlags,
73 psd, 105 resp.staticData,
74 defaultConfig); 106 resp.defaultConfiguration);
75 107
76 m_mapper.addPlugin(handle, plugin); 108 m_mapper.addPlugin(handle, plugin);
77 109
78 return plugin; 110 resp.plugin = plugin;
79 } 111 return resp;
80 112 }
81 PluginHandleMapper::Handle 113
82 serverLoad(std::string key, float inputSampleRate, int adapterFlags, 114 // PluginClient methods:
83 Vamp::HostExt::PluginStaticData &psd, 115
84 Vamp::HostExt::PluginConfiguration &defaultConfig) {
85
86 Vamp::HostExt::LoadRequest request;
87 request.pluginKey = key;
88 request.inputSampleRate = inputSampleRate;
89 request.adapterFlags = adapterFlags;
90
91 capnp::MallocMessageBuilder message;
92 RpcRequest::Builder builder = message.initRoot<RpcRequest>();
93
94 VampnProto::buildRpcRequest_Load(builder, request);
95 ReqId id = getId();
96 builder.getId().setNumber(id);
97
98 auto arr = capnp::messageToFlatArray(message);
99
100 auto responseBuffer = m_transport->call(arr.asChars().begin(),
101 arr.asChars().size());
102
103 //!!! ... --> will also need some way to kill this process
104 //!!! (from another thread)
105
106 auto karr = toKJArray(responseBuffer);
107 capnp::FlatArrayMessageReader responseMessage(karr);
108 RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>();
109
110 //!!! handle (explicit) error case
111
112 checkResponseType(reader, RpcResponse::Response::Which::LOAD, id);
113
114 const LoadResponse::Reader &lr = reader.getResponse().getLoad();
115 VampnProto::readExtractorStaticData(psd, lr.getStaticData());
116 VampnProto::readConfiguration(defaultConfig, lr.getDefaultConfiguration());
117 return lr.getHandle();
118 };
119
120 protected:
121 virtual 116 virtual
122 Vamp::Plugin::OutputList 117 Vamp::Plugin::OutputList
123 configure(PluginStub *plugin, 118 configure(PluginStub *plugin,
124 Vamp::HostExt::PluginConfiguration config) override { 119 Vamp::HostExt::PluginConfiguration config) override {
125 120
296 } 291 }
297 if (ReqId(r.getId().getNumber()) != id) { 292 if (ReqId(r.getId().getNumber()) != id) {
298 throw std::runtime_error("Wrong response id"); 293 throw std::runtime_error("Wrong response id");
299 } 294 }
300 } 295 }
296
297 PluginHandleMapper::Handle
298 serverLoad(std::string key, float inputSampleRate, int adapterFlags,
299 Vamp::HostExt::PluginStaticData &psd,
300 Vamp::HostExt::PluginConfiguration &defaultConfig) {
301
302 Vamp::HostExt::LoadRequest request;
303 request.pluginKey = key;
304 request.inputSampleRate = inputSampleRate;
305 request.adapterFlags = adapterFlags;
306
307 capnp::MallocMessageBuilder message;
308 RpcRequest::Builder builder = message.initRoot<RpcRequest>();
309
310 VampnProto::buildRpcRequest_Load(builder, request);
311 ReqId id = getId();
312 builder.getId().setNumber(id);
313
314 auto arr = capnp::messageToFlatArray(message);
315
316 auto responseBuffer = m_transport->call(arr.asChars().begin(),
317 arr.asChars().size());
318
319 //!!! ... --> will also need some way to kill this process
320 //!!! (from another thread)
321
322 auto karr = toKJArray(responseBuffer);
323 capnp::FlatArrayMessageReader responseMessage(karr);
324 RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>();
325
326 //!!! handle (explicit) error case
327
328 checkResponseType(reader, RpcResponse::Response::Which::LOAD, id);
329
330 const LoadResponse::Reader &lr = reader.getResponse().getLoad();
331 VampnProto::readExtractorStaticData(psd, lr.getStaticData());
332 VampnProto::readConfiguration(defaultConfig, lr.getDefaultConfiguration());
333 return lr.getHandle();
334 };
301 335
302 private: 336 private:
303 SynchronousTransport *m_transport; //!!! I don't own this, but should I? 337 SynchronousTransport *m_transport; //!!! I don't own this, but should I?
304 CompletenessChecker *m_completenessChecker; // I own this 338 CompletenessChecker *m_completenessChecker; // I own this
305 }; 339 };