diff utilities/vampipe-server.cpp @ 31:b376ab0ce003

Begin server
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 24 May 2016 16:30:01 +0100
parents
children 2d97883d20df
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utilities/vampipe-server.cpp	Tue May 24 16:30:01 2016 +0100
@@ -0,0 +1,131 @@
+
+#include "VampnProto.h"
+
+#include "bits/RequestOrResponse.h"
+
+#include <iostream>
+#include <sstream>
+#include <stdexcept>
+
+using namespace std;
+using namespace vampipe;
+
+void usage()
+{
+    string myname = "vampipe-server";
+    cerr << "\n" << myname <<
+	": Load and run Vamp plugins in response to messages from stdin\n\n"
+	"    Usage: " << myname << "\n\n"
+	"Expects Vamp request messages in Cap'n Proto packed format on stdin,\n"
+	"and writes Vamp response messages in the same format to stdout.\n\n";
+
+    exit(2);
+}
+
+RequestOrResponse
+readRequestCapnp()
+{
+    RequestOrResponse rr;
+    rr.direction = RequestOrResponse::Request;
+
+    ::capnp::PackedFdMessageReader message(0); // stdin
+    VampRequest::Reader reader = message.getRoot<VampRequest>();
+    
+    rr.type = VampnProto::getRequestResponseType(reader);
+
+    switch (rr.type) {
+
+    case RRType::List:
+	VampnProto::readVampRequest_List(reader); // type check only
+	break;
+    case RRType::Load:
+	VampnProto::readVampRequest_Load(rr.loadRequest, reader);
+	break;
+    case RRType::Configure:
+	VampnProto::readVampRequest_Configure(rr.configurationRequest, reader,
+					      rr.mapper);
+	break;
+    case RRType::Process:
+	VampnProto::readVampRequest_Process(rr.processRequest, reader,
+					    rr.mapper);
+	break;
+    case RRType::Finish:
+	VampnProto::readVampRequest_Finish(rr.finishPlugin, reader,
+					   rr.mapper);
+	break;
+    case RRType::NotValid:
+	break;
+    }
+
+    return rr;
+}
+
+void
+writeResponseCapnp(RequestOrResponse &rr)
+{
+    ::capnp::MallocMessageBuilder message;
+    VampResponse::Builder builder = message.initRoot<VampResponse>();
+
+    switch (rr.type) {
+
+    case RRType::List:
+	VampnProto::buildVampResponse_List(builder, "", rr.listResponse);
+	break;
+    case RRType::Load:
+	VampnProto::buildVampResponse_Load(builder, rr.loadResponse, rr.mapper);
+	break;
+    case RRType::Configure:
+	VampnProto::buildVampResponse_Configure(builder, rr.configurationResponse);
+	break;
+    case RRType::Process:
+	VampnProto::buildVampResponse_Process(builder, rr.processResponse);
+	break;
+    case RRType::Finish:
+	VampnProto::buildVampResponse_Finish(builder, rr.finishResponse);
+	break;
+    case RRType::NotValid:
+	break;
+    }
+
+    writePackedMessageToFd(1, message);
+}
+
+RequestOrResponse
+processRequest(const RequestOrResponse &request)
+{
+    RequestOrResponse response;
+    response.direction = RequestOrResponse::Response;
+    //!!! DO THE WORK!
+    return response;
+}
+
+int main(int argc, char **argv)
+{
+    if (argc != 1) {
+	usage();
+    }
+
+    while (true) {
+
+	try {
+
+	    RequestOrResponse request = readRequestCapnp();
+
+	    // NotValid without an exception indicates EOF:
+
+	    //!!! not yet it doesn't -- have to figure out how to
+	    //!!! handle this with capnp
+	    if (request.type == RRType::NotValid) break;
+
+	    RequestOrResponse response = processRequest(request);
+	    
+	    writeResponseCapnp(response);
+	    
+	} catch (std::exception &e) {
+	    cerr << "Error: " << e.what() << endl;
+	    exit(1);
+	}
+    }
+
+    exit(0);
+}