/[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 1471 by schoenebeck, Mon Nov 5 13:56:26 2007 UTC revision 1686 by schoenebeck, Thu Feb 14 14:58:50 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 25  Line 25 
25  #include "lscpresultset.h"  #include "lscpresultset.h"
26  #include "lscpevent.h"  #include "lscpevent.h"
27    
28    #if defined(WIN32)
29    #include <windows.h>
30    #else
31  #include <fcntl.h>  #include <fcntl.h>
32    #endif
33    
34  #if ! HAVE_SQLITE3  #if ! HAVE_SQLITE3
35  #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."  #define DOESNT_HAVE_SQLITE3 "No database support. SQLITE3 was not installed when linuxsampler was built."
# Line 97  Mutex LSCPServer::NotifyBufferMutex = Mu Line 101  Mutex LSCPServer::NotifyBufferMutex = Mu
101  Mutex LSCPServer::SubscriptionMutex = Mutex();  Mutex LSCPServer::SubscriptionMutex = Mutex();
102  Mutex LSCPServer::RTNotifyMutex = Mutex();  Mutex LSCPServer::RTNotifyMutex = Mutex();
103    
104  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) {
105      SocketAddress.sin_family      = AF_INET;      SocketAddress.sin_family      = AF_INET;
106      SocketAddress.sin_addr.s_addr = addr;      SocketAddress.sin_addr.s_addr = addr;
107      SocketAddress.sin_port        = port;      SocketAddress.sin_port        = port;
# Line 123  LSCPServer::LSCPServer(Sampler* pSampler Line 127  LSCPServer::LSCPServer(Sampler* pSampler
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        LSCPEvent::RegisterEvent(LSCPEvent::event_channel_midi, "CHANNEL_MIDI");
134      hSocket = -1;      hSocket = -1;
135  }  }
136    
137  LSCPServer::~LSCPServer() {  LSCPServer::~LSCPServer() {
138    #if defined(WIN32)
139        if (hSocket >= 0) closesocket(hSocket);
140    #else
141      if (hSocket >= 0) close(hSocket);      if (hSocket >= 0) close(hSocket);
142    #endif
143    }
144    
145    LSCPServer::EventHandler::EventHandler(LSCPServer* pParent) {
146        this->pParent = pParent;
147    }
148    
149    LSCPServer::EventHandler::~EventHandler() {
150        std::vector<midi_listener_entry> l = channelMidiListeners;
151        channelMidiListeners.clear();
152        for (int i = 0; i < l.size(); i++)
153            delete l[i].pMidiListener;
154  }  }
155    
156  void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {  void LSCPServer::EventHandler::ChannelCountChanged(int NewCount) {
157      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, NewCount));
158  }  }
159    
160    void LSCPServer::EventHandler::ChannelAdded(SamplerChannel* pChannel) {
161        pChannel->AddEngineChangeListener(this);
162    }
163    
164    void LSCPServer::EventHandler::ChannelToBeRemoved(SamplerChannel* pChannel) {
165        if (!pChannel->GetEngineChannel()) return;
166        EngineToBeChanged(pChannel->Index());
167    }
168    
169    void LSCPServer::EventHandler::EngineToBeChanged(int ChannelId) {
170        SamplerChannel* pSamplerChannel =
171            pParent->pSampler->GetSamplerChannel(ChannelId);
172        if (!pSamplerChannel) return;
173        EngineChannel* pEngineChannel =
174            pSamplerChannel->GetEngineChannel();
175        if (!pEngineChannel) return;
176        for (std::vector<midi_listener_entry>::iterator iter = channelMidiListeners.begin(); iter != channelMidiListeners.end(); ++iter) {
177            if ((*iter).pEngineChannel == pEngineChannel) {
178                VirtualMidiDevice* pMidiListener = (*iter).pMidiListener;
179                pEngineChannel->Disconnect(pMidiListener);
180                channelMidiListeners.erase(iter);
181                delete pMidiListener;
182                return;
183            }
184        }
185    }
186    
187    void LSCPServer::EventHandler::EngineChanged(int ChannelId) {
188        SamplerChannel* pSamplerChannel =
189            pParent->pSampler->GetSamplerChannel(ChannelId);
190        if (!pSamplerChannel) return;
191        EngineChannel* pEngineChannel =
192            pSamplerChannel->GetEngineChannel();
193        if (!pEngineChannel) return;
194        VirtualMidiDevice* pMidiListener = new VirtualMidiDevice;
195        pEngineChannel->Connect(pMidiListener);
196        midi_listener_entry entry = {
197            pSamplerChannel, pEngineChannel, pMidiListener
198        };
199        channelMidiListeners.push_back(entry);
200    }
201    
202  void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {  void LSCPServer::EventHandler::AudioDeviceCountChanged(int NewCount) {
203      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_audio_device_count, NewCount));
204  }  }
# Line 180  void LSCPServer::EventHandler::TotalVoic Line 243  void LSCPServer::EventHandler::TotalVoic
243      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_voice_count, NewCount));
244  }  }
245    
246    void LSCPServer::EventHandler::TotalStreamCountChanged(int NewCount) {
247        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_total_stream_count, NewCount));
248    }
249    
250  #if HAVE_SQLITE3  #if HAVE_SQLITE3
251  void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {  void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {
252      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 230  int LSCPServer::WaitUntilInitialized(lon Line 297  int LSCPServer::WaitUntilInitialized(lon
297  }  }
298    
299  int LSCPServer::Main() {  int LSCPServer::Main() {
300            #if defined(WIN32)
301            WSADATA wsaData;
302            int iResult;
303            iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
304            if (iResult != 0) {
305                    std::cerr << "LSCPServer: WSAStartup failed: " << iResult << "\n";
306                    exit(EXIT_FAILURE);
307            }
308            #endif
309      hSocket = socket(AF_INET, SOCK_STREAM, 0);      hSocket = socket(AF_INET, SOCK_STREAM, 0);
310      if (hSocket < 0) {      if (hSocket < 0) {
311          std::cerr << "LSCPServer: Could not create server socket." << std::endl;          std::cerr << "LSCPServer: Could not create server socket." << std::endl;
# Line 243  int LSCPServer::Main() { Line 319  int LSCPServer::Main() {
319              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {              if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
320                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {                  if (trial > LSCP_SERVER_BIND_TIMEOUT) {
321                      std::cerr << "gave up!" << std::endl;                      std::cerr << "gave up!" << std::endl;
322                        #if defined(WIN32)
323                        closesocket(hSocket);
324                        #else
325                      close(hSocket);                      close(hSocket);
326                        #endif
327                      //return -1;                      //return -1;
328                      exit(EXIT_FAILURE);                      exit(EXIT_FAILURE);
329                  }                  }
# Line 263  int LSCPServer::Main() { Line 343  int LSCPServer::Main() {
343      pSampler->AddVoiceCountListener(&eventHandler);      pSampler->AddVoiceCountListener(&eventHandler);
344      pSampler->AddStreamCountListener(&eventHandler);      pSampler->AddStreamCountListener(&eventHandler);
345      pSampler->AddBufferFillListener(&eventHandler);      pSampler->AddBufferFillListener(&eventHandler);
346        pSampler->AddTotalStreamCountListener(&eventHandler);
347      pSampler->AddTotalVoiceCountListener(&eventHandler);      pSampler->AddTotalVoiceCountListener(&eventHandler);
348      pSampler->AddFxSendCountListener(&eventHandler);      pSampler->AddFxSendCountListener(&eventHandler);
349      MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);      MidiInstrumentMapper::AddMidiInstrumentCountListener(&eventHandler);
# Line 282  int LSCPServer::Main() { Line 363  int LSCPServer::Main() {
363      timeval timeout;      timeval timeout;
364    
365      while (true) {      while (true) {
366            #if CONFIG_PTHREAD_TESTCANCEL
367                    TestCancel();
368            #endif
369          // 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
370          {          {
371              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
# Line 303  int LSCPServer::Main() { Line 387  int LSCPServer::Main() {
387              }              }
388          }          }
389    
390            // check if MIDI data arrived on some engine channel
391            for (int i = 0; i < eventHandler.channelMidiListeners.size(); ++i) {
392                const EventHandler::midi_listener_entry entry =
393                    eventHandler.channelMidiListeners[i];
394                VirtualMidiDevice* pMidiListener = entry.pMidiListener;
395                if (pMidiListener->NotesChanged()) {
396                    for (int iNote = 0; iNote < 128; iNote++) {
397                        if (pMidiListener->NoteChanged(iNote)) {
398                            const bool bActive = pMidiListener->NoteIsActive(iNote);
399                            LSCPServer::SendLSCPNotify(
400                                LSCPEvent(
401                                    LSCPEvent::event_channel_midi,
402                                    entry.pSamplerChannel->Index(),
403                                    std::string(bActive ? "NOTE_ON" : "NOTE_OFF"),
404                                    iNote,
405                                    bActive ? pMidiListener->NoteOnVelocity(iNote)
406                                            : pMidiListener->NoteOffVelocity(iNote)
407                                )
408                            );
409                        }
410                    }
411                }
412            }
413    
414          //Now let's deliver late notifies (if any)          //Now let's deliver late notifies (if any)
415          NotifyBufferMutex.Lock();          NotifyBufferMutex.Lock();
416          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 325  int LSCPServer::Main() { Line 433  int LSCPServer::Main() {
433                  continue; //Nothing try again                  continue; //Nothing try again
434          if (retval == -1) {          if (retval == -1) {
435                  std::cerr << "LSCPServer: Socket select error." << std::endl;                  std::cerr << "LSCPServer: Socket select error." << std::endl;
436                    #if defined(WIN32)
437                    closesocket(hSocket);
438                    #else
439                  close(hSocket);                  close(hSocket);
440                    #endif
441                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
442          }          }
443    
# Line 337  int LSCPServer::Main() { Line 449  int LSCPServer::Main() {
449                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
450                  }                  }
451    
452                    #if defined(WIN32)
453                    u_long nonblock_io = 1;
454                    if( ioctlsocket(socket, FIONBIO, &nonblock_io) ) {
455                      std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;
456                      exit(EXIT_FAILURE);
457                    }
458            #else
459                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
460                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
461                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
462                  }                  }
463                    #endif
464    
465                  // Parser initialization                  // Parser initialization
466                  yyparse_param_t yyparse_param;                  yyparse_param_t yyparse_param;
# Line 399  void LSCPServer::CloseConnection( std::v Line 519  void LSCPServer::CloseConnection( std::v
519          NotifyMutex.Lock();          NotifyMutex.Lock();
520          bufferedCommands.erase(socket);          bufferedCommands.erase(socket);
521          bufferedNotifies.erase(socket);          bufferedNotifies.erase(socket);
522            #if defined(WIN32)
523            closesocket(socket);
524            #else
525          close(socket);          close(socket);
526            #endif
527          NotifyMutex.Unlock();          NotifyMutex.Unlock();
528  }  }
529    
530    void LSCPServer::LockRTNotify() {
531        RTNotifyMutex.Lock();
532    }
533    
534    void LSCPServer::UnlockRTNotify() {
535        RTNotifyMutex.Unlock();
536    }
537    
538  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
539          int subs = 0;          int subs = 0;
540          SubscriptionMutex.Lock();          SubscriptionMutex.Lock();
# Line 478  bool LSCPServer::GetLSCPCommand( std::ve Line 610  bool LSCPServer::GetLSCPCommand( std::ve
610          char c;          char c;
611          int i = 0;          int i = 0;
612          while (true) {          while (true) {
613                    #if defined(WIN32)
614                    int result = recv(socket, (char *)&c, 1, 0); //Read one character at a time for now
615                    #else
616                  int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now                  int result = recv(socket, (void *)&c, 1, 0); //Read one character at a time for now
617                    #endif
618                  if (result == 0) { //socket was selected, so 0 here means client has closed the connection                  if (result == 0) { //socket was selected, so 0 here means client has closed the connection
619                          CloseConnection(iter);                          CloseConnection(iter);
620                          break;                          break;
# Line 493  bool LSCPServer::GetLSCPCommand( std::ve Line 629  bool LSCPServer::GetLSCPCommand( std::ve
629                          }                          }
630                          bufferedCommands[socket] += c;                          bufferedCommands[socket] += c;
631                  }                  }
632                    #if defined(WIN32)
633                    if (result == SOCKET_ERROR) {
634                        int wsa_lasterror = WSAGetLastError();
635                            if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.
636                                    return false;
637                            dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));
638                            CloseConnection(iter);
639                            break;
640                    }
641                    #else
642                  if (result == -1) {                  if (result == -1) {
643                          if (errno == EAGAIN) //Would block, try again later.                          if (errno == EAGAIN) //Would block, try again later.
644                                  return false;                                  return false;
# Line 531  bool LSCPServer::GetLSCPCommand( std::ve Line 677  bool LSCPServer::GetLSCPCommand( std::ve
677                          CloseConnection(iter);                          CloseConnection(iter);
678                          break;                          break;
679                  }                  }
680                    #endif
681          }          }
682          return false;          return false;
683  }  }
# Line 2295  String LSCPServer::GetServerInfo() { Line 2442  String LSCPServer::GetServerInfo() {
2442  }  }
2443    
2444  /**  /**
2445     * Will be called by the parser to return the current number of all active streams.
2446     */
2447    String LSCPServer::GetTotalStreamCount() {
2448        dmsg(2,("LSCPServer: GetTotalStreamCount()\n"));
2449        LSCPResultSet result;
2450        result.Add(pSampler->GetDiskStreamCount());
2451        return result.Produce();
2452    }
2453    
2454    /**
2455   * 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.
2456   */   */
2457  String LSCPServer::GetTotalVoiceCount() {  String LSCPServer::GetTotalVoiceCount() {
# Line 2332  String LSCPServer::SetGlobalVolume(doubl Line 2489  String LSCPServer::SetGlobalVolume(doubl
2489      return result.Produce();      return result.Produce();
2490  }  }
2491    
2492    String LSCPServer::GetFileInstruments(String Filename) {
2493        dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str()));
2494        LSCPResultSet result;
2495        try {
2496            VerifyFile(Filename);
2497        } catch (Exception e) {
2498            result.Error(e);
2499            return result.Produce();
2500        }
2501        // try to find a sampler engine that can handle the file
2502        bool bFound = false;
2503        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2504        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2505            Engine* pEngine = NULL;
2506            try {
2507                pEngine = EngineFactory::Create(engineTypes[i]);
2508                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2509                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2510                if (pManager) {
2511                    std::vector<InstrumentManager::instrument_id_t> IDs =
2512                        pManager->GetInstrumentFileContent(Filename);
2513                    // return the amount of instruments in the file
2514                    result.Add(IDs.size());
2515                    // no more need to ask other engine types
2516                    bFound = true;
2517                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2518            } catch (Exception e) {
2519                // NOOP, as exception is thrown if engine doesn't support file
2520            }
2521            if (pEngine) EngineFactory::Destroy(pEngine);
2522        }
2523    
2524        if (!bFound) result.Error("Unknown file format");
2525        return result.Produce();
2526    }
2527    
2528    String LSCPServer::ListFileInstruments(String Filename) {
2529        dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str()));
2530        LSCPResultSet result;
2531        try {
2532            VerifyFile(Filename);
2533        } catch (Exception e) {
2534            result.Error(e);
2535            return result.Produce();
2536        }
2537        // try to find a sampler engine that can handle the file
2538        bool bFound = false;
2539        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2540        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2541            Engine* pEngine = NULL;
2542            try {
2543                pEngine = EngineFactory::Create(engineTypes[i]);
2544                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2545                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2546                if (pManager) {
2547                    std::vector<InstrumentManager::instrument_id_t> IDs =
2548                        pManager->GetInstrumentFileContent(Filename);
2549                    // return a list of IDs of the instruments in the file
2550                    String s;
2551                    for (int j = 0; j < IDs.size(); j++) {
2552                        if (s.size()) s += ",";
2553                        s += ToString(IDs[j].Index);
2554                    }
2555                    result.Add(s);
2556                    // no more need to ask other engine types
2557                    bFound = true;
2558                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2559            } catch (Exception e) {
2560                // NOOP, as exception is thrown if engine doesn't support file
2561            }
2562            if (pEngine) EngineFactory::Destroy(pEngine);
2563        }
2564    
2565        if (!bFound) result.Error("Unknown file format");
2566        return result.Produce();
2567    }
2568    
2569    String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) {
2570        dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID));
2571        LSCPResultSet result;
2572        try {
2573            VerifyFile(Filename);
2574        } catch (Exception e) {
2575            result.Error(e);
2576            return result.Produce();
2577        }
2578        InstrumentManager::instrument_id_t id;
2579        id.FileName = Filename;
2580        id.Index    = InstrumentID;
2581        // try to find a sampler engine that can handle the file
2582        bool bFound = false;
2583        bool bFatalErr = false;
2584        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2585        for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) {
2586            Engine* pEngine = NULL;
2587            try {
2588                pEngine = EngineFactory::Create(engineTypes[i]);
2589                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2590                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2591                if (pManager) {
2592                    // check if the instrument index is valid
2593                    // FIXME: this won't work if an engine only supports parts of the instrument file
2594                    std::vector<InstrumentManager::instrument_id_t> IDs =
2595                        pManager->GetInstrumentFileContent(Filename);
2596                    if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) {
2597                        std::stringstream ss;
2598                        ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'";
2599                        bFatalErr = true;
2600                        throw Exception(ss.str());
2601                    }
2602                    // get the info of the requested instrument
2603                    InstrumentManager::instrument_info_t info =
2604                        pManager->GetInstrumentInfo(id);
2605                    // return detailed informations about the file
2606                    result.Add("NAME", info.InstrumentName);
2607                    result.Add("FORMAT_FAMILY", engineTypes[i]);
2608                    result.Add("FORMAT_VERSION", info.FormatVersion);
2609                    result.Add("PRODUCT", info.Product);
2610                    result.Add("ARTISTS", info.Artists);
2611                    // no more need to ask other engine types
2612                    bFound = true;
2613                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2614            } catch (Exception e) {
2615                // usually NOOP, as exception is thrown if engine doesn't support file
2616                if (bFatalErr) result.Error(e);
2617            }
2618            if (pEngine) EngineFactory::Destroy(pEngine);
2619        }
2620    
2621        if (!bFound && !bFatalErr) result.Error("Unknown file format");
2622        return result.Produce();
2623    }
2624    
2625    void LSCPServer::VerifyFile(String Filename) {
2626        #if WIN32
2627        WIN32_FIND_DATA win32FileAttributeData;
2628        BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData );
2629        if (!res) {
2630            std::stringstream ss;
2631            ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError();
2632            throw Exception(ss.str());
2633        }
2634        if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
2635            throw Exception("Directory is specified");
2636        }
2637        #else
2638        struct stat statBuf;
2639        int res = stat(Filename.c_str(), &statBuf);
2640        if (res) {
2641            std::stringstream ss;
2642            ss << "Fail to stat `" << Filename << "`: " << strerror(errno);
2643            throw Exception(ss.str());
2644        }
2645    
2646        if (S_ISDIR(statBuf.st_mode)) {
2647            throw Exception("Directory is specified");
2648        }
2649        #endif
2650    }
2651    
2652  /**  /**
2653   * 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
2654   * server for receiving event messages.   * server for receiving event messages.

Legend:
Removed from v.1471  
changed lines
  Added in v.1686

  ViewVC Help
Powered by ViewVC