diff utilities/vampipe-server.cpp @ 32:2d97883d20df

Wire up a couple of server actions
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 24 May 2016 17:17:03 +0100
parents b376ab0ce003
children 0b48b10140bb
line wrap: on
line diff
--- a/utilities/vampipe-server.cpp	Tue May 24 16:30:01 2016 +0100
+++ b/utilities/vampipe-server.cpp	Tue May 24 17:17:03 2016 +0100
@@ -7,8 +7,13 @@
 #include <sstream>
 #include <stdexcept>
 
+#include <map>
+#include <set>
+
 using namespace std;
 using namespace vampipe;
+using namespace Vamp;
+using namespace Vamp::HostExt;
 
 void usage()
 {
@@ -22,6 +27,50 @@
     exit(2);
 }
 
+class Mapper : public PluginHandleMapper
+{
+public:
+    Mapper() : m_nextHandle(1) { }
+
+    void addPlugin(Plugin *p) {
+	if (m_rplugins.find(p) == m_rplugins.end()) {
+	    int32_t h = m_nextHandle++;
+	    m_plugins[h] = p;
+	    m_rplugins[p] = h;
+	}
+    }
+    
+    int32_t pluginToHandle(Plugin *p) {
+	if (m_rplugins.find(p) == m_rplugins.end()) {
+	    throw NotFound();
+	}
+	return m_rplugins[p];
+    }
+    
+    Plugin *handleToPlugin(int32_t h) {
+	if (m_plugins.find(h) == m_plugins.end()) {
+	    throw NotFound();
+	}
+	return m_plugins[h];
+    }
+
+    bool isInitialised(int32_t h) {
+	return m_initialisedPlugins.find(h) != m_initialisedPlugins.end();
+    }
+
+    void markInitialised(int32_t h) {
+	m_initialisedPlugins.insert(h);
+    }
+    
+private:
+    int32_t m_nextHandle; // NB plugin handle type must fit in JSON number
+    map<uint32_t, Plugin *> m_plugins;
+    map<Plugin *, uint32_t> m_rplugins;
+    set<uint32_t> m_initialisedPlugins;
+};
+
+static Mapper mapper;
+
 RequestOrResponse
 readRequestCapnp()
 {
@@ -42,16 +91,14 @@
 	VampnProto::readVampRequest_Load(rr.loadRequest, reader);
 	break;
     case RRType::Configure:
-	VampnProto::readVampRequest_Configure(rr.configurationRequest, reader,
-					      rr.mapper);
+	VampnProto::readVampRequest_Configure(rr.configurationRequest,
+					      reader, mapper);
 	break;
     case RRType::Process:
-	VampnProto::readVampRequest_Process(rr.processRequest, reader,
-					    rr.mapper);
+	VampnProto::readVampRequest_Process(rr.processRequest, reader, mapper);
 	break;
     case RRType::Finish:
-	VampnProto::readVampRequest_Finish(rr.finishPlugin, reader,
-					   rr.mapper);
+	VampnProto::readVampRequest_Finish(rr.finishPlugin, reader, mapper);
 	break;
     case RRType::NotValid:
 	break;
@@ -72,7 +119,7 @@
 	VampnProto::buildVampResponse_List(builder, "", rr.listResponse);
 	break;
     case RRType::Load:
-	VampnProto::buildVampResponse_Load(builder, rr.loadResponse, rr.mapper);
+	VampnProto::buildVampResponse_Load(builder, rr.loadResponse, mapper);
 	break;
     case RRType::Configure:
 	VampnProto::buildVampResponse_Configure(builder, rr.configurationResponse);
@@ -95,7 +142,30 @@
 {
     RequestOrResponse response;
     response.direction = RequestOrResponse::Response;
-    //!!! DO THE WORK!
+    response.type = request.type;
+
+    auto loader = PluginLoader::getInstance();
+
+    switch (request.type) {
+
+    case RRType::List:
+	response.listResponse = loader->listPluginData();
+	response.success = true;
+	break;
+
+    case RRType::Load:
+	response.loadResponse = loader->loadPlugin(request.loadRequest);
+	if (response.loadResponse.plugin != nullptr) {
+	    mapper.addPlugin(response.loadResponse.plugin);
+	    response.success = true;
+	}
+	break;
+	
+    default:
+	//!!!
+	;
+    }
+    
     return response;
 }