c@5: c@5: #include "VampJson.h" c@6: #include "VampnProto.h" c@5: c@5: #include c@5: #include c@5: #include c@5: c@40: #include "bits/PreservingPluginHandleMapper.h" c@40: c@5: using namespace std; c@5: using namespace json11; c@5: using namespace vampipe; c@5: c@5: // Accepting JSON objects with two fields, "type" and "payload". The c@5: // "type" string corresponds to the JSON schema filename c@5: // (e.g. "outputdescriptor") and the "payload" is the JSON object c@5: // encoded with that schema. c@5: c@5: Json c@5: json_input(string input) c@5: { c@5: string err; c@5: Json j = Json::parse(input, err); c@5: if (err != "") { c@5: throw VampJson::Failure("invalid json: " + err); c@5: } c@5: if (!j.is_object()) { c@5: throw VampJson::Failure("object expected at top level"); c@5: } c@5: if (!j["type"].is_string()) { c@5: throw VampJson::Failure("string expected for type field"); c@5: } c@5: if (!j["payload"].is_object()) { c@5: throw VampJson::Failure("object expected for payload field"); c@5: } c@5: return j; c@5: } c@5: c@5: void c@5: handle_input(::capnp::MallocMessageBuilder &message, string input) c@5: { c@5: string err; c@5: c@5: Json j = json_input(input); c@5: string type = j["type"].string_value(); c@5: Json payload = j["payload"]; c@5: c@23: if (type == "configurationrequest") { c@20: auto req = message.initRoot(); c@20: PreservingPluginHandleMapper mapper; c@20: VampnProto::buildConfigurationRequest c@20: (req, VampJson::toConfigurationRequest(payload, mapper), mapper); c@5: c@5: } else if (type == "configurationresponse") { c@21: auto resp = message.initRoot(); c@21: VampnProto::buildConfigurationResponse c@21: (resp, VampJson::toConfigurationResponse(payload)); c@5: c@5: } else if (type == "feature") { c@5: auto f = message.initRoot(); c@6: VampnProto::buildFeature c@5: (f, VampJson::toFeature(payload)); c@5: c@5: } else if (type == "featureset") { c@5: auto fs = message.initRoot(); c@6: VampnProto::buildFeatureSet c@5: (fs, VampJson::toFeatureSet(payload)); c@5: c@5: } else if (type == "loadrequest") { c@5: auto req = message.initRoot(); c@6: VampnProto::buildLoadRequest c@5: (req, VampJson::toLoadRequest(payload)); c@5: c@5: } else if (type == "loadresponse") { c@21: auto resp = message.initRoot(); c@21: PreservingPluginHandleMapper mapper; c@21: VampnProto::buildLoadResponse c@21: (resp, VampJson::toLoadResponse(payload, mapper), mapper); c@5: c@5: } else if (type == "outputdescriptor") { c@5: auto od = message.initRoot(); c@6: VampnProto::buildOutputDescriptor c@5: (od, VampJson::toOutputDescriptor(payload)); c@5: c@5: } else if (type == "parameterdescriptor") { c@5: auto pd = message.initRoot(); c@6: VampnProto::buildParameterDescriptor c@5: (pd, VampJson::toParameterDescriptor(payload)); c@5: c@5: } else if (type == "pluginconfiguration") { c@5: auto pc = message.initRoot(); c@5: auto config = VampJson::toPluginConfiguration(payload); c@6: VampnProto::buildPluginConfiguration(pc, config); c@5: c@5: } else if (type == "pluginstaticdata") { c@5: auto pc = message.initRoot(); c@5: auto sd = VampJson::toPluginStaticData(payload); c@6: VampnProto::buildPluginStaticData(pc, sd); c@5: c@22: } else if (type == "processrequest") { c@22: auto p = message.initRoot(); c@22: PreservingPluginHandleMapper mapper; c@22: VampnProto::buildProcessRequest c@22: (p, VampJson::toProcessRequest(payload, mapper), mapper); c@5: c@5: } else if (type == "realtime") { c@5: auto b = message.initRoot(); c@6: VampnProto::buildRealTime c@5: (b, VampJson::toRealTime(payload)); c@5: c@5: } else { c@5: throw VampJson::Failure("unknown or unsupported JSON schema type " + c@5: type); c@5: } c@5: } c@5: c@5: int main(int, char **) c@5: { c@5: string input; c@5: c@5: while (getline(cin, input)) { c@5: try { c@5: ::capnp::MallocMessageBuilder message; c@5: handle_input(message, input); c@5: writePackedMessageToFd(1, message); // stdout c@5: return 0; c@5: } catch (const VampJson::Failure &e) { c@5: cerr << "Failed to convert JSON to Cap'n Proto message: " c@5: << e.what() << endl; c@5: return 1; c@5: } c@5: } c@5: } c@5: c@5: