@ -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 ) ) {