コード例 #1
0
ファイル: hw4.c プロジェクト: raghavan/networking
void start_peers() {

    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    {
        int bytes_left = 0;
        bytes_left = missing_blocks() * piece_length;
        if(piece_status[get_total_pieces_available()-1] == PIECE_EMPTY){
            bytes_left -= piece_length;
            bytes_left += file_length%piece_length;
        }
    printf("Announce URL: %s\n",announce_url);
    }
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, announce_url);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
        curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
        curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1);

        FILE *anno = fopen("/tmp/anno.tmp","w+");
        if(!anno) {
            perror("couldn't create temporary file\n");
        }

        int attempts=0;
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, anno); 
        while((res = curl_easy_perform(curl)) !=CURLE_OK && 
                    attempts < 5) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                            curl_easy_strerror(res));
            attempts++;
        }
        fclose(anno);

        if (attempts<5) {
            struct stat anno_stat;
            if(stat("/tmp/anno.tmp",&anno_stat)) {
                perror("couldn't stat /tmp/anno.tmp");
                exit(1);
            }
            // the announcement document is in /tmp/anno.tmp. 
            // so map that into memory, then call handle_announcement on the returned pointer
            handle_announcement(mmap(0,anno_stat.st_size,PROT_READ,MAP_SHARED,open("/tmp/anno.tmp",O_RDONLY),0),anno_stat.st_size);
        }
        curl_easy_cleanup(curl);
    }
}
コード例 #2
0
ファイル: hw4.c プロジェクト: megd732/bittorrent-client
/* contact the tracker to get announcement, call handle_announcement on the result */
void start_peers() {
	/* download the announcement document using libcurl */
	CURL *curl;
	CURLcode res;

	curl = curl_easy_init();
	if(curl) {
		curl_easy_setopt(curl, CURLOPT_URL, announce_url);
		curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
		curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
		curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1);

		FILE *anno = fopen("/tmp/anno_meg.tmp","w+");
		if(!anno) {
			perror("couldn't create temporary file\n");
		}

		int attempts=0;
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, anno); 
		while((res = curl_easy_perform(curl)) !=CURLE_OK && 
					attempts < 5) {
			fprintf(stderr, "curl_easy_perform() failed: %s\n",
							curl_easy_strerror(res));
			attempts++;
		}
		fclose(anno);

		if (attempts<5) {
			struct stat anno_stat;
			if(stat("/tmp/anno_meg.tmp",&anno_stat)) { // put the results of the stat call into anno_stat
				perror("couldn't stat /tmp/anno_meg.tmp");
				exit(1);
			}
			// map .tmp file into memory, then call handle_announcement on the returned pointer
			handle_announcement(mmap(0,anno_stat.st_size,PROT_READ,MAP_SHARED,open("/tmp/anno_meg.tmp",O_RDONLY),0),anno_stat.st_size);
		}
		curl_easy_cleanup(curl);
	}
}