comparison json/VampJson.h @ 69:91fda9470ee2

Merge from branch jsonrpc
author Chris Cannam <c.cannam@qmul.ac.uk>
date Thu, 06 Oct 2016 14:33:12 +0100
parents a5ba837bca28
children 16acd7d24b1a
comparison
equal deleted inserted replaced
66:6f160dee1192 69:91fda9470ee2
534 534
535 static json11::Json 535 static json11::Json
536 fromPluginStaticData(const Vamp::HostExt::PluginStaticData &d) { 536 fromPluginStaticData(const Vamp::HostExt::PluginStaticData &d) {
537 537
538 json11::Json::object jo; 538 json11::Json::object jo;
539 jo["pluginKey"] = d.pluginKey; 539 jo["key"] = d.pluginKey;
540 jo["basic"] = fromBasicDescriptor(d.basic); 540 jo["basic"] = fromBasicDescriptor(d.basic);
541 jo["maker"] = d.maker; 541 jo["maker"] = d.maker;
542 jo["copyright"] = d.copyright; 542 jo["copyright"] = d.copyright;
543 jo["pluginVersion"] = d.pluginVersion; 543 jo["version"] = d.pluginVersion;
544 544
545 json11::Json::array cat; 545 json11::Json::array cat;
546 for (const std::string &c: d.category) cat.push_back(c); 546 for (const std::string &c: d.category) cat.push_back(c);
547 jo["category"] = cat; 547 jo["category"] = cat;
548 548
571 571
572 static Vamp::HostExt::PluginStaticData 572 static Vamp::HostExt::PluginStaticData
573 toPluginStaticData(json11::Json j, std::string &err) { 573 toPluginStaticData(json11::Json j, std::string &err) {
574 574
575 if (!j.has_shape({ 575 if (!j.has_shape({
576 { "pluginKey", json11::Json::STRING }, 576 { "key", json11::Json::STRING },
577 { "pluginVersion", json11::Json::NUMBER }, 577 { "version", json11::Json::NUMBER },
578 { "minChannelCount", json11::Json::NUMBER }, 578 { "minChannelCount", json11::Json::NUMBER },
579 { "maxChannelCount", json11::Json::NUMBER }, 579 { "maxChannelCount", json11::Json::NUMBER },
580 { "inputDomain", json11::Json::STRING }}, err)) { 580 { "inputDomain", json11::Json::STRING }}, err)) {
581 581
582 err = "malformed plugin static data: " + err; 582 err = "malformed plugin static data: " + err;
621 621
622 } else { 622 } else {
623 623
624 Vamp::HostExt::PluginStaticData psd; 624 Vamp::HostExt::PluginStaticData psd;
625 625
626 psd.pluginKey = j["pluginKey"].string_value(); 626 psd.pluginKey = j["key"].string_value();
627 627
628 toBasicDescriptor(j["basic"], psd.basic, err); 628 toBasicDescriptor(j["basic"], psd.basic, err);
629 if (failed(err)) return {}; 629 if (failed(err)) return {};
630 630
631 psd.maker = j["maker"].string_value(); 631 psd.maker = j["maker"].string_value();
632 psd.copyright = j["copyright"].string_value(); 632 psd.copyright = j["copyright"].string_value();
633 psd.pluginVersion = j["pluginVersion"].int_value(); 633 psd.pluginVersion = j["version"].int_value();
634 634
635 for (const auto &c : j["category"].array_items()) { 635 for (const auto &c : j["category"].array_items()) {
636 if (!c.is_string()) { 636 if (!c.is_string()) {
637 err = "strings expected in category array"; 637 err = "strings expected in category array";
638 return {}; 638 return {};
800 800
801 static json11::Json 801 static json11::Json
802 fromLoadRequest(const Vamp::HostExt::LoadRequest &req) { 802 fromLoadRequest(const Vamp::HostExt::LoadRequest &req) {
803 803
804 json11::Json::object jo; 804 json11::Json::object jo;
805 jo["pluginKey"] = req.pluginKey; 805 jo["key"] = req.pluginKey;
806 jo["inputSampleRate"] = req.inputSampleRate; 806 jo["inputSampleRate"] = req.inputSampleRate;
807 jo["adapterFlags"] = fromAdapterFlags(req.adapterFlags); 807 jo["adapterFlags"] = fromAdapterFlags(req.adapterFlags);
808 return json11::Json(jo); 808 return json11::Json(jo);
809 } 809 }
810 810
811 static Vamp::HostExt::LoadRequest 811 static Vamp::HostExt::LoadRequest
812 toLoadRequest(json11::Json j, std::string &err) { 812 toLoadRequest(json11::Json j, std::string &err) {
813 813
814 if (!j.has_shape({ 814 if (!j.has_shape({
815 { "pluginKey", json11::Json::STRING }, 815 { "key", json11::Json::STRING },
816 { "inputSampleRate", json11::Json::NUMBER } }, err)) { 816 { "inputSampleRate", json11::Json::NUMBER } }, err)) {
817 err = "malformed load request: " + err; 817 err = "malformed load request: " + err;
818 return {}; 818 return {};
819 } 819 }
820 820
821 Vamp::HostExt::LoadRequest req; 821 Vamp::HostExt::LoadRequest req;
822 req.pluginKey = j["pluginKey"].string_value(); 822 req.pluginKey = j["key"].string_value();
823 req.inputSampleRate = j["inputSampleRate"].number_value(); 823 req.inputSampleRate = j["inputSampleRate"].number_value();
824 if (!j["adapterFlags"].is_null()) { 824 if (!j["adapterFlags"].is_null()) {
825 req.adapterFlags = toAdapterFlags(j["adapterFlags"], err); 825 req.adapterFlags = toAdapterFlags(j["adapterFlags"], err);
826 if (failed(err)) return {}; 826 if (failed(err)) return {};
827 } 827 }
831 static json11::Json 831 static json11::Json
832 fromLoadResponse(const Vamp::HostExt::LoadResponse &resp, 832 fromLoadResponse(const Vamp::HostExt::LoadResponse &resp,
833 const PluginHandleMapper &pmapper) { 833 const PluginHandleMapper &pmapper) {
834 834
835 json11::Json::object jo; 835 json11::Json::object jo;
836 jo["pluginHandle"] = double(pmapper.pluginToHandle(resp.plugin)); 836 jo["handle"] = double(pmapper.pluginToHandle(resp.plugin));
837 jo["staticData"] = fromPluginStaticData(resp.staticData); 837 jo["staticData"] = fromPluginStaticData(resp.staticData);
838 jo["defaultConfiguration"] = 838 jo["defaultConfiguration"] =
839 fromPluginConfiguration(resp.defaultConfiguration); 839 fromPluginConfiguration(resp.defaultConfiguration);
840 return json11::Json(jo); 840 return json11::Json(jo);
841 } 841 }
843 static Vamp::HostExt::LoadResponse 843 static Vamp::HostExt::LoadResponse
844 toLoadResponse(json11::Json j, 844 toLoadResponse(json11::Json j,
845 const PluginHandleMapper &pmapper, std::string &err) { 845 const PluginHandleMapper &pmapper, std::string &err) {
846 846
847 if (!j.has_shape({ 847 if (!j.has_shape({
848 { "pluginHandle", json11::Json::NUMBER }, 848 { "handle", json11::Json::NUMBER },
849 { "staticData", json11::Json::OBJECT }, 849 { "staticData", json11::Json::OBJECT },
850 { "defaultConfiguration", json11::Json::OBJECT } }, err)) { 850 { "defaultConfiguration", json11::Json::OBJECT } }, err)) {
851 err = "malformed load response: " + err; 851 err = "malformed load response: " + err;
852 return {}; 852 return {};
853 } 853 }
854 854
855 Vamp::HostExt::LoadResponse resp; 855 Vamp::HostExt::LoadResponse resp;
856 resp.plugin = pmapper.handleToPlugin(j["pluginHandle"].int_value()); 856 resp.plugin = pmapper.handleToPlugin(j["handle"].int_value());
857 resp.staticData = toPluginStaticData(j["staticData"], err); 857 resp.staticData = toPluginStaticData(j["staticData"], err);
858 if (failed(err)) return {}; 858 if (failed(err)) return {};
859 resp.defaultConfiguration = toPluginConfiguration(j["defaultConfiguration"], 859 resp.defaultConfiguration = toPluginConfiguration(j["defaultConfiguration"],
860 err); 860 err);
861 if (failed(err)) return {}; 861 if (failed(err)) return {};
866 fromConfigurationRequest(const Vamp::HostExt::ConfigurationRequest &cr, 866 fromConfigurationRequest(const Vamp::HostExt::ConfigurationRequest &cr,
867 const PluginHandleMapper &pmapper) { 867 const PluginHandleMapper &pmapper) {
868 868
869 json11::Json::object jo; 869 json11::Json::object jo;
870 870
871 jo["pluginHandle"] = pmapper.pluginToHandle(cr.plugin); 871 jo["handle"] = pmapper.pluginToHandle(cr.plugin);
872 jo["configuration"] = fromPluginConfiguration(cr.configuration); 872 jo["configuration"] = fromPluginConfiguration(cr.configuration);
873 873
874 return json11::Json(jo); 874 return json11::Json(jo);
875 } 875 }
876 876
877 static Vamp::HostExt::ConfigurationRequest 877 static Vamp::HostExt::ConfigurationRequest
878 toConfigurationRequest(json11::Json j, 878 toConfigurationRequest(json11::Json j,
879 const PluginHandleMapper &pmapper, std::string &err) { 879 const PluginHandleMapper &pmapper, std::string &err) {
880 880
881 if (!j.has_shape({ 881 if (!j.has_shape({
882 { "pluginHandle", json11::Json::NUMBER }, 882 { "handle", json11::Json::NUMBER },
883 { "configuration", json11::Json::OBJECT } }, err)) { 883 { "configuration", json11::Json::OBJECT } }, err)) {
884 err = "malformed configuration request: " + err; 884 err = "malformed configuration request: " + err;
885 return {}; 885 return {};
886 } 886 }
887 887
888 Vamp::HostExt::ConfigurationRequest cr; 888 Vamp::HostExt::ConfigurationRequest cr;
889 cr.plugin = pmapper.handleToPlugin(j["pluginHandle"].int_value()); 889 cr.plugin = pmapper.handleToPlugin(j["handle"].int_value());
890 cr.configuration = toPluginConfiguration(j["configuration"], err); 890 cr.configuration = toPluginConfiguration(j["configuration"], err);
891 if (failed(err)) return {}; 891 if (failed(err)) return {};
892 return cr; 892 return cr;
893 } 893 }
894 894
896 fromConfigurationResponse(const Vamp::HostExt::ConfigurationResponse &cr, 896 fromConfigurationResponse(const Vamp::HostExt::ConfigurationResponse &cr,
897 const PluginHandleMapper &pmapper) { 897 const PluginHandleMapper &pmapper) {
898 898
899 json11::Json::object jo; 899 json11::Json::object jo;
900 900
901 jo["pluginHandle"] = pmapper.pluginToHandle(cr.plugin); 901 jo["handle"] = pmapper.pluginToHandle(cr.plugin);
902 902
903 json11::Json::array outs; 903 json11::Json::array outs;
904 for (auto &d: cr.outputs) { 904 for (auto &d: cr.outputs) {
905 outs.push_back(fromOutputDescriptor(d)); 905 outs.push_back(fromOutputDescriptor(d));
906 } 906 }
913 toConfigurationResponse(json11::Json j, 913 toConfigurationResponse(json11::Json j,
914 const PluginHandleMapper &pmapper, std::string &err) { 914 const PluginHandleMapper &pmapper, std::string &err) {
915 915
916 Vamp::HostExt::ConfigurationResponse cr; 916 Vamp::HostExt::ConfigurationResponse cr;
917 917
918 cr.plugin = pmapper.handleToPlugin(j["pluginHandle"].int_value()); 918 cr.plugin = pmapper.handleToPlugin(j["handle"].int_value());
919 919
920 if (!j["outputList"].is_array()) { 920 if (!j["outputList"].is_array()) {
921 err = "array expected for output list"; 921 err = "array expected for output list";
922 return {}; 922 return {};
923 } 923 }
934 fromProcessRequest(const Vamp::HostExt::ProcessRequest &r, 934 fromProcessRequest(const Vamp::HostExt::ProcessRequest &r,
935 const PluginHandleMapper &pmapper, 935 const PluginHandleMapper &pmapper,
936 BufferSerialisation serialisation) { 936 BufferSerialisation serialisation) {
937 937
938 json11::Json::object jo; 938 json11::Json::object jo;
939 jo["pluginHandle"] = pmapper.pluginToHandle(r.plugin); 939 jo["handle"] = pmapper.pluginToHandle(r.plugin);
940 940
941 json11::Json::object io; 941 json11::Json::object io;
942 io["timestamp"] = fromRealTime(r.timestamp); 942 io["timestamp"] = fromRealTime(r.timestamp);
943 943
944 json11::Json::array chans; 944 json11::Json::array chans;
961 toProcessRequest(json11::Json j, 961 toProcessRequest(json11::Json j,
962 const PluginHandleMapper &pmapper, 962 const PluginHandleMapper &pmapper,
963 BufferSerialisation &serialisation, std::string &err) { 963 BufferSerialisation &serialisation, std::string &err) {
964 964
965 if (!j.has_shape({ 965 if (!j.has_shape({
966 { "pluginHandle", json11::Json::NUMBER }, 966 { "handle", json11::Json::NUMBER },
967 { "processInput", json11::Json::OBJECT } }, err)) { 967 { "processInput", json11::Json::OBJECT } }, err)) {
968 err = "malformed process request: " + err; 968 err = "malformed process request: " + err;
969 return {}; 969 return {};
970 } 970 }
971 971
977 err = "malformed process request: " + err; 977 err = "malformed process request: " + err;
978 return {}; 978 return {};
979 } 979 }
980 980
981 Vamp::HostExt::ProcessRequest r; 981 Vamp::HostExt::ProcessRequest r;
982 r.plugin = pmapper.handleToPlugin(j["pluginHandle"].int_value()); 982 r.plugin = pmapper.handleToPlugin(j["handle"].int_value());
983 983
984 r.timestamp = toRealTime(input["timestamp"], err); 984 r.timestamp = toRealTime(input["timestamp"], err);
985 if (failed(err)) return {}; 985 if (failed(err)) return {};
986 986
987 for (const auto &a: input["inputBuffers"].array_items()) { 987 for (const auto &a: input["inputBuffers"].array_items()) {
1007 } 1007 }
1008 } 1008 }
1009 1009
1010 return r; 1010 return r;
1011 } 1011 }
1012 1012
1013 static json11::Json 1013 private: // go private briefly for a couple of helper functions
1014 fromVampRequest_List() { 1014
1015 1015 static void
1016 json11::Json::object jo; 1016 checkTypeField(json11::Json j, std::string expected, std::string &err) {
1017 jo["type"] = "list"; 1017 if (!j["method"].is_string()) {
1018 return json11::Json(jo); 1018 err = "string expected for method";
1019 } 1019 return;
1020 1020 }
1021 static json11::Json 1021 if (j["method"].string_value() != expected) {
1022 fromVampResponse_List(const Vamp::HostExt::ListResponse &resp) { 1022 err = "expected value \"" + expected + "\" for type";
1023 1023 return;
1024 json11::Json::object jo; 1024 }
1025 jo["type"] = "list"; 1025 }
1026 jo["success"] = true; 1026
1027 static bool
1028 successful(json11::Json j, std::string &err) {
1029 if (!j["success"].is_bool()) {
1030 err = "bool expected for success";
1031 return false;
1032 }
1033 return j["success"].bool_value();
1034 }
1035
1036 static void
1037 markRPC(json11::Json::object &jo) {
1038 jo["jsonrpc"] = "2.0";
1039 }
1040
1041 public:
1042
1043 static json11::Json
1044 fromRpcRequest_List() {
1045
1046 json11::Json::object jo;
1047 markRPC(jo);
1048
1049 jo["method"] = "list";
1050 return json11::Json(jo);
1051 }
1052
1053 static json11::Json
1054 fromRpcResponse_List(const Vamp::HostExt::ListResponse &resp) {
1055
1056 json11::Json::object jo;
1057 markRPC(jo);
1027 1058
1028 json11::Json::array arr; 1059 json11::Json::array arr;
1029 for (const auto &a: resp.plugins) { 1060 for (const auto &a: resp.plugins) {
1030 arr.push_back(fromPluginStaticData(a)); 1061 arr.push_back(fromPluginStaticData(a));
1031 } 1062 }
1032 json11::Json::object po; 1063 json11::Json::object po;
1033 po["plugins"] = arr; 1064 po["available"] = arr;
1034 1065
1035 jo["content"] = po; 1066 jo["method"] = "list";
1036 return json11::Json(jo); 1067 jo["result"] = po;
1037 } 1068 return json11::Json(jo);
1038 1069 }
1039 static json11::Json 1070
1040 fromVampRequest_Load(const Vamp::HostExt::LoadRequest &req) { 1071 static json11::Json
1041 1072 fromRpcRequest_Load(const Vamp::HostExt::LoadRequest &req) {
1042 json11::Json::object jo; 1073
1043 jo["type"] = "load"; 1074 json11::Json::object jo;
1044 jo["content"] = fromLoadRequest(req); 1075 markRPC(jo);
1076
1077 jo["method"] = "load";
1078 jo["params"] = fromLoadRequest(req);
1045 return json11::Json(jo); 1079 return json11::Json(jo);
1046 } 1080 }
1047 1081
1048 static json11::Json 1082 static json11::Json
1049 fromVampResponse_Load(const Vamp::HostExt::LoadResponse &resp, 1083 fromRpcResponse_Load(const Vamp::HostExt::LoadResponse &resp,
1050 const PluginHandleMapper &pmapper) { 1084 const PluginHandleMapper &pmapper) {
1051 1085
1052 json11::Json::object jo; 1086 if (resp.plugin) {
1053 jo["type"] = "load"; 1087
1054 jo["success"] = (resp.plugin != 0); 1088 json11::Json::object jo;
1055 jo["errorText"] = ""; 1089 markRPC(jo);
1056 jo["content"] = fromLoadResponse(resp, pmapper); 1090
1057 return json11::Json(jo); 1091 jo["method"] = "load";
1058 } 1092 jo["result"] = fromLoadResponse(resp, pmapper);
1059 1093 return json11::Json(jo);
1060 static json11::Json 1094
1061 fromVampRequest_Configure(const Vamp::HostExt::ConfigurationRequest &req, 1095 } else {
1096 return fromError("Failed to load plugin", RRType::Load);
1097 }
1098 }
1099
1100 static json11::Json
1101 fromRpcRequest_Configure(const Vamp::HostExt::ConfigurationRequest &req,
1062 const PluginHandleMapper &pmapper) { 1102 const PluginHandleMapper &pmapper) {
1063 1103
1064 json11::Json::object jo; 1104 json11::Json::object jo;
1065 jo["type"] = "configure"; 1105 markRPC(jo);
1066 jo["content"] = fromConfigurationRequest(req, pmapper); 1106
1107 jo["method"] = "configure";
1108 jo["params"] = fromConfigurationRequest(req, pmapper);
1067 return json11::Json(jo); 1109 return json11::Json(jo);
1068 } 1110 }
1069 1111
1070 static json11::Json 1112 static json11::Json
1071 fromVampResponse_Configure(const Vamp::HostExt::ConfigurationResponse &resp, 1113 fromRpcResponse_Configure(const Vamp::HostExt::ConfigurationResponse &resp,
1072 const PluginHandleMapper &pmapper) { 1114 const PluginHandleMapper &pmapper) {
1073 1115
1074 json11::Json::object jo; 1116 if (!resp.outputs.empty()) {
1075 jo["type"] = "configure"; 1117
1076 jo["success"] = (!resp.outputs.empty()); 1118 json11::Json::object jo;
1077 jo["errorText"] = ""; 1119 markRPC(jo);
1078 jo["content"] = fromConfigurationResponse(resp, pmapper); 1120
1079 return json11::Json(jo); 1121 jo["method"] = "configure";
1080 } 1122 jo["result"] = fromConfigurationResponse(resp, pmapper);
1081 1123 return json11::Json(jo);
1082 static json11::Json 1124
1083 fromVampRequest_Process(const Vamp::HostExt::ProcessRequest &req, 1125 } else {
1126 return fromError("Failed to configure plugin", RRType::Configure);
1127 }
1128 }
1129
1130 static json11::Json
1131 fromRpcRequest_Process(const Vamp::HostExt::ProcessRequest &req,
1084 const PluginHandleMapper &pmapper, 1132 const PluginHandleMapper &pmapper,
1085 BufferSerialisation serialisation) { 1133 BufferSerialisation serialisation) {
1086 1134
1087 json11::Json::object jo; 1135 json11::Json::object jo;
1088 jo["type"] = "process"; 1136 markRPC(jo);
1089 jo["content"] = fromProcessRequest(req, pmapper, serialisation); 1137
1138 jo["method"] = "process";
1139 jo["params"] = fromProcessRequest(req, pmapper, serialisation);
1090 return json11::Json(jo); 1140 return json11::Json(jo);
1091 } 1141 }
1092 1142
1093 static json11::Json 1143 static json11::Json
1094 fromVampResponse_Process(const Vamp::HostExt::ProcessResponse &resp, 1144 fromRpcResponse_Process(const Vamp::HostExt::ProcessResponse &resp,
1095 const PluginHandleMapper &pmapper, 1145 const PluginHandleMapper &pmapper,
1096 BufferSerialisation serialisation) { 1146 BufferSerialisation serialisation) {
1097 1147
1098 json11::Json::object jo; 1148 json11::Json::object jo;
1099 jo["type"] = "process"; 1149 markRPC(jo);
1100 jo["success"] = true; 1150
1101 jo["errorText"] = "";
1102 json11::Json::object po; 1151 json11::Json::object po;
1103 po["pluginHandle"] = pmapper.pluginToHandle(resp.plugin); 1152 po["handle"] = pmapper.pluginToHandle(resp.plugin);
1104 po["features"] = fromFeatureSet(resp.features, 1153 po["features"] = fromFeatureSet(resp.features,
1105 *pmapper.pluginToOutputIdMapper(resp.plugin), 1154 *pmapper.pluginToOutputIdMapper(resp.plugin),
1106 serialisation); 1155 serialisation);
1107 jo["content"] = po; 1156 jo["method"] = "process";
1108 return json11::Json(jo); 1157 jo["result"] = po;
1109 } 1158 return json11::Json(jo);
1110 1159 }
1111 static json11::Json 1160
1112 fromVampRequest_Finish(const Vamp::HostExt::FinishRequest &req, 1161 static json11::Json
1162 fromRpcRequest_Finish(const Vamp::HostExt::FinishRequest &req,
1113 const PluginHandleMapper &pmapper) { 1163 const PluginHandleMapper &pmapper) {
1114 1164
1115 json11::Json::object jo; 1165 json11::Json::object jo;
1116 jo["type"] = "finish"; 1166 markRPC(jo);
1167
1117 json11::Json::object fo; 1168 json11::Json::object fo;
1118 fo["pluginHandle"] = pmapper.pluginToHandle(req.plugin); 1169 fo["handle"] = pmapper.pluginToHandle(req.plugin);
1119 jo["content"] = fo; 1170
1171 jo["method"] = "finish";
1172 jo["params"] = fo;
1120 return json11::Json(jo); 1173 return json11::Json(jo);
1121 } 1174 }
1122 1175
1123 static json11::Json 1176 static json11::Json
1124 fromVampResponse_Finish(const Vamp::HostExt::ProcessResponse &resp, 1177 fromRpcResponse_Finish(const Vamp::HostExt::ProcessResponse &resp,
1125 const PluginHandleMapper &pmapper, 1178 const PluginHandleMapper &pmapper,
1126 BufferSerialisation serialisation) { 1179 BufferSerialisation serialisation) {
1127 1180
1128 json11::Json::object jo; 1181 json11::Json::object jo;
1129 jo["type"] = "finish"; 1182 markRPC(jo);
1130 jo["success"] = true; 1183
1131 jo["errorText"] = "";
1132 json11::Json::object po; 1184 json11::Json::object po;
1133 po["pluginHandle"] = pmapper.pluginToHandle(resp.plugin); 1185 po["handle"] = pmapper.pluginToHandle(resp.plugin);
1134 po["features"] = fromFeatureSet(resp.features, 1186 po["features"] = fromFeatureSet(resp.features,
1135 *pmapper.pluginToOutputIdMapper(resp.plugin), 1187 *pmapper.pluginToOutputIdMapper(resp.plugin),
1136 serialisation); 1188 serialisation);
1137 jo["content"] = po; 1189 jo["method"] = "finish";
1190 jo["result"] = po;
1138 return json11::Json(jo); 1191 return json11::Json(jo);
1139 } 1192 }
1140 1193
1141 static json11::Json 1194 static json11::Json
1142 fromError(std::string errorText, RRType responseType) { 1195 fromError(std::string errorText, RRType responseType) {
1143 1196
1144 json11::Json::object jo; 1197 json11::Json::object jo;
1198 markRPC(jo);
1199
1145 std::string type; 1200 std::string type;
1146 1201
1147 if (responseType == RRType::List) type = "list"; 1202 if (responseType == RRType::List) type = "list";
1148 else if (responseType == RRType::Load) type = "load"; 1203 else if (responseType == RRType::Load) type = "load";
1149 else if (responseType == RRType::Configure) type = "configure"; 1204 else if (responseType == RRType::Configure) type = "configure";
1150 else if (responseType == RRType::Process) type = "process"; 1205 else if (responseType == RRType::Process) type = "process";
1151 else if (responseType == RRType::Finish) type = "finish"; 1206 else if (responseType == RRType::Finish) type = "finish";
1152 else type = "invalid"; 1207 else type = "invalid";
1153 1208
1154 jo["type"] = type; 1209 json11::Json::object eo;
1155 jo["success"] = false; 1210 //!!! + need code
1156 jo["errorText"] = std::string("error in ") + type + " request: " + errorText; 1211 eo["message"] =
1157 return json11::Json(jo); 1212 std::string("error in ") + type + " request: " + errorText;
1158 } 1213
1159 1214 jo["method"] = type;
1160 private: // go private briefly for a couple of helper functions 1215 jo["error"] = eo;
1161 1216
1162 static void 1217 return json11::Json(jo);
1163 checkTypeField(json11::Json j, std::string expected, std::string &err) { 1218 }
1164 if (!j["type"].is_string()) { 1219
1165 err = "string expected for type";
1166 return;
1167 }
1168 if (j["type"].string_value() != expected) {
1169 err = "expected value \"" + expected + "\" for type";
1170 return;
1171 }
1172 }
1173
1174 static bool
1175 successful(json11::Json j, std::string &err) {
1176 if (!j["success"].is_bool()) {
1177 err = "bool expected for success";
1178 return false;
1179 }
1180 return j["success"].bool_value();
1181 }
1182
1183 public:
1184 static RRType 1220 static RRType
1185 getRequestResponseType(json11::Json j, std::string &err) { 1221 getRequestResponseType(json11::Json j, std::string &err) {
1186 1222
1187 if (!j["type"].is_string()) { 1223 if (!j["method"].is_string()) {
1188 err = "string expected for type"; 1224 err = "string expected for method";
1189 return RRType::NotValid; 1225 return RRType::NotValid;
1190 } 1226 }
1191 1227
1192 std::string type = j["type"].string_value(); 1228 std::string type = j["method"].string_value();
1193 1229
1194 if (type == "list") return RRType::List; 1230 if (type == "list") return RRType::List;
1195 else if (type == "load") return RRType::Load; 1231 else if (type == "load") return RRType::Load;
1196 else if (type == "configure") return RRType::Configure; 1232 else if (type == "configure") return RRType::Configure;
1197 else if (type == "process") return RRType::Process; 1233 else if (type == "process") return RRType::Process;
1202 return RRType::NotValid; 1238 return RRType::NotValid;
1203 } 1239 }
1204 } 1240 }
1205 1241
1206 static void 1242 static void
1207 toVampRequest_List(json11::Json j, std::string &err) { 1243 toRpcRequest_List(json11::Json j, std::string &err) {
1208 checkTypeField(j, "list", err); 1244 checkTypeField(j, "list", err);
1209 } 1245 }
1210 1246
1211 static Vamp::HostExt::ListResponse 1247 static Vamp::HostExt::ListResponse
1212 toVampResponse_List(json11::Json j, std::string &err) { 1248 toRpcResponse_List(json11::Json j, std::string &err) {
1213 1249
1214 Vamp::HostExt::ListResponse resp; 1250 Vamp::HostExt::ListResponse resp;
1215 if (successful(j, err) && !failed(err)) { 1251 if (successful(j, err) && !failed(err)) {
1216 for (const auto &a: j["content"]["plugins"].array_items()) { 1252 for (const auto &a: j["result"]["available"].array_items()) {
1217 resp.plugins.push_back(toPluginStaticData(a, err)); 1253 resp.plugins.push_back(toPluginStaticData(a, err));
1218 if (failed(err)) return {}; 1254 if (failed(err)) return {};
1219 } 1255 }
1220 } 1256 }
1221 1257
1222 return resp; 1258 return resp;
1223 } 1259 }
1224 1260
1225 static Vamp::HostExt::LoadRequest 1261 static Vamp::HostExt::LoadRequest
1226 toVampRequest_Load(json11::Json j, std::string &err) { 1262 toRpcRequest_Load(json11::Json j, std::string &err) {
1227 1263
1228 checkTypeField(j, "load", err); 1264 checkTypeField(j, "load", err);
1229 if (failed(err)) return {}; 1265 if (failed(err)) return {};
1230 return toLoadRequest(j["content"], err); 1266 return toLoadRequest(j["params"], err);
1231 } 1267 }
1232 1268
1233 static Vamp::HostExt::LoadResponse 1269 static Vamp::HostExt::LoadResponse
1234 toVampResponse_Load(json11::Json j, 1270 toRpcResponse_Load(json11::Json j,
1235 const PluginHandleMapper &pmapper, 1271 const PluginHandleMapper &pmapper,
1236 std::string &err) { 1272 std::string &err) {
1237 1273
1238 Vamp::HostExt::LoadResponse resp; 1274 Vamp::HostExt::LoadResponse resp;
1239 if (successful(j, err) && !failed(err)) { 1275 if (successful(j, err) && !failed(err)) {
1240 resp = toLoadResponse(j["content"], pmapper, err); 1276 resp = toLoadResponse(j["result"], pmapper, err);
1241 } 1277 }
1242 return resp; 1278 return resp;
1243 } 1279 }
1244 1280
1245 static Vamp::HostExt::ConfigurationRequest 1281 static Vamp::HostExt::ConfigurationRequest
1246 toVampRequest_Configure(json11::Json j, 1282 toRpcRequest_Configure(json11::Json j,
1247 const PluginHandleMapper &pmapper, 1283 const PluginHandleMapper &pmapper,
1248 std::string &err) { 1284 std::string &err) {
1249 1285
1250 checkTypeField(j, "configure", err); 1286 checkTypeField(j, "configure", err);
1251 if (failed(err)) return {}; 1287 if (failed(err)) return {};
1252 return toConfigurationRequest(j["content"], pmapper, err); 1288 return toConfigurationRequest(j["params"], pmapper, err);
1253 } 1289 }
1254 1290
1255 static Vamp::HostExt::ConfigurationResponse 1291 static Vamp::HostExt::ConfigurationResponse
1256 toVampResponse_Configure(json11::Json j, 1292 toRpcResponse_Configure(json11::Json j,
1257 const PluginHandleMapper &pmapper, 1293 const PluginHandleMapper &pmapper,
1258 std::string &err) { 1294 std::string &err) {
1259 1295
1260 Vamp::HostExt::ConfigurationResponse resp; 1296 Vamp::HostExt::ConfigurationResponse resp;
1261 if (successful(j, err) && !failed(err)) { 1297 if (successful(j, err) && !failed(err)) {
1262 resp = toConfigurationResponse(j["content"], pmapper, err); 1298 resp = toConfigurationResponse(j["result"], pmapper, err);
1263 } 1299 }
1264 return resp; 1300 return resp;
1265 } 1301 }
1266 1302
1267 static Vamp::HostExt::ProcessRequest 1303 static Vamp::HostExt::ProcessRequest
1268 toVampRequest_Process(json11::Json j, const PluginHandleMapper &pmapper, 1304 toRpcRequest_Process(json11::Json j, const PluginHandleMapper &pmapper,
1269 BufferSerialisation &serialisation, std::string &err) { 1305 BufferSerialisation &serialisation, std::string &err) {
1270 1306
1271 checkTypeField(j, "process", err); 1307 checkTypeField(j, "process", err);
1272 if (failed(err)) return {}; 1308 if (failed(err)) return {};
1273 return toProcessRequest(j["content"], pmapper, serialisation, err); 1309 return toProcessRequest(j["params"], pmapper, serialisation, err);
1274 } 1310 }
1275 1311
1276 static Vamp::HostExt::ProcessResponse 1312 static Vamp::HostExt::ProcessResponse
1277 toVampResponse_Process(json11::Json j, 1313 toRpcResponse_Process(json11::Json j,
1278 const PluginHandleMapper &pmapper, 1314 const PluginHandleMapper &pmapper,
1279 BufferSerialisation &serialisation, std::string &err) { 1315 BufferSerialisation &serialisation, std::string &err) {
1280 1316
1281 Vamp::HostExt::ProcessResponse resp; 1317 Vamp::HostExt::ProcessResponse resp;
1282 if (successful(j, err) && !failed(err)) { 1318 if (successful(j, err) && !failed(err)) {
1283 auto jc = j["content"]; 1319 auto jc = j["result"];
1284 auto h = jc["pluginHandle"].int_value(); 1320 auto h = jc["handle"].int_value();
1285 resp.plugin = pmapper.handleToPlugin(h); 1321 resp.plugin = pmapper.handleToPlugin(h);
1286 resp.features = toFeatureSet(jc["features"], 1322 resp.features = toFeatureSet(jc["features"],
1287 *pmapper.handleToOutputIdMapper(h), 1323 *pmapper.handleToOutputIdMapper(h),
1288 serialisation, err); 1324 serialisation, err);
1289 } 1325 }
1290 return resp; 1326 return resp;
1291 } 1327 }
1292 1328
1293 static Vamp::HostExt::FinishRequest 1329 static Vamp::HostExt::FinishRequest
1294 toVampRequest_Finish(json11::Json j, const PluginHandleMapper &pmapper, 1330 toRpcRequest_Finish(json11::Json j, const PluginHandleMapper &pmapper,
1295 std::string &err) { 1331 std::string &err) {
1296 1332
1297 checkTypeField(j, "finish", err); 1333 checkTypeField(j, "finish", err);
1298 if (failed(err)) return {}; 1334 if (failed(err)) return {};
1299 Vamp::HostExt::FinishRequest req; 1335 Vamp::HostExt::FinishRequest req;
1300 req.plugin = pmapper.handleToPlugin 1336 req.plugin = pmapper.handleToPlugin
1301 (j["content"]["pluginHandle"].int_value()); 1337 (j["params"]["handle"].int_value());
1302 return req; 1338 return req;
1303 } 1339 }
1304 1340
1305 static Vamp::HostExt::ProcessResponse 1341 static Vamp::HostExt::ProcessResponse
1306 toVampResponse_Finish(json11::Json j, 1342 toRpcResponse_Finish(json11::Json j,
1307 const PluginHandleMapper &pmapper, 1343 const PluginHandleMapper &pmapper,
1308 BufferSerialisation &serialisation, std::string &err) { 1344 BufferSerialisation &serialisation, std::string &err) {
1309 1345
1310 Vamp::HostExt::ProcessResponse resp; 1346 Vamp::HostExt::ProcessResponse resp;
1311 if (successful(j, err) && !failed(err)) { 1347 if (successful(j, err) && !failed(err)) {
1312 auto jc = j["content"]; 1348 auto jc = j["result"];
1313 auto h = jc["pluginHandle"].int_value(); 1349 auto h = jc["handle"].int_value();
1314 resp.plugin = pmapper.handleToPlugin(h); 1350 resp.plugin = pmapper.handleToPlugin(h);
1315 resp.features = toFeatureSet(jc["features"], 1351 resp.features = toFeatureSet(jc["features"],
1316 *pmapper.handleToOutputIdMapper(h), 1352 *pmapper.handleToOutputIdMapper(h),
1317 serialisation, err); 1353 serialisation, err);
1318 } 1354 }