--- linuxsampler/trunk/src/network/lscpserver.cpp 2004/07/07 02:49:51 185 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2004/07/28 14:17:29 212 @@ -25,8 +25,8 @@ #include "lscpevent.h" #include "../engines/gig/Engine.h" -#include "../audiodriver/AudioOutputDeviceFactory.h" -#include "../mididriver/MidiInputDeviceFactory.h" +#include "../drivers/audio/AudioOutputDeviceFactory.h" +#include "../drivers/midi/MidiInputDeviceFactory.h" /** * Below are a few static members of the LSCPServer class. @@ -43,7 +43,7 @@ */ fd_set LSCPServer::fdSet; int LSCPServer::currentSocket = -1; -std::vector LSCPServer::hSessions = std::vector(); +std::vector LSCPServer::Sessions = std::vector(); std::map LSCPServer::bufferedNotifies = std::map(); std::map LSCPServer::bufferedCommands = std::map(); std::map< LSCPEvent::event_t, std::list > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list >(); @@ -61,6 +61,20 @@ LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); } +/** + * Blocks the calling thread until the LSCP Server is initialized and + * accepting socket connections, if the server is already initialized then + * this method will return immediately. + * @param TimeoutSeconds - optional: max. wait time in seconds + * (default: 0s) + * @param TimeoutNanoSeconds - optional: max wait time in nano seconds + * (default: 0ns) + * @returns 0 on success, a value less than 0 if timeout exceeded + */ +int LSCPServer::WaitUntilInitialized(long TimeoutSeconds, long TimeoutNanoSeconds) { + return Initialized.WaitAndUnlockIf(false, TimeoutSeconds, TimeoutNanoSeconds); +} + int LSCPServer::Main() { int hSocket = socket(AF_INET, SOCK_STREAM, 0); if (hSocket < 0) { @@ -81,33 +95,26 @@ } listen(hSocket, 1); - dmsg(1,("LSCPServer: Server running.\n")); // server running + Initialized.Set(true); // now wait for client connections and handle their requests sockaddr_in client; int length = sizeof(client); - struct timeval tv; - tv.tv_sec = 30; - tv.tv_usec = 0; FD_ZERO(&fdSet); FD_SET(hSocket, &fdSet); int maxSessions = hSocket; - - // Parser initialization - yyparse_param_t yyparse_param; - yyparse_param.pServer = this; while (true) { fd_set selectSet = fdSet; - int retval = select(maxSessions+1, &selectSet, NULL, NULL, &tv); + int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL); if (retval == 0) - continue; //Nothing in 30 seconds, try again + continue; //Nothing try again if (retval == -1) { std::cerr << "LSCPServer: Socket select error." << std::endl; close(hSocket); exit(EXIT_FAILURE); } - + //Accept new connections now (if any) if (FD_ISSET(hSocket, &selectSet)) { int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length); @@ -121,7 +128,12 @@ exit(EXIT_FAILURE); } - hSessions.push_back(socket); + // Parser initialization + yyparse_param_t yyparse_param; + yyparse_param.pServer = this; + yyparse_param.hSession = socket; + + Sessions.push_back(yyparse_param); FD_SET(socket, &fdSet); if (socket > maxSessions) maxSessions = socket; @@ -131,13 +143,16 @@ } //Something was selected and it was not the hSocket, so it must be some command(s) coming. - for (std::vector::iterator iter = hSessions.begin(); iter != hSessions.end(); iter++) { - if (FD_ISSET(*iter, &selectSet)) { //Was it this socket? + for (std::vector::iterator iter = Sessions.begin(); iter != Sessions.end(); iter++) { + if (FD_ISSET((*iter).hSession, &selectSet)) { //Was it this socket? if (GetLSCPCommand(iter)) { //Have we read the entire command? dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket)); - yylex_init(&yyparse_param.pScanner); - currentSocket = *iter; //a hack - int result = yyparse(&yyparse_param); + yylex_init(&((*iter).pScanner)); //FIXME: should me moved out of this loop and initialized only when a new session is created + currentSocket = (*iter).hSession; //a hack + if ((*iter).bVerbose) { // if echo mode enabled + AnswerClient(bufferedCommands[currentSocket]); + } + int result = yyparse(&(*iter)); currentSocket = -1; //continuation of a hack dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket)); if (result == LSCP_QUIT) { //Was it a quit command by any chance? @@ -146,7 +161,7 @@ } //socket may have been closed, iter may be invalid, get out of the loop for now. //we'll be back if there is data. - break; + break; } } @@ -162,11 +177,11 @@ //yylex_destroy(yyparse_param.pScanner); } -void LSCPServer::CloseConnection( std::vector::iterator iter ) { - int socket = *iter; +void LSCPServer::CloseConnection( std::vector::iterator iter ) { + int socket = (*iter).hSession; dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket)); LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket)); - hSessions.erase(iter); + Sessions.erase(iter); FD_CLR(socket, &fdSet); SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any) for (std::map< LSCPEvent::event_t, std::list >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) { @@ -178,6 +193,7 @@ bufferedNotifies.erase(socket); close(socket); NotifyMutex.Unlock(); + //yylex_destroy((*iter).pScanner); } void LSCPServer::SendLSCPNotify( LSCPEvent event ) { @@ -230,8 +246,8 @@ * If command is read, it will return true. Otherwise false is returned. * In any case the received portion (complete or incomplete) is saved into bufferedCommand map. */ -bool LSCPServer::GetLSCPCommand( std::vector::iterator iter ) { - int socket = *iter; +bool LSCPServer::GetLSCPCommand( std::vector::iterator iter ) { + int socket = (*iter).hSession; char c; int i = 0; while (true) { @@ -241,7 +257,7 @@ break; } if (result == 1) { - if (c == '\r') + if (c == '\r') continue; //Ignore CR if (c == '\n') { LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket)); @@ -268,23 +284,23 @@ break; case EAGAIN: dmsg(2,("LSCPScanner: The socket is marked non-blocking and the receive operation would block, or a receive timeout had been set and the timeout expired before data was received.\n")); - break; - case EINTR: + break; + case EINTR: dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n")); - break; - case EFAULT: - dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n")); - break; - case EINVAL: - dmsg(2,("LSCPScanner: Invalid argument passed.\n")); - break; - case ENOMEM: - dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n")); - break; - default: - dmsg(2,("LSCPScanner: Unknown recv() error.\n")); - break; - } + break; + case EFAULT: + dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n")); + break; + case EINVAL: + dmsg(2,("LSCPScanner: Invalid argument passed.\n")); + break; + case ENOMEM: + dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n")); + break; + default: + dmsg(2,("LSCPScanner: Unknown recv() error.\n")); + break; + } CloseConnection(iter); break; } @@ -457,6 +473,23 @@ } /** + * Will be called by the parser to get the list of sampler channels. + */ +String LSCPServer::ListChannels() { + dmsg(2,("LSCPServer: ListChannels()\n")); + String list; + std::map channels = pSampler->GetSamplerChannels(); + std::map::iterator iter = channels.begin(); + for (; iter != channels.end(); iter++) { + if (list != "") list += ","; + list += ToString(iter->first); + } + LSCPResultSet result; + result.Add(list); + return result.Produce(); +} + +/** * Will be called by the parser to add a sampler channel. */ String LSCPServer::AddChannel() { @@ -710,14 +743,14 @@ DeviceCreationParameter* pParameter = MidiInputDeviceFactory::GetDriverParameter(Driver, Parameter); result.Add("TYPE", pParameter->Type()); result.Add("DESCRIPTION", pParameter->Description()); - result.Add("MANDATORY", pParameter->Mandatory()); - result.Add("FIX", pParameter->Fix()); - result.Add("MULTIPLICITY", pParameter->Multiplicity()); - if (pParameter->Depends()) result.Add("DEPENDS", pParameter->Depends()); - if (pParameter->Default()) result.Add("DEFAULT", pParameter->Default()); - if (pParameter->RangeMin()) result.Add("RANGE_MIN", pParameter->RangeMin()); - if (pParameter->RangeMax()) result.Add("RANGE_MAX", pParameter->RangeMax()); - if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities()); + result.Add("MANDATORY", (pParameter->Mandatory()) ? "true" : "false"); + result.Add("FIX", (pParameter->Fix()) ? "true" : "false"); + result.Add("MULTIPLICITY", (pParameter->Multiplicity()) ? "true" : "false"); + if (pParameter->Depends()) result.Add("DEPENDS", *pParameter->Depends()); + if (pParameter->Default()) result.Add("DEFAULT", *pParameter->Default()); + if (pParameter->RangeMin()) result.Add("RANGE_MIN", *pParameter->RangeMin()); + if (pParameter->RangeMax()) result.Add("RANGE_MAX", *pParameter->RangeMax()); + if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities()); } catch (LinuxSamplerException e) { result.Error(e); @@ -732,14 +765,14 @@ DeviceCreationParameter* pParameter = AudioOutputDeviceFactory::GetDriverParameter(Driver, Parameter); result.Add("TYPE", pParameter->Type()); result.Add("DESCRIPTION", pParameter->Description()); - result.Add("MANDATORY", pParameter->Mandatory()); - result.Add("FIX", pParameter->Fix()); - result.Add("MULTIPLICITY", pParameter->Multiplicity()); - if (pParameter->Depends()) result.Add("DEPENDS", pParameter->Depends()); - if (pParameter->Default()) result.Add("DEFAULT", pParameter->Default()); - if (pParameter->RangeMin()) result.Add("RANGE_MIN", pParameter->RangeMin()); - if (pParameter->RangeMax()) result.Add("RANGE_MAX", pParameter->RangeMax()); - if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities()); + result.Add("MANDATORY", (pParameter->Mandatory()) ? "true" : "false"); + result.Add("FIX", (pParameter->Fix()) ? "true" : "false"); + result.Add("MULTIPLICITY", (pParameter->Multiplicity()) ? "true" : "false"); + if (pParameter->Depends()) result.Add("DEPENDS", *pParameter->Depends()); + if (pParameter->Default()) result.Add("DEFAULT", *pParameter->Default()); + if (pParameter->RangeMin()) result.Add("RANGE_MIN", *pParameter->RangeMin()); + if (pParameter->RangeMax()) result.Add("RANGE_MAX", *pParameter->RangeMax()); + if (pParameter->Possibilities()) result.Add("POSSIBILITIES", *pParameter->Possibilities()); } catch (LinuxSamplerException e) { result.Error(e); @@ -914,7 +947,7 @@ std::map parameters = pPort->DeviceParameters(); if (!parameters[ParameterName]) throw LinuxSamplerException("Midi port does not provice a parameters '" + ParameterName + "'."); DeviceCreationParameter* pParameter = parameters[ParameterName]; - + // return all fields of this audio channel parameter result.Add("TYPE", pParameter->Type()); result.Add("DESCRIPTION", pParameter->Description()); @@ -1077,7 +1110,7 @@ if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); // Driver type name aliasing... if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa"; - if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack"; + if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack"; // Check if there's one audio output device already created // for the intended audio driver type (AudioOutputDriver)... AudioOutputDevice *pDevice = NULL; @@ -1251,6 +1284,16 @@ } /** + * Will be called by the parser to reset the whole sampler. + */ +String LSCPServer::ResetSampler() { + dmsg(2,("LSCPServer: ResetSampler()\n")); + pSampler->Reset(); + LSCPResultSet result; + return result.Produce(); +} + +/** * Will be called by the parser to subscribe a client (frontend) on the * server for receiving event messages. */ @@ -1276,6 +1319,24 @@ return result.Produce(); } +/** + * Will be called by the parser to enable or disable echo mode; if echo + * mode is enabled, all commands from the client will (immediately) be + * echoed back to the client. + */ +String LSCPServer::SetEcho(yyparse_param_t* pSession, double boolean_value) { + dmsg(2,("LSCPServer: SetEcho(val=%f)\n", boolean_value)); + LSCPResultSet result; + try { + if (boolean_value == 0) pSession->bVerbose = false; + else if (boolean_value == 1) pSession->bVerbose = true; + else throw LinuxSamplerException("Not a boolean value, must either be 0 or 1"); + } + catch (LinuxSamplerException e) { + result.Error(e); + } + return result.Produce(); +} // Instrument loader constructor. LSCPLoadInstrument::LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument)