00001 00002 #include "srcp_socket.h" 00003 00004 // 00005 // SrcpThread : 00006 // 00007 00008 SrcpThread::SrcpThread( QObject* parent ) : QThread( parent ) 00009 { 00010 _stopFlag = TRUE ; 00011 _infoSocket = static_cast<SrcpSocket*>( parent ) ; 00012 } 00013 00014 SrcpThread::~SrcpThread() 00015 { 00016 stop() ; 00017 } 00018 00019 void SrcpThread::run() 00020 { 00021 _stopFlag = FALSE ; 00022 QString info ; 00023 00024 while( ! _stopFlag ) // The flag can be changed by calling stop() 00025 { 00026 info = _infoSocket->srcpReadLine() ; 00027 if ( ! info.isEmpty() ) 00028 emit infoReceived( info ); 00029 } 00030 00031 _infoSocket->disconnectFromHost() ; 00032 } 00033 00034 void SrcpThread::stop() 00035 { 00036 _stopFlag = TRUE ; 00037 } 00038 00039 00040 // 00041 // SrcpSocket : 00042 // 00043 00044 SrcpSocket::SrcpSocket( const QString &type, QObject* parent ) : QTcpSocket( parent ) 00045 { 00046 _type = type ; 00047 _internThread = 0 ; // Lazy object 00048 } 00049 00050 SrcpSocket::~SrcpSocket() 00051 { 00052 threadSafeDisconnectFromHost() ; 00053 } 00054 00055 QString SrcpSocket::type() 00056 { 00057 return _type ; 00058 } 00059 00060 void SrcpSocket::start() 00061 { 00062 if ( _internThread == 0 ) // Create and connect lazy object if needed 00063 { 00064 _internThread = new SrcpThread( this ) ; 00065 connect( _internThread, SIGNAL( infoReceived( QString ) ), 00066 this, SIGNAL( infoReceived( QString ) ) ) ; 00067 } 00068 _internThread->start() ; 00069 } 00070 00071 void SrcpSocket::threadSafeDisconnectFromHost() 00072 { 00073 if ( _internThread != 0 && _internThread->isRunning() ) 00074 _internThread->stop() ; 00075 else 00076 disconnectFromHost() ; 00077 } 00078 00079 bool SrcpSocket::sendMsg( QString msg ) 00080 { 00081 if ( state() != QAbstractSocket::ConnectedState ) 00082 return FALSE ; 00083 00084 qDebug() << type() << " Srcp 0.8.3 sent : " << msg ; 00085 00086 msg += "\n" ; // mandatory for srcp (end of message) 00087 00088 if ( write( msg.toAscii () ) == -1 ) // send message and check for socketError 00089 { 00090 emit errorOccured( trUtf8( "Impossible to send the message to the server") ); 00091 return FALSE ; 00092 } 00093 00094 return TRUE ; 00095 } 00096 00097 QString SrcpSocket::srcpReadLine() 00098 { 00099 if ( state() != QAbstractSocket::ConnectedState ) 00100 { 00101 emit errorOccured( trUtf8( "Impossible to read : not connected") ); 00102 return QString() ; 00103 } 00104 00105 char buf[1000]; // max allowed length of SRCP reply line 00106 00107 if ( atEnd() && ! waitForReadyRead( 1000 ) ) // [msec] return false if timedout 00108 { 00109 emit errorOccured( trUtf8( "Impossible to read : no data or timedout") ); 00110 return QString() ; 00111 } 00112 00113 if ( readLine( buf, sizeof( buf ) ) == -1) // read the server reply and check for socketError 00114 { 00115 emit errorOccured( trUtf8( "Impossible to read the reply of the server") ); 00116 return QString(); 00117 } 00118 00119 QString reply = QString( buf ).trimmed() ; // whitespace removed from the start and the end 00120 if ( reply.endsWith("\\") ) // check if reply is spread over many lines 00121 { 00122 reply = reply.replace( "\\", "" ) ; // clean string 00123 reply += srcpReadLine() ; 00124 } 00125 00126 qDebug() << type() << " SRCP 0.8.3 received : " << reply ; 00127 00128 return reply ; 00129 } 00130 00131 /* 00132 void ::handleSocketError( QAbstractSocket::SocketError socketError ) 00133 { 00134 switch( socketError ) 00135 { 00136 case QAbstractSocket::RemoteHostClosedError : 00137 00138 break ; 00139 00140 case QAbstractSocket::HostNotFoundError : 00141 00142 QMessageBox::information( 0, tr ( "orK network error" ), tr ( "The host was not found. Please check the host name and port settings." ) ) ; 00143 break ; 00144 00145 case QAbstractSocket::ConnectionRefusedError : 00146 00147 QMessageBox::information( 0, tr ( "orK network error" ), tr("The connection was refused by the peer. Make sure the SRCP server is running, and check that the host name and port settings are correct." ) ) ; 00148 break ; 00149 00150 default : 00151 00152 QMessageBox::information( 0, tr ( "orK network error" ),tr ( "Unreach error. %1" ).arg ( _socket->errorString () ) ) ; 00153 } 00154 } 00155 */