c@125
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@125
|
2 /*
|
c@125
|
3 Piper C++
|
c@125
|
4
|
c@125
|
5 An API for audio analysis and feature extraction plugins.
|
c@125
|
6
|
c@125
|
7 Centre for Digital Music, Queen Mary, University of London.
|
c@125
|
8 Copyright 2006-2016 Chris Cannam and QMUL.
|
c@125
|
9
|
c@125
|
10 Permission is hereby granted, free of charge, to any person
|
c@125
|
11 obtaining a copy of this software and associated documentation
|
c@125
|
12 files (the "Software"), to deal in the Software without
|
c@125
|
13 restriction, including without limitation the rights to use, copy,
|
c@125
|
14 modify, merge, publish, distribute, sublicense, and/or sell copies
|
c@125
|
15 of the Software, and to permit persons to whom the Software is
|
c@125
|
16 furnished to do so, subject to the following conditions:
|
c@125
|
17
|
c@125
|
18 The above copyright notice and this permission notice shall be
|
c@125
|
19 included in all copies or substantial portions of the Software.
|
c@125
|
20
|
c@125
|
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
c@125
|
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
c@125
|
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
c@125
|
24 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
c@125
|
25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
c@125
|
26 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
c@125
|
27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
c@125
|
28
|
c@125
|
29 Except as contained in this notice, the names of the Centre for
|
c@125
|
30 Digital Music; Queen Mary, University of London; and Chris Cannam
|
c@125
|
31 shall not be used in advertising or otherwise to promote the sale,
|
c@125
|
32 use or other dealings in this Software without prior written
|
c@125
|
33 authorization.
|
c@125
|
34 */
|
c@125
|
35
|
c@125
|
36 #include "vamp-json/VampJson.h"
|
c@125
|
37 #include "vamp-capnp/VampnProto.h"
|
c@125
|
38 #include "vamp-support/RequestOrResponse.h"
|
c@125
|
39 #include "vamp-support/CountingPluginHandleMapper.h"
|
c@125
|
40 #include "vamp-support/LoaderRequests.h"
|
c@125
|
41
|
c@125
|
42 #include <iostream>
|
c@125
|
43 #include <sstream>
|
c@125
|
44 #include <stdexcept>
|
c@125
|
45
|
c@125
|
46 #include <capnp/serialize.h>
|
c@125
|
47
|
c@125
|
48 #include <map>
|
c@125
|
49 #include <set>
|
c@125
|
50
|
c@125
|
51 // pid for logging
|
c@125
|
52 #ifdef _WIN32
|
c@125
|
53 #include <process.h>
|
c@125
|
54 static int pid = _getpid();
|
c@125
|
55 #else
|
c@125
|
56 #include <unistd.h>
|
c@125
|
57 static int pid = getpid();
|
c@125
|
58 #endif
|
c@125
|
59
|
c@125
|
60 // for _setmode stuff
|
c@125
|
61 #ifdef _WIN32
|
c@125
|
62 #include <io.h>
|
c@125
|
63 #include <fcntl.h>
|
c@125
|
64 #endif
|
c@125
|
65
|
c@125
|
66 using namespace std;
|
c@125
|
67 using namespace json11;
|
c@125
|
68 using namespace piper_vamp;
|
c@125
|
69 using namespace Vamp;
|
c@125
|
70
|
c@125
|
71 static string myname = "piper-vamp-simple-server";
|
c@125
|
72
|
c@125
|
73 static void version()
|
c@125
|
74 {
|
c@125
|
75 cout << "1.0" << endl;
|
c@125
|
76 exit(0);
|
c@125
|
77 }
|
c@125
|
78
|
c@125
|
79 static void usage(bool successful = false)
|
c@125
|
80 {
|
c@125
|
81 cerr << "\n" << myname <<
|
c@125
|
82 ": Load & run Vamp plugins in response to Piper messages\n\n"
|
c@125
|
83 " Usage: " << myname << " [-d] <format>\n"
|
c@125
|
84 " " << myname << " -v\n"
|
c@125
|
85 " " << myname << " -h\n\n"
|
c@125
|
86 " where\n"
|
c@125
|
87 " <format>: the format to read and write messages in (\"json\" or \"capnp\")\n"
|
c@125
|
88 " -d: also print debug information to stderr\n"
|
c@125
|
89 " -v: print version number to stdout and exit\n"
|
c@125
|
90 " -h: print this text to stderr and exit\n\n"
|
c@125
|
91 "Expects Piper request messages in either Cap'n Proto or JSON format on stdin,\n"
|
c@125
|
92 "and writes response messages in the same format to stdout.\n\n"
|
c@125
|
93 "This server is intended for simple process separation. It's only suitable for\n"
|
c@125
|
94 "use with a single trusted client per server invocation.\n\n"
|
c@125
|
95 "The two formats behave differently in case of parser errors. JSON messages are\n"
|
c@125
|
96 "expected one per input line; because the JSON support is really intended for\n"
|
c@125
|
97 "interactive troubleshooting, any unparseable message is reported and discarded\n"
|
c@125
|
98 "and the server waits for another message. In contrast, because of the assumption\n"
|
c@125
|
99 "that the client is trusted and coupled to the server instance, a mangled\n"
|
c@125
|
100 "Cap'n Proto message causes the server to exit.\n\n";
|
c@125
|
101 if (successful) exit(0);
|
c@125
|
102 else exit(2);
|
c@125
|
103 }
|
c@125
|
104
|
c@125
|
105 static CountingPluginHandleMapper mapper;
|
c@125
|
106
|
c@125
|
107 static RequestOrResponse::RpcId
|
c@125
|
108 readId(const piper::RpcRequest::Reader &r)
|
c@125
|
109 {
|
c@125
|
110 int number;
|
c@125
|
111 string tag;
|
c@125
|
112 switch (r.getId().which()) {
|
c@125
|
113 case piper::RpcRequest::Id::Which::NUMBER:
|
c@125
|
114 number = r.getId().getNumber();
|
c@125
|
115 return { RequestOrResponse::RpcId::Number, number, "" };
|
c@125
|
116 case piper::RpcRequest::Id::Which::TAG:
|
c@125
|
117 tag = r.getId().getTag();
|
c@125
|
118 return { RequestOrResponse::RpcId::Tag, 0, tag };
|
c@125
|
119 case piper::RpcRequest::Id::Which::NONE:
|
c@125
|
120 return { RequestOrResponse::RpcId::Absent, 0, "" };
|
c@125
|
121 }
|
c@125
|
122 return {};
|
c@125
|
123 }
|
c@125
|
124
|
c@125
|
125 static void
|
c@125
|
126 buildId(piper::RpcResponse::Builder &b, const RequestOrResponse::RpcId &id)
|
c@125
|
127 {
|
c@125
|
128 switch (id.type) {
|
c@125
|
129 case RequestOrResponse::RpcId::Number:
|
c@125
|
130 b.getId().setNumber(id.number);
|
c@125
|
131 break;
|
c@125
|
132 case RequestOrResponse::RpcId::Tag:
|
c@125
|
133 b.getId().setTag(id.tag);
|
c@125
|
134 break;
|
c@125
|
135 case RequestOrResponse::RpcId::Absent:
|
c@125
|
136 b.getId().setNone();
|
c@125
|
137 break;
|
c@125
|
138 }
|
c@125
|
139 }
|
c@125
|
140
|
c@125
|
141 static RequestOrResponse::RpcId
|
c@125
|
142 readJsonId(const Json &j)
|
c@125
|
143 {
|
c@125
|
144 RequestOrResponse::RpcId id;
|
c@125
|
145
|
c@125
|
146 if (j["id"].is_number()) {
|
c@125
|
147 id.type = RequestOrResponse::RpcId::Number;
|
c@125
|
148 id.number = j["id"].number_value();
|
c@125
|
149 } else if (j["id"].is_string()) {
|
c@125
|
150 id.type = RequestOrResponse::RpcId::Tag;
|
c@125
|
151 id.tag = j["id"].string_value();
|
c@125
|
152 } else {
|
c@125
|
153 id.type = RequestOrResponse::RpcId::Absent;
|
c@125
|
154 }
|
c@125
|
155
|
c@125
|
156 return id;
|
c@125
|
157 }
|
c@125
|
158
|
c@125
|
159 static Json
|
c@125
|
160 writeJsonId(const RequestOrResponse::RpcId &id)
|
c@125
|
161 {
|
c@125
|
162 if (id.type == RequestOrResponse::RpcId::Number) {
|
c@125
|
163 return id.number;
|
c@125
|
164 } else if (id.type == RequestOrResponse::RpcId::Tag) {
|
c@125
|
165 return id.tag;
|
c@125
|
166 } else {
|
c@125
|
167 return Json();
|
c@125
|
168 }
|
c@125
|
169 }
|
c@125
|
170
|
c@125
|
171 static Json
|
c@125
|
172 convertRequestJson(string input, string &err)
|
c@125
|
173 {
|
c@125
|
174 Json j = Json::parse(input, err);
|
c@125
|
175 if (err != "") {
|
c@125
|
176 err = "invalid json: " + err;
|
c@125
|
177 return {};
|
c@125
|
178 }
|
c@125
|
179 if (!j.is_object()) {
|
c@125
|
180 err = "object expected at top level";
|
c@125
|
181 } else if (!j["method"].is_string()) {
|
c@125
|
182 err = "string expected for method field";
|
c@125
|
183 } else if (!j["params"].is_null() && !j["params"].is_object()) {
|
c@125
|
184 err = "object expected for params field";
|
c@125
|
185 }
|
c@125
|
186 return j;
|
c@125
|
187 }
|
c@125
|
188
|
c@125
|
189 RequestOrResponse
|
c@125
|
190 readRequestJson(string &err)
|
c@125
|
191 {
|
c@125
|
192 RequestOrResponse rr;
|
c@125
|
193 rr.direction = RequestOrResponse::Request;
|
c@125
|
194
|
c@125
|
195 string input;
|
c@125
|
196 if (!getline(cin, input)) {
|
c@125
|
197 // the EOF case, not actually an error
|
c@125
|
198 rr.type = RRType::NotValid;
|
c@125
|
199 return rr;
|
c@125
|
200 }
|
c@125
|
201
|
c@125
|
202 Json j = convertRequestJson(input, err);
|
c@125
|
203 if (err != "") return {};
|
c@125
|
204
|
c@125
|
205 rr.type = VampJson::getRequestResponseType(j, err);
|
c@125
|
206 if (err != "") return {};
|
c@125
|
207
|
c@125
|
208 rr.id = readJsonId(j);
|
c@125
|
209
|
c@125
|
210 VampJson::BufferSerialisation serialisation =
|
c@125
|
211 VampJson::BufferSerialisation::Array;
|
c@125
|
212
|
c@125
|
213 switch (rr.type) {
|
c@125
|
214
|
c@125
|
215 case RRType::List:
|
c@130
|
216 rr.listRequest = VampJson::toRpcRequest_List(j, err);
|
c@125
|
217 break;
|
c@125
|
218 case RRType::Load:
|
c@125
|
219 rr.loadRequest = VampJson::toRpcRequest_Load(j, err);
|
c@125
|
220 break;
|
c@125
|
221 case RRType::Configure:
|
c@125
|
222 rr.configurationRequest = VampJson::toRpcRequest_Configure(j, mapper, err);
|
c@125
|
223 break;
|
c@125
|
224 case RRType::Process:
|
c@125
|
225 rr.processRequest = VampJson::toRpcRequest_Process(j, mapper, serialisation, err);
|
c@125
|
226 break;
|
c@125
|
227 case RRType::Finish:
|
c@125
|
228 rr.finishRequest = VampJson::toRpcRequest_Finish(j, mapper, err);
|
c@125
|
229 break;
|
c@125
|
230 case RRType::NotValid:
|
c@125
|
231 break;
|
c@125
|
232 }
|
c@125
|
233
|
c@125
|
234 return rr;
|
c@125
|
235 }
|
c@125
|
236
|
c@125
|
237 void
|
c@125
|
238 writeResponseJson(RequestOrResponse &rr, bool useBase64)
|
c@125
|
239 {
|
c@125
|
240 Json j;
|
c@125
|
241
|
c@125
|
242 VampJson::BufferSerialisation serialisation =
|
c@125
|
243 (useBase64 ?
|
c@125
|
244 VampJson::BufferSerialisation::Base64 :
|
c@125
|
245 VampJson::BufferSerialisation::Array);
|
c@125
|
246
|
c@125
|
247 Json id = writeJsonId(rr.id);
|
c@125
|
248
|
c@125
|
249 if (!rr.success) {
|
c@125
|
250
|
c@125
|
251 j = VampJson::fromError(rr.errorText, rr.type, id);
|
c@125
|
252
|
c@125
|
253 } else {
|
c@125
|
254
|
c@125
|
255 switch (rr.type) {
|
c@125
|
256
|
c@125
|
257 case RRType::List:
|
c@125
|
258 j = VampJson::fromRpcResponse_List(rr.listResponse, id);
|
c@125
|
259 break;
|
c@125
|
260 case RRType::Load:
|
c@125
|
261 j = VampJson::fromRpcResponse_Load(rr.loadResponse, mapper, id);
|
c@125
|
262 break;
|
c@125
|
263 case RRType::Configure:
|
c@125
|
264 j = VampJson::fromRpcResponse_Configure(rr.configurationResponse,
|
c@125
|
265 mapper, id);
|
c@125
|
266 break;
|
c@125
|
267 case RRType::Process:
|
c@125
|
268 j = VampJson::fromRpcResponse_Process
|
c@125
|
269 (rr.processResponse, mapper, serialisation, id);
|
c@125
|
270 break;
|
c@125
|
271 case RRType::Finish:
|
c@125
|
272 j = VampJson::fromRpcResponse_Finish
|
c@125
|
273 (rr.finishResponse, mapper, serialisation, id);
|
c@125
|
274 break;
|
c@125
|
275 case RRType::NotValid:
|
c@125
|
276 break;
|
c@125
|
277 }
|
c@125
|
278 }
|
c@125
|
279
|
c@125
|
280 cout << j.dump() << endl;
|
c@125
|
281 }
|
c@125
|
282
|
c@125
|
283 void
|
c@125
|
284 writeExceptionJson(const std::exception &e, RRType type)
|
c@125
|
285 {
|
c@125
|
286 Json j = VampJson::fromError(e.what(), type, Json());
|
c@125
|
287 cout << j.dump() << endl;
|
c@125
|
288 }
|
c@125
|
289
|
c@125
|
290 RequestOrResponse
|
c@125
|
291 readRequestCapnp()
|
c@125
|
292 {
|
c@125
|
293 RequestOrResponse rr;
|
c@125
|
294 rr.direction = RequestOrResponse::Request;
|
c@125
|
295
|
c@125
|
296 static kj::FdInputStream stream(0); // stdin
|
c@125
|
297 static kj::BufferedInputStreamWrapper buffered(stream);
|
c@125
|
298
|
c@125
|
299 if (buffered.tryGetReadBuffer() == nullptr) {
|
c@125
|
300 rr.type = RRType::NotValid;
|
c@125
|
301 return rr;
|
c@125
|
302 }
|
c@125
|
303
|
c@125
|
304 capnp::InputStreamMessageReader message(buffered);
|
c@125
|
305 piper::RpcRequest::Reader reader = message.getRoot<piper::RpcRequest>();
|
c@125
|
306
|
c@125
|
307 rr.type = VampnProto::getRequestResponseType(reader);
|
c@125
|
308 rr.id = readId(reader);
|
c@125
|
309
|
c@125
|
310 switch (rr.type) {
|
c@125
|
311
|
c@125
|
312 case RRType::List:
|
c@127
|
313 VampnProto::readRpcRequest_List(rr.listRequest, reader);
|
c@125
|
314 break;
|
c@125
|
315 case RRType::Load:
|
c@125
|
316 VampnProto::readRpcRequest_Load(rr.loadRequest, reader);
|
c@125
|
317 break;
|
c@125
|
318 case RRType::Configure:
|
c@125
|
319 VampnProto::readRpcRequest_Configure(rr.configurationRequest,
|
c@125
|
320 reader, mapper);
|
c@125
|
321 break;
|
c@125
|
322 case RRType::Process:
|
c@125
|
323 VampnProto::readRpcRequest_Process(rr.processRequest, reader, mapper);
|
c@125
|
324 break;
|
c@125
|
325 case RRType::Finish:
|
c@125
|
326 VampnProto::readRpcRequest_Finish(rr.finishRequest, reader, mapper);
|
c@125
|
327 break;
|
c@125
|
328 case RRType::NotValid:
|
c@125
|
329 break;
|
c@125
|
330 }
|
c@125
|
331
|
c@125
|
332 return rr;
|
c@125
|
333 }
|
c@125
|
334
|
c@125
|
335 void
|
c@125
|
336 writeResponseCapnp(RequestOrResponse &rr)
|
c@125
|
337 {
|
c@125
|
338 capnp::MallocMessageBuilder message;
|
c@125
|
339 piper::RpcResponse::Builder builder = message.initRoot<piper::RpcResponse>();
|
c@125
|
340
|
c@125
|
341 buildId(builder, rr.id);
|
c@125
|
342
|
c@125
|
343 if (!rr.success) {
|
c@125
|
344
|
c@125
|
345 VampnProto::buildRpcResponse_Error(builder, rr.errorText, rr.type);
|
c@125
|
346
|
c@125
|
347 } else {
|
c@125
|
348
|
c@125
|
349 switch (rr.type) {
|
c@125
|
350
|
c@125
|
351 case RRType::List:
|
c@125
|
352 VampnProto::buildRpcResponse_List(builder, rr.listResponse);
|
c@125
|
353 break;
|
c@125
|
354 case RRType::Load:
|
c@125
|
355 VampnProto::buildRpcResponse_Load(builder, rr.loadResponse, mapper);
|
c@125
|
356 break;
|
c@125
|
357 case RRType::Configure:
|
c@125
|
358 VampnProto::buildRpcResponse_Configure(builder, rr.configurationResponse, mapper);
|
c@125
|
359 break;
|
c@125
|
360 case RRType::Process:
|
c@125
|
361 VampnProto::buildRpcResponse_Process(builder, rr.processResponse, mapper);
|
c@125
|
362 break;
|
c@125
|
363 case RRType::Finish:
|
c@125
|
364 VampnProto::buildRpcResponse_Finish(builder, rr.finishResponse, mapper);
|
c@125
|
365 break;
|
c@125
|
366 case RRType::NotValid:
|
c@125
|
367 break;
|
c@125
|
368 }
|
c@125
|
369 }
|
c@125
|
370
|
c@125
|
371 writeMessageToFd(1, message);
|
c@125
|
372 }
|
c@125
|
373
|
c@125
|
374 void
|
c@125
|
375 writeExceptionCapnp(const std::exception &e, RRType type)
|
c@125
|
376 {
|
c@125
|
377 capnp::MallocMessageBuilder message;
|
c@125
|
378 piper::RpcResponse::Builder builder = message.initRoot<piper::RpcResponse>();
|
c@125
|
379 VampnProto::buildRpcResponse_Exception(builder, e, type);
|
c@125
|
380
|
c@125
|
381 writeMessageToFd(1, message);
|
c@125
|
382 }
|
c@125
|
383
|
c@125
|
384 RequestOrResponse
|
c@125
|
385 handleRequest(const RequestOrResponse &request, bool debug)
|
c@125
|
386 {
|
c@125
|
387 RequestOrResponse response;
|
c@125
|
388 response.direction = RequestOrResponse::Response;
|
c@125
|
389 response.type = request.type;
|
c@125
|
390
|
c@125
|
391 switch (request.type) {
|
c@125
|
392
|
c@125
|
393 case RRType::List:
|
c@127
|
394 response.listResponse =
|
c@127
|
395 LoaderRequests().listPluginData(request.listRequest);
|
c@125
|
396 response.success = true;
|
c@125
|
397 break;
|
c@125
|
398
|
c@125
|
399 case RRType::Load:
|
c@127
|
400 response.loadResponse =
|
c@127
|
401 LoaderRequests().loadPlugin(request.loadRequest);
|
c@125
|
402 if (response.loadResponse.plugin != nullptr) {
|
c@125
|
403 mapper.addPlugin(response.loadResponse.plugin);
|
c@125
|
404 if (debug) {
|
c@127
|
405 cerr << "piper-vamp-server " << pid
|
c@127
|
406 << ": loaded plugin, handle = "
|
c@127
|
407 << mapper.pluginToHandle(response.loadResponse.plugin)
|
c@127
|
408 << endl;
|
c@125
|
409 }
|
c@125
|
410 response.success = true;
|
c@125
|
411 }
|
c@125
|
412 break;
|
c@125
|
413
|
c@125
|
414 case RRType::Configure:
|
c@125
|
415 {
|
c@125
|
416 auto &creq = request.configurationRequest;
|
c@125
|
417 auto h = mapper.pluginToHandle(creq.plugin);
|
c@125
|
418 if (mapper.isConfigured(h)) {
|
c@125
|
419 throw runtime_error("plugin has already been configured");
|
c@125
|
420 }
|
c@125
|
421
|
c@125
|
422 response.configurationResponse = LoaderRequests().configurePlugin(creq);
|
c@125
|
423
|
c@125
|
424 if (!response.configurationResponse.outputs.empty()) {
|
c@125
|
425 mapper.markConfigured
|
c@125
|
426 (h, creq.configuration.channelCount, creq.configuration.blockSize);
|
c@125
|
427 response.success = true;
|
c@125
|
428 }
|
c@125
|
429 break;
|
c@125
|
430 }
|
c@125
|
431
|
c@125
|
432 case RRType::Process:
|
c@125
|
433 {
|
c@125
|
434 auto &preq = request.processRequest;
|
c@125
|
435 auto h = mapper.pluginToHandle(preq.plugin);
|
c@125
|
436 if (!mapper.isConfigured(h)) {
|
c@125
|
437 throw runtime_error("plugin has not been configured");
|
c@125
|
438 }
|
c@125
|
439
|
c@125
|
440 int channels = int(preq.inputBuffers.size());
|
c@125
|
441 if (channels != mapper.getChannelCount(h)) {
|
c@125
|
442 throw runtime_error("wrong number of channels supplied to process");
|
c@125
|
443 }
|
c@125
|
444
|
c@125
|
445 const float **fbuffers = new const float *[channels];
|
c@125
|
446 for (int i = 0; i < channels; ++i) {
|
c@125
|
447 if (int(preq.inputBuffers[i].size()) != mapper.getBlockSize(h)) {
|
c@125
|
448 delete[] fbuffers;
|
c@125
|
449 throw runtime_error("wrong block size supplied to process");
|
c@125
|
450 }
|
c@125
|
451 fbuffers[i] = preq.inputBuffers[i].data();
|
c@125
|
452 }
|
c@125
|
453
|
c@125
|
454 response.processResponse.plugin = preq.plugin;
|
c@125
|
455 response.processResponse.features =
|
c@125
|
456 preq.plugin->process(fbuffers, preq.timestamp);
|
c@125
|
457 response.success = true;
|
c@125
|
458
|
c@125
|
459 delete[] fbuffers;
|
c@125
|
460 break;
|
c@125
|
461 }
|
c@125
|
462
|
c@125
|
463 case RRType::Finish:
|
c@125
|
464 {
|
c@125
|
465 auto &freq = request.finishRequest;
|
c@125
|
466 response.finishResponse.plugin = freq.plugin;
|
c@125
|
467
|
c@125
|
468 auto h = mapper.pluginToHandle(freq.plugin);
|
c@125
|
469 // Finish can be called (to unload the plugin) even if the
|
c@125
|
470 // plugin has never been configured or used. But we want to
|
c@125
|
471 // make sure we call getRemainingFeatures only if we have
|
c@125
|
472 // actually configured the plugin.
|
c@125
|
473 if (mapper.isConfigured(h)) {
|
c@125
|
474 response.finishResponse.features = freq.plugin->getRemainingFeatures();
|
c@125
|
475 }
|
c@125
|
476
|
c@125
|
477 // We do not delete the plugin here -- we need it in the
|
c@125
|
478 // mapper when converting the features. It gets deleted in the
|
c@125
|
479 // calling function.
|
c@125
|
480 response.success = true;
|
c@125
|
481 break;
|
c@125
|
482 }
|
c@125
|
483
|
c@125
|
484 case RRType::NotValid:
|
c@125
|
485 break;
|
c@125
|
486 }
|
c@125
|
487
|
c@125
|
488 return response;
|
c@125
|
489 }
|
c@125
|
490
|
c@125
|
491 RequestOrResponse
|
c@125
|
492 readRequest(string format)
|
c@125
|
493 {
|
c@125
|
494 if (format == "capnp") {
|
c@125
|
495 return readRequestCapnp();
|
c@125
|
496 } else if (format == "json") {
|
c@125
|
497 string err;
|
c@125
|
498 auto result = readRequestJson(err);
|
c@125
|
499 if (err != "") throw runtime_error(err);
|
c@125
|
500 else return result;
|
c@125
|
501 } else {
|
c@125
|
502 throw runtime_error("unknown input format \"" + format + "\"");
|
c@125
|
503 }
|
c@125
|
504 }
|
c@125
|
505
|
c@125
|
506 void
|
c@125
|
507 writeResponse(string format, RequestOrResponse &rr)
|
c@125
|
508 {
|
c@125
|
509 if (format == "capnp") {
|
c@125
|
510 writeResponseCapnp(rr);
|
c@125
|
511 } else if (format == "json") {
|
c@125
|
512 writeResponseJson(rr, false);
|
c@125
|
513 } else {
|
c@125
|
514 throw runtime_error("unknown output format \"" + format + "\"");
|
c@125
|
515 }
|
c@125
|
516 }
|
c@125
|
517
|
c@125
|
518 void
|
c@125
|
519 writeException(string format, const std::exception &e, RRType type)
|
c@125
|
520 {
|
c@125
|
521 if (format == "capnp") {
|
c@125
|
522 writeExceptionCapnp(e, type);
|
c@125
|
523 } else if (format == "json") {
|
c@125
|
524 writeExceptionJson(e, type);
|
c@125
|
525 } else {
|
c@125
|
526 throw runtime_error("unknown output format \"" + format + "\"");
|
c@125
|
527 }
|
c@125
|
528 }
|
c@125
|
529
|
c@125
|
530 int main(int argc, char **argv)
|
c@125
|
531 {
|
c@125
|
532 if (argc != 2 && argc != 3) {
|
c@125
|
533 usage();
|
c@125
|
534 }
|
c@125
|
535
|
c@125
|
536 bool debug = false;
|
c@125
|
537
|
c@125
|
538 string arg = argv[1];
|
c@125
|
539 if (arg == "-h") {
|
c@125
|
540 if (argc == 2) {
|
c@125
|
541 usage(true);
|
c@125
|
542 } else {
|
c@125
|
543 usage();
|
c@125
|
544 }
|
c@125
|
545 } else if (arg == "-v") {
|
c@125
|
546 if (argc == 2) {
|
c@125
|
547 version();
|
c@125
|
548 } else {
|
c@125
|
549 usage();
|
c@125
|
550 }
|
c@125
|
551 } else if (arg == "-d") {
|
c@125
|
552 if (argc == 2) {
|
c@125
|
553 usage();
|
c@125
|
554 } else {
|
c@125
|
555 debug = true;
|
c@125
|
556 arg = argv[2];
|
c@125
|
557 }
|
c@125
|
558 }
|
c@125
|
559
|
c@125
|
560 string format = arg;
|
c@125
|
561
|
c@125
|
562 if (format != "capnp" && format != "json") {
|
c@125
|
563 usage();
|
c@125
|
564 }
|
c@125
|
565
|
c@125
|
566 #ifdef _WIN32
|
c@125
|
567 if (format == "capnp") {
|
c@125
|
568 int result = _setmode(_fileno(stdin), _O_BINARY);
|
c@125
|
569 if (result == -1) {
|
c@125
|
570 cerr << "Failed to set binary mode on stdin, necessary for capnp format" << endl;
|
c@125
|
571 exit(1);
|
c@125
|
572 }
|
c@125
|
573 result = _setmode(_fileno(stdout), _O_BINARY);
|
c@125
|
574 if (result == -1) {
|
c@125
|
575 cerr << "Failed to set binary mode on stdout, necessary for capnp format" << endl;
|
c@125
|
576 exit(1);
|
c@125
|
577 }
|
c@125
|
578 }
|
c@125
|
579 #endif
|
c@125
|
580
|
c@125
|
581 if (debug) {
|
c@125
|
582 cerr << myname << " " << pid << ": waiting for format: " << format << endl;
|
c@125
|
583 }
|
c@125
|
584
|
c@125
|
585 while (true) {
|
c@125
|
586
|
c@125
|
587 RequestOrResponse request;
|
c@125
|
588
|
c@125
|
589 try {
|
c@125
|
590
|
c@125
|
591 request = readRequest(format);
|
c@125
|
592
|
c@125
|
593 // NotValid without an exception indicates EOF:
|
c@125
|
594 if (request.type == RRType::NotValid) {
|
c@125
|
595 if (debug) {
|
c@125
|
596 cerr << myname << " " << pid << ": eof reached, exiting" << endl;
|
c@125
|
597 }
|
c@125
|
598 break;
|
c@125
|
599 }
|
c@125
|
600
|
c@125
|
601 if (debug) {
|
c@125
|
602 cerr << myname << " " << pid << ": request received, of type "
|
c@125
|
603 << int(request.type)
|
c@125
|
604 << endl;
|
c@125
|
605 }
|
c@125
|
606
|
c@125
|
607 RequestOrResponse response = handleRequest(request, debug);
|
c@125
|
608 response.id = request.id;
|
c@125
|
609
|
c@125
|
610 if (debug) {
|
c@125
|
611 cerr << myname << " " << pid << ": request handled, writing response"
|
c@125
|
612 << endl;
|
c@125
|
613 }
|
c@125
|
614
|
c@125
|
615 writeResponse(format, response);
|
c@125
|
616
|
c@125
|
617 if (debug) {
|
c@125
|
618 cerr << myname << " " << pid << ": response written" << endl;
|
c@125
|
619 }
|
c@125
|
620
|
c@125
|
621 if (request.type == RRType::Finish) {
|
c@125
|
622 auto h = mapper.pluginToHandle(request.finishRequest.plugin);
|
c@125
|
623 if (debug) {
|
c@125
|
624 cerr << myname << " " << pid << ": deleting the plugin with handle " << h << endl;
|
c@125
|
625 }
|
c@125
|
626 mapper.removePlugin(h);
|
c@125
|
627 delete request.finishRequest.plugin;
|
c@125
|
628 }
|
c@125
|
629
|
c@125
|
630 } catch (std::exception &e) {
|
c@125
|
631
|
c@125
|
632 if (debug) {
|
c@125
|
633 cerr << myname << " " << pid << ": error: " << e.what() << endl;
|
c@125
|
634 }
|
c@125
|
635
|
c@125
|
636 writeException(format, e, request.type);
|
c@125
|
637
|
c@125
|
638 if (format == "capnp") {
|
c@125
|
639 // Don't try to continue; we can't recover from a
|
c@125
|
640 // mangled input stream. However, we can return a
|
c@125
|
641 // successful error code because we are reporting the
|
c@125
|
642 // status in our Capnp output stream instead
|
c@125
|
643 if (debug) {
|
c@125
|
644 cerr << myname << " " << pid << ": not attempting to recover from capnp parse problems, exiting" << endl;
|
c@125
|
645 }
|
c@125
|
646 exit(0);
|
c@125
|
647 }
|
c@125
|
648 }
|
c@125
|
649 }
|
c@125
|
650
|
c@125
|
651 exit(0);
|
c@125
|
652 }
|