diff 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
line wrap: on
line diff
--- a/vamp-client/client.cpp	Tue Oct 11 16:52:48 2016 +0100
+++ b/vamp-client/client.cpp	Tue Oct 11 17:08:31 2016 +0100
@@ -5,6 +5,13 @@
 
 #include "vamp-support/AssignedPluginHandleMapper.h"
 
+#include <QProcess>
+
+#include <stdexcept>
+
+using std::cerr;
+using std::endl;
+
 // First cut plan: this is to be client-qt.cpp, using a QProcess, so
 // we're using pipes and the server is completely synchronous,
 // handling only one call at once. Our PiperClient will fire off a
@@ -19,7 +26,7 @@
 // servicing more than one request at a time).
 
 // Next level: Capnp RPC, but I want to get the first level to work
-// first.
+// first, not least because the server already exists.
 
 namespace piper { //!!! probably something different
 
@@ -29,12 +36,37 @@
     typedef uint32_t ReqId;
     
 public:
+    PiperClient() {
+        m_process = new QProcess();
+        m_process->setReadChannel(QProcess::StandardOutput);
+        m_process->setProcessChannelMode(QProcess::ForwardedErrorChannel);
+        m_process->start("../bin/piper-vamp-server"); //!!!
+        if (!m_process->waitForStarted()) {
+            cerr << "server failed to start" << endl;
+            delete m_process;
+            m_process = 0;
+        }
+    }
 
-    PiperClient() { }
+    ~PiperClient() {
+        if (m_process) {
+            if (m_process->state() != QProcess::NotRunning) {
+                m_process->close();
+                m_process->waitForFinished();
+            }
+            delete m_process;
+        }
+    }
 
+    //!!! obviously, factor out all repetitive guff
+    
     Vamp::Plugin *
     load(std::string key, float inputSampleRate, int adapterFlags) {
 
+        if (!m_process) {
+            throw std::runtime_error("Piper server failed to start");
+        }
+
         Vamp::HostExt::LoadRequest request;
         request.pluginKey = key;
         request.inputSampleRate = inputSampleRate;
@@ -46,6 +78,11 @@
         VampnProto::buildRpcRequest_Load(builder, request);
         ReqId id = getId();
         builder.getId().setNumber(id);
+
+        auto arr = messageToFlatArray(message);
+        m_process->write(arr.asChars().begin(), arr.asChars().size());
+
+        ///.... read...
     };     
     
     virtual
@@ -53,6 +90,10 @@
     configure(PiperStubPlugin *plugin,
               Vamp::HostExt::PluginConfiguration config) {
 
+        if (!m_process) {
+            throw std::runtime_error("Piper server failed to start");
+        }
+
         Vamp::HostExt::ConfigurationRequest request;
         request.plugin = plugin;
         request.configuration = config;
@@ -78,6 +119,7 @@
     finish(PiperStubPlugin *plugin) = 0;
 
 private:
+    QProcess *m_process;
     AssignedPluginHandleMapper m_mapper;
     int getId() {
         //!!! todo: mutex