changeset 25:5b9690d18241

Pull up type determination into VampJson
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 24 May 2016 10:43:34 +0100
parents 533ca5ca3404
children 13393bdfc7ef
files bits/RequestResponseType.h capnproto/VampnProto.h json/VampJson.h utilities/vampipe-convert.cpp
diffstat 4 files changed, 100 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bits/RequestResponseType.h	Tue May 24 10:43:34 2016 +0100
@@ -0,0 +1,47 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    Vampipe
+
+    Centre for Digital Music, Queen Mary, University of London.
+    Copyright 2006-2016 Chris Cannam and QMUL.
+  
+    Permission is hereby granted, free of charge, to any person
+    obtaining a copy of this software and associated documentation
+    files (the "Software"), to deal in the Software without
+    restriction, including without limitation the rights to use, copy,
+    modify, merge, publish, distribute, sublicense, and/or sell copies
+    of the Software, and to permit persons to whom the Software is
+    furnished to do so, subject to the following conditions:
+
+    The above copyright notice and this permission notice shall be
+    included in all copies or substantial portions of the Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
+    ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
+    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+    Except as contained in this notice, the names of the Centre for
+    Digital Music; Queen Mary, University of London; and Chris Cannam
+    shall not be used in advertising or otherwise to promote the sale,
+    use or other dealings in this Software without prior written
+    authorization.
+*/
+
+#ifndef VAMPIPE_REQUEST_RESPONSE_TYPE_H
+#define VAMPIPE_REQUEST_RESPONSE_TYPE_H
+
+namespace vampipe {
+
+enum class RRType {
+    List, Load, Configure, Process, Finish, NotValid
+};
+
+}
+
+#endif
+
--- a/capnproto/VampnProto.h	Mon May 23 16:09:25 2016 +0100
+++ b/capnproto/VampnProto.h	Tue May 24 10:43:34 2016 +0100
@@ -653,6 +653,13 @@
     }
 
     static void
