Commit 0d08aa4d authored by sletz's avatar sletz
Browse files

More robust server/client protocol.

git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@4697 0c269be4-1314-0410-8aa9-9f06e86f4224
parent 7a5abaa8
......@@ -36,9 +36,13 @@ John Emmas
Jackdmp changes log
---------------------------
2012-01-13 Stephane Letz <letz@grame.fr>
* More robust server/client protocol.
2012-01-11 Stephane Letz <letz@grame.fr>
* Factorize code the server/client request.
* Factorize code the server/client request in JackRequestDecoder class.
2012-01-06 Stephane Letz <letz@grame.fr>
......
......@@ -157,7 +157,7 @@ bool JackEngine::Process(jack_time_t cur_cycle_begin, jack_time_t prev_cycle_end
// Cycle begin
fEngineControl->CycleBegin(fClientTable, fGraphManager, cur_cycle_begin, prev_cycle_end);
// Graph
if (fGraphManager->IsFinishedGraph()) {
ProcessNext(cur_cycle_begin);
......@@ -1000,7 +1000,7 @@ void JackEngine::SessionNotify(int refnum, const char *target, jack_session_even
}
}
void JackEngine::SessionReply(int refnum)
int JackEngine::SessionReply(int refnum)
{
JackClientInterface* client = fClientTable[refnum];
char uuid_buf[JACK_UUID_SIZE];
......@@ -1019,72 +1019,73 @@ void JackEngine::SessionReply(int refnum)
}
fSessionResult = NULL;
}
return 0;
}
void JackEngine::GetUUIDForClientName(const char *client_name, char *uuid_res, int *result)
int JackEngine::GetUUIDForClientName(const char *client_name, char *uuid_res)
{
for (int i = 0; i < CLIENT_NUM; i++) {
JackClientInterface* client = fClientTable[i];
if (client && (strcmp(client_name, client->GetClientControl()->fName) == 0)) {
snprintf(uuid_res, JACK_UUID_SIZE, "%d", client->GetClientControl()->fSessionID);
*result = 0;
return;
return 0;
}
}
// Did not find name.
*result = -1;
return -1;
}
void JackEngine::GetClientNameForUUID(const char *uuid, char *name_res, int *result)
int JackEngine::GetClientNameForUUID(const char *uuid, char *name_res)
{
for (int i = 0; i < CLIENT_NUM; i++) {
JackClientInterface* client = fClientTable[i];
if (!client)
if (!client) {
continue;
}
char uuid_buf[JACK_UUID_SIZE];
snprintf(uuid_buf, JACK_UUID_SIZE, "%d", client->GetClientControl()->fSessionID);
if (strcmp(uuid,uuid_buf) == 0) {
strncpy(name_res, client->GetClientControl()->fName, JACK_CLIENT_NAME_SIZE);
*result = 0;
return;
return 0;
}
}
// Did not find uuid.
*result = -1;
return -1;
}
void JackEngine::ReserveClientName(const char *name, const char *uuid, int *result)
int JackEngine::ReserveClientName(const char *name, const char *uuid)
{
jack_log("JackEngine::ReserveClientName ( name = %s, uuid = %s )", name, uuid);
if (ClientCheckName(name)) {
*result = -1;
jack_log("name already taken");
return;
return -1;
}
EnsureUUID(atoi(uuid));
fReservationMap[atoi(uuid)] = name;
*result = 0;
return 0;
}
void JackEngine::ClientHasSessionCallback(const char *name, int *result)
int JackEngine::ClientHasSessionCallback(const char *name)
{
JackClientInterface* client = NULL;
for (int i = 0; i < CLIENT_NUM; i++) {
client = fClientTable[i];
if (client && (strcmp(client->GetClientControl()->fName, name) == 0))
if (client && (strcmp(client->GetClientControl()->fName, name) == 0)) {
break;
}
}
if (client) {
*result = client->GetClientControl()->fCallback[kSessionCallback];
return client->GetClientControl()->fCallback[kSessionCallback];
} else {
*result = -1;
return -1;
}
}
......
......@@ -148,12 +148,12 @@ class SERVER_EXPORT JackEngine : public JackLockAble
// Session management
void SessionNotify(int refnum, const char *target, jack_session_event_type_t type, const char *path, detail::JackChannelTransactionInterface *socket, JackSessionNotifyResult** result);
void SessionReply(int refnum);
int SessionReply(int refnum);
void GetUUIDForClientName(const char *client_name, char *uuid_res, int *result);
void GetClientNameForUUID(const char *uuid, char *name_res, int *result);
void ReserveClientName(const char *name, const char *uuid, int *result);
void ClientHasSessionCallback(const char *name, int *result);
int GetUUIDForClientName(const char *client_name, char *uuid_res);
int GetClientNameForUUID(const char *uuid, char *name_res);
int ReserveClientName(const char *name, const char *uuid);
int ClientHasSessionCallback(const char *name);
};
......
......@@ -142,8 +142,7 @@ class JackInternalClientChannel : public detail::JackClientChannelInterface
{
JackSessionNotifyResult* res;
fEngine->SessionNotify(refnum, target, type, path, NULL, &res);
if (res == NULL)
{
if (res == NULL) {
*result = NULL;
return;
}
......@@ -154,28 +153,27 @@ class JackInternalClientChannel : public detail::JackClientChannelInterface
void SessionReply(int refnum, int* result)
{
fEngine->SessionReply(refnum);
*result = 0;
*result = fEngine->SessionReply(refnum);
}
void GetUUIDForClientName(int refnum, const char* client_name, char* uuid_res, int* result)
{
fEngine->GetUUIDForClientName(client_name, uuid_res, result);
*result = fEngine->GetUUIDForClientName(client_name, uuid_res);
}
void GetClientNameForUUID(int refnum, const char* uuid, char* name_res, int* result)
{
fEngine->GetClientNameForUUID(uuid, name_res, result);
*result = fEngine->GetClientNameForUUID(uuid, name_res);
}
void ReserveClientName(int refnum, const char* client_name, const char *uuid, int* result)
{
fEngine->ReserveClientName(client_name, uuid, result);
*result = fEngine->ReserveClientName(client_name, uuid);
}
void ClientHasSessionCallback(const char* client_name, int* result)
{
fEngine->ClientHasSessionCallback(client_name, result);
*result = fEngine->ClientHasSessionCallback(client_name);
}
......
......@@ -333,42 +333,42 @@ class SERVER_EXPORT JackLockedEngine
CATCH_EXCEPTION
}
void SessionReply(int refnum)
int SessionReply(int refnum)
{
TRY_CALL
JackLock lock(&fEngine);
fEngine.SessionReply(refnum);
CATCH_EXCEPTION
return fEngine.SessionReply(refnum);
CATCH_EXCEPTION_RETURN
}
void GetUUIDForClientName(const char *client_name, char *uuid_res, int *result)
int GetUUIDForClientName(const char *client_name, char *uuid_res)
{
TRY_CALL
JackLock lock(&fEngine);
fEngine.GetUUIDForClientName(client_name, uuid_res, result);
CATCH_EXCEPTION
return fEngine.GetUUIDForClientName(client_name, uuid_res);
CATCH_EXCEPTION_RETURN
}
void GetClientNameForUUID(const char *uuid, char *name_res, int *result)
int GetClientNameForUUID(const char *uuid, char *name_res)
{
TRY_CALL
JackLock lock(&fEngine);
fEngine.GetClientNameForUUID(uuid, name_res, result);
CATCH_EXCEPTION
return fEngine.GetClientNameForUUID(uuid, name_res);
CATCH_EXCEPTION_RETURN
}
void ReserveClientName(const char *name, const char *uuid, int *result)
int ReserveClientName(const char *name, const char *uuid)
{
TRY_CALL
JackLock lock(&fEngine);
fEngine.ReserveClientName(name, uuid, result);
CATCH_EXCEPTION
return fEngine.ReserveClientName(name, uuid);
CATCH_EXCEPTION_RETURN
}
void ClientHasSessionCallback(const char *name, int *result)
int ClientHasSessionCallback(const char *name)
{
TRY_CALL
JackLock lock(&fEngine);
fEngine.ClientHasSessionCallback(name, result);
CATCH_EXCEPTION
return fEngine.ClientHasSessionCallback(name);
CATCH_EXCEPTION_RETURN
}
};
......
This diff is collapsed.
This diff is collapsed.
......@@ -52,7 +52,7 @@ class JackRequestDecoder
JackRequestDecoder(JackServer* server, JackClientHandlerInterface* handler);
virtual ~JackRequestDecoder();
void HandleRequest(detail::JackChannelTransactionInterface* socket, int type);
int HandleRequest(detail::JackChannelTransactionInterface* socket, int type);
};
} // end of namespace
......
This diff is collapsed.
......@@ -42,7 +42,7 @@ void* JackPosixThread::ThreadHandler(void* arg)
}
// Signal creation thread when started with StartSync
jack_log("ThreadHandler: start");
jack_log("JackPosixThread::ThreadHandler : start");
obj->fStatus = kIniting;
// Call Init method
......@@ -59,7 +59,7 @@ void* JackPosixThread::ThreadHandler(void* arg)
res = runnable->Execute();
}
jack_log("ThreadHandler: exit");
jack_log("JackPosixThread::ThreadHandler : exit");
pthread_exit(0);
return 0; // never reached
}
......@@ -112,7 +112,7 @@ int JackPosixThread::StartImp(jack_native_thread_t* thread, int priority, int re
if (realtime) {
jack_log("Create RT thread");
jack_log("JackPosixThread::StartImp : create RT thread");
if ((res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED))) {
jack_error("Cannot request explicit scheduling for RT thread res = %d", res);
......@@ -133,7 +133,7 @@ int JackPosixThread::StartImp(jack_native_thread_t* thread, int priority, int re
}
} else {
jack_log("Create non RT thread");
jack_log("JackPosixThread::StartImp : create non RT thread");
}
if ((res = pthread_attr_setstacksize(&attributes, THREAD_STACK))) {
......
......@@ -141,7 +141,7 @@ void JackSocketServerChannel::ClientAdd(detail::JackChannelTransactionInterface*
#ifdef __APPLE__
int on = 1;
if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&on, sizeof(on)) < 0) {
jack_log("setsockopt SO_NOSIGPIPE fd = %ld err = %s", fd, strerror(errno));
jack_log("JackSocketServerChannel::ClientAdd : setsockopt SO_NOSIGPIPE fd = %ld err = %s", fd, strerror(errno));
}
#endif
} else {
......@@ -202,7 +202,7 @@ void JackSocketServerChannel::BuildPoolTable()
int i;
for (i = 1, it = fSocketTable.begin(); it != fSocketTable.end(); it++, i++) {
jack_log("fSocketTable i = %ld fd = %ld", i, it->first);
jack_log("JackSocketServerChannel::BuildPoolTable fSocketTable i = %ld fd = %ld", i, it->first);
fPollTable[i].fd = it->first;
fPollTable[i].events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL;
}
......@@ -224,44 +224,46 @@ bool JackSocketServerChannel::Execute()
// Global poll
if ((poll(fPollTable, fSocketTable.size() + 1, 10000) < 0) && (errno != EINTR)) {
jack_error("Engine poll failed err = %s request thread quits...", strerror(errno));
jack_error("JackSocketServerChannel::Execute : engine poll failed err = %s request thread quits...", strerror(errno));
return false;
} else {
// Poll all clients
for (unsigned int i = 1; i < fSocketTable.size() + 1; i++) {
int fd = fPollTable[i].fd;
jack_log("fPollTable i = %ld fd = %ld", i, fd);
jack_log("JackSocketServerChannel::Execute : fPollTable i = %ld fd = %ld", i, fd);
if (fPollTable[i].revents & ~POLLIN) {
jack_log("Poll client error err = %s", strerror(errno));
jack_log("JackSocketServerChannel::Execute : poll client error err = %s", strerror(errno));
ClientKill(fd);
} else if (fPollTable[i].revents & POLLIN) {
// Read header
JackClientSocket* socket = fSocketTable[fd].second;
// Decode header
JackRequest header;
if (header.Read(socket) < 0) {
jack_log("HandleRequest: cannot read header");
jack_log("JackSocketServerChannel::Execute : cannot decode header");
ClientKill(fd);
return false;
} else {
fDecoder->HandleRequest(socket, header.fType);
// Decode request
} else if (fDecoder->HandleRequest(socket, header.fType) < 0) {
jack_log("JackSocketServerChannel::Execute : cannot decode request");
}
}
}
// Check the server request socket */
if (fPollTable[0].revents & POLLERR)
if (fPollTable[0].revents & POLLERR) {
jack_error("Error on server request socket err = %s", strerror(errno));
}
if (fPollTable[0].revents & POLLIN)
if (fPollTable[0].revents & POLLIN) {
ClientCreate();
}
}
BuildPoolTable();
return true;
} catch (JackQuitException& e) {
jack_log("JackSocketServerChannel::Execute JackQuitException");
jack_log("JackSocketServerChannel::Execute : JackQuitException");
return false;
}
}
......
......@@ -85,34 +85,36 @@ void JackClientPipeThread::Close() // Close
bool JackClientPipeThread::Execute()
{
try{
try {
jack_log("JackClientPipeThread::Execute");
JackRequest header;
int res = header.Read(fPipe);
bool ret = true;
// Lock the global mutex
if (WaitForSingleObject(fMutex, INFINITE) == WAIT_FAILED) {
jack_error("JackClientPipeThread::HandleRequest: mutex wait error");
jack_error("JackClientPipeThread::Execute : mutex wait error");
}
// Decode header
if (res < 0) {
jack_log("HandleRequest: cannot read header");
jack_log("JackClientPipeThread::Execute : cannot decode header");
ClientKill();
ret = false;
// Decode request
} else if {fDecoder->HandleRequest(fPipe, header.fType) < 0) {
jack_log("JackClientPipeThread::Execute : cannot decode request");
}
fDecoder->HandleRequest(fPipe, header.fType);
// Unlock the global mutex
if (!ReleaseMutex(fMutex)) {
jack_error("JackClientPipeThread::HandleRequest: mutex release error");
jack_error("JackClientPipeThread::Execute : mutex release error");
}
return ret;
} catch (JackQuitException& e) {
jack_log("JackClientPipeThread::Execute JackQuitException");
jack_log("JackClientPipeThread::Execute : JackQuitException");
return false;
}
}
......
......@@ -32,7 +32,7 @@ DWORD WINAPI JackWinThread::ThreadHandler(void* arg)
JackRunnableInterface* runnable = obj->fRunnable;
// Signal creation thread when started with StartSync
jack_log("ThreadHandler: start");
jack_log("JackWinThread::ThreadHandler : start");
obj->fStatus = kIniting;
// Call Init method
......@@ -50,7 +50,7 @@ DWORD WINAPI JackWinThread::ThreadHandler(void* arg)
}
SetEvent(obj->fEvent);
jack_log("ThreadHandler: exit");
jack_log("JackWinThread::ThreadHandler : exit");
return 0;
}
......@@ -109,14 +109,14 @@ int JackWinThread::StartImp(jack_native_thread_t* thread, int priority, int real
if (realtime) {
jack_log("Create RT thread");
jack_log("JackWinThread::StartImp : create RT thread");
if (!SetThreadPriority(*thread, THREAD_PRIORITY_TIME_CRITICAL)) {
jack_error("Cannot set priority class = %d", GetLastError());
return -1;
}
} else {
jack_log("Create non RT thread");
jack_log("JackWinThread::StartImp : create non RT thread");
}
return 0;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment