0

I have the following code:

void GameScene::onGetServersRequestCompleted( cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response )
{

if( 200 == response->getResponseCode() ) {
    std::vector<char> *buffer = response->getResponseData();
    std::string serversString( buffer->begin(), buffer->end() );

    Json::Reader reader;
    Json::Value root;

    reader.parse(serversString, root);

    const Json::Value servers = root["servers"];
    for ( int i = 0; i < servers.size(); ++i ) {
        //CCLOG("%s",  servers[i].asString().c_str());
        cocos2d::network::HttpRequest *request = new cocos2d::network::HttpRequest();
        request->setUrl("http://" + servers[i].asString() + "/info");
        request->setRequestType(cocos2d::network::HttpRequest::Type::GET);
        request->setResponseCallback(CC_CALLBACK_2(GameScene::onServerRequestCompleted, this));
        cocos2d::network::HttpClient::getInstance()->send(request);
        request->release();
    }

} else {
    CCLOG("Request Failed! Response Code: %i\n", response->getResponseCode());
    /*std::vector<char> *buffer = response->getResponseData();
    std::string serversString( buffer->begin(), buffer->end() );
    CCLOG("%s", serversString.c_str());*/
    this->serverIp = "185.8.166.41";
}

}

I am trying to get the right server. What I would like to do is to get the execution time of each server's request and get the fastest one. Is it possible? because it's an asynchronous function I don't know how to do it. How would you do it?

duri
  • 71
  • 2
  • 8

1 Answers1

1

Does this help? From the documentation of HTTPRequest and HTTPResponse ....

You can set a string tag to identify your request, this tag can be found in HttpResponse->getHttpRequest->getTag()

    void HTTPRequest::setTag(const char *tag);

Get the string tag back to identify the request. The best practice is to use it in your MyClass::onMyHttpRequestCompleted(sender, HttpResponse*) callback

    const char* HTTPResponse::getTag();

Edit: You can use these unique tags to pair up the requests with asynchronous responses in a global hash and thus measure "response time". There doesn't seem to be any interface in HTTP* classes to give you the time of execution.


Edit: There are two ways to pair up HTTPResponse with a HTTPRequest. Here I try to use the second method with some quick/dirty code. Please convert/use it as you see fit.

  1. setTag/getTag
  2. setUserData/getUserData

Create a file HTTPCustomData.h :

#include <time.h>
class HTTPCustomData {
    private:
        time_t t1;
    public:
        HTTPCustomData() { time(&t1); }
        double getTimePassed(void) {
            time_t t2; time(&t2); return(difftime(t2,t1));
        }
};

Use the following code where you send request:

#include "HTTPCustomData.h"
for ( int i = 0; i < servers.size(); ++i ) {
    // ...
    // ... Before "send"
    request->setUserData(new HTTPCustomData());
    cocos2d::network::HttpClient::getInstance()->send(request);
    request->release();
}

Later in GameScene::onServerRequestCompleted method, ...

#include <memory>
#include <iostream>
#include "HTTPCustomData.h"
void GameScene::onServerRequestCompleted(...) {
{
    // ...
    std::unique_ptr<HTTPCustomData> h(
        (HTTPCustomData *)(response->getHTTPRequst()->getUserData())
    );
    std::cout << h->getTimePassed() << std::endl;
    // ....
}
blackpen
  • 2,339
  • 13
  • 15
  • Yes this is very helpful, but I won't mark it as an answer cause I can't get the time of execution, but Thank you very much, I appreciate your helpful answer. – duri Aug 18 '16 at 10:37
  • @duri, Once I am able to match requests with responses, I could measure "response time", by putting requests/timeValue in a global hash. I admit that it would be much easier if we had HTTPResponse::getResponseTime(), but I don't see such an interface. – blackpen Aug 18 '16 at 11:13
  • That's what I need. I am new to C++ and I don't know how to store key value pair. Right now I am trying to do it with Json::Value but I don't know if it works. Can you please provide an example how to create this global hash please? – duri Aug 18 '16 at 11:23
  • @duri, What is your c++ version (C++03,C++11,C++14)? What is your operating system (unix/linux, Windows)? – blackpen Aug 18 '16 at 13:05
  • I want to do this cross-platform, but I have Ubuntu 14.04 and I am using C++11. I just found out to get the fastest server I simply write down the IP address of the first response (it's the fastest one because it's response came first). – duri Aug 18 '16 at 15:09
  • @duri, does the posted code seem to address your need to get processing time? – blackpen Aug 22 '16 at 17:48