+    readVampRequest_List(const VampRequest::Reader &r) {
+        if (r.getRequest().which() != VampRequest::Request::Which::LIST) {
+            throw std::runtime_error("not a list request");
+        }
+    }
+    
+    static void
     buildVampResponse_List(VampResponse::Builder &b,
                            std::string errorText,
                            const std::vector<Vamp::HostExt::PluginStaticData> &d) {
--- a/json/VampJson.h	Mon May 23 16:09:25 2016 +0100
+++ b/json/VampJson.h	Tue May 24 10:43:34 2016 +0100
@@ -47,6 +47,7 @@
 #include <vamp-hostsdk/PluginLoader.h>
 
 #include "bits/PluginHandleMapper.h"
+#include "bits/RequestResponseType.h"
 
 namespace vampipe {
 
@@ -992,6 +993,26 @@
     }
 
 public:
+    static RRType
+    getRequestResponseType(json11::Json j) {
+
+        if (!j["type"].is_string()) {
+            throw Failure("string expected for type");
+        }
+        
+        std::string type = j["type"].string_value();
+
+	if (type == "list") return RRType::List;
+	else if (type == "load") return RRType::Load;
+	else if (type == "configure") return RRType::Configure;
+	else if (type == "process") return RRType::Process;
+	else if (type == "finish") return RRType::Finish;
+	else {
+	    throw Failure("unknown or unexpected request/response type \"" +
+                          type + "\"");
+	}
+    }
+    
     static void
     toVampRequest_List(json11::Json j) {
         
--- a/utilities/vampipe-convert.cpp	Mon May 23 16:09:25 2016 +0100
+++ b/utilities/vampipe-convert.cpp	Tue May 24 10:43:34 2016 +0100
@@ -62,31 +62,15 @@
     enum Direction {
 	Request, Response
     };
-    enum Type {
-	List, Load, Configure, Process, Finish, Eof
-    };
-
-    static Type typeForName(string name) {
-	if (name == "list") return List;
-	else if (name == "load") return Load;
-	else if (name == "configure") return Configure;
-	else if (name == "process") return Process;
-	else if (name == "finish") return Finish;
-	else if (name == "eof") return Eof;
-	else {
-	    throw runtime_error("unknown or unexpected request/response type \"" +
-				name + "\"");
-	}
-    }
     
     RequestOrResponse() : // nothing by default
 	direction(Request),
-	type(Eof),
+	type(RRType::NotValid),
 	success(false),
 	finishPlugin(0) { }
 
     Direction direction;
-    Type type;
+    RRType type;
     bool success;
     string errorText;
 
@@ -151,24 +135,23 @@
 
     string input;
     if (!getline(cin, input)) {
-	rr.type = RequestOrResponse::Eof;
+	rr.type = RRType::NotValid;
 	return rr;
     }
     
     Json j = convertRequestJson(input);
-    string type = j["type"].string_value();
-    rr.type = RequestOrResponse::typeForName(type);
+    rr.type = VampJson::getRequestResponseType(j);
 
-    if (rr.type == RequestOrResponse::Load) {
+    if (rr.type == RRType::Load) {
 	rr.loadRequest = VampJson::toVampRequest_Load(j);
 
-    } else if (rr.type == RequestOrResponse::Configure) {
+    } else if (rr.type == RRType::Configure) {
 	rr.configurationRequest = VampJson::toVampRequest_Configure(j, rr.mapper);
 
-    } else if (rr.type == RequestOrResponse::Process) {
+    } else if (rr.type == RRType::Process) {
 	rr.processRequest = VampJson::toVampRequest_Process(j, rr.mapper);
 
-    } else if (rr.type == RequestOrResponse::Finish) {
+    } else if (rr.type == RRType::Finish) {
 	rr.finishPlugin = VampJson::toVampRequest_Finish(j, rr.mapper);
     }
 
@@ -180,19 +163,19 @@
 {
     Json j;
 
-    if (rr.type == RequestOrResponse::List) {
+    if (rr.type == RRType::List) {
 	j = VampJson::fromVampRequest_List();
 	
-    } else if (rr.type == RequestOrResponse::Load) {
+    } else if (rr.type == RRType::Load) {
 	j = VampJson::fromVampRequest_Load(rr.loadRequest);
 	
-    } else if (rr.type == RequestOrResponse::Configure) {
+    } else if (rr.type == RRType::Configure) {
 	j = VampJson::fromVampRequest_Configure(rr.configurationRequest, rr.mapper);
 	
-    } else if (rr.type == RequestOrResponse::Process) {
+    } else if (rr.type == RRType::Process) {
 	j = VampJson::fromVampRequest_Process(rr.processRequest, rr.mapper);
 	
-    } else if (rr.type == RequestOrResponse::Finish) {
+    } else if (rr.type == RRType::Finish) {
 	j = VampJson::fromVampRequest_Finish(rr.finishPlugin, rr.mapper);
     }
 
@@ -207,27 +190,26 @@
 
     string input;
     if (!getline(cin, input)) {
-	rr.type = RequestOrResponse::Eof;
+	rr.type = RRType::NotValid;
 	return rr;
     }
 
     Json j = convertResponseJson(input);
-    string type = j["type"].string_value();
-    rr.type = RequestOrResponse::typeForName(type);
+    rr.type = VampJson::getRequestResponseType(j);
 
-    if (rr.type == RequestOrResponse::List) {
+    if (rr.type == RRType::List) {
 	rr.listResponse = VampJson::toVampResponse_List(j);
 	
-    } else if (rr.type == RequestOrResponse::Load) {
+    } else if (rr.type == RRType::Load) {
 	rr.loadResponse = VampJson::toVampResponse_Load(j, rr.mapper);
 
-    } else if (rr.type == RequestOrResponse::Configure) {
+    } else if (rr.type == RRType::Configure) {
 	rr.configurationResponse = VampJson::toVampResponse_Configure(j);
 
-    } else if (rr.type == RequestOrResponse::Process) {
+    } else if (rr.type == RRType::Process) {
 	rr.processResponse = VampJson::toVampResponse_Process(j);
 
-    } else if (rr.type == RequestOrResponse::Finish) {
+    } else if (rr.type == RRType::Finish) {
 	rr.finishResponse = VampJson::toVampResponse_Finish(j);
     }
 
@@ -239,13 +221,15 @@
 {
     Json j;
 
-    if (rr.type == RequestOrResponse::List) {
+    if (rr.type == RRType::List) {
 	j = VampJson::fromVampResponse_List("", rr.listResponse);
 	
-    } else if (rr.type == RequestOrResponse::Load) {
+    } else if (rr.type == RRType::Load) {
 	j = VampJson::fromVampResponse_Load(rr.loadResponse, rr.mapper);
     }
 
+    //!!!
+
     cout << j.dump() << endl;
 }
 
@@ -322,7 +306,7 @@
 	try {
 
 	    RequestOrResponse rr = readInput(informat, direction);
-	    if (rr.type == RequestOrResponse::Eof) break;
+	    if (rr.type == RRType::NotValid) break;
 	    writeOutput(outformat, rr);
 	    
 	} catch (std::exception &e) {