/[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 2375 by schoenebeck, Thu Oct 4 17:45:22 2012 UTC revision 2427 by persson, Sat Mar 2 07:03:04 2013 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 - 2012 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2013 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 97  static String _escapeLscpResponse(String Line 97  static String _escapeLscpResponse(String
97   */   */
98  fd_set LSCPServer::fdSet;  fd_set LSCPServer::fdSet;
99  int LSCPServer::currentSocket = -1;  int LSCPServer::currentSocket = -1;
100  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();  std::vector<yyparse_param_t> LSCPServer::Sessions;
101  std::vector<yyparse_param_t>::iterator itCurrentSession = std::vector<yyparse_param_t>::iterator();  std::vector<yyparse_param_t>::iterator itCurrentSession;
102  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedNotifies;
103  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedCommands;
104  std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();  std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions;
105  Mutex LSCPServer::NotifyMutex = Mutex();  Mutex LSCPServer::NotifyMutex;
106  Mutex LSCPServer::NotifyBufferMutex = Mutex();  Mutex LSCPServer::NotifyBufferMutex;
107  Mutex LSCPServer::SubscriptionMutex = Mutex();  Mutex LSCPServer::SubscriptionMutex;
108  Mutex LSCPServer::RTNotifyMutex = Mutex();  Mutex LSCPServer::RTNotifyMutex;
109    
110  LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4), eventHandler(this) {  LSCPServer::LSCPServer(Sampler* pSampler, long int addr, short int port) : Thread(true, false, 0, -4), eventHandler(this) {
111      SocketAddress.sin_family      = AF_INET;      SocketAddress.sin_family      = AF_INET;
# Line 447  int LSCPServer::Main() { Line 447  int LSCPServer::Main() {
447          #endif          #endif
448          // 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
449          {          {
450              EngineChannelFactory::EngineChannelsMutex.Lock();              LockGuard lock(EngineChannelFactory::EngineChannelsMutex);
451              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();              std::set<EngineChannel*> engineChannels = EngineChannelFactory::EngineChannelInstances();
452              std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();              std::set<EngineChannel*>::iterator itEngineChannel = engineChannels.begin();
453              std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();              std::set<EngineChannel*>::iterator itEnd           = engineChannels.end();
# Line 465  int LSCPServer::Main() { Line 465  int LSCPServer::Main() {
465                      }                      }
466                  }                  }
467              }              }
             EngineChannelFactory::EngineChannelsMutex.Unlock();  
468          }          }
469    
470          // check if MIDI data arrived on some engine channel          // check if MIDI data arrived on some engine channel
# Line 517  int LSCPServer::Main() { Line 516  int LSCPServer::Main() {
516              }              }
517          }          }
518    
519          //Now let's deliver late notifies (if any)          //Now let's deliver late notifies (if any)
520          NotifyBufferMutex.Lock();          {
521          for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {              LockGuard lock(NotifyBufferMutex);
522                for (std::map<int,String>::iterator iterNotify = bufferedNotifies.begin(); iterNotify != bufferedNotifies.end(); iterNotify++) {
523  #ifdef MSG_NOSIGNAL  #ifdef MSG_NOSIGNAL
524                  send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);                  send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), MSG_NOSIGNAL);
525  #else  #else
526                  send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);                  send(iterNotify->first, iterNotify->second.c_str(), iterNotify->second.size(), 0);
527  #endif  #endif
528          }              }
529          bufferedNotifies.clear();              bufferedNotifies.clear();
530          NotifyBufferMutex.Unlock();          }
531    
532          fd_set selectSet = fdSet;          fd_set selectSet = fdSet;
533          timeout.tv_sec  = 0;          timeout.tv_sec  = 0;
# Line 624  void LSCPServer::CloseConnection( std::v Line 624  void LSCPServer::CloseConnection( std::v
624          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket));
625          Sessions.erase(iter);          Sessions.erase(iter);
626          FD_CLR(socket,  &fdSet);          FD_CLR(socket,  &fdSet);
627          SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any)          {
628          for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {              LockGuard lock(SubscriptionMutex);
629                  iter->second.remove(socket);              // Must unsubscribe this socket from all events (if any)
630          }              for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) {
631          SubscriptionMutex.Unlock();                  iter->second.remove(socket);
632          NotifyMutex.Lock();              }
633            }
634            LockGuard lock(NotifyMutex);
635          bufferedCommands.erase(socket);          bufferedCommands.erase(socket);
636          bufferedNotifies.erase(socket);          bufferedNotifies.erase(socket);
637          #if defined(WIN32)          #if defined(WIN32)
# Line 637  void LSCPServer::CloseConnection( std::v Line 639  void LSCPServer::CloseConnection( std::v
639          #else          #else
640          close(socket);          close(socket);
641          #endif          #endif
         NotifyMutex.Unlock();  
