/[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 1551 by schoenebeck, Wed Dec 5 22:05:28 2007 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 367  int LSCPServer::Main() { Line 374  int LSCPServer::Main() {
374                    std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;                    std::cerr << "LSCPServer: ioctlsocket: set FIONBIO failed. Error " << WSAGetLastError() << std::endl;
375                    exit(EXIT_FAILURE);                    exit(EXIT_FAILURE);
376                  }                  }
377          #else                    #else
378                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {                  if (fcntl(socket, F_SETFL, O_NONBLOCK)) {
379                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;                          std::cerr << "LSCPServer: F_SETFL O_NONBLOCK failed." << std::endl;
380                          exit(EXIT_FAILURE);                          exit(EXIT_FAILURE);
# Line 439  void LSCPServer::CloseConnection( std::v Line 446  void LSCPServer::CloseConnection( std::v
446          NotifyMutex.Unlock();          NotifyMutex.Unlock();
447  }  }
448    
449    void LSCPServer::LockRTNotify() {
450        RTNotifyMutex.Lock();
451    }
452    
453    void LSCPServer::UnlockRTNotify() {
454        RTNotifyMutex.Unlock();
455    }
456    
457  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
458          int subs = 0;          int subs = 0;
459          SubscriptionMutex.Lock();          SubscriptionMutex.Lock();
# Line 538  bool LSCPServer::GetLSCPCommand( std::ve Line 553  bool LSCPServer::GetLSCPCommand( std::ve
553                      int wsa_lasterror = WSAGetLastError();                      int wsa_lasterror = WSAGetLastError();
554                          if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.                          if (wsa_lasterror == WSAEWOULDBLOCK) //Would block, try again later.
555                                  return false;                                  return false;
556                          dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));                            dmsg(2,("LSCPScanner: Socket error after recv() Error %d.\n", wsa_lasterror));
557                          CloseConnection(iter);                          CloseConnection(iter);
558                          break;                          break;
559                  }                  }
# Line 2346  String LSCPServer::GetServerInfo() { Line 2361  String LSCPServer::GetServerInfo() {
2361  }  }
2362    
2363  /**  /**
2364     * Will be called by the parser to return the current number of all active streams.
2365     */
2366    String LSCPServer::GetTotalStreamCount() {
2367        dmsg(2,("LSCPServer: GetTotalStreamCount()\n"));
2368        LSCPResultSet result;
2369        result.Add(pSampler->GetDiskStreamCount());
2370        return result.Produce();
2371    }
2372    
2373    /**
2374   * 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.
2375   */   */
2376  String LSCPServer::GetTotalVoiceCount() {  String LSCPServer::GetTotalVoiceCount() {
# Line 2383  String LSCPServer::SetGlobalVolume(doubl Line 2408  String LSCPServer::SetGlobalVolume(doubl
2408      return result.Produce();      return result.Produce();
2409  }  }
2410    
2411    String LSCPServer::GetFileInstruments(String Filename) {
2412        dmsg(2,("LSCPServer: GetFileInstruments(String Filename=%s)\n",Filename.c_str()));
2413        LSCPResultSet result;
2414        try {
2415            VerifyFile(Filename);
2416        } catch (Exception e) {
2417            result.Error(e);
2418            return result.Produce();
2419        }
2420        // try to find a sampler engine that can handle the file
2421        bool bFound = false;
2422        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2423        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2424            Engine* pEngine = NULL;
2425            try {
2426                pEngine = EngineFactory::Create(engineTypes[i]);
2427                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2428                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2429                if (pManager) {
2430                    std::vector<InstrumentManager::instrument_id_t> IDs =
2431                        pManager->GetInstrumentFileContent(Filename);
2432                    // return the amount of instruments in the file
2433                    result.Add(IDs.size());
2434                    // no more need to ask other engine types
2435                    bFound = true;
2436                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2437            } catch (Exception e) {
2438                // NOOP, as exception is thrown if engine doesn't support file
2439            }
2440            if (pEngine) EngineFactory::Destroy(pEngine);
2441        }
2442    
2443        if (!bFound) result.Error("Unknown file format");
2444        return result.Produce();
2445    }
2446    
2447    String LSCPServer::ListFileInstruments(String Filename) {
2448        dmsg(2,("LSCPServer: ListFileInstruments(String Filename=%s)\n",Filename.c_str()));
2449        LSCPResultSet result;
2450        try {
2451            VerifyFile(Filename);
2452        } catch (Exception e) {
2453            result.Error(e);
2454            return result.Produce();
2455        }
2456        // try to find a sampler engine that can handle the file
2457        bool bFound = false;
2458        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2459        for (int i = 0; !bFound && i < engineTypes.size(); i++) {
2460            Engine* pEngine = NULL;
2461            try {
2462                pEngine = EngineFactory::Create(engineTypes[i]);
2463                if (!pEngine) throw Exception("Internal error: could not create '" + engineTypes[i] + "' engine");
2464                InstrumentManager* pManager = pEngine->GetInstrumentManager();
2465                if (pManager) {
2466                    std::vector<InstrumentManager::instrument_id_t> IDs =
2467                        pManager->GetInstrumentFileContent(Filename);
2468                    // return a list of IDs of the instruments in the file
2469                    String s;
2470                    for (int j = 0; j < IDs.size(); j++) {
2471                        if (s.size()) s += ",";
2472                        s += ToString(IDs[j].Index);
2473                    }
2474                    result.Add(s);
2475                    // no more need to ask other engine types
2476                    bFound = true;
2477                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2478            } catch (Exception e) {
2479                // NOOP, as exception is thrown if engine doesn't support file
2480            }
2481            if (pEngine) EngineFactory::Destroy(pEngine);
2482        }
2483    
2484        if (!bFound) result.Error("Unknown file format");
2485        return result.Produce();
2486    }
2487    
2488    String LSCPServer::GetFileInstrumentInfo(String Filename, uint InstrumentID) {
2489        dmsg(2,("LSCPServer: GetFileInstrumentInfo(String Filename=%s, InstrumentID=%d)\n",Filename.c_str(),InstrumentID));
2490        LSCPResultSet result;
2491        try {
2492            VerifyFile(Filename);
2493        } catch (Exception e) {
2494            result.Error(e);
2495            return result.Produce();
2496        }
2497        InstrumentManager::instrument_id_t id;
2498        id.FileName = Filename;
2499        id.Index    = InstrumentID;
2500        // try to find a sampler engine that can handle the file
2501        bool bFound = false;
2502        bool bFatalErr = false;
2503        std::vector<String> engineTypes = EngineFactory::AvailableEngineTypes();
2504        for (int i = 0; !bFound && !bFatalErr && 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                    // check if the instrument index is valid
2512                    // FIXME: this won't work if an engine only supports parts of the instrument file
2513                    std::vector<InstrumentManager::instrument_id_t> IDs =
2514                        pManager->GetInstrumentFileContent(Filename);
2515                    if (std::find(IDs.begin(), IDs.end(), id) == IDs.end()) {
2516                        std::stringstream ss;
2517                        ss << "Invalid instrument index " << InstrumentID << " for instrument file '" << Filename << "'";
2518                        bFatalErr = true;
2519                        throw Exception(ss.str());
2520                    }
2521                    // get the info of the requested instrument
2522                    InstrumentManager::instrument_info_t info =
2523                        pManager->GetInstrumentInfo(id);
2524                    // return detailed informations about the file
2525                    result.Add("NAME", info.InstrumentName);
2526                    result.Add("FORMAT_FAMILY", engineTypes[i]);
2527                    result.Add("FORMAT_VERSION", info.FormatVersion);
2528                    result.Add("PRODUCT", info.Product);
2529                    result.Add("ARTISTS", info.Artists);
2530                    // no more need to ask other engine types
2531                    bFound = true;
2532                } else dmsg(1,("Warning: engine '%s' does not provide an instrument manager\n", engineTypes[i].c_str()));
2533            } catch (Exception e) {
2534                // usually NOOP, as exception is thrown if engine doesn't support file
2535                if (bFatalErr) result.Error(e);
2536            }
2537            if (pEngine) EngineFactory::Destroy(pEngine);
2538        }
2539    
2540        if (!bFound && !bFatalErr) result.Error("Unknown file format");
2541        return result.Produce();
2542    }
2543    
2544    void LSCPServer::VerifyFile(String Filename) {
2545        #if WIN32
2546        WIN32_FIND_DATA win32FileAttributeData;
2547        BOOL res = GetFileAttributesEx( Filename.c_str(), GetFileExInfoStandard, &win32FileAttributeData );
2548        if (!res) {
2549            std::stringstream ss;
2550            ss << "File does not exist, GetFileAttributesEx failed `" << Filename << "`: Error " << GetLastError();
2551            throw Exception(ss.str());
2552        }
2553        if ( win32FileAttributeData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
2554            throw Exception("Directory is specified");
2555        }
2556        #else
2557        struct stat statBuf;
2558        int res = stat(Filename.c_str(), &statBuf);
2559        if (res) {
2560            std::stringstream ss;
2561            ss << "Fail to stat `" << Filename << "`: " << strerror(errno);
2562            throw Exception(ss.str());
2563        }
2564    
2565        if (S_ISDIR(statBuf.st_mode)) {
2566            throw Exception("Directory is specified");
2567        }
2568        #endif
2569    }
2570    
2571  /**  /**
2572   * 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
2573   * server for receiving event messages.   * server for receiving event messages.

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

  ViewVC Help
Powered by ViewVC