From a4094d60f45dcdf75f4bff25dcaa2d66b9f5eb37 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Sun, 23 Jun 2024 20:50:15 -0500 Subject: [PATCH] debug print cleanup --- ping_pong.c | 10 +++++----- ping_pong.h | 2 ++ serial_forwarder.c | 18 +++++++----------- time_display.c | 20 +++++++++++++++----- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/ping_pong.c b/ping_pong.c index 4ad4402..6f53680 100644 --- a/ping_pong.c +++ b/ping_pong.c @@ -19,7 +19,7 @@ int handle_ping_pong(int sockfd, return 0; // recoverable } - printf("recv ping; send pong\n"); + //printf("recv ping; send pong\n"); struct packet_ping_pong * ping_pkt = (struct packet_ping_pong *)buf; int ret = packet_send_pong(sockfd, dest_addr, &ping_pkt->time); if (ret == -1) { @@ -34,7 +34,7 @@ int handle_ping_pong(int sockfd, return 0; // recoverable } - printf("recv pong\n"); + //printf("recv pong\n"); struct packet_ping_pong * ping_pkt = (struct packet_ping_pong *)buf; struct timespec now; int ret = clock_gettime(CLOCK_MONOTONIC_RAW, &now); @@ -44,10 +44,9 @@ int handle_ping_pong(int sockfd, struct timespec * rtt = &link_state->ping_pong_rtt[link_state->rtt_ix]; link_state->rtt_ix = (link_state->rtt_ix + 1) % RTT_AVERAGE_SAMPLES; *rtt = diff_timespec(&now, &ping_pkt->time); - printf("rtt: %ld.%09ld\n", rtt->tv_sec, rtt->tv_nsec); + printf("pong rtt: %ld.%09ld\n", rtt->tv_sec, rtt->tv_nsec); struct timespec average_rtt = average_timespec(link_state->ping_pong_rtt, RTT_AVERAGE_SAMPLES); - printf("send_average_rtt\n"); ret = packet_send_average_rtt(sockfd, dest_addr, &average_rtt); if (ret == -1) { return -1; @@ -60,8 +59,9 @@ int handle_ping_pong(int sockfd, printf("handle_ping_pong: average_rtt: invalid length\n"); return 0; // recoverable } + struct packet_ping_pong * ping_pkt = (struct packet_ping_pong *)buf; - printf("recv average_rtt\n"); + printf("recv average_rtt: %ld.%09ld\n", ping_pkt->time.tv_sec, ping_pkt->time.tv_nsec); } break; default: diff --git a/ping_pong.h b/ping_pong.h index c83b7f1..3103f11 100644 --- a/ping_pong.h +++ b/ping_pong.h @@ -4,6 +4,8 @@ #include "link.h" +#define PING_INTERVAL 5 + int handle_ping_pong(int sockfd, struct sockaddr_in6 * dest_addr, void * buf, ssize_t length, diff --git a/serial_forwarder.c b/serial_forwarder.c index 3894659..4bdddbe 100644 --- a/serial_forwarder.c +++ b/serial_forwarder.c @@ -71,7 +71,7 @@ int handle_sockfd(int sockfd, (struct sockaddr *)&src_addr, &addrlen); if (recv_len == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { - perror("sock eagain"); + //perror("sock eagain"); return 0; } else { perror("recvfrom sock"); @@ -79,10 +79,10 @@ int handle_sockfd(int sockfd, } } - char src_addr_str[INET6_ADDRSTRLEN]; - inet_ntop(AF_INET6, &src_addr.sin6_addr, src_addr_str, INET6_ADDRSTRLEN); - printf("Received packet from %s:%d\n", src_addr_str, ntohs(src_addr.sin6_port)); - printf("length: %ld\n", recv_len); + //char src_addr_str[INET6_ADDRSTRLEN]; + //inet_ntop(AF_INET6, &src_addr.sin6_addr, src_addr_str, INET6_ADDRSTRLEN); + //printf("received packet from %s:%d\n", src_addr_str, ntohs(src_addr.sin6_port)); + //printf("length: %ld\n", recv_len); int ret = handle_buf(sockfd, dest_addr, buf, recv_len, link_state); if (ret == -1) { @@ -94,7 +94,7 @@ int handle_sockfd(int sockfd, int rearm_timer(int timerfd) { struct itimerspec value; - value.it_value.tv_sec = 5; + value.it_value.tv_sec = PING_INTERVAL; value.it_value.tv_nsec = 0; value.it_interval.tv_sec = 0; value.it_interval.tv_nsec = 0; @@ -118,7 +118,7 @@ int handle_timerfd(int timerfd, ssize_t len = read(timerfd, &expired_count, (sizeof (expired_count))); if (len == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { - fprintf(stderr, "timerfd eagain\n"); + //fprintf(stderr, "timerfd eagain\n"); break; } else { perror("read timerfd"); @@ -126,11 +126,9 @@ int handle_timerfd(int timerfd, } } assert(len == (sizeof (expired_count))); - printf("len %ld\n", len); } rearm_timer(timerfd); - printf("do timer stuff\n"); int ret = packet_send_ping(sockfd, dest_addr); if (ret == -1) { @@ -289,8 +287,6 @@ int main(void) struct link_state link_state = {0}; while (1) { - printf("Wait for datagram\n"); - int nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); if (nfds == -1) { perror("epoll_wait"); diff --git a/time_display.c b/time_display.c index 78ecb13..55bd69f 100644 --- a/time_display.c +++ b/time_display.c @@ -239,10 +239,11 @@ int handle_sockfd(int sockfd, } } - char src_addr_str[INET6_ADDRSTRLEN]; - inet_ntop(AF_INET6, &src_addr.sin6_addr, src_addr_str, INET6_ADDRSTRLEN); - printf("Received packet from %s:%d\n", src_addr_str, ntohs(src_addr.sin6_port)); - printf("length: %ld\n", recv_len); + //char src_addr_str[INET6_ADDRSTRLEN]; + //inet_ntop(AF_INET6, &src_addr.sin6_addr, src_addr_str, INET6_ADDRSTRLEN); + //printf("received packet from %s:%d\n", src_addr_str, ntohs(src_addr.sin6_port)); + //printf("length: %ld\n", recv_len); + int ret = handle_buf(sockfd, dest_addr, buf, recv_len, timer_state, link_state); if (ret == -1) { return -1; @@ -301,6 +302,7 @@ int main() int last_width = -1; uint64_t ticks = SDL_GetTicks(); + uint64_t last_ping_tick = 0; const int min_length = 5; int max_length = min_length; @@ -380,7 +382,7 @@ int main() int window_height; ret = SDL_GetWindowSizeInPixels(window, &window_width, &window_height); assert(ret == 0); - if ((window_width != last_width) || (length > max_length)) { + if ((window_width != last_width) || (length > max_length)) { max_length = max(min_length, length); last_width = window_width; @@ -395,6 +397,14 @@ int main() while (SDL_GetTicks() - ticks < (1000 / 60)) { SDL_Delay(1); } SDL_RenderPresent(renderer); ticks = SDL_GetTicks(); + if (ticks - last_ping_tick > PING_INTERVAL * 1000) { + //printf("send ping\n"); + int ret = packet_send_ping(sockfd, &dest_addr); + if (ret == -1) { + return -1; + } + last_ping_tick = ticks; + } SDL_Event event; while (SDL_PollEvent(&event)) {