diff --git a/src/chat/main/authority.c b/src/chat/main/authority.c new file mode 100644 index 0000000..d5232b7 --- /dev/null +++ b/src/chat/main/authority.c @@ -0,0 +1,86 @@ +/* +This is part of WHY2 +Copyright (C) 2022 Václav Šmejkal + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include + +int main(void) +{ + why2_check_version(); //CHECK FOR UPDATES + why2_chat_init_authority(); //CREATE AUTHORITY DIRECTORY + + int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE CA SERVER SOCKET + pthread_t thread; + size_t line_length_buffer = 0; + char *line_buffer = NULL; + + if (listen_socket < 0) why2_die("Failed creating socket."); + + //DEFINE CA SERVER ADDRESS + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(WHY2_CHAT_AUTHORITY_PORT); + server_addr.sin_addr.s_addr = INADDR_ANY; + + //BIND SOCKET + if (bind(listen_socket, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) why2_die("Failed binding socket."); + + //LISTEN + if (listen(listen_socket, WHY2_MAX_CONNECTIONS) < 0) why2_die("Binding failed."); + + printf("CA server enabled.\n\n"); + + pthread_create(&thread, NULL, why2_accept_thread, &listen_socket); + + for (;;) + { + if (getline(&line_buffer, &line_length_buffer, stdin) == -1) why2_die("Reading input failed."); + + if (strcmp(line_buffer, WHY2_CHAT_COMMAND_PREFIX WHY2_CHAT_COMMAND_EXIT "\n") == 0) //USER REQUESTED PROGRAM EXIT + { + printf("Exiting...\n"); + break; + } + } + + //QUIT COMMUNICATION + why2_clean_connections(); //STOP LISTENING; DISCONNECT ALL USERS + why2_clean_threads(); //STOP WAITING FOR MESSAGES + + //DEALLOCATION + free(line_buffer); + close(listen_socket); + pthread_cancel(thread); + + why2_clean_memory(""); //RUN GARBAGE COLLECTOR + + return 0; +} \ No newline at end of file