Modified mapfetcher.cpp and mapfetcher.h.
authorJussi Laitinen <jussi.laitinen@ixonos.com>
Tue, 30 Mar 2010 10:22:27 +0000 (13:22 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Tue, 30 Mar 2010 10:22:27 +0000 (13:22 +0300)
Situare.pro
src/map/mapfetcher.cpp
src/map/mapfetcher.h
tests/testMap/testmapfetcher.cpp

index a6ae45b..d6c4060 100644 (file)
@@ -8,13 +8,13 @@ QT += network \
 INCLUDEPATH += . \
     src/tests/testMap/ \
     src/map/
-SOURCES += src/ui/mainwindow.cpp \
-    src/map/mapfetcherqueue.cpp \
-    src/map/mapengine.cpp \
-    tests/testMap/testmapfetcher.cpp
-
-# tests/testMap/testmapengine.cpp
+SOURCES += src/main.cpp \
+    src/ui/mainwindow.cpp \
+    src/map/mapfetcher.cpp \
+    src/map/mapengine.cpp
+#    tests/testMap/testmapfetcher.cpp
 # src/main.cpp \
+# tests/testMap/testmapengine.cpp
 HEADERS += src/ui/mainwindow.h \
-    src/map/mapfetcherqueue.h \
+    src/map/mapfetcher.h \
     src/map/mapengine.h
index 2f8f4b7..9d3cc9d 100644 (file)
@@ -9,6 +9,8 @@
 
 #include "mapfetcher.h"
 
+static int MAX_PARALLEL_DOWNLOADS = 2;
+
 MapFetcher::MapFetcher(QObject *parent)
     : QObject(parent)
 {
@@ -26,19 +28,34 @@ void MapFetcher::fetchMapImage(const QUrl &url)
     if (url.isEmpty())
         return;
 
+    //Limit parallel downloads
+    if (downloadQueue.length() < MAX_PARALLEL_DOWNLOADS)
+        QTimer::singleShot(0, this, SLOT(startNextDownload()));
+
+    downloadQueue.enqueue(url);
+}
+
+void MapFetcher::startNextDownload()
+{
+    qDebug() << "startNextDownload()";
+    if (downloadQueue.isEmpty())
+        return;
+
+    QUrl url = downloadQueue.dequeue();
     QNetworkRequest request(url);
     request.setRawHeader("User-Agent", "Map Test");
     request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
                                               QNetworkRequest::PreferCache);
 
-    m_manager.get(request);
+    QNetworkReply *reply = m_manager.get(request);
+
+    currentDownloads.append(reply);
 }
 
 void MapFetcher::downloadFinished(QNetworkReply *reply)
 {
     qDebug() << "downloadFinished()";
     if (reply->error()) {
-        qDebug() << reply->errorString();
         emit error(reply->errorString());
     }
     else {
@@ -50,7 +67,9 @@ void MapFetcher::downloadFinished(QNetworkReply *reply)
         emit mapImageReceived(url, QPixmap::fromImage(image));
     }
 
+    currentDownloads.removeAll(reply);
     reply->deleteLater();
+    startNextDownload();
 }
 
 MapFetcher::~MapFetcher()
index 26fb27e..bbd5284 100644 (file)
@@ -68,9 +68,12 @@ private slots:
     * @param reply
     */
     void downloadFinished(QNetworkReply *reply);
+    void startNextDownload();
 
 private:
     QNetworkAccessManager m_manager;
+    QList<QNetworkReply *> currentDownloads;
+    QQueue<QUrl> downloadQueue;
 };
 
 #endif
index 4ed9ac7..820ac0e 100644 (file)
@@ -10,18 +10,19 @@ class TestMapFetcher : public QObject
 public:
     TestMapFetcher();
 
+
 private slots:
     void testFetchImage();
     void testSignals();
 
 
 private:
-    MapFetcherQueue *mapFetcher;
+    MapFetcher *mapFetcher;
 };
 
 TestMapFetcher::TestMapFetcher()
 {
-    mapFetcher = new MapFetcherQueue();
+    mapFetcher = new MapFetcher();
 }
 
 void TestMapFetcher::testFetchImage()
@@ -51,19 +52,34 @@ void TestMapFetcher::testSignals()
     mapFetcher->fetchMapImage(url2);
     QTest::qWait(1000);
     QCOMPARE(imageReceivedSpy.count(), 1);
-    QList<QVariant> signalArgs2 = imageReceivedSpy.takeFirst();
+    QList<QVariant> signalArgs2 = imageReceivedSpy.takeLast();
     QCOMPARE(url2, signalArgs2.at(0).toUrl());*/
 
     //20 requests immediately
-    for (int i = 0; i < 10; ++i) {
-        QUrl url(QString("http://tile.openstreetmap.org/7/63/2%1.png").arg(i));
-        mapFetcher->fetchMapImage(url);
+    /*for (int i = 1; i < 3; ++i) {
+        for (int j = 0; j < 10; ++j) {
+            QUrl url(QString("http://tile.openstreetmap.org/7/53/%1%2.png").arg(i).arg(j));
+            mapFetcher->fetchMapImage(url);
+        }
+    }
+    QTest::qWait(400);
+
+    //50 requests immediately
+    for (int i = 1; i < 6; ++i) {
+        for (int j = 0; j < 10; ++j) {
+            QUrl url(QString("http://tile.openstreetmap.org/7/63/%1%2.png").arg(i).arg(j));
+            mapFetcher->fetchMapImage(url);
+        }
     }
+
+    QTest::qWait(2000);*/
+
     for (int i = 0; i < 10; ++i) {
-        QUrl url(QString("http://tile.openstreetmap.org/7/63/3%1.png").arg(i));
+        QUrl url(QString("http://tile.openstreetmap.org/7/53/%1%2.png").arg(i));
         mapFetcher->fetchMapImage(url);
     }
-    QTest::qWait(2000);
+
+    QTest::qWait(1000);
 
 }