Пример #1
0
void calc_send(repa_sock_t *sock, const char* interest, size_t packet_size,
		const int wait_time, const int64_t total_bytes) {
	long long int start_time, end_time, diff_time;
	int64_t total_bytes_sent = 0;

	{
		size_t max_packet_size = LIBREPA_MAX_DATA_LEN(strlen(interest));
		packet_size = (packet_size > max_packet_size ? max_packet_size : packet_size);
	}

	char data[packet_size];

	/* initialize random seed: */
	srand (time(NULL));

	printf("Starting sending %"PRId64" [%zd]...\n", total_bytes, sizeof(int64_t));
	repa_send(sock, interest, &total_bytes, sizeof(int64_t), 0);
	start_time = get_nanoseconds();
	while (true) {
		if (repa_send(sock, interest, data, packet_size, 0) > 0) {
			total_bytes_sent += packet_size;
			if (total_bytes_sent >= total_bytes) {
				// Finalizando o envio
				break;
			}
		} else {
			// Erro ao tentar enviar pacote
			break;
		}

		if (wait_time == 1) usleep((rand()%8000)+2500);

		//printf("\rSent %jd bytes...[%jd%%]", total_bytes_sent,
		///		(total_bytes_sent*100)/total_bytes);
	//	fflush(stdout);
	}
	end_time = get_nanoseconds();
	diff_time = end_time - start_time;

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

	printf("\rSent %ju*%zd=%jd bytes in %llds %lldns -> %.2fKB/s\n",
			total_bytes_sent/packet_size, packet_size, total_bytes_sent,
			(diff_time/NSEC_PER_SEC), (diff_time%NSEC_PER_SEC), kbps);
}
void sendMessage(char* data){
    char* interest = (char*)malloc(255*sizeof(char));
    strcpy(interest,"server");
    printf("%s\n",data);
    size_t data_len = strlen(data);
    if (data_len > 0) {
        repa_send(sock, interest, data, data_len, 0);
        printf("Message sent I: \"%s\" D: \"%s\"\n", interest, data);
    }
    free(interest);
}
static PyObject* repaSend(PyObject* self, PyObject* args) {
	repa_sock_t sock;
	prefix_addr_t prefix = 0;
	Py_ssize_t buffer_size, result;
	const char *interest, *buffer;

    /* Wait for two strings and two integers */
    /* Python call is: f(sock, 'interest', 'data', data_size, prefix) */
	if (PyArg_ParseTuple(args, "(ii)ss#|i", &sock.sock_send, &sock.sock_recv, &interest, &buffer, &buffer_size, &prefix)) {
		result = repa_send(sock, interest, buffer, buffer_size, prefix);
	} else {
        PyErr_SetString(RepaError, "Error on read parameters");
        return NULL;
	}

	return Py_BuildValue("i", result);
}