#include <QtCore/QCoreApplication>
#include <QThread>
#include <QHttp>
#include <QTimer>
#include <QUrl>
#include <QHash>
#include "MyThread.h"
#include "Windows.h"
//----------------------------------------------------------------------------------
MyThread::MyThread()
{
}
//----------------------------------------------------------------------------------
void MyThread::done( bool error ) // true if error occurs
{
if(error)
{
QHttp::Error err
= m_pHttp
->error
();
QString err_Str
= m_pHttp
->errorString
();
qDebug("HTTP done signal slot handler. Error: %s", err_Str.toAscii().data());
}
else
qDebug("HTTP done signal slot handler. No Error.");
}
//-------------------------------------------------------------------------------------------
void MyThread::init(void)
{
m_Host = "169.254.177.84";
m_UserName = "root";
m_Password = "smokey";
m_HttpPort = 80;
m_SerialPort = 1;
m_HttpStateTable
[QHttp::Unconnected] = "Unconnected";
m_HttpStateTable
[QHttp::HostLookup] = "HostLookup";
m_HttpStateTable
[QHttp::Connecting] = "Connecting";
m_HttpStateTable
[QHttp::Sending] = "Sending";
m_HttpStateTable
[QHttp::Reading] = "Reading";
m_HttpStateTable
[QHttp::Connected] = "Connected";
m_HttpStateTable
[QHttp::Closing] = "Closing";
}
// -----------------------------------------------------------------------------
void MyThread::initSignalsAndSlots(void)
{
connect(m_pHttp,
SIGNAL(authenticationRequired
(const QString &, quint16, QAuthenticator
*)),
this,
SLOT(slotAuthenticationRequired
(const QString &, quint16, QAuthenticator
*)));
connect( m_pHttp, SIGNAL( requestStarted ( int ) ), this, SLOT( requestStarted( int ) ) );
connect( m_pHttp, SIGNAL( requestFinished ( int , bool ) ), this, SLOT( requestFinished ( int , bool ) ) );
connect( m_pHttp, SIGNAL( stateChanged ( int ) ), this, SLOT( stateChanged ( int ) ) );
connect( m_pHttp, SIGNAL( done ( bool ) ), this, SLOT( done (bool ) ) );
}
// -----------------------------------------------------------------------------
void MyThread::requestStarted( int id )
{
qDebug( "HTTP requestStarted slot handler for request type %s (id = %d)",
m_httpRequestIdHashTable[id].toAscii().data(),id);
int nothing = 0;
nothing++;
}
// -----------------------------------------------------------------------------
void MyThread::requestFinished( int id, bool error )
{
//qDebug( "HTTP requestFinished slot handler for request type %s (id = %d), error = %d",
// m_httpRequestIdHashTable[id].toAscii().data(), id, error);
int nothing = 0;
nothing++;
if( error )
{
QHttp::Error err
= m_pHttp
->error
();
err_Str = m_pHttp->errorString();
nothing--;
}
qDebug( "HTTP requestFinished handler: error for request %s, error = %s",
m_httpRequestIdHashTable[id].toAscii().data(), err_Str.toAscii().data());
}
// -----------------------------------------------------------------------------
{
# if 1
switch (responseHeader.statusCode()) {
case 200: // Ok
//case 301: // Moved Permanently
//case 302: // Found
//case 303: // See Other
//case 307: // Temporary Redirect
{
qDebug("HTTP Header status 200 (OK) found");
// See if there are pending requests
if(m_pHttp->hasPendingRequests())
qDebug("There are pending requests.");
else
qDebug("There are NO pending requests.");
// Get info on current ID
int id = m_pHttp->currentId();
qDebug( "Closing connection for request type %s",
m_httpRequestIdHashTable[id].toAscii().data());
//emit( abortHttpConnection() ); //??ejp This kills the TCP link and stops the video stream.
m_pHttp->clearPendingRequests(); // clear pending request and start close request.
id = m_pHttp->close(); // Tried close() but nothing happened.
m_httpRequestIdHashTable.insert(id,"close");
break;
}
default:
//QMessageBox::information(this, tr("HTTP"),
// tr("Download failed: %1.")
// .arg(responseHeader.reasonPhrase()));
break;
}
#endif
}
//----------------------------------------------------------------------------------
void MyThread
::slotAuthenticationRequired(const QString & hostname,
quint16 port, QAuthenticator * authenticator)
{
qDebug( "Authentication req'd for host %s, User Name = %s, Password = %s",
hostname.toAscii().data(), m_UserName.toAscii().data(), m_Password.toAscii().data() ); //??ejp debug
authenticator->setUser( m_UserName );
authenticator->setPassword( m_Password );
}
//----------------------------------------------------------------------------------
void MyThread::stateChanged( int state)
{
Q_ASSERT(state < 7); // There are only 7 states for QHTTP
QString request
= m_httpRequestIdHashTable
[m_pHttp
->currentId
()];
qDebug( "HTTP State for request type %s changed to %s",
request.toAscii().data(), m_HttpStateTable[state].toAscii().data());
}
//----------------------------------------------------------------------------------
void MyThread::callbackFunc(void)
{
int id
= m_pHttp
->setHost
( m_Host,
QHttp::ConnectionModeHttp, m_HttpPort
);
m_httpRequestIdHashTable.insert(id,"setHost");
Sleep( 100); // wait for setHost to finish.
#if 0
QString url_for_cmds
= QString("/axis-cgi/com/serial.cgi?port=" + port_str
);
#else
#endif
id
= m_pHttp
->get
(QUrl::toPercentEncoding(url_for_cmds
) );
m_httpRequestIdHashTable.insert(id,"GET");
id = m_pHttp->get(url_for_cmds );
m_httpRequestIdHashTable.insert(id,"GET");
//while(1)
// Sleep( 10);
//m_pHttp->get();
};
//----------------------------------------------------------------------------------
void MyThread::run(void)
{
init();
initSignalsAndSlots(); // Need m_pHttp initialized first!
QTimer::singleShot(1000,
this,
SLOT(callbackFunc
()));
// milli second timeout.
//m_pTimer = new QTimer(callback ;
//int res = QCoreApplication::exec();
};
#include <QtCore/QCoreApplication>
#include <QThread>
#include <QHttp>
#include <QTimer>
#include <QUrl>
#include <QHash>
#include "MyThread.h"
#include "Windows.h"
//----------------------------------------------------------------------------------
MyThread::MyThread()
{
}
//----------------------------------------------------------------------------------
void MyThread::done( bool error ) // true if error occurs
{
if(error)
{
QHttp::Error err = m_pHttp->error();
QString err_Str = m_pHttp->errorString();
qDebug("HTTP done signal slot handler. Error: %s", err_Str.toAscii().data());
}
else
qDebug("HTTP done signal slot handler. No Error.");
}
//-------------------------------------------------------------------------------------------
void MyThread::init(void)
{
m_Host = "169.254.177.84";
m_UserName = "root";
m_Password = "smokey";
m_HttpPort = 80;
m_SerialPort = 1;
m_HttpStateTable[QHttp::Unconnected] = "Unconnected";
m_HttpStateTable[QHttp::HostLookup] = "HostLookup";
m_HttpStateTable[QHttp::Connecting] = "Connecting";
m_HttpStateTable[QHttp::Sending] = "Sending";
m_HttpStateTable[QHttp::Reading] = "Reading";
m_HttpStateTable[QHttp::Connected] = "Connected";
m_HttpStateTable[QHttp::Closing] = "Closing";
}
// -----------------------------------------------------------------------------
void MyThread::initSignalsAndSlots(void)
{
connect(m_pHttp, SIGNAL(authenticationRequired(const QString &, quint16, QAuthenticator *)),
this, SLOT(slotAuthenticationRequired(const QString &, quint16, QAuthenticator *)));
connect( m_pHttp, SIGNAL( requestStarted ( int ) ), this, SLOT( requestStarted( int ) ) );
connect( m_pHttp, SIGNAL( requestFinished ( int , bool ) ), this, SLOT( requestFinished ( int , bool ) ) );
connect( m_pHttp, SIGNAL( stateChanged ( int ) ), this, SLOT( stateChanged ( int ) ) );
connect( m_pHttp, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
connect( m_pHttp, SIGNAL( done ( bool ) ), this, SLOT( done (bool ) ) );
}
// -----------------------------------------------------------------------------
void MyThread::requestStarted( int id )
{
qDebug( "HTTP requestStarted slot handler for request type %s (id = %d)",
m_httpRequestIdHashTable[id].toAscii().data(),id);
int nothing = 0;
nothing++;
}
// -----------------------------------------------------------------------------
void MyThread::requestFinished( int id, bool error )
{
//qDebug( "HTTP requestFinished slot handler for request type %s (id = %d), error = %d",
// m_httpRequestIdHashTable[id].toAscii().data(), id, error);
int nothing = 0;
nothing++;
QString err_Str = "No error";
if( error )
{
QHttp::Error err = m_pHttp->error();
err_Str = m_pHttp->errorString();
nothing--;
}
qDebug( "HTTP requestFinished handler: error for request %s, error = %s",
m_httpRequestIdHashTable[id].toAscii().data(), err_Str.toAscii().data());
}
// -----------------------------------------------------------------------------
void MyThread::readResponseHeader(const QHttpResponseHeader &responseHeader)
{
# if 1
switch (responseHeader.statusCode()) {
case 200: // Ok
//case 301: // Moved Permanently
//case 302: // Found
//case 303: // See Other
//case 307: // Temporary Redirect
{
qDebug("HTTP Header status 200 (OK) found");
// See if there are pending requests
if(m_pHttp->hasPendingRequests())
qDebug("There are pending requests.");
else
qDebug("There are NO pending requests.");
// Get info on current ID
int id = m_pHttp->currentId();
qDebug( "Closing connection for request type %s",
m_httpRequestIdHashTable[id].toAscii().data());
//emit( abortHttpConnection() ); //??ejp This kills the TCP link and stops the video stream.
m_pHttp->clearPendingRequests(); // clear pending request and start close request.
id = m_pHttp->close(); // Tried close() but nothing happened.
m_httpRequestIdHashTable.insert(id,"close");
break;
}
default:
//QMessageBox::information(this, tr("HTTP"),
// tr("Download failed: %1.")
// .arg(responseHeader.reasonPhrase()));
break;
}
#endif
}
//----------------------------------------------------------------------------------
void MyThread::slotAuthenticationRequired(const QString & hostname,
quint16 port, QAuthenticator * authenticator)
{
qDebug( "Authentication req'd for host %s, User Name = %s, Password = %s",
hostname.toAscii().data(), m_UserName.toAscii().data(), m_Password.toAscii().data() ); //??ejp debug
authenticator->setUser( m_UserName );
authenticator->setPassword( m_Password );
}
//----------------------------------------------------------------------------------
void MyThread::stateChanged( int state)
{
Q_ASSERT(state < 7); // There are only 7 states for QHTTP
QString request = m_httpRequestIdHashTable[m_pHttp->currentId()];
qDebug( "HTTP State for request type %s changed to %s",
request.toAscii().data(), m_HttpStateTable[state].toAscii().data());
}
//----------------------------------------------------------------------------------
void MyThread::callbackFunc(void)
{
int id = m_pHttp->setHost( m_Host, QHttp::ConnectionModeHttp, m_HttpPort );
m_httpRequestIdHashTable.insert(id,"setHost");
Sleep( 100); // wait for setHost to finish.
QString port_str = QString::number( m_SerialPort+1 );
#if 0
QString url_for_cmds = QString("/axis-cgi/com/serial.cgi?port=" + port_str );
#else
QString url_for_cmds = QString("/axis-cgi/mjpg/video.cgi");
#endif
id = m_pHttp->get(QUrl::toPercentEncoding(url_for_cmds) );
m_httpRequestIdHashTable.insert(id,"GET");
id = m_pHttp->get(url_for_cmds );
m_httpRequestIdHashTable.insert(id,"GET");
//while(1)
// Sleep( 10);
//m_pHttp->get();
};
//----------------------------------------------------------------------------------
void MyThread::run(void)
{
init();
m_pHttp = new QHttp();
initSignalsAndSlots(); // Need m_pHttp initialized first!
QTimer::singleShot(1000, this, SLOT(callbackFunc())); // milli second timeout.
//m_pTimer = new QTimer(callback ;
QThread::exec ();
//int res = QCoreApplication::exec();
};
To copy to clipboard, switch view to plain text mode
Bookmarks