annotate utilities/json-to-capnp.cpp @ 16:913fc1d3710a

Create process request in JSON version, plus a fix
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 18 May 2016 14:38:27 +0100
parents d8358afe3f2c
children f379b0e9a8e1
rev   line source
c@5 1
c@5 2 #include "VampJson.h"
c@6 3 #include "VampnProto.h"
c@5 4
c@5 5 #include <iostream>
c@5 6 #include <sstream>
c@5 7 #include <stdexcept>
c@5 8
c@5 9 using namespace std;
c@5 10 using namespace json11;
c@5 11 using namespace vampipe;
c@5 12
c@5 13 // Accepting JSON objects with two fields, "type" and "payload". The
c@5 14 // "type" string corresponds to the JSON schema filename
c@5 15 // (e.g. "outputdescriptor") and the "payload" is the JSON object
c@5 16 // encoded with that schema.
c@5 17
c@5 18 Json
c@5 19 json_input(string input)
c@5 20 {
c@5 21 string err;
c@5 22 Json j = Json::parse(input, err);
c@5 23 if (err != "") {
c@5 24 throw VampJson::Failure("invalid json: " + err);
c@5 25 }
c@5 26 if (!j.is_object()) {
c@5 27 throw VampJson::Failure("object expected at top level");
c@5 28 }
c@5 29 if (!j["type"].is_string()) {
c@5 30 throw VampJson::Failure("string expected for type field");
c@5 31 }
c@5 32 if (!j["payload"].is_object()) {
c@5 33 throw VampJson::Failure("object expected for payload field");
c@5 34 }
c@5 35 return j;
c@5 36 }
c@5 37
c@5 38 void
c@5 39 handle_input(::capnp::MallocMessageBuilder &message, string input)
c@5 40 {
c@5 41 string err;
c@5 42
c@5 43 Json j = json_input(input);
c@5 44 string type = j["type"].string_value();
c@5 45 Json payload = j["payload"];
c@5 46
c@5 47 if (type == "basic") {
c@5 48 throw VampJson::Failure("can't convert Basic block on its own");
c@5 49
c@5 50 } else if (type == "configurationrequest") {
c@5 51 throw VampJson::Failure("not implemented yet"); ///!!!
c@5 52
c@5 53 } else if (type == "configurationresponse") {
c@5 54 throw VampJson::Failure("not implemented yet"); ///!!!
c@5 55
c@5 56 } else if (type == "feature") {
c@5 57 auto f = message.initRoot<Feature>();
c@6 58 VampnProto::buildFeature
c@5 59 (f, VampJson::toFeature(payload));
c@5 60
c@5 61 } else if (type == "featureset") {
c@5 62 auto fs = message.initRoot<FeatureSet>();
c@6 63 VampnProto::buildFeatureSet
c@5 64 (fs, VampJson::toFeatureSet(payload));
c@5 65
c@5 66 } else if (type == "loadrequest") {
c@5 67 auto req = message.initRoot<LoadRequest>();
c@6 68 VampnProto::buildLoadRequest
c@5 69 (req, VampJson::toLoadRequest(payload));
c@5 70
c@5 71 } else if (type == "loadresponse") {
c@5 72 //!!! response types & configure call for plugin handles, but
c@5 73 //!!! we don't have any context in which a plugin handle can
c@5 74 //!!! be persistent here
c@5 75 throw VampJson::Failure("not implemented yet"); ///!!!
c@5 76
c@5 77 } else if (type == "outputdescriptor") {
c@5 78 auto od = message.initRoot<OutputDescriptor>();
c@6 79 VampnProto::buildOutputDescriptor
c@5 80 (od, VampJson::toOutputDescriptor(payload));
c@5 81
c@5 82 } else if (type == "parameterdescriptor") {
c@5 83 auto pd = message.initRoot<ParameterDescriptor>();
c@6 84 VampnProto::buildParameterDescriptor
c@5 85 (pd, VampJson::toParameterDescriptor(payload));
c@5 86
c@5 87 } else if (type == "pluginconfiguration") {
c@5 88 auto pc = message.initRoot<PluginConfiguration>();
c@5 89 auto config = VampJson::toPluginConfiguration(payload);
c@6 90 VampnProto::buildPluginConfiguration(pc, config);
c@5 91
c@5 92 } else if (type == "pluginstaticdata") {
c@5 93 auto pc = message.initRoot<PluginStaticData>();
c@5 94 auto sd = VampJson::toPluginStaticData(payload);
c@6 95 VampnProto::buildPluginStaticData(pc, sd);
c@5 96
c@5 97 } else if (type == "processblock") {
c@5 98 throw VampJson::Failure("not implemented yet"); ///!!!
c@5 99
c@5 100 } else if (type == "realtime") {
c@5 101 auto b = message.initRoot<RealTime>();
c@6 102 VampnProto::buildRealTime
c@5 103 (b, VampJson::toRealTime(payload));
c@5 104
c@5 105 } else if (type == "valueextents") {
c@5 106 throw VampJson::Failure("no ValueExtents struct in Cap'n Proto mapping");
c@5 107
c@5 108 } else {
c@5 109 throw VampJson::Failure("unknown or unsupported JSON schema type " +
c@5 110 type);
c@5 111 }
c@5 112 }
c@5 113
c@5 114 int main(int, char **)
c@5 115 {
c@5 116 string input;
c@5 117
c@5 118 while (getline(cin, input)) {
c@5 119 try {
c@5 120 ::capnp::MallocMessageBuilder message;
c@5 121 handle_input(message, input);
c@5 122 writePackedMessageToFd(1, message); // stdout
c@5 123 return 0;
c@5 124 } catch (const VampJson::Failure &e) {
c@5 125 cerr << "Failed to convert JSON to Cap'n Proto message: "
c@5 126 << e.what() << endl;
c@5 127 return 1;
c@5 128 }
c@5 129 }
c@5 130 }
c@5 131
c@5 132