comparison vamp-client/client.cpp @ 83:154e94ea84d4

Toward QProcess pipe comms take on it
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 11 Oct 2016 17:08:31 +0100
parents fa2c0358c2b6
children db9a6ab618bc
comparison
equal deleted inserted replaced
82:fa2c0358c2b6 83:154e94ea84d4
2 #include "stub.h" 2 #include "stub.h"
3 3
4 #include "vamp-capnp/VampnProto.h" 4 #include "vamp-capnp/VampnProto.h"
5 5
6 #include "vamp-support/AssignedPluginHandleMapper.h" 6 #include "vamp-support/AssignedPluginHandleMapper.h"
7
8 #include <QProcess>
9
10 #include <stdexcept>
11
12 using std::cerr;
13 using std::endl;
7 14
8 // First cut plan: this is to be client-qt.cpp, using a QProcess, so 15 // First cut plan: this is to be client-qt.cpp, using a QProcess, so
9 // we're using pipes and the server is completely synchronous, 16 // we're using pipes and the server is completely synchronous,
10 // handling only one call at once. Our PiperClient will fire off a 17 // handling only one call at once. Our PiperClient will fire off a
11 // QProcess and refer to its io device. Each request message is 18 // QProcess and refer to its io device. Each request message is
17 // message. If the response message's id does not match the request 24 // message. If the response message's id does not match the request
18 // message's, then the server has gone wrong (it should never be 25 // message's, then the server has gone wrong (it should never be
19 // servicing more than one request at a time). 26 // servicing more than one request at a time).
20 27
21 // Next level: Capnp RPC, but I want to get the first level to work 28 // Next level: Capnp RPC, but I want to get the first level to work
22 // first. 29 // first, not least because the server already exists.
23 30
24 namespace piper { //!!! probably something different 31 namespace piper { //!!! probably something different
25 32
26 class PiperClient : public PiperClientBase 33 class PiperClient : public PiperClientBase
27 { 34 {
28 // unsigned to avoid undefined behaviour on possible wrap 35 // unsigned to avoid undefined behaviour on possible wrap
29 typedef uint32_t ReqId; 36 typedef uint32_t ReqId;
30 37
31 public: 38 public:
39 PiperClient() {
40 m_process = new QProcess();
41 m_process->setReadChannel(QProcess::StandardOutput);
42 m_process->setProcessChannelMode(QProcess::ForwardedErrorChannel);
43 m_process->start("../bin/piper-vamp-server"); //!!!
44 if (!m_process->waitForStarted()) {
45 cerr << "server failed to start" << endl;
46 delete m_process;
47 m_process = 0;
48 }
49 }
32 50
33 PiperClient() { } 51 ~PiperClient() {
52 if (m_process) {
53 if (m_process->state() != QProcess::NotRunning) {
54 m_process->close();
55 m_process->waitForFinished();
56 }
57 delete m_process;
58 }
59 }
34 60
61 //!!! obviously, factor out all repetitive guff
62
35 Vamp::Plugin * 63 Vamp::Plugin *
36 load(std::string key, float inputSampleRate, int adapterFlags) { 64 load(std::string key, float inputSampleRate, int adapterFlags) {
65
66 if (!m_process) {
67 throw std::runtime_error("Piper server failed to start");
68 }
37 69
38 Vamp::HostExt::LoadRequest request; 70 Vamp::HostExt::LoadRequest request;
39 request.pluginKey = key; 71 request.pluginKey = key;
40 request.inputSampleRate = inputSampleRate; 72 request.inputSampleRate = inputSampleRate;
41 request.adapterFlags = adapterFlags; 73 request.adapterFlags = adapterFlags;
44 RpcRequest::Builder builder = message.initRoot<RpcRequest>(); 76 RpcRequest::Builder builder = message.initRoot<RpcRequest>();
45 77
46 VampnProto::buildRpcRequest_Load(builder, request); 78 VampnProto::buildRpcRequest_Load(builder, request);
47 ReqId id = getId(); 79 ReqId id = getId();
48 builder.getId().setNumber(id); 80 builder.getId().setNumber(id);
81
82 auto arr = messageToFlatArray(message);
83 m_process->write(arr.asChars().begin(), arr.asChars().size());
84
85 ///.... read...
49 }; 86 };
50 87
51 virtual 88 virtual
52 Vamp::Plugin::OutputList 89 Vamp::Plugin::OutputList
53 configure(PiperStubPlugin *plugin, 90 configure(PiperStubPlugin *plugin,
54 Vamp::HostExt::PluginConfiguration config) { 91 Vamp::HostExt::PluginConfiguration config) {
92
93 if (!m_process) {
94 throw std::runtime_error("Piper server failed to start");
95 }
55 96
56 Vamp::HostExt::ConfigurationRequest request; 97 Vamp::HostExt::ConfigurationRequest request;
57 request.plugin = plugin; 98 request.plugin = plugin;
58 request.configuration = config; 99 request.configuration = config;
59 100
76 117
77 virtual Vamp::Plugin::FeatureSet 118 virtual Vamp::Plugin::FeatureSet
78 finish(PiperStubPlugin *plugin) = 0; 119 finish(PiperStubPlugin *plugin) = 0;
79 120
80 private: 121 private:
122 QProcess *m_process;
81 AssignedPluginHandleMapper m_mapper; 123 AssignedPluginHandleMapper m_mapper;
82 int getId() { 124 int getId() {
83 //!!! todo: mutex 125 //!!! todo: mutex
84 static ReqId m_nextId = 0; 126 static ReqId m_nextId = 0;
85 return m_nextId++; 127 return m_nextId++;