|
| 1 | +#include "common.h" |
| 2 | +#include <pthread.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | +#include <sys/socket.h> |
| 7 | +#include <sys/types.h> |
| 8 | +#include <sys/un.h> |
| 9 | +#include <sys/epoll.h> |
| 10 | +#include <netinet/in.h> |
| 11 | +#include <unistd.h> |
| 12 | +#include <signal.h> |
| 13 | + |
| 14 | +Message message; |
| 15 | +int id; |
| 16 | +int socket_fd; |
| 17 | + |
| 18 | +MessageType to_message_type(const char *); |
| 19 | +void *receipt_routine(void *); |
| 20 | +void init(const int); |
| 21 | +void list(const int); |
| 22 | +void on_list(const char *); |
| 23 | +void send_all(const int); |
| 24 | +void send_one(const int); |
| 25 | +void on_message(); |
| 26 | +void stop(const int, const bool); |
| 27 | +void on_sigint(const int); |
| 28 | +void _on_exit(); |
| 29 | + |
| 30 | +int main(int argc, char **argv) |
| 31 | +{ |
| 32 | + printf("[Client] Started\n\n"); |
| 33 | + |
| 34 | + // Handle SIGINT signal |
| 35 | + struct sigaction action; |
| 36 | + action.sa_handler = on_sigint; |
| 37 | + sigaction(SIGINT, &action, NULL); |
| 38 | + |
| 39 | + // Start thread to receiving messages from the server |
| 40 | + pthread_t receipt_thread; |
| 41 | + pthread_create(&receipt_thread, NULL, receipt_routine, (void *) &socket_fd); |
| 42 | + |
| 43 | + const char *domain = argv[2]; |
| 44 | + |
| 45 | + // Network socket |
| 46 | + if (/*!strcmp(domain, "net")*/ true) { |
| 47 | + if ((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { |
| 48 | + perror("[Error] Failed to create the network socket"); |
| 49 | + return 1; |
| 50 | + } |
| 51 | + |
| 52 | + const char *address = argv[1]; |
| 53 | + int port = atoi(argv[2]); |
| 54 | + struct sockaddr_in socket_address; |
| 55 | + socket_address.sin_family = AF_INET; |
| 56 | + socket_address.sin_port = port; |
| 57 | + socket_address.sin_addr.s_addr = INADDR_ANY; |
| 58 | + connect(socket_fd, (struct sockaddr *) &socket_address, sizeof(socket_address)); |
| 59 | + |
| 60 | + init(socket_fd); |
| 61 | + |
| 62 | + char buffer[32]; |
| 63 | + char msg[MAX_MESSAGE_SIZE] = ""; |
| 64 | + while (fgets(buffer, 32, stdin) != NULL) { |
| 65 | + char command[32]; |
| 66 | + int client_id; |
| 67 | + sscanf(buffer, "%s", command); |
| 68 | + |
| 69 | + MessageType message_type = to_message_type(command); |
| 70 | + printf("%d\n", message_type); |
| 71 | + switch (message_type) { |
| 72 | + case MT_STOP: |
| 73 | + stop(socket_fd, true); |
| 74 | + break; |
| 75 | + case MT_LIST: |
| 76 | + list(socket_fd); |
| 77 | + break; |
| 78 | + case MT_SEND_ALL: |
| 79 | + sscanf(buffer, "%*s %[^\r\n]", msg); |
| 80 | + strcpy(message.message, msg); |
| 81 | + send_all(socket_fd); |
| 82 | + break; |
| 83 | + case MT_SEND_ONE: |
| 84 | + sscanf(buffer, "%*s %d %[^\r\n]", &client_id, msg); |
| 85 | + message.to_client_id = client_id; |
| 86 | + strcpy(message.message, msg); |
| 87 | + send_one(socket_fd); |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Local socket |
| 94 | + // else if (!strcmp(domain, "local")) { |
| 95 | + // if ((socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 96 | + // perror("[Error] Failed to create the local socket"); |
| 97 | + // return 1; |
| 98 | + // } |
| 99 | + |
| 100 | + // struct sockaddr_un socket_address; |
| 101 | + // socket_address.sun_family = AF_UNIX; |
| 102 | + // strcpy(socket_address.sun_path, LOCAL_SERVER_PATH); |
| 103 | + |
| 104 | + // connect(socket_fd, (struct sockaddr *) &socket_address, sizeof(socket_address)); |
| 105 | + // } |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +MessageType to_message_type(const char *str) |
| 111 | +{ |
| 112 | + if (strcmp(str, "LIST") == 0) |
| 113 | + return MT_LIST; |
| 114 | + if (strcmp(str, "2ALL") == 0) |
| 115 | + return MT_SEND_ALL; |
| 116 | + if (strcmp(str, "2ONE") == 0) |
| 117 | + return MT_SEND_ONE; |
| 118 | + if (strcmp(str, "STOP") == 0) |
| 119 | + return MT_STOP; |
| 120 | + |
| 121 | + printf("[Client] Wrong command (allowed: [LIST|2ALL|2ONE|STOP]). Exiting...\n"); |
| 122 | + exit(-1); |
| 123 | +} |
| 124 | + |
| 125 | +void *receipt_routine(void *args) |
| 126 | +{ |
| 127 | + int socket_fd = *(int *) args; |
| 128 | + while (true) |
| 129 | + { |
| 130 | + int flags = 0; |
| 131 | + int received_bytes = recv(socket_fd, &message, MESSAGE_SIZE, flags); |
| 132 | + if (received_bytes >= 0) { |
| 133 | + switch (message.message_type) { |
| 134 | + case MT_STOP: |
| 135 | + pthread_exit(NULL); |
| 136 | + break; |
| 137 | + case MT_LIST: |
| 138 | + on_list(message.message); |
| 139 | + break; |
| 140 | + case MT_MESSAGE: |
| 141 | + on_message(); |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +void init(const int socket_fd) |
| 149 | +{ |
| 150 | + // Send |
| 151 | + printf("[Client] Sending INIT message to the server... "); |
| 152 | + |
| 153 | + message.message_type = MT_INIT; |
| 154 | + int flags = 0; |
| 155 | + int sent_bytes = send(socket_fd, &message, MESSAGE_SIZE, flags); |
| 156 | + if (sent_bytes == -1) { |
| 157 | + printf("Failed\n"); |
| 158 | + perror("Send error"); |
| 159 | + } |
| 160 | + else { |
| 161 | + printf("Succeeded\n"); |
| 162 | + } |
| 163 | + |
| 164 | + // Receive |
| 165 | + printf("[Client] Receiving INIT message from the server... "); |
| 166 | + |
| 167 | + flags = 0; |
| 168 | + int received_bytes = recv(socket_fd, &message, MESSAGE_SIZE, flags); |
| 169 | + if (received_bytes == -1) { |
| 170 | + printf("Failed\n"); |
| 171 | + perror("Receive error"); |
| 172 | + } |
| 173 | + else { |
| 174 | + printf("Succeeded\n"); |
| 175 | + } |
| 176 | + |
| 177 | + id = message.client_id; |
| 178 | + printf("[Client] Client ID: %d\n\n", id); |
| 179 | +} |
| 180 | + |
| 181 | +void list(const int socket_fd) |
| 182 | +{ |
| 183 | + // Send |
| 184 | + printf("[Client] Sending LIST message to the server... "); |
| 185 | + |
| 186 | + message.message_type = MT_LIST; |
| 187 | + message.client_id = id; |
| 188 | + int flags = 0; |
| 189 | + int sent_bytes = send(socket_fd, &message, MESSAGE_SIZE, flags); |
| 190 | + if (sent_bytes == -1) { |
| 191 | + printf("Failed\n"); |
| 192 | + perror("Send error"); |
| 193 | + } |
| 194 | + else { |
| 195 | + printf("Succeed\n"); |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +void on_list(const char *message) |
| 200 | +{ |
| 201 | + // Receive |
| 202 | + printf("[Client] Received LIST message from the server\n"); |
| 203 | + printf("%s\n", message); |
| 204 | +} |
| 205 | + |
| 206 | +void send_all(const int socket_fd) |
| 207 | +{ |
| 208 | + printf("[Client] Sending 2ALL message to the server... "); |
| 209 | + |
| 210 | + message.message_type = MT_SEND_ALL; |
| 211 | + message.client_id = id; |
| 212 | + int flags = 0; |
| 213 | + int sent_bytes = send(socket_fd, &message, MESSAGE_SIZE, flags); |
| 214 | + if (sent_bytes == -1) { |
| 215 | + printf("Failed\n"); |
| 216 | + perror("send error"); |
| 217 | + printf("\n"); |
| 218 | + } |
| 219 | + else { |
| 220 | + printf("Succeed\n\n"); |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +void send_one(const int socket_id) |
| 225 | +{ |
| 226 | + printf("[Client] Sending 2ONE message to the server... "); |
| 227 | + |
| 228 | + message.message_type = MT_SEND_ALL; |
| 229 | + message.client_id = id; |
| 230 | + int flags = 0; |
| 231 | + int sent_bytes = send(socket_fd, &message, MESSAGE_SIZE, flags); |
| 232 | + if (sent_bytes == -1) { |
| 233 | + printf("Failed\n"); |
| 234 | + perror("send error"); |
| 235 | + printf("\n"); |
| 236 | + } |
| 237 | + else { |
| 238 | + printf("Succeed\n\n"); |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +void on_message() |
| 243 | +{ |
| 244 | + printf("\n[Client] Receive message from the server\n"); |
| 245 | + printf("[Client %d] %s\n", message.client_id, message.message); |
| 246 | + fflush(stdout); |
| 247 | +} |
| 248 | + |
| 249 | +void stop(const int socket_fd, const bool send_msg) |
| 250 | +{ |
| 251 | + if (send_msg) { |
| 252 | + printf("[Client] Sending STOP message to the server... "); |
| 253 | + |
| 254 | + message.message_type = MT_STOP; |
| 255 | + message.client_id = id; |
| 256 | + int flags = 0; |
| 257 | + int sent_bytes = send(socket_fd, &message, MESSAGE_SIZE, flags); |
| 258 | + if (sent_bytes == -1) { |
| 259 | + printf("Failed\n"); |
| 260 | + perror("Send error"); |
| 261 | + } |
| 262 | + else { |
| 263 | + printf("Succeed\n"); |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + close(socket_fd); |
| 268 | + |
| 269 | + exit(EXIT_SUCCESS); |
| 270 | +} |
| 271 | + |
| 272 | +void on_sigint(const int signum) |
| 273 | +{ |
| 274 | + printf("\n[Client] Received SIGINT\n"); |
| 275 | + stop(socket_fd, true); |
| 276 | +} |
| 277 | + |
| 278 | +// void onexit() { |
| 279 | +// if (receiver_pid > 0) |
| 280 | +// kill(receiver_pid, SIGKILL); |
| 281 | +// else if (getppid() > 0) |
| 282 | +// kill(getppid(), SIGINT); |
| 283 | + |
| 284 | +// printf("[Client] Stopped\n"); |
| 285 | +// } |
0 commit comments