25 |
#include "lscpevent.h" |
#include "lscpevent.h" |
26 |
|
|
27 |
#include "../engines/gig/Engine.h" |
#include "../engines/gig/Engine.h" |
28 |
#include "../audiodriver/AudioOutputDeviceFactory.h" |
#include "../drivers/audio/AudioOutputDeviceFactory.h" |
29 |
#include "../mididriver/MidiInputDeviceFactory.h" |
#include "../drivers/midi/MidiInputDeviceFactory.h" |
30 |
|
|
31 |
/** |
/** |
32 |
* Below are a few static members of the LSCPServer class. |
* Below are a few static members of the LSCPServer class. |
43 |
*/ |
*/ |
44 |
fd_set LSCPServer::fdSet; |
fd_set LSCPServer::fdSet; |
45 |
int LSCPServer::currentSocket = -1; |
int LSCPServer::currentSocket = -1; |
46 |
std::vector<int> LSCPServer::hSessions = std::vector<int>(); |
std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>(); |
47 |
std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>(); |
std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>(); |
48 |
std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>(); |
std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>(); |
49 |
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 = std::map< LSCPEvent::event_t, std::list<int> >(); |
86 |
// now wait for client connections and handle their requests |
// now wait for client connections and handle their requests |
87 |
sockaddr_in client; |
sockaddr_in client; |
88 |
int length = sizeof(client); |
int length = sizeof(client); |
|
struct timeval tv; |
|
|
tv.tv_sec = 30; |
|
|
tv.tv_usec = 0; |
|
89 |
FD_ZERO(&fdSet); |
FD_ZERO(&fdSet); |
90 |
FD_SET(hSocket, &fdSet); |
FD_SET(hSocket, &fdSet); |
91 |
int maxSessions = hSocket; |
int maxSessions = hSocket; |
|
|
|
|
// Parser initialization |
|
|
yyparse_param_t yyparse_param; |
|
|
yyparse_param.pServer = this; |
|
92 |
|
|
93 |
while (true) { |
while (true) { |
94 |
fd_set selectSet = fdSet; |
fd_set selectSet = fdSet; |
95 |
int retval = select(maxSessions+1, &selectSet, NULL, NULL, &tv); |
int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL); |
96 |
if (retval == 0) |
if (retval == 0) |
97 |
continue; //Nothing in 30 seconds, try again |
continue; //Nothing try again |
98 |
if (retval == -1) { |
if (retval == -1) { |
99 |
std::cerr << "LSCPServer: Socket select error." << std::endl; |
std::cerr << "LSCPServer: Socket select error." << std::endl; |
100 |
close(hSocket); |
close(hSocket); |
101 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
102 |
} |
} |
103 |
|
|
104 |
//Accept new connections now (if any) |
//Accept new connections now (if any) |
105 |
if (FD_ISSET(hSocket, &selectSet)) { |
if (FD_ISSET(hSocket, &selectSet)) { |
106 |
int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length); |
int socket = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length); |
114 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
115 |
} |
} |
116 |
|
|
117 |
hSessions.push_back(socket); |
// Parser initialization |
118 |
|
yyparse_param_t yyparse_param; |
119 |
|
yyparse_param.pServer = this; |
120 |
|
yyparse_param.hSession = socket; |
121 |
|
|
122 |
|
Sessions.push_back(yyparse_param); |
123 |
FD_SET(socket, &fdSet); |
FD_SET(socket, &fdSet); |
124 |
if (socket > maxSessions) |
if (socket > maxSessions) |
125 |
maxSessions = socket; |
maxSessions = socket; |
129 |
} |
} |
130 |
|
|
131 |
//Something was selected and it was not the hSocket, so it must be some command(s) coming. |
//Something was selected and it was not the hSocket, so it must be some command(s) coming. |
132 |
for (std::vector<int>::iterator iter = hSessions.begin(); iter != hSessions.end(); iter++) { |
for (std::vector<yyparse_param_t>::iterator iter = Sessions.begin(); iter != Sessions.end(); iter++) { |
133 |
if (FD_ISSET(*iter, &selectSet)) { //Was it this socket? |
if (FD_ISSET((*iter).hSession, &selectSet)) { //Was it this socket? |
134 |
if (GetLSCPCommand(iter)) { //Have we read the entire command? |
if (GetLSCPCommand(iter)) { //Have we read the entire command? |
135 |
dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket)); |
dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket)); |
136 |
yylex_init(&yyparse_param.pScanner); |
yylex_init(&((*iter).pScanner)); //FIXME: should me moved out of this loop and initialized only when a new session is created |
137 |
currentSocket = *iter; //a hack |
currentSocket = (*iter).hSession; //a hack |
138 |
int result = yyparse(&yyparse_param); |
if ((*iter).bVerbose) { // if echo mode enabled |
139 |
|
AnswerClient(bufferedCommands[currentSocket]); |
140 |
|
} |
141 |
|
int result = yyparse(&(*iter)); |
142 |
currentSocket = -1; //continuation of a hack |
currentSocket = -1; //continuation of a hack |
143 |
dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket)); |
dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket)); |
144 |
if (result == LSCP_QUIT) { //Was it a quit command by any chance? |
if (result == LSCP_QUIT) { //Was it a quit command by any chance? |
147 |
} |
} |
148 |
//socket may have been closed, iter may be invalid, get out of the loop for now. |
//socket may have been closed, iter may be invalid, get out of the loop for now. |
149 |
//we'll be back if there is data. |
//we'll be back if there is data. |
150 |
break; |
break; |
151 |
} |
} |
152 |
} |
} |
153 |
|
|
163 |
//yylex_destroy(yyparse_param.pScanner); |
//yylex_destroy(yyparse_param.pScanner); |
164 |
} |
} |
165 |
|
|
166 |
void LSCPServer::CloseConnection( std::vector<int>::iterator iter ) { |
void LSCPServer::CloseConnection( std::vector<yyparse_param_t>::iterator iter ) { |
167 |
int socket = *iter; |
int socket = (*iter).hSession; |
168 |
dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket)); |
dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket)); |
169 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket)); |
170 |
hSessions.erase(iter); |
Sessions.erase(iter); |
171 |
FD_CLR(socket, &fdSet); |
FD_CLR(socket, &fdSet); |
172 |
SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any) |
SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any) |
173 |
for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) { |
for (std::map< LSCPEvent::event_t, std::list<int> >::iterator iter = eventSubscriptions.begin(); iter != eventSubscriptions.end(); iter++) { |
179 |
bufferedNotifies.erase(socket); |
bufferedNotifies.erase(socket); |
180 |
close(socket); |
close(socket); |
181 |
NotifyMutex.Unlock(); |
NotifyMutex.Unlock(); |
182 |
|
//yylex_destroy((*iter).pScanner); |
183 |
} |
} |
184 |
|
|
185 |
void LSCPServer::SendLSCPNotify( LSCPEvent event ) { |
void LSCPServer::SendLSCPNotify( LSCPEvent event ) { |
232 |
* If command is read, it will return true. Otherwise false is returned. |
* If command is read, it will return true. Otherwise false is returned. |
233 |
* In any case the received portion (complete or incomplete) is saved into bufferedCommand map. |
* In any case the received portion (complete or incomplete) is saved into bufferedCommand map. |
234 |
*/ |
*/ |
235 |
bool LSCPServer::GetLSCPCommand( std::vector<int>::iterator iter ) { |
bool LSCPServer::GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ) { |
236 |
int socket = *iter; |
int socket = (*iter).hSession; |
237 |
char c; |
char c; |
238 |
int i = 0; |
int i = 0; |
239 |
while (true) { |
while (true) { |
243 |
break; |
break; |
244 |
} |
} |
245 |
if (result == 1) { |
if (result == 1) { |
246 |
if (c == '\r') |
if (c == '\r') |
247 |
continue; //Ignore CR |
continue; //Ignore CR |
248 |
if (c == '\n') { |
if (c == '\n') { |
249 |
|
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Received \'" + bufferedCommands[socket] + "\' on socket", socket)); |
250 |
bufferedCommands[socket] += "\n"; |
bufferedCommands[socket] += "\n"; |
251 |
return true; //Complete command was read |
return true; //Complete command was read |
252 |
} |
} |
270 |
break; |
break; |
271 |
case EAGAIN: |
case EAGAIN: |
272 |
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")); |
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")); |
273 |
break; |
break; |
274 |
case EINTR: |
case EINTR: |
275 |
dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n")); |
dmsg(2,("LSCPScanner: The receive was interrupted by delivery of a signal before any data were available.\n")); |
276 |
break; |
break; |
277 |
case EFAULT: |
case EFAULT: |
278 |
dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n")); |
dmsg(2,("LSCPScanner: The receive buffer pointer(s) point outside the process's address space.\n")); |
279 |
break; |
break; |
280 |
case EINVAL: |
case EINVAL: |
281 |
dmsg(2,("LSCPScanner: Invalid argument passed.\n")); |
dmsg(2,("LSCPScanner: Invalid argument passed.\n")); |
282 |
break; |
break; |
283 |
case ENOMEM: |
case ENOMEM: |
284 |
dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n")); |
dmsg(2,("LSCPScanner: Could not allocate memory for recvmsg.\n")); |
285 |
break; |
break; |
286 |
default: |
default: |
287 |
dmsg(2,("LSCPScanner: Unknown recv() error.\n")); |
dmsg(2,("LSCPScanner: Unknown recv() error.\n")); |
288 |
break; |
break; |
289 |
} |
} |
290 |
CloseConnection(iter); |
CloseConnection(iter); |
291 |
break; |
break; |
292 |
} |
} |
459 |
} |
} |
460 |
|
|
461 |
/** |
/** |
462 |
|
* Will be called by the parser to get the list of sampler channels. |
463 |
|
*/ |
464 |
|
String LSCPServer::ListChannels() { |
465 |
|
dmsg(2,("LSCPServer: ListChannels()\n")); |
466 |
|
String list; |
467 |
|
std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels(); |
468 |
|
std::map<uint,SamplerChannel*>::iterator iter = channels.begin(); |
469 |
|
for (; iter != channels.end(); iter++) { |
470 |
|
if (list != "") list += ","; |
471 |
|
list += ToString(iter->first); |
472 |
|
} |
473 |
|
LSCPResultSet result; |
474 |
|
result.Add(list); |
475 |
|
return result.Produce(); |
476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
* Will be called by the parser to add a sampler channel. |
* Will be called by the parser to add a sampler channel. |
480 |
*/ |
*/ |
481 |
String LSCPServer::AddChannel() { |
String LSCPServer::AddChannel() { |
916 |
return result.Produce(); |
return result.Produce(); |
917 |
} |
} |
918 |
|
|
919 |
|
String LSCPServer::GetMidiInputPortParameterInfo(uint DeviceId, uint PortId, String ParameterName) { |
920 |
|
dmsg(2,("LSCPServer: GetMidiInputPortParameterInfo(DeviceId=%d,PortId=%d,ParameterName=%s)\n",DeviceId,PortId,ParameterName.c_str())); |
921 |
|
LSCPResultSet result; |
922 |
|
try { |
923 |
|
// get audio output device |
924 |
|
std::map<uint,MidiInputDevice*> devices = pSampler->GetMidiInputDevices(); |
925 |
|
if (!devices[DeviceId]) throw LinuxSamplerException("There is no midi input device with index " + ToString(DeviceId) + "."); |
926 |
|
MidiInputDevice* pDevice = devices[DeviceId]; |
927 |
|
|
928 |
|
// get midi port |
929 |
|
MidiInputDevice::MidiInputPort* pPort = pDevice->GetPort(PortId); |
930 |
|
if (!pPort) throw LinuxSamplerException("Midi input device does not have port " + ToString(PortId) + "."); |
931 |
|
|
932 |
|
// get desired port parameter |
933 |
|
std::map<String,DeviceCreationParameter*> parameters = pPort->DeviceParameters(); |
934 |
|
if (!parameters[ParameterName]) throw LinuxSamplerException("Midi port does not provice a parameters '" + ParameterName + "'."); |
935 |
|
DeviceCreationParameter* pParameter = parameters[ParameterName]; |
936 |
|
|
937 |
|
// return all fields of this audio channel parameter |
938 |
|
result.Add("TYPE", pParameter->Type()); |
939 |
|
result.Add("DESCRIPTION", pParameter->Description()); |
940 |
|
result.Add("FIX", pParameter->Fix()); |
941 |
|
result.Add("MULTIPLICITY", pParameter->Multiplicity()); |
942 |
|
if (pParameter->RangeMin()) result.Add("RANGE_MIN", pParameter->RangeMin()); |
943 |
|
if (pParameter->RangeMax()) result.Add("RANGE_MAX", pParameter->RangeMax()); |
944 |
|
if (pParameter->Possibilities()) result.Add("POSSIBILITIES", pParameter->Possibilities()); |
945 |
|
} |
946 |
|
catch (LinuxSamplerException e) { |
947 |
|
result.Error(e); |
948 |
|
} |
949 |
|
return result.Produce(); |
950 |
|
} |
951 |
|
|
952 |
String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) { |
String LSCPServer::GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName) { |
953 |
dmsg(2,("LSCPServer: GetAudioOutputChannelParameterInfo(DeviceId=%d,ChannelId=%d,ParameterName=%s)\n",DeviceId,ChannelId,ParameterName.c_str())); |
dmsg(2,("LSCPServer: GetAudioOutputChannelParameterInfo(DeviceId=%d,ChannelId=%d,ParameterName=%s)\n",DeviceId,ChannelId,ParameterName.c_str())); |
954 |
LSCPResultSet result; |
LSCPResultSet result; |
1096 |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); |
if (!pSamplerChannel) throw LinuxSamplerException("Invalid channel number " + ToString(uiSamplerChannel)); |
1097 |
// Driver type name aliasing... |
// Driver type name aliasing... |
1098 |
if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa"; |
if (AudioOutputDriver == "ALSA") AudioOutputDriver = "Alsa"; |
1099 |
if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack"; |
if (AudioOutputDriver == "JACK") AudioOutputDriver = "Jack"; |
1100 |
// Check if there's one audio output device already created |
// Check if there's one audio output device already created |
1101 |
// for the intended audio driver type (AudioOutputDriver)... |
// for the intended audio driver type (AudioOutputDriver)... |
1102 |
AudioOutputDevice *pDevice = NULL; |
AudioOutputDevice *pDevice = NULL; |
1295 |
return result.Produce(); |
return result.Produce(); |
1296 |
} |
} |
1297 |
|
|
1298 |
|
/** |
1299 |
|
* Will be called by the parser to enable or disable echo mode; if echo |
1300 |
|
* mode is enabled, all commands from the client will (immediately) be |
1301 |
|
* echoed back to the client. |
1302 |
|
*/ |
1303 |
|
String LSCPServer::SetEcho(yyparse_param_t* pSession, double boolean_value) { |
1304 |
|
dmsg(2,("LSCPServer: SetEcho(val=%f)\n", boolean_value)); |
1305 |
|
LSCPResultSet result; |
1306 |
|
try { |
1307 |
|
if (boolean_value == 0) pSession->bVerbose = false; |
1308 |
|
else if (boolean_value == 1) pSession->bVerbose = true; |
1309 |
|
else throw LinuxSamplerException("Not a boolean value, must either be 0 or 1"); |
1310 |
|
} |
1311 |
|
catch (LinuxSamplerException e) { |
1312 |
|
result.Error(e); |
1313 |
|
} |
1314 |
|
return result.Produce(); |
1315 |
|
} |
1316 |
|
|
1317 |
// Instrument loader constructor. |
// Instrument loader constructor. |
1318 |
LSCPLoadInstrument::LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument) |
LSCPLoadInstrument::LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument) |