/[svn]/linuxsampler/trunk/src/network/lscpserver.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/network/lscpserver.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1481 by senoner, Wed Nov 14 23:42:15 2007 UTC revision 1713 by persson, Thu Mar 6 20:42:22 2008 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6   *   Copyright (C) 2005 - 2007 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2008 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 21  Line 21 
21   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
22   ***************************************************************************/   ***************************************************************************/
23    
24    #include <algorithm>
25    
26  #include "lscpserver.h"  #include "lscpserver.h"
27  #include "lscpresultset.h"  #include "lscpresultset.h"
28  #include "lscpevent.h"  #include "lscpevent.h"
29    
30  #if defined(WIN32)  #if defined(WIN32)
31    #include <windows.h>
32  #else  #else
33  #include <fcntl.h>  #include <fcntl.h>
34  #endif  #endif
# Line 100  Mutex LSCPServer::NotifyBufferMutex = Mu Line 103  Mutex LSCPServer::NotifyBufferMutex = Mu
103  Mutex LSCPServer::SubscriptionMutex = Mutex();  Mutex LSCPServer::SubscriptionMutex = Mutex();
104  Mutex LSCPServer::RTNotifyMutex = Mutex();  Mutex LSCPServer::RTNotifyMutex = Mutex();
105    
106  LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4) {  LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4), eventHandler(this) {
107      SocketAddress.sin_family      = AF_INET;      SocketAddress.sin_family      = AF_INET;
108      SocketAddress.sin_addr.s_addr = addr;      SocketAddress.sin_addr.s_addr = addr;
109      SocketAddress.sin_port        = port;      SocketAddress.sin_port        = port;
# Line 126  LSCPServer::LSCPServer(Sampler* pSampler Line 129  LSCPServer::LSCPServer(Sampler* pSampler
129      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");
130      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");
131      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
132        LSCPEvent::RegisterEvent(LSCPEvent::event_total_stream_count, "TOTAL_STREAM_COUNT");
133      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
134      LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");
135        LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI");
136        LSCPEvent::RegisterEvent(LSCPEvent::event_device_midi, "DEVICE_MIDI");
137      hSocket = -1;      hSocket = -1;
138  }  }
139    
# Line 139  LSCPServer::~LSCPServer() { Line 145  LSCPServer::~LSCPServer() {
145  #endif  #endif
146  }  }
147    
148    LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) {
149        this->pParent = pParent;
150    }
151    
152    LSCPServer::EventHandler::~EventHandler() {
153        std::vector<midi_listener_entry> l = channelMidiListeners;
154        channelMidiListeners.clear();
155        for (int i = 0; i < l.size(); i++)
156            delete l[i].pMidiListener;
157    }
158    
159  void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {  void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {
160      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));
161  }  }
162    
163    void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) {
164        pChannel->AddEngineChangeListener(this);
165    }
166    
167    void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) {
168        if (!pChannel->GetEngineChannel()) return;
169        EngineToBeChanged(pChannel->Index());
170    }
171    
172    void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) {
173        SamplerChannel* pSamplerChannel =
174            pParent->pSampler->GetSamplerChannel(ChannelId);
175        if (!pSamplerChannel) return;
176        EngineChannel* pEngineChannel =
177            pSamplerChannel->GetEngineChannel();
178        if (!pEngineChannel) return;
179        for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) {
180            if ((*iter).pEngineChannel == pEngineChannel) {
181                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
182                pEngineChannel->Disconnect(pMidiListener);
183                channelMidiListeners.erase(iter);
184                delete pMidiListener;
185                return;
186            }
187        }
188    }
189    
190    void LSCPServer::EventHandler::EngineChanged(int ChannelId) {
191        SamplerChannel* pSamplerChannel =
192            pParent->pSampler->GetSamplerChannel(ChannelId);
193        if (!pSamplerChannel) return;
194        EngineChannel* pEngineChannel =
195            pSamplerChannel->GetEngineChannel();
196        if (!pEngineChannel) return;
197        VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
198        pEngineChannel->Connect(pMidiListener);
199        midi_listener_entry entry = {
200            pSamplerChannel, pEngineChannel, pMidiListener
201        };
202        channelMidiListeners.push_back(entry);
203    }
204    
205  void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {  void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {
206      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));
207  }  }
# Line 151  void LSCPServer::EventHandler::MidiDevic Line 210  void LSCPServer::EventHandler::MidiDevic
210      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_device_count, NewCount));
211  }  }
212    
213    void LSCPServer::EventHandler::MidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
214        pDevice->RemoveMidiPortCountListener(this);
215        for (int i = 0; i < pDevice->PortCount(); ++i)
216            MidiPortToBeRemoved(pDevice->GetPort(i));
217    }
218    
219    void LSCPServer::EventHandler::MidiDeviceCreated(MidiInputDevice* pDevice) {
220        pDevice->AddMidiPortCountListener(this);
221        for (int i = 0; i < pDevice->PortCount(); ++i)
222            MidiPortAdded(pDevice->GetPort(i));
223    }
224    
225    void LSCPServer::EventHandler::MidiPortCountChanged(int NewCount) {
226        // yet unused
227    }
228    
229    void LSCPServer::EventHandler::MidiPortToBeRemoved(MidiInputPort* pPort) {
230        for (std::vector<device_midi_listener_entry>::iterator iter = deviceMidiListeners.begin(); iter != deviceMidiListeners.end(); ++iter) {
231            if ((*iter).pPort == pPort) {
232                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
233                pPort->Disconnect(pMidiListener);
234                deviceMidiListeners.erase(iter);
235                delete pMidiListener;
236                return;
237            }
238        }
239    }
240    
241    void LSCPServer::EventHandler::MidiPortAdded(MidiInputPort* pPort) {
242        // find out the device ID
243        std::map<uint, MidiInputDevice*> devices =
244            pParent->pSampler->GetMidiInputDevices();
245        for (
246            std::map<uint, MidiInputDevice*>::iterator iter = devices.begin();
247            iter != devices.end(); ++iter
248        ) {
249            if (iter->second == pPort->GetDevice()) { // found
250                VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
251                pPort->Connect(pMidiListener);
252                device_midi_listener_entry entry = {
253                    pPort, pMidiListener, iter->first
254                };
255                deviceMidiListeners.push_back(entry);
256                return;
257            }
258        }
259    }
260    
261  void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {  void LSCPServer::EventHandler::MidiInstrumentCountChanged(int MapId, int NewCount) {
262      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, MapId, NewCount));
263  }  }
# Line 187  void LSCPServer::EventHandler::TotalVoic Line 294  void LSCPServer::EventHandler::TotalVoic
294      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));
295  }  }
296    
297    void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) {
298        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount));
299    }
300    
301  #if HAVE_SQLITE3  #if HAVE_SQLITE3
302  void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {  void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {
303      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));
# Line 283  int LSCPServer::Main() { Line 394  int LSCPServer::Main() {
394      pSampler->AddVoiceCountListener(&eventHandler);      pSampler->AddVoiceCountListener(&eventHandler);
395      pSampler->AddStreamCountListener(&eventHandler);      pSampler->AddStreamCountListener(&eventHandler);
396      pSampler->AddBufferFillListener(&eventHandler);      pSampler->AddBufferFillListener(&eventHandler);
397        pSampler->AddTotalStreamCountListener(&eventHandler);
398      pSampler->AddTotalVoiceCountListener(&eventHandler);      pSampler->AddTotalVoiceCountListener(&eventHandler);
399      pSampler->AddFxSendCountListener(&eventHandler);      pSampler->AddFxSendCountListener(&eventHandler);
400      MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);      MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);
# Line 302  int LSCPServer::Main() { Line 414  int LSCPServer::Main() {
414      timeval timeout;      timeval timeout;
415    
416      while (true) {      while (true) {
417            #if CONFIG_PTHREAD_TESTCANCEL
418                    TestCancel();
419            #endif
420          // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers          // check if some engine channel's parameter / status changed, if so notify the respective LSCP event subscribers
421          {          {
422              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
# Line 323  int LSCPServer::Main() { Line 438  int LSCPServer::Main() {
438              }              }
439          }          }
440    
441            // check if MIDI data arrived on some engine channel
442            for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) {
443                const EventHandler::midi_listener_entry entry =
444                    eventHandler.channelMidiListeners[i];
445                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
446                if (pMidiListener->NotesChanged()) {
447                    for (int iNote = 0; iNote < 128; iNote++) {
448                        if (pMidiListener->NoteChanged(iNote)) {
449                            const bool bActive = pMidiListener->NoteIsActive(iNote);
450                            LSCPServer::SendLSCPNotify(
451                                LSCPEvent(
452                                    LSCPEvent::event_channel_midi,
453                                    entry.pSamplerChannel->Index(),
454                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
455                                    iNote,
456                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
457                                            : pMidiListener->NoteOffVelocity(iNote)
458                                )
459                            );
460                        }
461                    }
462                }
463            }
464    
465            // check if MIDI data arrived on some MIDI device
466            for (int i = 0; i < eventHandler.deviceMidiListeners.size(); ++i) {
467                const EventHandler::device_midi_listener_entry entry =
468                    eventHandler.deviceMidiListeners[i];
469                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
470                if (pMidiListener->NotesChanged()) {
471                    for (int iNote = 0; iNote < 128; iNote++) {
472                        if (pMidiListener->NoteChanged(iNote)) {
473                            const bool bActive = pMidiListener->NoteIsActive(iNote);
474                            LSCPServer::SendLSCPNotify(
475                                LSCPEvent(
476                                    LSCPEvent::event_device_midi,
477                                    entry.uiDeviceID,
478                                    entry.pPort->GetPortNumber(),
479                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
480                                    iNote,
481                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
482                                            : pMidiListener->NoteOffVelocity(iNote)
483                                )
484                            );
485                        }
486                    }
487                }
488            }
489    
490          //Now let's deliver late notifies (if any)          //Now let's deliver late notifies (if any)
491          NotifyBufferMutex.Lock();          NotifyBufferMutex.Lock();
492          for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {          for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
# Line 367  int LSCPServer::Main() { Line 531  int LSCPServer::Main() {
531                    std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;                    std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;
532                    exit(EXIT_FAILURE);                    exit(EXIT_FAILURE);
533                  }                  }
534          #else                    #else
535                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
536                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
537                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
# Line 439  void LSCPServer::CloseConnection( std::v Line 603  void LSCPServer::CloseConnection( std::v
603          NotifyMutex.Unlock();          NotifyMutex.Unlock();
604  }  }
605    
606    void LSCPServer::LockRTNotify() {
607        RTNotifyMutex.Lock();
608    }
609    
610    void LSCPServer::UnlockRTNotify() {
611        RTNotifyMutex.Unlock();
612    }
613    
614  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
615          int subs = 0;          int subs = 0;
616          SubscriptionMutex.Lock();          SubscriptionMutex.Lock();
# Line 538  bool LSCPServer::GetLSCPCommand( std::ve Line 710  bool LSCPServer::GetLSCPCommand( std::ve
710                      int wsa_lasterror = WSAGetLastError();                      int wsa_lasterror = WSAGetLastError();
711                          if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.                          if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.
712                                  return false;                                  return false;
713                          dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));                            dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));
714                          CloseConnection(iter);                          CloseConnection(iter);
715                          break;                          break;
716                  }                  }
# Line 2346  String LSCPServer::GetServerInfo() { Line 2518  String LSCPServer::GetServerInfo() {
2518  }  }
2519    
2520  /**  /**
2521     * Will be called by the parser to return the current number of all active streams.
2522     */
2523    String LSCPServer::GetTotalStreamCount() {
2524        dmsg(2,("LSCPServer: GetTotalStreamCount()\n"));
2525        LSCPResultSet result;
2526        result.Add(pSampler->GetDiskStreamCount());
2527        return result.Produce();
2528    }
2529    
2530    /**
2531   * 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.
2532   */   */
2533  String LSCPServer::GetTotalVoiceCount() {  String LSCPServer::GetTotalVoiceCount() {
# Line 2383  String LSCPServer::SetGlobalVolume(doubl Line 2565  String LSCPServer::SetGlobalVolume(doubl
2565      return result.Produce();      return result.Produce();
2566  }  }
2567    
2568    String LSCPServer::GetFileInstruments(String Filename) {
2569        dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str()));
2570        LSCPResultSet result;
2571        try {
2572            VerifyFile(Filename);
2573        } catch (Exception e) {
2574            result.Error(e);
2575            return result.Produce();
2576        }
2577        // try to find a sampler engine that can handle the file
2578        bool bFound = false;
2579        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2580        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2581            Engine* pEngine = NULL;
2582            try {
2583                pEngine = EngineFactory::Create(engineTypes[i]);
2584                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2585                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2586                if (pManager) {
2587                    std::vector<InstrumentManager::instrument_id_t> IDs =
2588                        pManager->GetInstrumentFileContent(Filename);
2589                    // return the amount of instruments in the file
2590                    result.Add(IDs.size());
2591                    // no more need to ask other engine types
2592                    bFound = true;
2593                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2594            } catch (Exception e) {
2595                // NOOP, as exception is thrown if engine doesn't support file
2596            }
2597            if (pEngine) EngineFactory::Destroy(pEngine);
2598        }
2599    
2600        if (!bFound) result.Error("Unknown file format");
2601        return result.Produce();
2602    }
2603    
2604    String LSCPServer::ListFileInstruments(String Filename) {
2605        dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str()));
2606        LSCPResultSet result;
2607        try {
2608            VerifyFile(Filename);
2609        } catch (Exception e) {
2610            result.Error(e);
2611            return result.Produce();
2612        }
2613        // try to find a sampler engine that can handle the file
2614        bool bFound = false;
2615        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2616        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2617            Engine* pEngine = NULL;
2618            try {
2619                pEngine = EngineFactory::Create(engineTypes[i]);
2620                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2621                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2622                if (pManager) {
2623                    std::vector<InstrumentManager::instrument_id_t> IDs =
2624                        pManager->GetInstrumentFileContent(Filename);
2625                    // return a list of IDs of the instruments in the file
2626                    String s;
2627                    for (int j = 0; j < IDs.size(); j++) {
2628                        if (s.size()) s += ",";
2629                        s += ToString(IDs[j].Index);
2630                    }
2631                    result.Add(s);
2632                    // no more need to ask other engine types
2633                    bFound = true;
2634                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2635            } catch (Exception e) {
2636                // NOOP, as exception is thrown if engine doesn't support file
2637            }
2638            if (pEngine) EngineFactory::Destroy(pEngine);
2639        }
2640    
2641        if (!bFound) result.Error("Unknown file format");
2642        return result.Produce();
2643    }
2644    
2645    String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) {
2646        dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID));
2647        LSCPResultSet result;
2648        try {
2649            VerifyFile(Filename);
2650        } catch (Exception e) {
2651            result.Error(e);
2652            return result.Produce();
2653        }
2654        InstrumentManager::instrument_id_t id;
2655        id.FileName = Filename;
2656        id.Index    = InstrumentID;
2657        // try to find a sampler engine that can handle the file
2658        bool bFound = false;
2659        bool bFatalErr = false;
2660        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2661        for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) {
2662            Engine* pEngine = NULL;
2663            try {
2664                pEngine = EngineFactory::Create(engineTypes[i]);
2665                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2666                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2667                if (pManager) {
2668                    // check if the instrument index is valid
2669                    // FIXME: this won't work if an engine only supports parts of the instrument file
2670                    std::vector<InstrumentManager::instrument_id_t> IDs =
2671                        pManager->GetInstrumentFileContent(Filename);
2672                    if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) {
2673                        std::stringstream ss;
2674                        ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'";
2675                        bFatalErr = true;
2676                        throw Exception(ss.str());
2677                    }
2678                    // get the info of the requested instrument
2679                    InstrumentManager::instrument_info_t info =
2680                        pManager->GetInstrumentInfo(id);
2681                    // return detailed informations about the file
2682                    result.Add("NAME", info.InstrumentName);
2683                    result.Add("FORMAT_FAMILY", engineTypes[i]);
2684                    result.Add("FORMAT_VERSION", info.FormatVersion);
2685                    result.Add("PRODUCT", info.Product);
2686                    result.Add("ARTISTS", info.Artists);
2687                    // no more need to ask other engine types
2688                    bFound = true;
2689                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2690            } catch (Exception e) {
2691                // usually NOOP, as exception is thrown if engine doesn't support file
2692                if (bFatalErr) result.Error(e);
2693            }
2694            if (pEngine) EngineFactory::Destroy(pEngine);
2695        }
2696    
2697        if (!bFound && !bFatalErr) result.Error("Unknown file format");
2698        return result.Produce();
2699    }
2700    
2701    void LSCPServer::VerifyFile(String Filename) {
2702        #if WIN32
2703        WIN32_FIND_DATA win32FileAttributeData;
2704        BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData );
2705        if (!res) {
2706            std::stringstream ss;
2707            ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError();
2708            throw Exception(ss.str());
2709        }
2710        if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
2711            throw Exception("Directory is specified");
2712        }
2713        #else
2714        struct stat statBuf;
2715        int res = stat(Filename.c_str(), &statBuf);
2716        if (res) {
2717            std::stringstream ss;
2718            ss << "Fail to stat `" << Filename << "`: " << strerror(errno);
2719            throw Exception(ss.str());
2720        }
2721    
2722        if (S_ISDIR(statBuf.st_mode)) {
2723            throw Exception("Directory is specified");
2724        }
2725        #endif
2726    }
2727    
2728  /**  /**
2729   * 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
2730   * server for receiving event messages.   * server for receiving event messages.

Legend:
Removed from v.1481  
changed lines
  Added in v.1713

  ViewVC Help
Powered by ViewVC