26 |
#include "lscpevent.h" |
#include "lscpevent.h" |
27 |
|
|
28 |
#if defined(WIN32) |
#if defined(WIN32) |
29 |
|
#include <windows.h> |
30 |
#else |
#else |
31 |
#include <fcntl.h> |
#include <fcntl.h> |
32 |
#endif |
#endif |
368 |
std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl; |
std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl; |
369 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
370 |
} |
} |
371 |
#else |
#else |
372 |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
373 |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
374 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
539 |
int wsa_lasterror = WSAGetLastError(); |
int wsa_lasterror = WSAGetLastError(); |
540 |
if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later. |
if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later. |
541 |
return false; |
return false; |
542 |
dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror)); |
dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror)); |
543 |
CloseConnection(iter); |
CloseConnection(iter); |
544 |
break; |
break; |
545 |
} |
} |
2385 |
} |
} |
2386 |
|
|
2387 |
String LSCPServer::GetFileInstruments(String Filename) { |
String LSCPServer::GetFileInstruments(String Filename) { |
2388 |
|
dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str())); |
2389 |
LSCPResultSet result; |
LSCPResultSet result; |
2390 |
|
try { |
2391 |
|
VerifyFile(Filename); |
2392 |
|
} catch (Exception e) { |
2393 |
|
result.Error(e); |
2394 |
|
return result.Produce(); |
2395 |
|
} |
2396 |
// try to find a sampler engine that can handle the file |
// try to find a sampler engine that can handle the file |
2397 |
bool bFound = false; |
bool bFound = false; |
2398 |
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2415 |
} |
} |
2416 |
if (pEngine) EngineFactory::Destroy(pEngine); |
if (pEngine) EngineFactory::Destroy(pEngine); |
2417 |
} |
} |
2418 |
|
|
2419 |
|
if (!bFound) result.Error("Unknown file format"); |
2420 |
return result.Produce(); |
return result.Produce(); |
2421 |
} |
} |
2422 |
|
|
2423 |
String LSCPServer::ListFileInstruments(String Filename) { |
String LSCPServer::ListFileInstruments(String Filename) { |
2424 |
|
dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str())); |
2425 |
LSCPResultSet result; |
LSCPResultSet result; |
2426 |
|
try { |
2427 |
|
VerifyFile(Filename); |
2428 |
|
} catch (Exception e) { |
2429 |
|
result.Error(e); |
2430 |
|
return result.Produce(); |
2431 |
|
} |
2432 |
// try to find a sampler engine that can handle the file |
// try to find a sampler engine that can handle the file |
2433 |
bool bFound = false; |
bool bFound = false; |
2434 |
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2456 |
} |
} |
2457 |
if (pEngine) EngineFactory::Destroy(pEngine); |
if (pEngine) EngineFactory::Destroy(pEngine); |
2458 |
} |
} |
2459 |
|
|
2460 |
|
if (!bFound) result.Error("Unknown file format"); |
2461 |
return result.Produce(); |
return result.Produce(); |
2462 |
} |
} |
2463 |
|
|
2464 |
String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) { |
String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) { |
2465 |
|
dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID)); |
2466 |
LSCPResultSet result; |
LSCPResultSet result; |
2467 |
|
try { |
2468 |
|
VerifyFile(Filename); |
2469 |
|
} catch (Exception e) { |
2470 |
|
result.Error(e); |
2471 |
|
return result.Produce(); |
2472 |
|
} |
2473 |
InstrumentManager::instrument_id_t id; |
InstrumentManager::instrument_id_t id; |
2474 |
id.FileName = Filename; |
id.FileName = Filename; |
2475 |
id.Index = InstrumentID; |
id.Index = InstrumentID; |
2476 |
// try to find a sampler engine that can handle the file |
// try to find a sampler engine that can handle the file |
2477 |
bool bFound = false; |
bool bFound = false; |
2478 |
|
bool bFatalErr = false; |
2479 |
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2480 |
for (int i = 0; !bFound && i < engineTypes.size(); i++) { |
for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) { |
2481 |
Engine* pEngine = NULL; |
Engine* pEngine = NULL; |
2482 |
try { |
try { |
2483 |
pEngine = EngineFactory::Create(engineTypes[i]); |
pEngine = EngineFactory::Create(engineTypes[i]); |
2484 |
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2485 |
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2486 |
if (pManager) { |
if (pManager) { |
2487 |
|
// check if the instrument index is valid |
2488 |
|
// FIXME: this won't work if an engine only supports parts of the instrument file |
2489 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2490 |
|
pManager->GetInstrumentFileContent(Filename); |
2491 |
|
if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) { |
2492 |
|
std::stringstream ss; |
2493 |
|
ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'"; |
2494 |
|
bFatalErr = true; |
2495 |
|
throw Exception(ss.str()); |
2496 |
|
} |
2497 |
|
// get the info of the requested instrument |
2498 |
InstrumentManager::instrument_info_t info = |
InstrumentManager::instrument_info_t info = |
2499 |
pManager->GetInstrumentInfo(id); |
pManager->GetInstrumentInfo(id); |
2500 |
// return detailed informations about the file |
// return detailed informations about the file |
2501 |
result.Add("NAME", info.InstrumentName); |
result.Add("NAME", info.InstrumentName); |
2502 |
result.Add("FORMAT_NAME", engineTypes[i]); |
result.Add("FORMAT_FAMILY", engineTypes[i]); |
2503 |
result.Add("FORMAT_VERSION", info.FormatVersion); |
result.Add("FORMAT_VERSION", info.FormatVersion); |
2504 |
result.Add("PRODUCT", info.Product); |
result.Add("PRODUCT", info.Product); |
2505 |
result.Add("ARTISTS", info.Artists); |
result.Add("ARTISTS", info.Artists); |
2507 |
bFound = true; |
bFound = true; |
2508 |
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2509 |
} catch (Exception e) { |
} catch (Exception e) { |
2510 |
// NOOP, as exception is thrown if engine doesn't support file |
// usually NOOP, as exception is thrown if engine doesn't support file |
2511 |
|
if (bFatalErr) result.Error(e); |
2512 |
} |
} |
2513 |
if (pEngine) EngineFactory::Destroy(pEngine); |
if (pEngine) EngineFactory::Destroy(pEngine); |
2514 |
} |
} |
2515 |
|
|
2516 |
|
if (!bFound && !bFatalErr) result.Error("Unknown file format"); |
2517 |
return result.Produce(); |
return result.Produce(); |
2518 |
} |
} |
2519 |
|
|
2520 |
|
void LSCPServer::VerifyFile(String Filename) { |
2521 |
|
#if WIN32 |
2522 |
|
WIN32_FIND_DATA win32FileAttributeData; |
2523 |
|
BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData ); |
2524 |
|
if (!res) { |
2525 |
|
std::stringstream ss; |
2526 |
|
ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError(); |
2527 |
|
throw Exception(ss.str()); |
2528 |
|
} |
2529 |
|
if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { |
2530 |
|
throw Exception("Directory is specified"); |
2531 |
|
} |
2532 |
|
#else |
2533 |
|
struct stat statBuf; |
2534 |
|
int res = stat(Filename.c_str(), &statBuf); |
2535 |
|
if (res) { |
2536 |
|
std::stringstream ss; |
2537 |
|
ss << "Fail to stat `" << Filename << "`: " << strerror(errno); |
2538 |
|
throw Exception(ss.str()); |
2539 |
|
} |
2540 |
|
|
2541 |
|
if (S_ISDIR(statBuf.st_mode)) { |
2542 |
|
throw Exception("Directory is specified"); |
2543 |
|
} |
2544 |
|
#endif |
2545 |
|
} |
2546 |
|
|
2547 |
/** |
/** |
2548 |
* Will be called by the parser to subscribe a client (frontend) on the |
* Will be called by the parser to subscribe a client (frontend) on the |
2549 |
* server for receiving event messages. |
* server for receiving event messages. |