642  }  }
643    
644  void LSCPServer::CloseAllConnections() {  void LSCPServer::CloseAllConnections() {
# Line 648  void LSCPServer::CloseAllConnections() { Line 649  void LSCPServer::CloseAllConnections() {
649      }      }
650  }  }
651    
 void LSCPServer::LockRTNotify() {  
     RTNotifyMutex.Lock();  
 }  
   
 void LSCPServer::UnlockRTNotify() {  
     RTNotifyMutex.Unlock();  
 }  
   
652  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {  int LSCPServer::EventSubscribers( std::list<LSCPEvent::event_t> events ) {
653          int subs = 0;          int subs = 0;
654          SubscriptionMutex.Lock();          LockGuard lock(SubscriptionMutex);
655          for( std::list<LSCPEvent::event_t>::iterator iter = events.begin();          for( std::list<LSCPEvent::event_t>::iterator iter = events.begin();
656                          iter != events.end(); iter++)                          iter != events.end(); iter++)
657          {          {
658                  subs += eventSubscriptions.count(*iter);                  subs += eventSubscriptions.count(*iter);
659          }          }
         SubscriptionMutex.Unlock();  
660          return subs;          return subs;
661  }  }
662    
663  void LSCPServer::SendLSCPNotify( LSCPEvent event ) {  void LSCPServer::SendLSCPNotify( LSCPEvent event ) {
664          SubscriptionMutex.Lock();          LockGuard lock(SubscriptionMutex);
665          if (eventSubscriptions.count(event.GetType()) == 0) {          if (eventSubscriptions.count(event.GetType()) == 0) {
666                  SubscriptionMutex.Unlock();     //Nobody is subscribed to this event                  // Nobody is subscribed to this event
667                  return;                  return;
668          }          }
669          std::list<int>::iterator iter = eventSubscriptions[event.GetType()].begin();          std::list<int>::iterator iter = eventSubscriptions[event.GetType()].begin();
# Line 697  void LSCPServer::SendLSCPNotify( LSCPEve Line 689  void LSCPServer::SendLSCPNotify( LSCPEve
689                          }                          }
690                  }                  }
691          }          }
         SubscriptionMutex.Unlock();  
