tor_multipath_voip/src/measure.c
2019-10-09 17:07:51 +02:00

89 lines
3 KiB
C

#include "measure.h"
void measure_parse(int size, struct measure_conf* mc) {
struct timespec curr;
uint64_t micro_sec;
if (size != mc->payload_size) {
perror("read error");
exit(EXIT_FAILURE);
}
struct measure_packet* head = (struct measure_packet*) mc->payload;
if (clock_gettime(CLOCK_REALTIME, &curr) == -1){
perror("clock_gettime error");
exit(EXIT_FAILURE);
}
time_t now;
time(&now);
char* ctime_no_newline = strtok(ctime(&now), "\n");
micro_sec = elapsed_micros (&head->emit_time, &curr);
printf("[%s] Packet %llu latency %luµs with flag %d\n", ctime_no_newline, (unsigned long long)head->counter, micro_sec, head->flag);
if (!mc->is_server && head->counter >= mc->max_measure) {
printf("Measurement done\n");
exit(EXIT_SUCCESS);
}
}
void measure_prepare(struct measure_conf* mc) {
if (mc->interval <= 0) mc->interval = 1000;
if (mc->max_measure <= 0) mc->max_measure = 1;
if (mc->payload_size < sizeof(struct measure_packet)) mc->payload_size = sizeof(struct measure_packet);
if ((mc->payload = malloc(sizeof(char) * mc->payload_size)) == NULL) {
perror("payload malloc failed");
exit(EXIT_FAILURE);
}
memset(mc->payload, 0, mc->payload_size);
char *my_msg = "Tu n'es pas tout a fait la misere,\nCar les levres les plus pauvres te denoncent\nPar un sourire.";
size_t msg_len = strlen(my_msg);
size_t cursor_msg = 0;
for (size_t i = sizeof(struct measure_packet); i < mc->payload_size; i++) {
mc->payload[i] = my_msg[cursor_msg];
cursor_msg = (cursor_msg + 1) % msg_len;
}
}
struct measure_packet* measure_generate(struct measure_conf* mc) {
struct measure_packet* head = (struct measure_packet*)mc->payload;
mc->counter++;
head->counter = mc->counter;
head->is_echo = mc->is_rtt && !mc->is_server;
head->flag = 0;
if (clock_gettime(CLOCK_REALTIME, &head->emit_time) == -1) {
perror("clock_gettime error");
exit(EXIT_FAILURE);
}
return head;
}
void measure_next_tick(struct measure_conf *mc, struct timespec *next) {
struct measure_packet *head = (struct measure_packet*) mc->payload;
struct timespec now, *sent_at = &head->emit_time;
mc->counter = head->counter;
if (clock_gettime(CLOCK_REALTIME, &now) == -1) {
perror("clock_gettime error");
exit(EXIT_FAILURE);
}
memcpy(next, sent_at, sizeof(struct timespec));
while(!timespec_gt (next, &now)) {
next->tv_nsec += mc->interval * 1000000L;
if (next->tv_nsec > ONE_SEC) {
next->tv_sec += next->tv_nsec / ONE_SEC;
next->tv_nsec = next->tv_nsec % ONE_SEC;
}
mc->counter++;
}
mc->counter--;
printf("interval: %ld\n", mc->interval);
printf("sent_at: sec=%ld nsec=%ld \n", (uint64_t) sent_at->tv_sec, (uint64_t) sent_at->tv_nsec);
printf("now: sec=%ld nsec=%ld \n", (uint64_t) now.tv_sec, (uint64_t) now.tv_nsec);
printf("next: sec=%ld nsec=%ld \n", (uint64_t) next->tv_sec, (uint64_t) next->tv_nsec);
}
uint8_t measure_need_reply(struct measure_conf* mc) {
return mc->is_server && ((struct measure_packet*)mc->payload)->is_echo;
};