Logo Search packages:      
Sourcecode: libwfut version File versions  Download package

int WFUT::IO::poll (  ) 

Poll network for data chunks to download and write to disk.

Definition at line 244 of file IO.cpp.

References DownloadComplete, and DownloadFailed.

Referenced by WFUT::WFUTClient::poll().

             {
  int num_handles;
  curl_multi_perform(m_mhandle, &num_handles);

  struct CURLMsg *msg = NULL;
  int msgs;

  while ((msg = curl_multi_info_read(m_mhandle, &msgs)) != NULL) {

    DataStruct *ds = NULL;
    int err = curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &ds);
    if (err != CURLE_OK) {
      // Do something on error
      fprintf(stderr, "Got some error on curl_easy_getinfo (%d)\n", err);
      continue;
    }

    bool failed = true; 
    std::string errormsg = "Unknown failure";
    switch (msg->msg) {
      case CURLMSG_DONE: {
        if (msg->data.result == CURLE_OK) {
          assert(ds != NULL);
          if (ds->expected_crc32 == 0L || 
              ds->expected_crc32 == ds->actual_crc32) {
            // Download success!
            failed = false;
            // Copy file to proper location
            if (copy_file(ds->fp, ds->path + "/" + ds->filename)) {
              errormsg = "Error copying file to target location.\n";
              failed = true;
            }
            if (ds->executable) {
              os_set_executable(ds->path + "/" + ds->filename);
            }
          } else {
            // CRC32 check failed
            failed = true;
            errormsg = "CRC32 mismatch";
          }
        } else {
          // Error downloading file
          failed = true;
          errormsg = "There was an error downloading the requested file: "
                   + std::string(curl_easy_strerror(msg->data.result));
        }
        break;
      }
      default:
        // Something not too good with curl...
        failed = true;
        errormsg = "There was an unknown error downloading the requested file";
    }

    if (debug) printf("Removing Handle\n");
    // Close handle     
    curl_multi_remove_handle(m_mhandle, msg->easy_handle);

    if (ds) {
      if (ds->fp) os_free_tmpfile(ds->fp);
      ds->fp = NULL;
      if (failed) {
        if (debug) printf("Download Failed\n");
        DownloadFailed.emit(ds->url, ds->filename, errormsg);
      } else {
        if (debug) printf("Download Complete\n");
        DownloadComplete.emit(ds->url, ds->filename);
      }
      m_files.erase(m_files.find(ds->url));
      curl_easy_cleanup(ds->handle);
      delete ds;
    }
  }

  // Spare capacity? Lets queue some more items.
  int diff = m_num_to_process - num_handles;
  if (diff > 0) {
    while (diff--) {
      if (!m_handles.empty()) {
        // This is where we tell curl about our downloads.
        curl_multi_add_handle(m_mhandle, m_handles.front());
        m_handles.pop();
        ++num_handles;
      }
    }
  }

  return num_handles;
}


Generated by  Doxygen 1.6.0   Back to index