/[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 1525 by schoenebeck, Sun Nov 25 17:47:46 2007 UTC revision 1649 by nagata, Fri Jan 25 15:06:02 2008 UTC
# Line 26  Line 26 
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
# Line 126  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      hSocket = -1;      hSocket = -1;
# Line 187  void LSCPServer::EventHandler::TotalVoic Line 189  void LSCPServer::EventHandler::TotalVoic
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)));
# Line 283  int LSCPServer::Main() { Line 289  int LSCPServer::Main() {
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);
# Line 302  int LSCPServer::Main() { Line 309  int LSCPServer::Main() {
309      timeval timeout;      timeval timeout;
310    
311      while (true) {      while (true) {
312            #if CONFIG_PTHREAD_TESTCANCEL
313                    TestCancel();
314            #endif
315          // 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
316          {          {
317              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
# Line 367  int LSCPServer::Main() { Line 377  int LSCPServer::Main() {
377                    std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;                    std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;
378                    exit(EXIT_FAILURE);                    exit(EXIT_FAILURE);
379                  }                  }
380          #else                    #else
381                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
382                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
383                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
# Line 439  void LSCPServer::CloseConnection( std::v Line 449  void LSCPServer::CloseConnection( std::v
449          NotifyMutex.Unlock();          NotifyMutex.Unlock();
450  }  }
451    
452    void LSCPServer::LockRTNotify() {
453        RTNotifyMutex.Lock();
454    }
455    
456    void LSCPServer::UnlockRTNotify() {
457        RTNotifyMutex.Unlock();
458    }
459    
460  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
461          int subs = 0;          int subs = 0;
462          SubscriptionMutex.Lock();          SubscriptionMutex.Lock();
# Line 538  bool LSCPServer::GetLSCPCommand( std::ve Line 556  bool LSCPServer::GetLSCPCommand( std::ve
556                      int wsa_lasterror = WSAGetLastError();                      int wsa_lasterror = WSAGetLastError();
557                          if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.                          if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.
558                                  return false;                                  return false;
559                          dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));                            dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));
560                          CloseConnection(iter);                          CloseConnection(iter);
561                          break;                          break;
562                  }                  }
# Line 2346  String LSCPServer::GetServerInfo() { Line 2364  String LSCPServer::GetServerInfo() {
2364  }  }
2365    
2366  /**  /**
2367     * Will be called by the parser to return the current number of all active streams.
2368     */
2369    String LSCPServer::GetTotalStreamCount() {
2370        dmsg(2,("LSCPServer: GetTotalStreamCount()\n"));
2371        LSCPResultSet result;
2372        result.Add(pSampler->GetDiskStreamCount());
2373        return result.Produce();
2374    }
2375    
2376    /**
2377   * 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.
2378   */   */
2379  String LSCPServer::GetTotalVoiceCount() {  String LSCPServer::GetTotalVoiceCount() {
# Line 2384  String LSCPServer::SetGlobalVolume(doubl Line 2412  String LSCPServer::SetGlobalVolume(doubl
2412  }  }
2413    
2414  String LSCPServer::GetFileInstruments(String Filename) {  String LSCPServer::GetFileInstruments(String Filename) {
2415        dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str()));
2416      LSCPResultSet result;      LSCPResultSet result;
2417        try {
2418            VerifyFile(Filename);
2419        } catch (Exception e) {
2420            result.Error(e);
2421            return result.Produce();
2422        }
2423      // try to find a sampler engine that can handle the file      // try to find a sampler engine that can handle the file
2424      bool bFound = false;      bool bFound = false;
2425      std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();      std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
# Line 2407  String LSCPServer::GetFileInstruments(St Line 2442  String LSCPServer::GetFileInstruments(St
2442          }          }
2443          if (pEngine) EngineFactory::Destroy(pEngine);          if (pEngine) EngineFactory::Destroy(pEngine);
2444      }      }
2445    
2446        if (!bFound) result.Error("Unknown file format");
2447      return result.Produce();      return result.Produce();
2448  }  }
2449    
2450  String LSCPServer::ListFileInstruments(String Filename) {  String LSCPServer::ListFileInstruments(String Filename) {
2451        dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str()));
2452      LSCPResultSet result;      LSCPResultSet result;
2453        try {
2454            VerifyFile(Filename);
2455        } catch (Exception e) {
2456            result.Error(e);
2457            return result.Produce();
2458        }
2459      // try to find a sampler engine that can handle the file      // try to find a sampler engine that can handle the file
2460      bool bFound = false;      bool bFound = false;
2461      std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();      std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
# Line 2439  String LSCPServer::ListFileInstruments(S Line 2483  String LSCPServer::ListFileInstruments(S
2483          }          }
2484          if (pEngine) EngineFactory::Destroy(pEngine);          if (pEngine) EngineFactory::Destroy(pEngine);
2485      }      }
2486    
2487        if (!bFound) result.Error("Unknown file format");
2488      return result.Produce();      return result.Produce();
2489  }  }
2490    
2491  String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) {  String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) {
2492        dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID));
2493      LSCPResultSet result;      LSCPResultSet result;
2494        try {
2495            VerifyFile(Filename);
2496        } catch (Exception e) {
2497            result.Error(e);
2498            return result.Produce();
2499        }
2500      InstrumentManager::instrument_id_t id;      InstrumentManager::instrument_id_t id;
2501      id.FileName = Filename;      id.FileName = Filename;
2502      id.Index    = InstrumentID;      id.Index    = InstrumentID;
2503      // try to find a sampler engine that can handle the file      // try to find a sampler engine that can handle the file
2504      bool bFound = false;      bool bFound = false;
2505        bool bFatalErr = false;
2506      std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();      std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2507      for (int i = 0; !bFound && i < engineTypes.size(); i++) {      for (int i = 0; !bFound && !bFatalErr && i < engineTypes.size(); i++) {
2508          Engine* pEngine = NULL;          Engine* pEngine = NULL;
2509          try {          try {
2510              pEngine = EngineFactory::Create(engineTypes[i]);              pEngine = EngineFactory::Create(engineTypes[i]);
2511              if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");              if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2512              InstrumentManager* pManager = pEngine->GetInstrumentManager();              InstrumentManager* pManager = pEngine->GetInstrumentManager();
2513              if (pManager) {              if (pManager) {
2514                    // check if the instrument index is valid
2515                    // FIXME: this won't work if an engine only supports parts of the instrument file
2516                    std::vector<InstrumentManager::instrument_id_t> IDs =
2517                        pManager->GetInstrumentFileContent(Filename);
2518                    if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) {
2519                        std::stringstream ss;
2520                        ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'";
2521                        bFatalErr = true;
2522                        throw Exception(ss.str());
2523                    }
2524                    // get the info of the requested instrument
2525                  InstrumentManager::instrument_info_t info =                  InstrumentManager::instrument_info_t info =
2526                      pManager->GetInstrumentInfo(id);                      pManager->GetInstrumentInfo(id);
2527                  // return detailed informations about the file                  // return detailed informations about the file
2528                  result.Add("NAME", info.InstrumentName);                  result.Add("NAME", info.InstrumentName);
2529                  result.Add("FORMAT_NAME", engineTypes[i]);                  result.Add("FORMAT_FAMILY", engineTypes[i]);
2530                  result.Add("FORMAT_VERSION", info.FormatVersion);                  result.Add("FORMAT_VERSION", info.FormatVersion);
2531                  result.Add("PRODUCT", info.Product);                  result.Add("PRODUCT", info.Product);
2532                  result.Add("ARTISTS", info.Artists);                  result.Add("ARTISTS", info.Artists);
# Line 2469  String LSCPServer::GetFileInstrumentInfo Line 2534  String LSCPServer::GetFileInstrumentInfo
2534                  bFound = true;                  bFound = true;
2535              } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));              } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2536          } catch (Exception e) {          } catch (Exception e) {
2537              // NOOP, as exception is thrown if engine doesn't support file              // usually NOOP, as exception is thrown if engine doesn't support file
2538                if (bFatalErr) result.Error(e);
2539          }          }
2540          if (pEngine) EngineFactory::Destroy(pEngine);          if (pEngine) EngineFactory::Destroy(pEngine);
2541      }      }
2542    
2543        if (!bFound && !bFatalErr) result.Error("Unknown file format");
2544      return result.Produce();      return result.Produce();
2545  }  }
2546    
2547    void LSCPServer::VerifyFile(String Filename) {
2548        #if WIN32
2549        WIN32_FIND_DATA win32FileAttributeData;
2550        BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData );
2551        if (!res) {
2552            std::stringstream ss;
2553            ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError();
2554            throw Exception(ss.str());
2555        }
2556        if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
2557            throw Exception("Directory is specified");
2558        }
2559        #else
2560        struct stat statBuf;
2561        int res = stat(Filename.c_str(), &statBuf);
2562        if (res) {
2563            std::stringstream ss;
2564            ss << "Fail to stat `" << Filename << "`: " << strerror(errno);
2565            throw Exception(ss.str());
2566        }
2567    
2568        if (S_ISDIR(statBuf.st_mode)) {
2569            throw Exception("Directory is specified");
2570        }
2571        #endif
2572    }
2573    
2574  /**  /**
2575   * 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
2576   * server for receiving event messages.   * server for receiving event messages.

Legend:
Removed from v.1525  
changed lines
  Added in v.1649

  ViewVC Help
Powered by ViewVC