--- linuxsampler/trunk/src/network/lscpserver.cpp 2008/04/22 08:52:54 1724 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2009/05/10 09:31:51 1897 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 - 2008 Christian Schoenebeck * + * Copyright (C) 2005 - 2009 Christian Schoenebeck * * * * This library is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -22,7 +22,9 @@ ***************************************************************************/ #include +#include +#include "../common/File.h" #include "lscpserver.h" #include "lscpresultset.h" #include "lscpevent.h" @@ -42,6 +44,7 @@ #include "../drivers/audio/AudioOutputDeviceFactory.h" #include "../drivers/midi/MidiInputDeviceFactory.h" +namespace LinuxSampler { /** * Returns a copy of the given string where all special characters are @@ -138,6 +141,8 @@ } LSCPServer::~LSCPServer() { + CloseAllConnections(); + InstrumentManager::StopBackgroundThread(); #if defined(WIN32) if (hSocket >= 0) closesocket(hSocket); #else @@ -332,6 +337,24 @@ } #endif // HAVE_SQLITE3 +void LSCPServer::RemoveListeners() { + pSampler->RemoveChannelCountListener(&eventHandler); + pSampler->RemoveAudioDeviceCountListener(&eventHandler); + pSampler->RemoveMidiDeviceCountListener(&eventHandler); + pSampler->RemoveVoiceCountListener(&eventHandler); + pSampler->RemoveStreamCountListener(&eventHandler); + pSampler->RemoveBufferFillListener(&eventHandler); + pSampler->RemoveTotalStreamCountListener(&eventHandler); + pSampler->RemoveTotalVoiceCountListener(&eventHandler); + pSampler->RemoveFxSendCountListener(&eventHandler); + MidiInstrumentMapper::RemoveMidiInstrumentCountListener(&eventHandler); + MidiInstrumentMapper::RemoveMidiInstrumentInfoListener(&eventHandler); + MidiInstrumentMapper::RemoveMidiInstrumentMapCountListener(&eventHandler); + MidiInstrumentMapper::RemoveMidiInstrumentMapInfoListener(&eventHandler); +#if HAVE_SQLITE3 + InstrumentsDb::GetInstrumentsDb()->RemoveInstrumentsDbListener(&dbInstrumentsEventHandler); +#endif +} /** * Blocks the calling thread until the LSCP Server is initialized and @@ -424,13 +447,13 @@ std::set::iterator itEnd = engineChannels.end(); for (; itEngineChannel != itEnd; ++itEngineChannel) { if ((*itEngineChannel)->StatusChanged()) { - SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->iSamplerChannelIndex)); + SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_info, (*itEngineChannel)->GetSamplerChannel()->Index())); } for (int i = 0; i < (*itEngineChannel)->GetFxSendCount(); i++) { FxSend* fxs = (*itEngineChannel)->GetFxSend(i); if(fxs != NULL && fxs->IsInfoChanged()) { - int chn = (*itEngineChannel)->iSamplerChannelIndex; + int chn = (*itEngineChannel)->GetSamplerChannel()->Index(); LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_fx_send_info, chn, fxs->Id())); fxs->SetInfoChanged(false); } @@ -505,7 +528,7 @@ int retval = select(maxSessions+1, &selectSet, NULL, NULL, &timeout); - if (retval == 0) + if (retval == 0 || (retval == -1 && errno == EINTR)) continue; //Nothing try again if (retval == -1) { std::cerr << "LSCPServer: Socket select error." << std::endl; @@ -532,6 +555,13 @@ exit(EXIT_FAILURE); } #else + struct linger linger; + linger.l_onoff = 1; + linger.l_linger = 0; + if(setsockopt(socket, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger))) { + std::cerr << "LSCPServer: Failed to set SO_LINGER\n"; + } + if (fcntl(socket, F_SETFL, O_NONBLOCK)) { std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl; exit(EXIT_FAILURE); @@ -603,6 +633,14 @@ NotifyMutex.Unlock(); } +void LSCPServer::CloseAllConnections() { + std::vector::iterator iter = Sessions.begin(); + while(iter != Sessions.end()) { + CloseConnection(iter); + iter = Sessions.begin(); + } +} + void LSCPServer::LockRTNotify() { RTNotifyMutex.Lock(); } @@ -1133,10 +1171,7 @@ dmsg(2,("LSCPServer: GetVoiceCount(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine loaded on sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); result.Add(pEngineChannel->GetEngine()->VoiceCount()); } @@ -1154,10 +1189,7 @@ dmsg(2,("LSCPServer: GetStreamCount(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); result.Add(pEngineChannel->GetEngine()->DiskStreamCount()); } @@ -1175,10 +1207,7 @@ dmsg(2,("LSCPServer: GetBufferFill(ResponseType=%d, SamplerChannel=%d)\n", ResponseType, uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (!pEngineChannel->GetEngine()) throw Exception("No audio output device connected to sampler channel"); if (!pEngineChannel->GetEngine()->DiskStreamSupported()) result.Add("NA"); else { @@ -1266,6 +1295,7 @@ for (;iter != parameters.end(); iter++) { if (s != "") s += ","; s += iter->first; + delete iter->second; } result.Add("PARAMETERS", s); } @@ -1290,6 +1320,7 @@ for (;iter != parameters.end(); iter++) { if (s != "") s += ","; s += iter->first; + delete iter->second; } result.Add("PARAMETERS", s); } @@ -1320,6 +1351,7 @@ if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin); if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); + delete pParameter; } catch (Exception e) { result.Error(e); @@ -1347,6 +1379,7 @@ if (oRangeMin) result.Add("RANGE_MIN", *oRangeMin); if (oRangeMax) result.Add("RANGE_MAX", *oRangeMax); if (oPossibilities) result.Add("POSSIBILITIES", *oPossibilities); + delete pParameter; } catch (Exception e) { result.Error(e); @@ -1813,7 +1846,6 @@ pDevice = pSampler->CreateMidiInputDevice(MidiInputDriver, params); // Make it with at least one initial port. std::map parameters = pDevice->DeviceParameters(); - parameters["PORTS"]->SetValue("1"); } // Must have a device... if (pDevice == NULL) @@ -1856,10 +1888,7 @@ dmsg(2,("LSCPServer: SetVolume(Volume=%f, SamplerChannel=%d)\n", dVolume, uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); pEngineChannel->Volume(dVolume); } catch (Exception e) { @@ -1875,11 +1904,7 @@ dmsg(2,("LSCPServer: SetChannelMute(bMute=%d,uiSamplerChannel=%d)\n",bMute,uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if(!bMute) pEngineChannel->SetMute((HasSoloChannel() && !pEngineChannel->GetSolo()) ? -1 : 0); else pEngineChannel->SetMute(1); @@ -1896,11 +1921,7 @@ dmsg(2,("LSCPServer: SetChannelSolo(bSolo=%d,uiSamplerChannel=%d)\n",bSolo,uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); bool oldSolo = pEngineChannel->GetSolo(); bool hadSoloChannel = HasSoloChannel(); @@ -2020,7 +2041,7 @@ dmsg(2,("LSCPServer: GetMidiInstrumentMappings()\n")); LSCPResultSet result; try { - result.Add(MidiInstrumentMapper::Entries(MidiMapID).size()); + result.Add(MidiInstrumentMapper::GetInstrumentCount(MidiMapID)); } catch (Exception e) { result.Error(e); } @@ -2031,14 +2052,11 @@ String LSCPServer::GetAllMidiInstrumentMappings() { dmsg(2,("LSCPServer: GetAllMidiInstrumentMappings()\n")); LSCPResultSet result; - std::vector maps = MidiInstrumentMapper::Maps(); - int totalMappings = 0; - for (int i = 0; i < maps.size(); i++) { - try { - totalMappings += MidiInstrumentMapper::Entries(maps[i]).size(); - } catch (Exception e) { /*NOOP*/ } + try { + result.Add(MidiInstrumentMapper::GetInstrumentCount()); + } catch (Exception e) { + result.Error(e); } - result.Add(totalMappings); return result.Produce(); } @@ -2046,56 +2064,46 @@ dmsg(2,("LSCPServer: GetMidiIstrumentMapping()\n")); LSCPResultSet result; try { - midi_prog_index_t idx; - idx.midi_bank_msb = (MidiBank >> 7) & 0x7f; - idx.midi_bank_lsb = MidiBank & 0x7f; - idx.midi_prog = MidiProg; - - std::map mappings = MidiInstrumentMapper::Entries(MidiMapID); - std::map::iterator iter = mappings.find(idx); - if (iter == mappings.end()) result.Error("there is no map entry with that index"); - else { // found - - // convert the filename into the correct encoding as defined for LSCP - // (especially in terms of special characters -> escape sequences) + MidiInstrumentMapper::entry_t entry = MidiInstrumentMapper::GetEntry(MidiMapID, MidiBank, MidiProg); + // convert the filename into the correct encoding as defined for LSCP + // (especially in terms of special characters -> escape sequences) #if WIN32 - const String instrumentFileName = Path::fromWindows(iter->second.InstrumentFile).toLscp(); + const String instrumentFileName = Path::fromWindows(entry.InstrumentFile).toLscp(); #else - // assuming POSIX - const String instrumentFileName = Path::fromPosix(iter->second.InstrumentFile).toLscp(); + // assuming POSIX + const String instrumentFileName = Path::fromPosix(entry.InstrumentFile).toLscp(); #endif - result.Add("NAME", _escapeLscpResponse(iter->second.Name)); - result.Add("ENGINE_NAME", iter->second.EngineName); - result.Add("INSTRUMENT_FILE", instrumentFileName); - result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex); - String instrumentName; - Engine* pEngine = EngineFactory::Create(iter->second.EngineName); - if (pEngine) { - if (pEngine->GetInstrumentManager()) { - InstrumentManager::instrument_id_t instrID; - instrID.FileName = iter->second.InstrumentFile; - instrID.Index = iter->second.InstrumentIndex; - instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID); - } - EngineFactory::Destroy(pEngine); + result.Add("NAME", _escapeLscpResponse(entry.Name)); + result.Add("ENGINE_NAME", entry.EngineName); + result.Add("INSTRUMENT_FILE", instrumentFileName); + result.Add("INSTRUMENT_NR", (int) entry.InstrumentIndex); + String instrumentName; + Engine* pEngine = EngineFactory::Create(entry.EngineName); + if (pEngine) { + if (pEngine->GetInstrumentManager()) { + InstrumentManager::instrument_id_t instrID; + instrID.FileName = entry.InstrumentFile; + instrID.Index = entry.InstrumentIndex; + instrumentName = pEngine->GetInstrumentManager()->GetInstrumentName(instrID); } - result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName)); - switch (iter->second.LoadMode) { - case MidiInstrumentMapper::ON_DEMAND: - result.Add("LOAD_MODE", "ON_DEMAND"); - break; - case MidiInstrumentMapper::ON_DEMAND_HOLD: - result.Add("LOAD_MODE", "ON_DEMAND_HOLD"); - break; - case MidiInstrumentMapper::PERSISTENT: - result.Add("LOAD_MODE", "PERSISTENT"); - break; - default: - throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!"); - } - result.Add("VOLUME", iter->second.Volume); + EngineFactory::Destroy(pEngine); } + result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName)); + switch (entry.LoadMode) { + case MidiInstrumentMapper::ON_DEMAND: + result.Add("LOAD_MODE", "ON_DEMAND"); + break; + case MidiInstrumentMapper::ON_DEMAND_HOLD: + result.Add("LOAD_MODE", "ON_DEMAND_HOLD"); + break; + case MidiInstrumentMapper::PERSISTENT: + result.Add("LOAD_MODE", "PERSISTENT"); + break; + default: + throw Exception("entry reflects invalid LOAD_MODE, consider this as a bug!"); + } + result.Add("VOLUME", entry.Volume); } catch (Exception e) { result.Error(e); } @@ -2266,11 +2274,7 @@ dmsg(2,("LSCPServer: SetChannelMap()\n")); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (MidiMapID == -1) pEngineChannel->SetMidiInstrumentMapToNone(); else if (MidiMapID == -2) pEngineChannel->SetMidiInstrumentMapToDefault(); @@ -2449,10 +2453,7 @@ dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel"); Engine* pEngine = pEngineChannel->GetEngine(); InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager(); @@ -2467,6 +2468,44 @@ return result.Produce(); } +String LSCPServer::SendChannelMidiData(String MidiMsg, uint uiSamplerChannel, uint Arg1, uint Arg2) { + dmsg(2,("LSCPServer: SendChannelMidiData(MidiMsg=%s,uiSamplerChannel=%d,Arg1=%d,Arg2=%d)\n", MidiMsg.c_str(), uiSamplerChannel, Arg1, Arg2)); + LSCPResultSet result; + try { + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); + + if (Arg1 > 127 || Arg2 > 127) { + throw Exception("Invalid MIDI message"); + } + + VirtualMidiDevice* pMidiDevice = NULL; + std::vector::iterator iter = eventHandler.channelMidiListeners.begin(); + for (; iter != eventHandler.channelMidiListeners.end(); ++iter) { + if ((*iter).pEngineChannel == pEngineChannel) { + pMidiDevice = (*iter).pMidiListener; + break; + } + } + + if(pMidiDevice == NULL) throw Exception("Couldn't find virtual MIDI device"); + + if (MidiMsg == "NOTE_ON") { + pMidiDevice->SendNoteOnToDevice(Arg1, Arg2); + bool b = pMidiDevice->SendNoteOnToSampler(Arg1, Arg2); + if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2)); + } else if (MidiMsg == "NOTE_OFF") { + pMidiDevice->SendNoteOffToDevice(Arg1, Arg2); + bool b = pMidiDevice->SendNoteOffToSampler(Arg1, Arg2); + if (!b) throw Exception("MIDI event failed: " + MidiMsg + " " + ToString(Arg1) + " " + ToString(Arg2)); + } else { + throw Exception("Unknown MIDI message type: " + MidiMsg); + } + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + /** * Will be called by the parser to reset a particular sampler channel. */ @@ -2474,10 +2513,7 @@ dmsg(2,("LSCPServer: ResetChannel(SamplerChannel=%d)\n", uiSamplerChannel)); LSCPResultSet result; try { - SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel); - if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel)); - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel"); + EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel); pEngineChannel->Reset(); } catch (Exception e) { @@ -2543,7 +2579,79 @@ String LSCPServer::GetTotalVoiceCountMax() { dmsg(2,("LSCPServer: GetTotalVoiceCountMax()\n")); LSCPResultSet result; - result.Add(EngineFactory::EngineInstances().size() * CONFIG_MAX_VOICES); + result.Add(EngineFactory::EngineInstances().size() * GLOBAL_MAX_VOICES); + return result.Produce(); +} + +/** + * Will be called by the parser to return the sampler global maximum + * allowed number of voices. + */ +String LSCPServer::GetGlobalMaxVoices() { + dmsg(2,("LSCPServer: GetGlobalMaxVoices()\n")); + LSCPResultSet result; + result.Add(GLOBAL_MAX_VOICES); + return result.Produce(); +} + +/** + * Will be called by the parser to set the sampler global maximum number of + * voices. + */ +String LSCPServer::SetGlobalMaxVoices(int iVoices) { + dmsg(2,("LSCPServer: SetGlobalMaxVoices(%d)\n", iVoices)); + LSCPResultSet result; + try { + if (iVoices < 1) throw Exception("Maximum voices may not be less than 1"); + GLOBAL_MAX_VOICES = iVoices; // see common/global_private.cpp + const std::set& engines = EngineFactory::EngineInstances(); + if (engines.size() > 0) { + std::set::iterator iter = engines.begin(); + std::set::iterator end = engines.end(); + for (; iter != end; ++iter) { + (*iter)->SetMaxVoices(iVoices); + } + } + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "VOICES", GLOBAL_MAX_VOICES)); + } catch (Exception e) { + result.Error(e); + } + return result.Produce(); +} + +/** + * Will be called by the parser to return the sampler global maximum + * allowed number of disk streams. + */ +String LSCPServer::GetGlobalMaxStreams() { + dmsg(2,("LSCPServer: GetGlobalMaxStreams()\n")); + LSCPResultSet result; + result.Add(GLOBAL_MAX_STREAMS); + return result.Produce(); +} + +/** + * Will be called by the parser to set the sampler global maximum number of + * disk streams. + */ +String LSCPServer::SetGlobalMaxStreams(int iStreams) { + dmsg(2,("LSCPServer: SetGlobalMaxStreams(%d)\n", iStreams)); + LSCPResultSet result; + try { + if (iStreams < 0) throw Exception("Maximum disk streams may not be negative"); + GLOBAL_MAX_STREAMS = iStreams; // see common/global_private.cpp + const std::set& engines = EngineFactory::EngineInstances(); + if (engines.size() > 0) { + std::set::iterator iter = engines.begin(); + std::set::iterator end = engines.end(); + for (; iter != end; ++iter) { + (*iter)->SetMaxDiskStreams(iStreams); + } + } + LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_global_info, "STREAMS", GLOBAL_MAX_STREAMS)); + } catch (Exception e) { + result.Error(e); + } return result.Produce(); } @@ -2684,6 +2792,26 @@ result.Add("FORMAT_VERSION", info.FormatVersion); result.Add("PRODUCT", info.Product); result.Add("ARTISTS", info.Artists); + + std::stringstream ss; + bool b = false; + for (int i = 0; i < 128; i++) { + if (info.KeyBindings[i]) { + if (b) ss << ','; + ss << i; b = true; + } + } + result.Add("KEY_BINDINGS", ss.str()); + + b = false; + std::stringstream ss2; + for (int i = 0; i < 128; i++) { + if (info.KeySwitchBindings[i]) { + if (b) ss2 << ','; + ss2 << i; b = true; + } + } + result.Add("KEYSWITCH_BINDINGS", ss2.str()); // no more need to ask other engine types bFound = true; } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str())); @@ -2711,17 +2839,9 @@ throw Exception("Directory is specified"); } #else - struct stat statBuf; - int res = stat(Filename.c_str(), &statBuf); - if (res) { - std::stringstream ss; - ss << "Fail to stat `" << Filename << "`: " << strerror(errno); - throw Exception(ss.str()); - } - - if (S_ISDIR(statBuf.st_mode)) { - throw Exception("Directory is specified"); - } + File f(Filename); + if(!f.Exist()) throw Exception(f.GetErrorMsg()); + if (f.IsDirectory()) throw Exception("Directory is specified"); #endif } @@ -2916,19 +3036,19 @@ return result.Produce(); } -String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground) { - dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground)); +String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground, bool insDir) { + dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d,insDir=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground, insDir)); LSCPResultSet result; #if HAVE_SQLITE3 try { int id; InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb(); if (ScanMode.compare("RECURSIVE") == 0) { - id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground); + id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground, insDir); } else if (ScanMode.compare("NON_RECURSIVE") == 0) { - id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground); + id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground, insDir); } else if (ScanMode.compare("FLAT") == 0) { - id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground); + id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground, insDir); } else { throw Exception("Unknown scan mode: " + ScanMode); } @@ -3104,6 +3224,44 @@ return result.Produce(); } +String LSCPServer::SetDbInstrumentFilePath(String OldPath, String NewPath) { + dmsg(2,("LSCPServer: SetDbInstrumentFilePath(OldPath=%s,NewPath=%s)\n", OldPath.c_str(), NewPath.c_str())); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + InstrumentsDb::GetInstrumentsDb()->SetInstrumentFilePath(OldPath, NewPath); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + +String LSCPServer::FindLostDbInstrumentFiles() { + dmsg(2,("LSCPServer: FindLostDbInstrumentFiles()\n")); + LSCPResultSet result; +#if HAVE_SQLITE3 + try { + String list; + StringListPtr pLostFiles = InstrumentsDb::GetInstrumentsDb()->FindLostInstrumentFiles(); + + for (int i = 0; i < pLostFiles->size(); i++) { + if (list != "") list += ","; + list += "'" + pLostFiles->at(i) + "'"; + } + + result.Add(list); + } catch (Exception e) { + result.Error(e); + } +#else + result.Error(String(DOESNT_HAVE_SQLITE3), 0); +#endif + return result.Produce(); +} + String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map Parameters, bool Recursive) { dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str())); LSCPResultSet result; @@ -3234,3 +3392,5 @@ } return result.Produce(); } + +}