Muli-Thread For HTTP Downloading

Muli-Thread For HTTP Downloading

BY 10 Apr 2013 Native Application Development

In our app, we are using threads (not main app thread) to handle the dowloading files in achive file (put on server) as screenshot:

Assuming that the network connection is open, the code snippet for downloading should be:

//Downloader.cpp
result Downloader::RequestFileInZip(int fileIndex/*, int numFileNeedToDownload*/)
{

    //The 1st thread

    pHttpClient1 = new HttpClient();
    pHttpClient1->Construct();
    result r = pHttpClient1->SendPartialGetRequest(FileOffsetInZip[fileIndex], FileOffsetInZip[fileIndex] + FileLengthInZip[fileIndex]);

    //The 2nd thread

    pHttpClient2 = new HttpClient();
    pHttpClient2->Construct();
    result r = pHttpClient2->SendPartialGetRequest(FileOffsetInZip[fileIndex + 1], FileOffsetInZip[fileIndex + 1] + FileLengthInZip[fileIndex + 1]);
    return r;
}

/*..................................................*/

//HttpClient.cpp

result HttpClient::Construct()
{
    return Thread::Construct(THREAD_TYPE_EVENT_DRIVEN);
}

result HttpClient::SendPartialGetRequest(int startPosition, int endPosition)
{
    this->startPosition = startPosition;
    this->endPosition = endPosition;
    return Start();
}

void HttpClient::OnStop(void) {}

bool HttpClient::OnStart(void)
{
    StartDownloading(this->startPosition, this->endPosition);
    return true;
}

result HttpClient::StartDownloading(int startPosition, int endPosition)

{

   //Open HTTP Session

   //Open HTTP Transaction

   //Add HTTP Transaction Listener

   //Handle HTTP Request

   //Submit HTTP

}

void HttpClient::OnTransactionReadyToRead(Tizen::Net::Http::HttpSession& httpSession, Tizen::Net::Http::HttpTransaction& httpTransaction, int availableBodyLen)

{

   //Saving file from HTTP Response

}

 

Our problem is:

   1. The app freezes:

  • The 1st thread downloads file 1 in some bytes.
  • The 2nd thread WOULD NOT able to construct the http session.

   2. If we ONLY use one thread, the 1st for instance, all 2 files will be downloaded successfully.

 

Our questions are:

    – How to fix the above #1 issue?

    – We did search muti-thread app in Tizen as well as its sibling – Bada, but there are some samples with one main thread + another thread; Does anyone recommend us other samples pose actual multi-threads?

Written by