Mercurial > hg > piper-cpp
comparison utilities/json-cli.cpp @ 11:aa61cb5c5754
Use mapper
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Tue, 17 May 2016 11:15:25 +0100 |
parents | c8451896c40e |
children | 828930f9a65d |
comparison
equal
deleted
inserted
replaced
10:c8451896c40e | 11:aa61cb5c5754 |
---|---|
12 using namespace Vamp; | 12 using namespace Vamp; |
13 using namespace Vamp::HostExt; | 13 using namespace Vamp::HostExt; |
14 using namespace json11; | 14 using namespace json11; |
15 using namespace vampipe; | 15 using namespace vampipe; |
16 | 16 |
17 static map<uint32_t, Plugin *> loadedPlugins; | 17 class Mapper : public PluginHandleMapper |
18 static set<uint32_t> initialisedPlugins; | 18 { |
19 | 19 public: |
20 static uint32_t nextHandle = 1; // plugin handle type must fit in JSON number | 20 Mapper() : m_nextHandle(1) { } |
21 | |
22 void addPlugin(Plugin *p) { | |
23 if (m_rplugins.find(p) == m_rplugins.end()) { | |
24 int32_t h = m_nextHandle++; | |
25 m_plugins[h] = p; | |
26 m_rplugins[p] = h; | |
27 } | |
28 } | |
29 | |
30 int32_t pluginToHandle(Plugin *p) { | |
31 if (m_rplugins.find(p) == m_rplugins.end()) { | |
32 throw NotFound(); | |
33 } | |
34 return m_rplugins[p]; | |
35 } | |
36 | |
37 Plugin *handleToPlugin(int32_t h) { | |
38 if (m_plugins.find(h) == m_plugins.end()) { | |
39 throw NotFound(); | |
40 } | |
41 return m_plugins[h]; | |
42 } | |
43 | |
44 bool isInitialised(int32_t h) { | |
45 return m_initialisedPlugins.find(h) != m_initialisedPlugins.end(); | |
46 } | |
47 | |
48 void markInitialised(int32_t h) { | |
49 m_initialisedPlugins.insert(h); | |
50 } | |
51 | |
52 private: | |
53 //!!! + mutex | |
54 int32_t m_nextHandle; // plugin handle type must fit in JSON number | |
55 map<uint32_t, Plugin *> m_plugins; | |
56 map<Plugin *, uint32_t> m_rplugins; | |
57 set<uint32_t> m_initialisedPlugins; | |
58 }; | |
59 | |
60 static Mapper mapper; | |
21 | 61 |
22 Vamp::HostExt::LoadResponse | 62 Vamp::HostExt::LoadResponse |
23 loadPlugin(json11::Json j) { | 63 loadPlugin(json11::Json j) { |
24 | 64 |
25 auto req = VampJson::toLoadRequest(j); | 65 auto req = VampJson::toLoadRequest(j); |
70 auto loadResponse = loadPlugin(j); | 110 auto loadResponse = loadPlugin(j); |
71 | 111 |
72 if (!loadResponse.plugin) { | 112 if (!loadResponse.plugin) { |
73 throw VampJson::Failure("plugin load failed"); | 113 throw VampJson::Failure("plugin load failed"); |
74 } | 114 } |
75 | 115 |
76 uint32_t h = nextHandle++; | 116 mapper.addPlugin(loadResponse.plugin); |
77 loadedPlugins[h] = loadResponse.plugin; | 117 |
78 | 118 return VampJson::fromLoadResponse(loadResponse, mapper); |
79 Json::object response; | |
80 response["pluginHandle"] = double(h); | |
81 response["staticData"] = | |
82 VampJson::fromPluginStaticData(loadResponse.staticData); | |
83 response["defaultConfiguration"] = | |
84 VampJson::fromPluginConfiguration(loadResponse.defaultConfiguration); | |
85 | |
86 cerr << "Loaded plugin: handle is " << h << endl; | |
87 | |
88 return Json(response); | |
89 } | 119 } |
90 | 120 |
91 Json | 121 Json |
92 handle_configure(Json j) | 122 handle_configure(Json j) |
93 { | 123 { |
97 { "pluginHandle", Json::NUMBER }, | 127 { "pluginHandle", Json::NUMBER }, |
98 { "configuration", Json::OBJECT }}, err)) { | 128 { "configuration", Json::OBJECT }}, err)) { |
99 throw VampJson::Failure("malformed configuration request: " + err); | 129 throw VampJson::Failure("malformed configuration request: " + err); |
100 } | 130 } |
101 | 131 |
102 uint32_t handle = j["pluginHandle"].int_value(); | 132 int32_t handle = j["pluginHandle"].int_value(); |
103 | 133 |
104 if (loadedPlugins.find(handle) == loadedPlugins.end()) { | 134 if (mapper.isInitialised(handle)) { |
105 throw VampJson::Failure("unknown plugin handle"); | |
106 } | |
107 | |
108 if (initialisedPlugins.find(handle) != initialisedPlugins.end()) { | |
109 throw VampJson::Failure("plugin has already been initialised"); | 135 throw VampJson::Failure("plugin has already been initialised"); |
110 } | 136 } |
111 | 137 |
112 Plugin *plugin = loadedPlugins[handle]; | 138 Plugin *plugin = mapper.handleToPlugin(handle); |
113 | 139 |
114 Json config = j["configuration"]; | 140 Json config = j["configuration"]; |
115 | 141 |
116 configurePlugin(plugin, config); | 142 configurePlugin(plugin, config); |
117 | 143 |
118 initialisedPlugins.insert(handle); | 144 mapper.markInitialised(handle); |
119 | 145 |
120 cerr << "Configured and initialised plugin " << handle << endl; | 146 cerr << "Configured and initialised plugin " << handle << endl; |
121 | 147 |
148 //!!! to VampJson: | |
122 Json::object jout; | 149 Json::object jout; |
123 Json::array outs; | 150 Json::array outs; |
124 Vamp::Plugin::OutputList vouts = plugin->getOutputDescriptors(); | 151 Vamp::Plugin::OutputList vouts = plugin->getOutputDescriptors(); |
125 for (auto &o: vouts) { | 152 for (auto &o: vouts) { |
126 outs.push_back(VampJson::fromOutputDescriptor(o)); | 153 outs.push_back(VampJson::fromOutputDescriptor(o)); |
199 try { | 226 try { |
200 Json result = handle(line); | 227 Json result = handle(line); |
201 cout << success_response(result).dump() << endl; | 228 cout << success_response(result).dump() << endl; |
202 } catch (const VampJson::Failure &e) { | 229 } catch (const VampJson::Failure &e) { |
203 cout << error_response(e.what()).dump() << endl; | 230 cout << error_response(e.what()).dump() << endl; |
231 } catch (const PluginHandleMapper::NotFound &e) { | |
232 cout << error_response(e.what()).dump() << endl; | |
204 } | 233 } |
205 } | 234 } |
206 | 235 |
207 return 0; | 236 return 0; |
208 } | 237 } |