示例#1
0
void calc_recv(repa_sock_t *sock) {
	prefix_addr_t src_address = 0;
	int64_t num_packets = 0, bytes_recv = 0, total_bytes_recv = 0, total_bytes = INT64_MAX;
	long long int start_time, end_time, diff_time;
	char interest[LIBREPA_MAX_INTEREST_LEN], data[LIBREPA_MAX_DATA_LEN(0)];

	printf("Waiting to start...\n");
	bytes_recv = (int64_t)repa_recv(sock, interest, data, LIBREPA_MAX_DATA_LEN(0), &src_address, NULL);
	if (bytes_recv == sizeof(int64_t)) {
		memcpy(&total_bytes, data, sizeof(int64_t));
	}
	printf("Receiving...\n");
	start_time = get_nanoseconds();
	while (true) {
		if ((bytes_recv = repa_timed_recv(sock, interest, data, LIBREPA_MAX_DATA_LEN(0),
				&src_address, NULL, 1*USEC_PER_SEC)) > 0) {
			num_packets++;
			total_bytes_recv += bytes_recv;
		} else {
			// Nenhum byte recebido no intervalo de tempo de 1seg
			// indica o fim do recebimento
			break;
		}
		//printf("\rReceive %jd bytes...[%jd%%]", total_bytes_recv,
	//			(total_bytes_recv*100)/total_bytes);
	//	fflush(stdout);
	}
	end_time = get_nanoseconds();
	diff_time = end_time - start_time;

	// Retira 1 segundo de espera caso não tenha recebido
	// o byte de finalização do fluxo
	diff_time -= 1*NSEC_PER_SEC;

	double kbps = (((double)total_bytes_recv)/((double)diff_time))*((double)NSEC_PER_SEC/(double)1024);

	num_packets = (num_packets == 0 ? INT64_MAX : num_packets);
	total_bytes = (total_bytes == 0 ? INT64_MAX : total_bytes);
	printf("\rReceived %jd*%jd=%jd/%jd bytes in %llds %lldns -> %.2fKB/s - %jd%% loss\n",
			num_packets, total_bytes_recv/num_packets, total_bytes_recv,
			total_bytes, (diff_time/NSEC_PER_SEC), (diff_time%NSEC_PER_SEC), kbps,
			100-((total_bytes_recv*100)/total_bytes));
}
static PyObject* repaRecv(PyObject* self, PyObject* args) {
	repa_sock_t sock;
	char interest[255];
	char buffer[BUFFER_LEN];
	long int microseconds = 0;
	ssize_t buffer_size = 0;
	prefix_addr_t prefix = 0;

	if (PyArg_ParseTuple(args, "(ii)l", &sock.sock_send, &sock.sock_recv, &microseconds)) {
		if (microseconds == 0)
			microseconds = 1;
		buffer_size = repa_timed_recv(sock, interest, buffer, prefix, microseconds);
	} else {
		PyErr_SetString(RepaError, "Error on read parameters");
		return NULL;
	}

	if (buffer_size > 0) {
		return Py_BuildValue("(ss#i)", interest, buffer, buffer_size, prefix);
	}

	Py_RETURN_NONE;
}