--- linuxsampler/trunk/src/network/lscpserver.cpp 2004/07/18 00:29:39 209 +++ linuxsampler/trunk/src/network/lscpserver.cpp 2004/08/17 20:35:04 219 @@ -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,7 +95,7 @@ } 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; @@ -90,10 +104,6 @@ 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, NULL); @@ -118,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; @@ -128,13 +143,17 @@ } //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); + int dummy; // just a temporary hack to fulfill the restart() function prototype + restart(NULL, dummy); // restart the 'scanner' + 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? @@ -155,15 +174,13 @@ } NotifyBufferMutex.Unlock(); } - //It will never get here anyway - //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++) { @@ -227,8 +244,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) { @@ -724,14 +741,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); @@ -746,14 +763,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); @@ -1265,6 +1282,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. */ @@ -1290,6 +1317,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)