692  }  }
693    
694  extern int GetLSCPCommand( void *buf, int max_size ) {  extern int GetLSCPCommand( void *buf, int max_size ) {
# Line 812  bool LSCPServer::GetLSCPCommand( std::ve Line 803  bool LSCPServer::GetLSCPCommand( std::ve
803  void LSCPServer::AnswerClient(String ReturnMessage) {  void LSCPServer::AnswerClient(String ReturnMessage) {
804      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));      dmsg(2,("LSCPServer::AnswerClient(ReturnMessage=%s)", ReturnMessage.c_str()));
805      if (currentSocket != -1) {      if (currentSocket != -1) {
806              NotifyMutex.Lock();              LockGuard lock(NotifyMutex);
807  #ifdef MSG_NOSIGNAL  #ifdef MSG_NOSIGNAL
808              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), MSG_NOSIGNAL);
809  #else  #else
810              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);              send(currentSocket, ReturnMessage.c_str(), ReturnMessage.size(), 0);
811  #endif  #endif
             NotifyMutex.Unlock();  
812      }      }
813  }  }
814    
# Line 968  String LSCPServer::SetEngineType(String Line 958  String LSCPServer::SetEngineType(String
958      try {      try {
959          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
960          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
961          LockRTNotify();          LockGuard lock(RTNotifyMutex);
962          pSamplerChannel->SetEngineType(EngineName);          pSamplerChannel->SetEngineType(EngineName);
963          if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);          if(HasSoloChannel()) pSamplerChannel->GetEngineChannel()->SetMute(-1);
         UnlockRTNotify();  
964      }      }
965      catch (Exception e) {      catch (Exception e) {
966           result.Error(e);           result.Error(e);
# Line 1011  String LSCPServer::ListChannels() { Line 1000  String LSCPServer::ListChannels() {
1000   */   */
1001  String LSCPServer::AddChannel() {  String LSCPServer::AddChannel() {
1002      dmsg(2,("LSCPServer: AddChannel()\n"));      dmsg(2,("LSCPServer: AddChannel()\n"));
1003      LockRTNotify();      SamplerChannel* pSamplerChannel;
1004      SamplerChannel* pSamplerChannel = pSampler->AddSamplerChannel();      {
1005      UnlockRTNotify();          LockGuard lock(RTNotifyMutex);
1006            pSamplerChannel = pSampler->AddSamplerChannel();
1007        }
1008      LSCPResultSet result(pSamplerChannel->Index());      LSCPResultSet result(pSamplerChannel->Index());
1009      return result.Produce();      return result.Produce();
1010  }  }
# Line 1024  String LSCPServer::AddChannel() { Line 1015  String LSCPServer::AddChannel() {
1015  String LSCPServer::RemoveChannel(uint uiSamplerChannel) {  String LSCPServer::RemoveChannel(uint uiSamplerChannel) {
1016      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));      dmsg(2,("LSCPServer: RemoveChannel(SamplerChannel=%d)\n", uiSamplerChannel));
1017      LSCPResultSet result;      LSCPResultSet result;
1018      LockRTNotify();      {
1019      pSampler->RemoveSamplerChannel(uiSamplerChannel);          LockGuard lock(RTNotifyMutex);
1020      UnlockRTNotify();          pSampler->RemoveSamplerChannel(uiSamplerChannel);
1021        }
1022      return result.Produce();      return result.Produce();
1023  }  }
1024    
# Line 1069  String LSCPServer::ListAvailableEngines( Line 1061  String LSCPServer::ListAvailableEngines(
1061  String LSCPServer::GetEngineInfo(String EngineName) {  String LSCPServer::GetEngineInfo(String EngineName) {
1062      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));      dmsg(2,("LSCPServer: GetEngineInfo(EngineName=%s)\n", EngineName.c_str()));
1063      LSCPResultSet result;      LSCPResultSet result;
1064      LockRTNotify();      {
1065      try {          LockGuard lock(RTNotifyMutex);
1066          Engine* pEngine = EngineFactory::Create(EngineName);          try {
1067          result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));              Engine* pEngine = EngineFactory::Create(EngineName);
1068          result.Add("VERSION",     pEngine->Version());              result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));
1069          EngineFactory::Destroy(pEngine);              result.Add("VERSION",     pEngine->Version());
1070      }              EngineFactory::Destroy(pEngine);
1071      catch (Exception e) {          }
1072           result.Error(e);          catch (Exception e) {
1073                result.Error(e);
1074            }
1075      }      }
     UnlockRTNotify();  
1076      return result.Produce();      return result.Produce();
1077  }  }
1078    
# Line 1728  String LSCPServer::SetAudioOutputChannel Line 1721  String LSCPServer::SetAudioOutputChannel
1721  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputDevice(uint AudioDeviceId, uint uiSamplerChannel) {
1722      dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));      dmsg(2,("LSCPServer: SetAudiotOutputDevice(AudioDeviceId=%d, SamplerChannel=%d)\n",AudioDeviceId,uiSamplerChannel));
1723      LSCPResultSet result;      LSCPResultSet result;
1724      LockRTNotify();      {
1725      try {          LockGuard lock(RTNotifyMutex);
1726          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          try {
1727          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));              SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1728          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();              if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1729          if (!devices.count(AudioDeviceId)) throw Exception("There is no audio output device with index " + ToString(AudioDeviceId));              std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1730          AudioOutputDevice* pDevice = devices[AudioDeviceId];              if (!devices.count(AudioDeviceId)) throw Exception("There is no audio output device with index " + ToString(AudioDeviceId));
1731          pSamplerChannel->SetAudioOutputDevice(pDevice);              AudioOutputDevice* pDevice = devices[AudioDeviceId];
1732      }              pSamplerChannel->SetAudioOutputDevice(pDevice);
1733      catch (Exception e) {          }
1734           result.Error(e);          catch (Exception e) {
1735                result.Error(e);
1736            }
1737      }      }
     UnlockRTNotify();  
1738      return result.Produce();      return result.Produce();
1739  }  }
1740    
1741  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {  String LSCPServer::SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel) {
1742      dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));      dmsg(2,("LSCPServer: SetAudioOutputType(String AudioOutputDriver=%s, SamplerChannel=%d)\n",AudioOutputDriver.c_str(),uiSamplerChannel));
1743      LSCPResultSet result;      LSCPResultSet result;
1744      LockRTNotify();      {
1745      try {          LockGuard lock(RTNotifyMutex);
1746          SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);          try {
1747          if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));              SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
1748          // Driver type name aliasing...              if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
1749          if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";              // Driver type name aliasing...
1750          if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";              if (AudioOutputDriver == "Alsa") AudioOutputDriver = "ALSA";
1751          // Check if there's one audio output device already created              if (AudioOutputDriver == "Jack") AudioOutputDriver = "JACK";
1752          // for the intended audio driver type (AudioOutputDriver)...              // Check if there's one audio output device already created
1753          AudioOutputDevice *pDevice = NULL;              // for the intended audio driver type (AudioOutputDriver)...
1754          std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();              AudioOutputDevice *pDevice = NULL;
1755          std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();              std::map<uint, AudioOutputDevice*> devices = pSampler->GetAudioOutputDevices();
1756          for (; iter != devices.end(); iter++) {              std::map<uint, AudioOutputDevice*>::iterator iter = devices.begin();
1757              if ((iter->second)->Driver() == AudioOutputDriver) {              for (; iter != devices.end(); iter++) {
1758                  pDevice = iter->second;                  if ((iter->second)->Driver() == AudioOutputDriver) {
1759                  break;                      pDevice = iter->second;
1760                        break;
1761                    }
1762              }              }
1763                // If it doesn't exist, create a new one with default parameters...
1764                if (pDevice == NULL) {
1765                    std::map<String,String> params;
1766                    pDevice = pSampler->CreateAudioOutputDevice(AudioOutputDriver, params);
1767                }
1768                // Must have a device...
1769                if (pDevice == NULL)
1770                    throw Exception("Internal error: could not create audio output device.");
1771                // Set it as the current channel device...
1772                pSamplerChannel->SetAudioOutputDevice(pDevice);
1773          }          }
1774          // If it doesn't exist, create a new one with default parameters...          catch (Exception e) {
1775          if (pDevice == NULL) {              result.Error(e);
             std::map<String,String> params;  
             pDevice = pSampler->CreateAudioOutputDevice(AudioOutputDriver, params);  
1776          }          }
         // Must have a device...  
         if (pDevice == NULL)  
             throw Exception("Internal error: could not create audio output device.");  
         // Set it as the current channel device...  
         pSamplerChannel->SetAudioOutputDevice(pDevice);  
1777      }      }
     catch (Exception e) {  
          result.Error(e);  
     }  
     UnlockRTNotify();  
1778      return result.Produce();      return result.Produce();
1779  }  }
1780    
# Line 3322  void LSCPServer::VerifyFile(String Filen Line 3317  void LSCPServer::VerifyFile(String Filen
3317  String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) {  String LSCPServer::SubscribeNotification(LSCPEvent::event_t type) {
3318      dmsg(2,("LSCPServer: SubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));      dmsg(2,("LSCPServer: SubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
3319      LSCPResultSet result;      LSCPResultSet result;
3320      SubscriptionMutex.Lock();      {
3321      eventSubscriptions[type].push_back(currentSocket);          LockGuard lock(SubscriptionMutex);
3322      SubscriptionMutex.Unlock();          eventSubscriptions[type].push_back(currentSocket);
3323        }
3324      return result.Produce();      return result.Produce();
3325  }  }
3326    
# Line 3335  String LSCPServer::SubscribeNotification Line 3331  String LSCPServer::SubscribeNotification
3331  String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) {  String LSCPServer::UnsubscribeNotification(LSCPEvent::event_t type) {
3332      dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));      dmsg(2,("LSCPServer: UnsubscribeNotification(Event=%s)\n", LSCPEvent::Name(type).c_str()));
3333      LSCPResultSet result;      LSCPResultSet result;
3334      SubscriptionMutex.Lock();      {
3335      eventSubscriptions[type].remove(currentSocket);          LockGuard lock(SubscriptionMutex);
3336      SubscriptionMutex.Unlock();          eventSubscriptions[type].remove(currentSocket);
3337        }
3338      return result.Produce();      return result.Produce();
3339  }  }
3340    

Legend:
Removed from v.2375  
changed lines
  Added in v.2427

  ViewVC Help
Powered by ViewVC