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 |
127 |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO"); |
128 |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO"); |
129 |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
130 |
|
LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT"); |
131 |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT"); |
132 |
LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO"); |
133 |
hSocket = -1; |
hSocket = -1; |
189 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount)); |
190 |
} |
} |
191 |
|
|
192 |
|
void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) { |
193 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount)); |
194 |
|
} |
195 |
|
|
196 |
#if HAVE_SQLITE3 |
#if HAVE_SQLITE3 |
197 |
void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) { |
void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) { |
198 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir))); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir))); |
289 |
pSampler->AddVoiceCountListener(&eventHandler); |
pSampler->AddVoiceCountListener(&eventHandler); |
290 |
pSampler->AddStreamCountListener(&eventHandler); |
pSampler->AddStreamCountListener(&eventHandler); |
291 |
pSampler->AddBufferFillListener(&eventHandler); |
pSampler->AddBufferFillListener(&eventHandler); |
292 |
|
pSampler->AddTotalStreamCountListener(&eventHandler); |
293 |
pSampler->AddTotalVoiceCountListener(&eventHandler); |
pSampler->AddTotalVoiceCountListener(&eventHandler); |
294 |
pSampler->AddFxSendCountListener(&eventHandler); |
pSampler->AddFxSendCountListener(&eventHandler); |
295 |
MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler); |
MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler); |
374 |
std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl; |
std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl; |
375 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
376 |
} |
} |
377 |
#else |
#else |
378 |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
if (fcntl(socket, F_SETFL, O_NONBLOCK)) { |
379 |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; |
380 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
545 |
int wsa_lasterror = WSAGetLastError(); |
int wsa_lasterror = WSAGetLastError(); |
546 |
if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later. |
if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later. |
547 |
return false; |
return false; |
548 |
dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror)); |
dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror)); |
549 |
CloseConnection(iter); |
CloseConnection(iter); |
550 |
break; |
break; |
551 |
} |
} |
2353 |
} |
} |
2354 |
|
|
2355 |
/** |
/** |
2356 |
|
* Will be called by the parser to return the current number of all active streams. |
2357 |
|
*/ |
2358 |
|
String LSCPServer::GetTotalStreamCount() { |
2359 |
|
dmsg(2,("LSCPServer: GetTotalStreamCount()\n")); |
2360 |
|
LSCPResultSet result; |
2361 |
|
result.Add(pSampler->GetDiskStreamCount()); |
2362 |
|
return result.Produce(); |
2363 |
|
} |
2364 |
|
|
2365 |
|
/** |
2366 |
* Will be called by the parser to return the current number of all active voices. |
* Will be called by the parser to return the current number of all active voices. |
2367 |
*/ |
*/ |
2368 |
String LSCPServer::GetTotalVoiceCount() { |
String LSCPServer::GetTotalVoiceCount() { |
2400 |
return result.Produce(); |
return result.Produce(); |
2401 |
} |
} |
2402 |
|
|
2403 |
|
String LSCPServer::GetFileInstruments(String Filename) { |
2404 |
|
dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str())); |
2405 |
|
LSCPResultSet result; |
2406 |
|
try { |
2407 |
|
VerifyFile(Filename); |
2408 |
|
} catch (Exception e) { |
2409 |
|
result.Error(e); |
2410 |
|
return result.Produce(); |
2411 |
|
} |
2412 |
|
// try to find a sampler engine that can handle the file |
2413 |
|
bool bFound = false; |
2414 |
|
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2415 |
|
for (int i = 0; !bFound && i < engineTypes.size(); i++) { |
2416 |
|
Engine* pEngine = NULL; |
2417 |
|
try { |
2418 |
|
pEngine = EngineFactory::Create(engineTypes[i]); |
2419 |
|
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2420 |
|
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2421 |
|
if (pManager) { |
2422 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2423 |
|
pManager->GetInstrumentFileContent(Filename); |
2424 |
|
// return the amount of instruments in the file |
2425 |
|
result.Add(IDs.size()); |
2426 |
|
// no more need to ask other engine types |
2427 |
|
bFound = true; |
2428 |
|
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2429 |
|
} catch (Exception e) { |
2430 |
|
// NOOP, as exception is thrown if engine doesn't support file |
2431 |
|
} |
2432 |
|
if (pEngine) EngineFactory::Destroy(pEngine); |
2433 |
|
} |
2434 |
|
|
2435 |
|
if (!bFound) result.Error("Unknown file format"); |
2436 |
|
return result.Produce(); |
2437 |
|
} |
2438 |
|
|
2439 |
|
String LSCPServer::ListFileInstruments(String Filename) { |
2440 |
|
dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str())); |
2441 |
|
LSCPResultSet result; |
2442 |
|
try { |
2443 |
|
VerifyFile(Filename); |
2444 |
|
} catch (Exception e) { |
2445 |
|
result.Error(e); |
2446 |
|
return result.Produce(); |
2447 |
|
} |
2448 |
|
// try to find a sampler engine that can handle the file |
2449 |
|
bool bFound = false; |
2450 |
|
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2451 |
|
for (int i = 0; !bFound && i < engineTypes.size(); i++) { |
2452 |
|
Engine* pEngine = NULL; |
2453 |
|
try { |
2454 |
|
pEngine = EngineFactory::Create(engineTypes[i]); |
2455 |
|
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2456 |
|
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2457 |
|
if (pManager) { |
2458 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2459 |
|
pManager->GetInstrumentFileContent(Filename); |
2460 |
|
// return a list of IDs of the instruments in the file |
2461 |
|
String s; |
2462 |
|
for (int j = 0; j < IDs.size(); j++) { |
2463 |
|
if (s.size()) s += ","; |
2464 |
|
s += ToString(IDs[j].Index); |
2465 |
|
} |
2466 |
|
result.Add(s); |
2467 |
|
// no more need to ask other engine types |
2468 |
|
bFound = true; |
2469 |
|
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2470 |
|
} catch (Exception e) { |
2471 |
|
// NOOP, as exception is thrown if engine doesn't support file |
2472 |
|
} |
2473 |
|
if (pEngine) EngineFactory::Destroy(pEngine); |
2474 |
|
} |
2475 |
|
|
2476 |
|
if (!bFound) result.Error("Unknown file format"); |
2477 |
|
return result.Produce(); |
2478 |
|
} |
2479 |
|
|
2480 |
|
String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) { |
2481 |
|
dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID)); |
2482 |
|
LSCPResultSet result; |
2483 |
|
try { |
2484 |
|
VerifyFile(Filename); |
2485 |
|
} catch (Exception e) { |
2486 |
|
result.Error(e); |
2487 |
|
return result.Produce(); |
2488 |
|
} |
2489 |
|
InstrumentManager::instrument_id_t id; |
2490 |
|
id.FileName = Filename; |
2491 |
|
id.Index = InstrumentID; |
2492 |
|
// try to find a sampler engine that can handle the file |
2493 |
|
bool bFound = false; |
2494 |
|
bool bFatalErr = false; |
2495 |
|
std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes(); |
2496 |
|
for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) { |
2497 |
|
Engine* pEngine = NULL; |
2498 |
|
try { |
2499 |
|
pEngine = EngineFactory::Create(engineTypes[i]); |
2500 |
|
if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine"); |
2501 |
|
InstrumentManager* pManager = pEngine->GetInstrumentManager(); |
2502 |
|
if (pManager) { |
2503 |
|
// check if the instrument index is valid |
2504 |
|
// FIXME: this won't work if an engine only supports parts of the instrument file |
2505 |
|
std::vector<InstrumentManager::instrument_id_t> IDs = |
2506 |
|
pManager->GetInstrumentFileContent(Filename); |
2507 |
|
if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) { |
2508 |
|
std::stringstream ss; |
2509 |
|
ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'"; |
2510 |
|
bFatalErr = true; |
2511 |
|
throw Exception(ss.str()); |
2512 |
|
} |
2513 |
|
// get the info of the requested instrument |
2514 |
|
InstrumentManager::instrument_info_t info = |
2515 |
|
pManager->GetInstrumentInfo(id); |
2516 |
|
// return detailed informations about the file |
2517 |
|
result.Add("NAME", info.InstrumentName); |
2518 |
|
result.Add("FORMAT_FAMILY", engineTypes[i]); |
2519 |
|
result.Add("FORMAT_VERSION", info.FormatVersion); |
2520 |
|
result.Add("PRODUCT", info.Product); |
2521 |
|
result.Add("ARTISTS", info.Artists); |
2522 |
|
// no more need to ask other engine types |
2523 |
|
bFound = true; |
2524 |
|
} else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); |
2525 |
|
} catch (Exception e) { |
2526 |
|
// usually NOOP, as exception is thrown if engine doesn't support file |
2527 |
|
if (bFatalErr) result.Error(e); |
2528 |
|
} |
2529 |
|
if (pEngine) EngineFactory::Destroy(pEngine); |
2530 |
|
} |
2531 |
|
|
2532 |
|
if (!bFound && !bFatalErr) result.Error("Unknown file format"); |
2533 |
|
return result.Produce(); |
2534 |
|
} |
2535 |
|
|
2536 |
|
void LSCPServer::VerifyFile(String Filename) { |
2537 |
|
#if WIN32 |
2538 |
|
WIN32_FIND_DATA win32FileAttributeData; |
2539 |
|
BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData ); |
2540 |
|
if (!res) { |
2541 |
|
std::stringstream ss; |
2542 |
|
ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError(); |
2543 |
|
throw Exception(ss.str()); |
2544 |
|
} |
2545 |
|
if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { |
2546 |
|
throw Exception("Directory is specified"); |
2547 |
|
} |
2548 |
|
#else |
2549 |
|
struct stat statBuf; |
2550 |
|
int res = stat(Filename.c_str(), &statBuf); |
2551 |
|
if (res) { |
2552 |
|
std::stringstream ss; |
2553 |
|
ss << "Fail to stat `" << Filename << "`: " << strerror(errno); |
2554 |
|
throw Exception(ss.str()); |
2555 |
|
} |
2556 |
|
|
2557 |
|
if (S_ISDIR(statBuf.st_mode)) { |
2558 |
|
throw Exception("Directory is specified"); |
2559 |
|
} |
2560 |
|
#endif |
2561 |
|
} |
2562 |
|
|
2563 |
/** |
/** |
2564 |
* 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 |
2565 |
* server for receiving event messages. |
* server for receiving event messages. |