|
| 1 | +#include "include/bp_socket.h" |
| 2 | +#include <errno.h> |
| 3 | +#include <pthread.h> |
| 4 | +#include <signal.h> |
| 5 | +#include <stdint.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <string.h> |
| 9 | +#include <sys/socket.h> |
| 10 | +#include <sys/types.h> |
| 11 | +#include <time.h> |
| 12 | +#include <unistd.h> |
| 13 | + |
| 14 | +#define BUFFER_SIZE 1024 |
| 15 | +#define AF_BP 28 |
| 16 | + |
| 17 | +volatile int running = 1; |
| 18 | + |
| 19 | +struct client_data { |
| 20 | + int fd; |
| 21 | + struct sockaddr_bp dest_addr; |
| 22 | + struct sockaddr_bp src_addr; |
| 23 | +}; |
| 24 | + |
| 25 | +void handle_sigint(int sig) { |
| 26 | + printf("\nInterrupt received, shutting down...\n"); |
| 27 | + running = 0; |
| 28 | +} |
| 29 | + |
| 30 | +void *send_thread(void *arg) { |
| 31 | + struct client_data *data = (struct client_data *)arg; |
| 32 | + char send_buffer[BUFFER_SIZE]; |
| 33 | + int message_count = 0; |
| 34 | + |
| 35 | + printf("Send thread started\n"); |
| 36 | + |
| 37 | + while (running) { |
| 38 | + message_count++; |
| 39 | + snprintf(send_buffer, sizeof(send_buffer), "Hello from client! Message #%d", |
| 40 | + message_count); |
| 41 | + |
| 42 | + int flags = 0; |
| 43 | + flags |= MSG_ACK_REQUESTED; |
| 44 | + |
| 45 | + int ret = |
| 46 | + sendto(data->fd, send_buffer, strlen(send_buffer) + 1, flags, |
| 47 | + (struct sockaddr *)&data->dest_addr, sizeof(data->dest_addr)); |
| 48 | + if (ret < 0) { |
| 49 | + if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + printf("sendto failed for ipn:%u.%u: %s\n", |
| 53 | + data->dest_addr.bp_addr.ipn.node_id, |
| 54 | + data->dest_addr.bp_addr.ipn.service_id, strerror(errno)); |
| 55 | + break; |
| 56 | + } |
| 57 | + |
| 58 | + printf("Message sent: %s\n", send_buffer); |
| 59 | + } |
| 60 | + |
| 61 | + printf("Send thread exiting\n"); |
| 62 | + return NULL; |
| 63 | +} |
| 64 | + |
| 65 | +void *receive_thread(void *arg) { |
| 66 | + struct client_data *data = (struct client_data *)arg; |
| 67 | + struct msghdr msg; |
| 68 | + struct iovec iov; |
| 69 | + char buffer[BUFFER_SIZE]; |
| 70 | + struct sockaddr_bp src_addr; |
| 71 | + |
| 72 | + // Set up message structure for receiving |
| 73 | + iov.iov_base = buffer; |
| 74 | + iov.iov_len = sizeof(buffer); |
| 75 | + memset(&msg, 0, sizeof(msg)); |
| 76 | + msg.msg_iov = &iov; |
| 77 | + msg.msg_iovlen = 1; |
| 78 | + memset(&src_addr, 0, sizeof(src_addr)); |
| 79 | + msg.msg_name = &src_addr; |
| 80 | + msg.msg_namelen = sizeof(src_addr); |
| 81 | + |
| 82 | + printf("Receive thread started\n"); |
| 83 | + |
| 84 | + while (running) { |
| 85 | + ssize_t n = recvmsg(data->fd, &msg, 0); |
| 86 | + if (n < 0) { |
| 87 | + if (errno == EINTR) { |
| 88 | + printf("\nInterrupted by signal, exiting...\n"); |
| 89 | + break; |
| 90 | + } |
| 91 | + if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 92 | + // Timeout reached, continue loop to check running flag |
| 93 | + continue; |
| 94 | + } |
| 95 | + perror("recvmsg failed"); |
| 96 | + break; |
| 97 | + } |
| 98 | + |
| 99 | + printf("Received message (%zd bytes): %.*s\n", n, (int)n, buffer); |
| 100 | + if (msg.msg_namelen >= sizeof(struct sockaddr_bp)) { |
| 101 | + printf("Bundle sent by ipn:%u.%u\n", src_addr.bp_addr.ipn.node_id, |
| 102 | + src_addr.bp_addr.ipn.service_id); |
| 103 | + } |
| 104 | + |
| 105 | + memset(&msg, 0, sizeof(msg)); |
| 106 | + msg.msg_iov = &iov; |
| 107 | + msg.msg_iovlen = 1; |
| 108 | + memset(&src_addr, 0, sizeof(src_addr)); |
| 109 | + msg.msg_name = &src_addr; |
| 110 | + msg.msg_namelen = sizeof(src_addr); |
| 111 | + } |
| 112 | + |
| 113 | + printf("Receive thread exiting\n"); |
| 114 | + return NULL; |
| 115 | +} |
| 116 | + |
| 117 | +int main(int argc, char *argv[]) { |
| 118 | + struct sockaddr_bp dest_addr, src_addr; |
| 119 | + int fd; |
| 120 | + uint32_t node_id, service_id; |
| 121 | + int ret = 0; |
| 122 | + pthread_t send_tid, recv_tid; |
| 123 | + struct client_data data; |
| 124 | + |
| 125 | + if (argc < 3) { |
| 126 | + printf("Usage: %s <node_id> <service_id>\n", argv[0]); |
| 127 | + return EXIT_FAILURE; |
| 128 | + } |
| 129 | + |
| 130 | + signal(SIGINT, handle_sigint); |
| 131 | + |
| 132 | + node_id = (uint32_t)atoi(argv[1]); |
| 133 | + service_id = (uint32_t)atoi(argv[2]); |
| 134 | + |
| 135 | + if (service_id < 1 || service_id > 255) { |
| 136 | + fprintf(stderr, "Invalid service_id (must be in 1-255)\n"); |
| 137 | + return EXIT_FAILURE; |
| 138 | + } |
| 139 | + |
| 140 | + if (node_id == 0) { |
| 141 | + fprintf(stderr, "Invalid node_id (cannot be 0)\n"); |
| 142 | + return EXIT_FAILURE; |
| 143 | + } |
| 144 | + |
| 145 | + fd = socket(AF_BP, SOCK_DGRAM, 0); |
| 146 | + if (fd < 0) { |
| 147 | + perror("socket creation failed"); |
| 148 | + return EXIT_FAILURE; |
| 149 | + } |
| 150 | + |
| 151 | + src_addr.bp_family = AF_BP; |
| 152 | + src_addr.bp_scheme = BP_SCHEME_IPN; |
| 153 | + src_addr.bp_addr.ipn.node_id = 10; |
| 154 | + src_addr.bp_addr.ipn.service_id = 2; |
| 155 | + if (bind(fd, (struct sockaddr *)&src_addr, sizeof(src_addr)) == -1) { |
| 156 | + perror("Failed to bind socket"); |
| 157 | + ret = EXIT_FAILURE; |
| 158 | + goto out; |
| 159 | + } |
| 160 | + |
| 161 | + struct timeval timeout; |
| 162 | + timeout.tv_sec = 1; // 1 second timeout |
| 163 | + timeout.tv_usec = 0; |
| 164 | + if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) { |
| 165 | + perror("Failed to set socket timeout"); |
| 166 | + ret = EXIT_FAILURE; |
| 167 | + goto out; |
| 168 | + } |
| 169 | + |
| 170 | + dest_addr.bp_family = AF_BP; |
| 171 | + dest_addr.bp_scheme = BP_SCHEME_IPN; |
| 172 | + dest_addr.bp_addr.ipn.node_id = node_id; |
| 173 | + dest_addr.bp_addr.ipn.service_id = service_id; |
| 174 | + |
| 175 | + data.fd = fd; |
| 176 | + data.dest_addr = dest_addr; |
| 177 | + data.src_addr = src_addr; |
| 178 | + |
| 179 | + printf("BP Client started - sending messages to ipn:%u.%u\n", node_id, |
| 180 | + service_id); |
| 181 | + printf("Press Ctrl+C to exit.\n"); |
| 182 | + |
| 183 | + if (pthread_create(&send_tid, NULL, send_thread, &data) != 0) { |
| 184 | + perror("Failed to create send thread"); |
| 185 | + ret = EXIT_FAILURE; |
| 186 | + goto out; |
| 187 | + } |
| 188 | + |
| 189 | + if (pthread_create(&recv_tid, NULL, receive_thread, &data) != 0) { |
| 190 | + perror("Failed to create receive thread"); |
| 191 | + running = 0; |
| 192 | + pthread_join(send_tid, NULL); |
| 193 | + ret = EXIT_FAILURE; |
| 194 | + goto out; |
| 195 | + } |
| 196 | + |
| 197 | + pthread_join(send_tid, NULL); |
| 198 | + pthread_join(recv_tid, NULL); |
| 199 | + |
| 200 | +out: |
| 201 | + close(fd); |
| 202 | + printf("Socket closed.\n"); |
| 203 | + return ret; |
| 204 | +} |
0 commit comments