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> >(); |
61 |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS"); |
62 |
} |
} |
63 |
|
|
64 |
|
/** |
65 |
|
* Blocks the calling thread until the LSCP Server is initialized and |
66 |
|
* accepting socket connections, if the server is already initialized then |
67 |
|
* this method will return immediately. |
68 |
|
* @param TimeoutSeconds - optional: max. wait time in seconds |
69 |
|
* (default: 0s) |
70 |
|
* @param TimeoutNanoSeconds - optional: max wait time in nano seconds |
71 |
|
* (default: 0ns) |
72 |
|
* @returns 0 on success, a value less than 0 if timeout exceeded |
73 |
|
*/ |
74 |
|
int LSCPServer::WaitUntilInitialized(long TimeoutSeconds, long TimeoutNanoSeconds) { |
75 |
|
return Initialized.WaitAndUnlockIf(false, TimeoutSeconds, TimeoutNanoSeconds); |
76 |
|
} |
77 |
|
|
78 |
int LSCPServer::Main() { |
int LSCPServer::Main() { |
79 |
int hSocket = socket(AF_INET, SOCK_STREAM, 0); |
int hSocket = socket(AF_INET, SOCK_STREAM, 0); |
80 |
if (hSocket < 0) { |
if (hSocket < 0) { |
95 |
} |
} |
96 |
|
|
97 |
listen(hSocket, 1); |
listen(hSocket, 1); |
98 |
dmsg(1,("LSCPServer: Server running.\n")); // server running |
Initialized.Set(true); |
99 |
|
|
100 |
// now wait for client connections and handle their requests |
// now wait for client connections and handle their requests |
101 |
sockaddr_in client; |
sockaddr_in client; |
104 |
FD_SET(hSocket, &fdSet); |
FD_SET(hSocket, &fdSet); |
105 |
int maxSessions = hSocket; |
int maxSessions = hSocket; |
106 |
|
|
|
// Parser initialization |
|
|
yyparse_param_t yyparse_param; |
|
|
yyparse_param.pServer = this; |
|
|
|
|
107 |
while (true) { |
while (true) { |
108 |
fd_set selectSet = fdSet; |
fd_set selectSet = fdSet; |
109 |
int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL); |
int retval = select(maxSessions+1, &selectSet, NULL, NULL, NULL); |
128 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
129 |
} |
} |
130 |
|
|
131 |
hSessions.push_back(socket); |
// Parser initialization |
132 |
|
yyparse_param_t yyparse_param; |
133 |
|
yyparse_param.pServer = this; |
134 |
|
yyparse_param.hSession = socket; |
135 |
|
|
136 |
|
Sessions.push_back(yyparse_param); |
137 |
FD_SET(socket, &fdSet); |
FD_SET(socket, &fdSet); |
138 |
if (socket > maxSessions) |
if (socket > maxSessions) |
139 |
maxSessions = socket; |
maxSessions = socket; |
143 |
} |
} |
144 |
|
|
145 |
//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. |
146 |
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++) { |
147 |
if (FD_ISSET(*iter, &selectSet)) { //Was it this socket? |
if (FD_ISSET((*iter).hSession, &selectSet)) { //Was it this socket? |
148 |
if (GetLSCPCommand(iter)) { //Have we read the entire command? |
if (GetLSCPCommand(iter)) { //Have we read the entire command? |
149 |
dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket)); |
dmsg(3,("LSCPServer: Got command on socket %d, calling parser.\n", currentSocket)); |
150 |
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 |
151 |
currentSocket = *iter; //a hack |
currentSocket = (*iter).hSession; //a hack |
152 |
int result = yyparse(&yyparse_param); |
if ((*iter).bVerbose) { // if echo mode enabled |
153 |
|
AnswerClient(bufferedCommands[currentSocket]); |
154 |
|
} |
155 |
|
int result = yyparse(&(*iter)); |
156 |
currentSocket = -1; //continuation of a hack |
currentSocket = -1; //continuation of a hack |
157 |
dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket)); |
dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket)); |
158 |
if (result == LSCP_QUIT) { //Was it a quit command by any chance? |
if (result == LSCP_QUIT) { //Was it a quit command by any chance? |
177 |
//yylex_destroy(yyparse_param.pScanner); |
//yylex_destroy(yyparse_param.pScanner); |
178 |
} |
} |
179 |
|
|
180 |
void LSCPServer::CloseConnection( std::vector<int>::iterator iter ) { |
void LSCPServer::CloseConnection( std::vector<yyparse_param_t>::iterator iter ) { |
181 |
int socket = *iter; |
int socket = (*iter).hSession; |
182 |
dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket)); |
dmsg(1,("LSCPServer: Client connection terminated on socket:%d.\n",socket)); |
183 |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket)); |
LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_misc, "Client connection terminated on socket", socket)); |
184 |
hSessions.erase(iter); |
Sessions.erase(iter); |
185 |
FD_CLR(socket, &fdSet); |
FD_CLR(socket, &fdSet); |
186 |
SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any) |
SubscriptionMutex.Lock(); //Must unsubscribe this socket from all events (if any) |
187 |
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++) { |
193 |
bufferedNotifies.erase(socket); |
bufferedNotifies.erase(socket); |
194 |
close(socket); |
close(socket); |
195 |
NotifyMutex.Unlock(); |
NotifyMutex.Unlock(); |
196 |
|
//yylex_destroy((*iter).pScanner); |
197 |
} |
} |
198 |
|
|
199 |
void LSCPServer::SendLSCPNotify( LSCPEvent event ) { |
void LSCPServer::SendLSCPNotify( LSCPEvent event ) { |
246 |
* If command is read, it will return true. Otherwise false is returned. |
* If command is read, it will return true. Otherwise false is returned. |
247 |
* 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. |
248 |
*/ |
*/ |
249 |
bool LSCPServer::GetLSCPCommand( std::vector<int>::iterator iter ) { |
bool LSCPServer::GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ) { |
250 |
int socket = *iter; |
int socket = (*iter).hSession; |
251 |
char c; |
char c; |
252 |
int i = 0; |
int i = 0; |
253 |
while (true) { |
while (true) { |
473 |
} |
} |
474 |
|
|
475 |
/** |
/** |
476 |
|
* Will be called by the parser to get the list of sampler channels. |
477 |
|
*/ |
478 |
|
String LSCPServer::ListChannels() { |
479 |
|
dmsg(2,("LSCPServer: ListChannels()\n")); |
480 |
|
String list; |
481 |
|
std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels(); |
482 |
|
std::map<uint,SamplerChannel*>::iterator iter = channels.begin(); |
483 |
|
for (; iter != channels.end(); iter++) { |
484 |
|
if (list != "") list += ","; |
485 |
|
list += ToString(iter->first); |
486 |
|
} |
487 |
|
LSCPResultSet result; |
488 |
|
result.Add(list); |
489 |
|
return result.Produce(); |
490 |
|
} |
491 |
|
|
492 |
|
/** |
493 |
* Will be called by the parser to add a sampler channel. |
* Will be called by the parser to add a sampler channel. |
494 |
*/ |
*/ |
495 |
String LSCPServer::AddChannel() { |
String LSCPServer::AddChannel() { |
1309 |
return result.Produce(); |
return result.Produce(); |
1310 |
} |
} |
1311 |
|
|
1312 |
|
/** |
1313 |
|
* Will be called by the parser to enable or disable echo mode; if echo |
1314 |
|
* mode is enabled, all commands from the client will (immediately) be |
1315 |
|
* echoed back to the client. |
1316 |
|
*/ |
1317 |
|
String LSCPServer::SetEcho(yyparse_param_t* pSession, double boolean_value) { |
1318 |
|
dmsg(2,("LSCPServer: SetEcho(val=%f)\n", boolean_value)); |
1319 |
|
LSCPResultSet result; |
1320 |
|
try { |
1321 |
|
if (boolean_value == 0) pSession->bVerbose = false; |
1322 |
|
else if (boolean_value == 1) pSession->bVerbose = true; |
1323 |
|
else throw LinuxSamplerException("Not a boolean value, must either be 0 or 1"); |
1324 |
|
} |
1325 |
|
catch (LinuxSamplerException e) { |
1326 |
|
result.Error(e); |
1327 |
|
} |
1328 |
|
return result.Produce(); |
1329 |
|
} |
1330 |
|
|
1331 |
// Instrument loader constructor. |
// Instrument loader constructor. |
1332 |
LSCPLoadInstrument::LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument) |
LSCPLoadInstrument::LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument) |