QtNetwork. How to send multiple simultaneous requests via multiple proxy servers?



  • Hello. First of all I want to apologize for my bad English. Okay...
    I have list of accounts in format login:password and list of proxies ip:port. My task is to authorize an account and verify some of its characteristics. It is simple to do if I have one connection.

    By it sounds good if I will be able to send, for example, five requests (POST) from proxies A, B, C, D, E at the same time. Than, if some of them replied I can send request (GET) from the same proxies. If some of them replies I can check content of reply. When all requests replied I select new proxies and repeat the process until all accounts are checked.

    Please, see the pictures below to understand better what I am talking about...
    !https://dl.dropboxusercontent.com/u/58160380/alg.PNG(algorithm)!

    I am using Qt 5.1 and now I have this...
    (few comments because the function names represent what they are doing)

    @/// Types
    class MyQNetworkAccessManager: public QNetworkAccessManager
    {
    Q_OBJECT

    public:
    MyQNetworkAccessManager():
    QNetworkAccessManager(nullptr),
    account( {"", ""} ) {}
    std::pair<QString, QString> account; // I need network manager to know what account it is checking
    };

    /// Variables...
    QVector<MyQNetworkAccessManager*> nam;
    QVector<QNetworkProxy> proxies;
    QVector<std::pair<QString, QString>> accounts;
    qint32 threads; //the size of nam is equal to this

    /// slots...
    void authorizationDone(QNetworkReply *reply);
    void openHomepageDone(QNetworkReply *reply);

    /// functions...
    void authorizationRequest(QNetworkAccessManager *manager);
    void openHomepageRequest(QNetworkAccessManager *manager);
    void process(); // main funtion which send initial requests

    // simply casts base object to derived
    MyQNetworkAccessManager* castToDerived(QNetworkAccessManager *base) const;

    /// implementation...

    MyQNetworkAccessManager* castToDerived(QNetworkAccessManager base) const
    {
    return static_cast<MyQNetworkAccessManager
    >(base);
    }

    void process()
    {
    foreach(MyQNetworkAccessManager *ptr, nam) {
    authorizationRequest( ptr );
    }
    }

    void authorizationRequest(QNetworkAccessManager *manager)
    {
    if ( accounts.isEmpty() || proxies.isEmpty() ) {
    return;
    }

    manager->clearAccessCache();
    

    // set next proxy
    manager->setProxy( proxies.back() );
    proxies.pop_back();

    // set next account
    castToDerived(manager)->account = accounts.back();
    accounts.pop_back();

    // send POST request
    manager->post(postUrl, postData);

    // when reply is finished it means that auth done
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(authorizationDone(QNetworkReply*)));
    }

    void openHomepageRequest(QNetworkAccessManager manager)
    {
    manager->get(getRequest);
    connect(manager, SIGNAL(finished(QNetworkReply
    )), this, SLOT(openHomepageDone(QNetworkReply*)));
    }

    void authorizationDone(QNetworkReply reply)
    {
    disconnect(reply->manager(), SIGNAL(finished(QNetworkReply
    )), this, SLOT(authorizationDone(QNetworkReply*)));

    if ( reply->error() == QNetworkReply::NoError ) {
           openHomepageRequest(reply->manager());
        }
    } else {
        authorizationRequest(reply->manager());
    }
    

    }

    void openHomepageDone(QNetworkReply reply)
    {
    disconnect(reply->manager(), SIGNAL(finished(QNetworkReply
    )), this, SLOT(openHomepageDone(QNetworkReply*)));

    if (reply->error() == QNetworkReply::NoError) {
    

    // extract some info from reply
    }
    authorizationRequest(reply->manager());
    }
    @

    This code works.
    !!! But I think it is bad idea to use vector of network managers because documentation says that application should have only one manages. One manager is enough.

    How can I rewrite this code or algorithm to become better? What to read or study?

    Thank you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.