PDA

View Full Version : Console Application to hit different url multiple times within a program



Qt4user
26th March 2013, 01:45
Hi,

I am trying to write a console application to hit url using QT. I want to have a seperate function (normal C++ function) which does this job. There is a main program that will invoke this function several times (say with different urls) before exiting.

In order to test this, I tried below changes with the download program under http://harmattan-dev.nokia.com/docs/library/html/qt4/network-download-main-cpp.html

The change I have done to the program is replaced main function with a different function name (say test)
and written a new main function that will call test. For the first invocation of test everything works as expected
and if I invoke again the test function without exiting the program, I see segmentation fault.

QObject::connect: Cannot connect (null)::configurationAdded(QNetworkConfiguration) to QNetworkConfigurationManager::configurationAdded(Q NetworkConfiguration)
QObject::connect: Cannot connect (null)::configurationRemoved(QNetworkConfiguration ) to QNetworkConfigurationManager::configurationRemoved (QNetworkConfiguration)
QObject::connect: Cannot connect (null)::configurationUpdateComplete() to QNetworkConfigurationManager::updateCompleted()
QObject::connect: Cannot connect (null)::onlineStateChanged(bool) to QNetworkConfigurationManager::onlineStateChanged(b ool)
QObject::connect: Cannot connect (null)::configurationChanged(QNetworkConfiguration ) to QNetworkConfigurationManager::configurationChanged (QNetworkConfiguration)

I am very new to QT. Appreciate a way to accompolish this.

Qt4user
26th March 2013, 23:42
Below is the backtrace I see when I try hit the url for the second time without exiting the program.

#0 0xb6e94e47 in QMutex::lock () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtCore.so.4
#1 0xb7172be3 in QNetworkConfigurationManagerPrivate::enablePolling () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtNetwork.so.4
#2 0xb7170dd0 in QNetworkConfigurationManager::QNetworkConfiguratio nManager () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtNetwork.so.4
#3 0xb713fb7c in QNetworkAccessManager::createRequest () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtNetwork.so.4
#4 0xb713dea9 in QNetworkAccessManager::get () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtNetwork.so.4
#5 0x08049df8 in DownloadManager::doDownload ()
#6 0x0804a738 in DownloadManager::execute ()
#7 0x0804a8cc in DownloadManager::qt_metacall ()
#8 0xb6fad50b in QMetaObject::metacall () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtCore.so.4
#9 0xb6fb87b6 in QMetaCallEvent::placeMetaCall () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtCore.so.4
#10 0xb6fb97c7 in QObject::event () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtCore.so.4
#11 0xb6fa7173 in QCoreApplicationPrivate::notify_helper () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtCore.so.4
#12 0xb6fa71e3 in QCoreApplication::notify () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtCore.so.4
#13 0xb6fa6d9b in QCoreApplication::notifyInternal () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtCore.so.4
#14 0xb6fa7936 in QCoreApplicationPrivate::sendPostedEvents () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtCore.so.4
#15 0xb6fa7b9d in QCoreApplication::sendPostedEvents () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtCore.so.4
#16 0xb6fd5604 in __gxx_personality_v0 () from /usr/local/Trolltech/Qt-4.7.2/lib/libQtCore.so.4
#17 0xb6b7fbcd in g_main_context_dispatch () from /opt/gnome/lib/libglib-2.0.so.0
#18 0xb6b82dcf in __gxx_personality_v0 () from /opt/gnome/lib/libglib-2.0.so.0
#19 0x080522c0 in ?? ()
#20 0x00000000 in ?? ()

ChrisW67
27th March 2013, 00:12
Post your actual main() and test() function code.

Qt4user
27th March 2013, 00:43
int test(int argc, char **argv)
{
QCoreApplication app(argc, argv);

DownloadManager manager;
QTimer::singleShot(0, &manager, SLOT(execute()));

app.exec();
}

int main(int argc, char **argv)
{
/* Hit the url for the first time. Everything works fine */
test(argc, argv);
/* Hit the same url for second time. Program crashes. It would happen even when a different url is passed*/
test(argc, argv);
exit(0);
}

ChrisW67
27th March 2013, 05:10
The example you started with already allows you to specify multiple URLs on the command line and it will fetch them all.

Your code is highly unusual: QCoreApplication is intended to a a single instance per program. My guess is that your antique Qt version has issues with there being two QCoreApplication instances (at different times). Your program, nonetheless, does not crash on Qt 4.8.4 Linux but it does download everything on the command line twice.

Qt4user
27th March 2013, 22:57
Hi,

Thank your very much for the reply. While I understand that multiple url could be hit at once with the example program, per requirement I have, I do not have all the urls ready at one instance. I have a non-GUI application from where when a new url is posted (it can be posted any number of times through the flow), I will have to fetch data from the url and return control back to application. That is the reason I want to have a seperate function which will hit QT and get the details. I do not want to have a seperate executable for Qt related processing and put main function there.


I could not find what exactly happens internally upon putting a get request (at line QNetworkReply *reply = manager.get(request);). It crashes at that point. Is there a way when I come out of function I can delete QCoreApplication object. Not sure if I can use deleteLater method on QCoreApplication. it did not solve the problem though.

Thank you for your additional information on that it works with Qt4.8.

May I ask if there is any other way to handle the same requirement with Qt4.7 in a linux platform?

ChrisW67
28th March 2013, 00:25
So just run the example program (unmodified) once for each time your need a URL fetched. I fails to see where the issue is.

As an aside, you are on a UNIX machine... have you ever heard of wget or curl?

Qt4user
28th March 2013, 01:11
Thanks for the reply.

As I said in my earlier post, I did not want to have a seperate program to hit the url. Wanted to have it done within main of my application.

ChrisW67
28th March 2013, 02:12
You can have as many instances of a downloader class as you want inside your program. You need to read and understand the DownloadManager class code, and modify it to your purpose. You just need to change where DownloadManager::execute() gets the URL to fetch.