diff options
author | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2012-06-23 17:23:49 -0500 |
---|---|---|
committer | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2012-06-23 17:23:49 -0500 |
commit | 8dcfe72c396a6f0c4bafd2ed23ba52a475154ef2 (patch) | |
tree | a0a77ec1d7d7cd826e1f2ba92ea4f96351bae5e3 /clients/tde/src/part | |
parent | b48b26b86975d2166a4da7fc41086facefb3c4f2 (diff) | |
download | ulab-8dcfe72c396a6f0c4bafd2ed23ba52a475154ef2.tar.gz ulab-8dcfe72c396a6f0c4bafd2ed23ba52a475154ef2.zip |
Fix a number of crashes and generally clean up the code
Diffstat (limited to 'clients/tde/src/part')
-rw-r--r-- | clients/tde/src/part/commanalyzer/Makefile.am | 5 | ||||
-rw-r--r-- | clients/tde/src/part/commanalyzer/part.cpp | 309 |
2 files changed, 180 insertions, 134 deletions
diff --git a/clients/tde/src/part/commanalyzer/Makefile.am b/clients/tde/src/part/commanalyzer/Makefile.am index db98e9e..d2db726 100644 --- a/clients/tde/src/part/commanalyzer/Makefile.am +++ b/clients/tde/src/part/commanalyzer/Makefile.am @@ -1,5 +1,6 @@ -INCLUDES = $(all_includes) -I$(top_srcdir)/src -I$(top_srcdir)/src/widgets -METASOURCES = AUTO +INCLUDES = $(all_includes) -I$(top_srcdir)/src -I$(top_srcdir)/src/widgets +KDE_CXXFLAGS = $(USE_EXCEPTIONS) +METASOURCES = AUTO #Part kde_module_LTLIBRARIES = libremotelab_commanalyzer.la diff --git a/clients/tde/src/part/commanalyzer/part.cpp b/clients/tde/src/part/commanalyzer/part.cpp index 592e64d..dbee9be 100644 --- a/clients/tde/src/part/commanalyzer/part.cpp +++ b/clients/tde/src/part/commanalyzer/part.cpp @@ -26,6 +26,12 @@ #include "floatspinbox.h" #include "layout.h" +/* exception handling */ +struct exit_exception { + int c; + exit_exception(int c):c(c) { } +}; + namespace RemoteLab { typedef KParts::GenericFactory<RemoteLab::CommAnalyzerPart> Factory; @@ -88,7 +94,11 @@ bool CommAnalyzerPart::closeURL() { } m_url = KURL(); - + + if (m_instrumentMutex->locked()) { + throw exit_exception(-1); + } + return true; } @@ -97,24 +107,30 @@ TQString CommAnalyzerPart::callServerMethod(int command) { printf("[WARN] An attempt was made to access the instrument asynchronously, and was rejected to prevent a lockup\n\r"); fflush(stdout); return TQString::null; } - m_instrumentMutex->lock(); - if (m_socket->state() == TQSocket::Connected) { - TQString cmd = TQChar(command); - cmd.append('\r'); - m_socket->writeBlock(cmd.latin1(), cmd.length()); - // Read from the server - TQString serverRet; - while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { - char data[1]; - if( m_socket->readBlock(data, 1) > 0) { - serverRet.append(data[0]); + try { + m_instrumentMutex->lock(); + if (m_socket->state() == TQSocket::Connected) { + TQString cmd = TQChar(command); + cmd.append('\r'); + m_socket->writeBlock(cmd.latin1(), cmd.length()); + // Read from the server + TQString serverRet; + while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { + char data[1]; + if( m_socket->readBlock(data, 1) > 0) { + serverRet.append(data[0]); + } + tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); } - tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); + m_instrumentMutex->unlock(); + return serverRet; + } + else { + m_instrumentMutex->unlock(); + return TQString::null; } - m_instrumentMutex->unlock(); - return serverRet; } - else { + catch (exit_exception& e) { m_instrumentMutex->unlock(); return TQString::null; } @@ -125,33 +141,39 @@ int16_t CommAnalyzerPart::callServerMethodInt16(int command) { printf("[WARN] An attempt was made to access the instrument asynchronously, and was rejected to prevent a lockup\n\r"); fflush(stdout); return 0; } - m_instrumentMutex->lock(); - if (m_socket->state() == TQSocket::Connected) { - TQString cmd = TQChar(command); - cmd.append('\r'); - m_socket->writeBlock(cmd.latin1(), cmd.length()); - // Read from the server - int bytesread = 0; - int16_t data[1]; - while ((bytesread < 2) && (m_socket->state() == TQSocket::Connected)) { - int ret = m_socket->readBlock(((char*)data)+bytesread, 1); - if (ret > 0) { - bytesread += ret; + try { + m_instrumentMutex->lock(); + if (m_socket->state() == TQSocket::Connected) { + TQString cmd = TQChar(command); + cmd.append('\r'); + m_socket->writeBlock(cmd.latin1(), cmd.length()); + // Read from the server + int bytesread = 0; + int16_t data[1]; + while ((bytesread < 2) && (m_socket->state() == TQSocket::Connected)) { + int ret = m_socket->readBlock(((char*)data)+bytesread, 1); + if (ret > 0) { + bytesread += ret; + } + tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); } - tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); - } - TQString serverRet; - while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { - char data[1]; - if( m_socket->readBlock(data, 1) > 0) { - serverRet.append(data[0]); + TQString serverRet; + while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { + char data[1]; + if( m_socket->readBlock(data, 1) > 0) { + serverRet.append(data[0]); + } + tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); } - tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); + m_instrumentMutex->unlock(); + return data[0]; + } + else { + m_instrumentMutex->unlock(); + return 0; } - m_instrumentMutex->unlock(); - return data[0]; } - else { + catch (exit_exception& e) { m_instrumentMutex->unlock(); return 0; } @@ -162,33 +184,39 @@ double CommAnalyzerPart::callServerMethodDouble(int command) { printf("[WARN] An attempt was made to access the instrument asynchronously, and was rejected to prevent a lockup\n\r"); fflush(stdout); return 0; } - m_instrumentMutex->lock(); - if (m_socket->state() == TQSocket::Connected) { - TQString cmd = TQChar(command); - cmd.append('\r'); - m_socket->writeBlock(cmd.latin1(), cmd.length()); - // Read from the server - unsigned int bytesread = 0; - double data[1]; - while ((bytesread < sizeof(double)) && (m_socket->state() == TQSocket::Connected)) { - int ret = m_socket->readBlock(((char*)data)+bytesread, 1); - if (ret > 0) { - bytesread += ret; + try { + m_instrumentMutex->lock(); + if (m_socket->state() == TQSocket::Connected) { + TQString cmd = TQChar(command); + cmd.append('\r'); + m_socket->writeBlock(cmd.latin1(), cmd.length()); + // Read from the server + unsigned int bytesread = 0; + double data[1]; + while ((bytesread < sizeof(double)) && (m_socket->state() == TQSocket::Connected)) { + int ret = m_socket->readBlock(((char*)data)+bytesread, 1); + if (ret > 0) { + bytesread += ret; + } + tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); } - tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); - } - TQString serverRet; - while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { - char data[1]; - if( m_socket->readBlock(data, 1) > 0) { - serverRet.append(data[0]); + TQString serverRet; + while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { + char data[1]; + if( m_socket->readBlock(data, 1) > 0) { + serverRet.append(data[0]); + } + tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); } - tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); + m_instrumentMutex->unlock(); + return data[0]; + } + else { + m_instrumentMutex->unlock(); + return 0; } - m_instrumentMutex->unlock(); - return data[0]; } - else { + catch (exit_exception& e) { m_instrumentMutex->unlock(); return 0; } @@ -199,23 +227,30 @@ void CommAnalyzerPart::sendServerCommandWithParameter(int command, TQString para printf("[WARN] An attempt was made to access the instrument asynchronously, and was rejected to prevent a lockup\n\r"); fflush(stdout); return; } - m_instrumentMutex->lock(); - if (m_socket->state() == TQSocket::Connected) { - TQString cmd = TQChar(command); - param = TQString("%1%2%3").arg(param).arg(TQChar('°')).arg(TQChar('\r')); - cmd += param; - m_socket->writeBlock(cmd.ascii(), cmd.length()); - // Read from the server - TQString serverRet; - while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { - char data[1]; - if( m_socket->readBlock(data, 1) > 0) { - serverRet.append(data[0]); + try { + m_instrumentMutex->lock(); + if (m_socket->state() == TQSocket::Connected) { + TQString cmd = TQChar(command); + param = TQString("%1%2%3").arg(param).arg(TQChar('°')).arg(TQChar('\r')); + cmd += param; + m_socket->writeBlock(cmd.ascii(), cmd.length()); + // Read from the server + TQString serverRet; + while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { + char data[1]; + if( m_socket->readBlock(data, 1) > 0) { + serverRet.append(data[0]); + } + tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); } - tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); } + m_instrumentMutex->unlock(); + return; + } + catch (exit_exception& e) { + m_instrumentMutex->unlock(); + return; } - m_instrumentMutex->unlock(); } void CommAnalyzerPart::sendServerCommand(int command) { @@ -223,22 +258,29 @@ void CommAnalyzerPart::sendServerCommand(int command) { printf("[WARN] An attempt was made to access the instrument asynchronously, and was rejected to prevent a lockup\n\r"); fflush(stdout); return; } - m_instrumentMutex->lock(); - if (m_socket->state() == TQSocket::Connected) { - TQString cmd = TQChar(command); - cmd.append('\r'); - m_socket->writeBlock(cmd.latin1(), cmd.length()); - // Read from the server - TQString serverRet; - while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { - char data[1]; - if( m_socket->readBlock(data, 1) > 0) { - serverRet.append(data[0]); + try { + m_instrumentMutex->lock(); + if (m_socket->state() == TQSocket::Connected) { + TQString cmd = TQChar(command); + cmd.append('\r'); + m_socket->writeBlock(cmd.latin1(), cmd.length()); + // Read from the server + TQString serverRet; + while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { + char data[1]; + if( m_socket->readBlock(data, 1) > 0) { + serverRet.append(data[0]); + } + tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); } - tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); } + m_instrumentMutex->unlock(); + return; + } + catch (exit_exception& e) { + m_instrumentMutex->unlock(); + return; } - m_instrumentMutex->unlock(); } void CommAnalyzerPart::callServerMethodDoubleArray(int command, double * array, int arrayLen) { @@ -246,62 +288,65 @@ void CommAnalyzerPart::callServerMethodDoubleArray(int command, double * array, printf("[WARN] An attempt was made to access the instrument asynchronously, and was rejected to prevent a lockup\n\r"); fflush(stdout); return; } - m_instrumentMutex->lock(); - if (m_socket->state() == TQSocket::Connected) { - TQString cmd = TQChar(command); - cmd.append('\r'); - m_socket->writeBlock(cmd.latin1(), cmd.length()); - // Read from the server - TQString serverRet; - while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { - char data[1]; - if( m_socket->readBlock(data, 1) > 0) { - serverRet.append(data[0]); + try { + m_instrumentMutex->lock(); + if (m_socket->state() == TQSocket::Connected) { + TQString cmd = TQChar(command); + cmd.append('\r'); + m_socket->writeBlock(cmd.latin1(), cmd.length()); + // Read from the server + TQString serverRet; + while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { + char data[1]; + if( m_socket->readBlock(data, 1) > 0) { + serverRet.append(data[0]); + } + tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); } - tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); - } - unsigned int bytesread = 0; - int16_t data[1]; - while ((bytesread < 2) && (m_socket->state() == TQSocket::Connected)) { - int ret = m_socket->readBlock(((char*)data)+bytesread, 1); - if (ret > 0) { - bytesread += ret; + unsigned int bytesread = 0; + int16_t data[1]; + while ((bytesread < 2) && (m_socket->state() == TQSocket::Connected)) { + int ret = m_socket->readBlock(((char*)data)+bytesread, 1); + if (ret > 0) { + bytesread += ret; + } + tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); } - tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); - } - serverRet = ""; - while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { - char data[1]; - if( m_socket->readBlock(data, 1) > 0) { - serverRet.append(data[0]); + serverRet = ""; + while ((!serverRet.contains('\r')) && (m_socket->state() == TQSocket::Connected)) { + char data[1]; + if( m_socket->readBlock(data, 1) > 0) { + serverRet.append(data[0]); + } + tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); } - tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); - } - bytesread = 0; - int elementsread = 0; - for (elementsread=0;elementsread<arrayLen;elementsread++) { bytesread = 0; - while ((bytesread < sizeof(double)) && (m_socket->state() == TQSocket::Connected)) { - if (m_socket->size() < 1) { - tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); - } - int ret = m_socket->readBlock(((char*)array)+bytesread+(elementsread*sizeof(double)), 1); - if (ret > 0) { - bytesread += ret; + int elementsread = 0; + for (elementsread=0;elementsread<arrayLen;elementsread++) { + bytesread = 0; + while ((bytesread < sizeof(double)) && (m_socket->state() == TQSocket::Connected)) { + if (m_socket->size() < 1) { + tqApp->eventLoop()->processEvents(TQEventLoop::AllEvents); + } + int ret = m_socket->readBlock(((char*)array)+bytesread+(elementsread*sizeof(double)), 1); + if (ret > 0) { + bytesread += ret; + } } } } + m_instrumentMutex->unlock(); + return; + } + catch (exit_exception& e) { + m_instrumentMutex->unlock(); + return; } - m_instrumentMutex->unlock(); } int CommAnalyzerPart::connectToServer(TQString server) { if (!m_socket) { m_socket = new TQSocket(this); -// connect(m_socket, SIGNAL(connected()), SLOT(socketConnected())); -// connect(m_socket, SIGNAL(connectionClosed()), SLOT(socketConnectionClosed())); -// connect(m_socket, SIGNAL(readyRead()), SLOT(socketReadyRead())); -// connect(m_socket, SIGNAL(error(int)), SLOT(socketError(int))); } m_socket->connectToHost(server, 4002); while ((m_socket->state() != TQSocket::Connected) && (m_socket->state() != TQSocket::Idle)) { |