comparison vamp-server/server.cpp @ 105:491a4bc10a01

Merge
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 17 Oct 2016 16:41:00 +0100
parents cd438188e3f9 6fad5154778e
children c996610b8a46
comparison
equal deleted inserted replaced
102:cd438188e3f9 105:491a4bc10a01
11 #include <capnp/serialize.h> 11 #include <capnp/serialize.h>
12 12
13 #include <map> 13 #include <map>
14 #include <set> 14 #include <set>
15 15
16 #include <unistd.h> // getpid for logging
17
16 using namespace std; 18 using namespace std;
17 using namespace piper_vamp; 19 using namespace piper_vamp;
18 using namespace Vamp; 20 using namespace Vamp;
19 21
20 //!!! This could be faster and lighter: 22 //!!! This could be faster and lighter:
21 // - Use Capnp structures directly rather than converting to vamp-support ones 23 // - Use Capnp structures directly rather than converting to vamp-support ones
22 // - Use Vamp C API (vamp.h) directly rather than converting to C++ 24 // - Use Vamp C API (vamp.h) directly rather than converting to C++
23 //!!! Doing the above for process() and finish() alone would be a good start 25 //!!! Doing the above for process() and finish() alone would be a good start
24 26
27 static int pid = getpid();
28
25 void usage() 29 void usage()
26 { 30 {
27 string myname = "piper-vamp-server"; 31 string myname = "piper-vamp-server";
28 cerr << "\n" << myname << 32 cerr << "\n" << myname <<
29 ": Load and run Vamp plugins in response to messages from stdin\n\n" 33 ": Load and run Vamp plugins in response to messages from stdin\n\n"
178 182
179 case RRType::Load: 183 case RRType::Load:
180 response.loadResponse = LoaderRequests().loadPlugin(request.loadRequest); 184 response.loadResponse = LoaderRequests().loadPlugin(request.loadRequest);
181 if (response.loadResponse.plugin != nullptr) { 185 if (response.loadResponse.plugin != nullptr) {
182 mapper.addPlugin(response.loadResponse.plugin); 186 mapper.addPlugin(response.loadResponse.plugin);
183 cerr << "loaded plugin, handle = " << mapper.pluginToHandle(response.loadResponse.plugin) << endl; 187 cerr << "piper-vamp-server " << pid << ": loaded plugin, handle = " << mapper.pluginToHandle(response.loadResponse.plugin) << endl;
184 response.success = true; 188 response.success = true;
185 } 189 }
186 break; 190 break;
187 191
188 case RRType::Configure: 192 case RRType::Configure:
260 } 264 }
261 265
262 return response; 266 return response;
263 } 267 }
264 268
265 int main(int argc, char **argv) 269 int main(int argc, char **)
266 { 270 {
267 if (argc != 1) { 271 if (argc != 1) {
268 usage(); 272 usage();
269 } 273 }
270 274
274 278
275 try { 279 try {
276 280
277 request = readRequestCapnp(); 281 request = readRequestCapnp();
278 282
279 cerr << "piper-vamp-server: request received, of type " 283 cerr << "piper-vamp-server " << pid << ": request received, of type "
280 << int(request.type) 284 << int(request.type)
281 << endl; 285 << endl;
282 286
283 // NotValid without an exception indicates EOF: 287 // NotValid without an exception indicates EOF:
284 if (request.type == RRType::NotValid) { 288 if (request.type == RRType::NotValid) {
285 cerr << "piper-vamp-server: eof reached, exiting" << endl; 289 cerr << "piper-vamp-server " << pid << ": eof reached, exiting" << endl;
286 break; 290 break;
287 } 291 }
288 292
289 RequestOrResponse response = handleRequest(request); 293 RequestOrResponse response = handleRequest(request);
290 response.id = request.id; 294 response.id = request.id;
291 295
292 cerr << "piper-vamp-server: request handled, writing response" 296 cerr << "piper-vamp-server " << pid << ": request handled, writing response"
293 << endl; 297 << endl;
294 298
295 writeResponseCapnp(response); 299 writeResponseCapnp(response);
296 300
297 cerr << "piper-vamp-server: response written" << endl; 301 cerr << "piper-vamp-server " << pid << ": response written" << endl;
298 302
299 if (request.type == RRType::Finish) { 303 if (request.type == RRType::Finish) {
300 auto h = mapper.pluginToHandle(request.finishRequest.plugin); 304 auto h = mapper.pluginToHandle(request.finishRequest.plugin);
301 cerr << "deleting the plugin with handle " << h << endl; 305 cerr << "piper-vamp-server " << pid << ": deleting the plugin with handle " << h << endl;
302 mapper.removePlugin(h); 306 mapper.removePlugin(h);
303 delete request.finishRequest.plugin; 307 delete request.finishRequest.plugin;
304 } 308 }
305 309
306 } catch (std::exception &e) { 310 } catch (std::exception &e) {
307 311
308 cerr << "piper-vamp-server: error: " << e.what() << endl; 312 cerr << "piper-vamp-server " << pid << ": error: " << e.what() << endl;
309 313
310 writeExceptionCapnp(e, request.type); 314 writeExceptionCapnp(e, request.type);
311 315
312 exit(1); 316 exit(1);
313 } 317 }