diff --git a/log/log b/log/log index 07aac05..5ada5f8 100755 Binary files a/log/log and b/log/log differ diff --git a/log/log.cpp b/log/log.cpp index 4144e56..adf6fbf 100644 --- a/log/log.cpp +++ b/log/log.cpp @@ -1,33 +1,64 @@ -#include -#include -#include -#include #include -#include #include -#include +#include +#include +#include +#include +#include +#include +#include +#include + +const char* LOG_FIFO_NAME = "/pipe/log"; std::string LOG_DIR = "/log_data"; -void log(std::string msg) { - mkdir(LOG_DIR.c_str(), 0755); - time_t now = time(0); - struct tm tstruct; - tstruct = *localtime(&now); - char filename[12]; - std::strftime(filename, 12, "%Y-%m-%d", &tstruct); - std::ofstream log_file; - log_file.open(LOG_DIR + "/" + filename + ".log", std::ios_base::app); - char timestamp[32]; - std::strftime(timestamp, 32, "%Y/%m/%d %H:%M:%S ", &tstruct); - log_file << timestamp << msg << std::endl; - log_file.close(); +int pipeFd; + +void handle_signal(int sig) { + if (sig == SIGTERM || sig == SIGINT) { + close(pipeFd); + unlink(LOG_FIFO_NAME); + } } int main() { - log("uwu"); - log("owo"); - log("yay"); + signal(SIGINT, handle_signal); + signal(SIGTERM, handle_signal); + mkdir(LOG_DIR.c_str(), 0755); + + // Create or open the named pipe + mkfifo(LOG_FIFO_NAME, 0666); + + // Open the pipe for reading + pipeFd = open(LOG_FIFO_NAME, O_RDONLY); + if (pipeFd == -1) { + std::cout << "Error opening pipe!" << std::endl; + return 1; + } + + // Read log messages from the pipe and write them to the log file + char buffer[256]; + while (true) { + ssize_t bytesRead = read(pipeFd, buffer, sizeof(buffer) - 1); + if (bytesRead > 0) { + buffer[bytesRead] = '\0'; + time_t now = time(0); + struct tm tstruct; + tstruct = *localtime(&now); + char filename[12]; + std::strftime(filename, 12, "%Y-%m-%d", &tstruct); + std::ofstream log_file; + log_file.open(LOG_DIR + "/" + filename + ".log", std::ios_base::app); + char timestamp[32]; + std::strftime(timestamp, 32, "%Y/%m/%d %H:%M:%S ", &tstruct); + log_file << timestamp << buffer << std::endl; + log_file.close(); + } + } + + // Clean up + close(pipeFd); return 0; } \ No newline at end of file diff --git a/log_data/2024-12-12.log b/log_data/2024-12-12.log index c34ba05..0078a10 100644 --- a/log_data/2024-12-12.log +++ b/log_data/2024-12-12.log @@ -1,6 +1,151 @@ -2024/12/12 00:24:34 uwu -2024/12/12 00:24:34 owo -2024/12/12 00:24:34 yay -2024/12/12 00:24:50 uwu -2024/12/12 00:24:50 owo -2024/12/12 00:24:50 yay +2024/12/12 19:56:03 Creating socket... +2024/12/12 19:56:03 Socket created +2024/12/12 19:56:03 Listening for connections... +2024/12/12 19:56:10 New connection accepted +2024/12/12 19:56:10 New thread created for socket 5 +2024/12/12 19:56:12 Got new message from client 5: +2024/12/12 19:56:12 Sent new info to client 5 +2024/12/12 19:56:14 Got new message from client 5: +2024/12/12 19:56:15 Got new message from client 5: +2024/12/12 19:56:16 Got new message from client 5: +2024/12/12 19:56:16 Sent new info to client 5 +2024/12/12 19:56:18 Got new message from client 5: +2024/12/12 19:56:18 Client 5 info is not up to dateSent new info to client 5 +2024/12/12 19:56:19 Got new message from client 5: +2024/12/12 19:56:19 Actual info already on client 5 +2024/12/12 19:56:20 Got new message from client 5: +2024/12/12 19:56:20 Sent new info to client 5 +2024/12/12 19:56:21 Got new message from client 5: +2024/12/12 19:56:21 Sent new info to client 5 +2024/12/12 19:56:22 Got new message from client 5: +2024/12/12 19:56:23 Got new message from client 5: +2024/12/12 19:56:23 Client 5 info is not up to dateSent new info to client 5 +2024/12/12 19:56:24 Got new message from client 5: +2024/12/12 19:56:24 Sent new info to client 5 +2024/12/12 19:56:25 Got new message from client 5: +2024/12/12 19:56:26 Got new message from client 5: +2024/12/12 19:56:26 Client 5 info is not up to date +2024/12/12 19:56:26 Sent new info to client 5 +2024/12/12 19:56:27 Got new message from client 5: +2024/12/12 19:56:27 Sent new info to client 5 +2024/12/12 19:56:28 Got new message from client 5: +2024/12/12 19:56:29 Got new message from client 5: +2024/12/12 19:56:29 Sent new info to client 5 +2024/12/12 19:56:30 Got new message from client 5: +2024/12/12 19:56:31 Got new message from client 5: +2024/12/12 19:56:32 Got new message from client 5: +2024/12/12 19:56:32 Client 5 info is not up to dateSent new info to client 5 +2024/12/12 19:56:33 Got new message from client 5: +2024/12/12 19:56:33 Sent new info to client 5 +2024/12/12 19:56:34 Got new message from client 5: +2024/12/12 19:56:35 Got new message from client 5: +2024/12/12 19:56:35 Sent new info to client 5 +2024/12/12 19:56:36 Got new message from client 5: +2024/12/12 19:56:36 Client 5 info is not up to date +2024/12/12 19:56:36 Sent new info to client 5 +2024/12/12 19:56:37 Got new message from client 5: +2024/12/12 19:56:38 Got new message from client 5: +2024/12/12 19:56:38 Sent new info to client 5 +2024/12/12 19:56:39 Got new message from client 5: +2024/12/12 19:56:39 Client 5 info is not up to date +2024/12/12 19:56:39 Sent new info to client 5 +2024/12/12 19:56:40 Got new message from client 5: +2024/12/12 19:56:40 Actual info already on client 5 +2024/12/12 19:56:40 Connection with client 5 closed +2024/12/12 19:57:04 Creating socket... +2024/12/12 19:57:04 Socket created +2024/12/12 19:57:04 Listening for connections... +2024/12/12 19:57:10 New connection accepted +2024/12/12 19:57:10 New thread created for socket 5 +2024/12/12 19:57:12 Got new message from client 5: +2024/12/12 19:57:12 Client 5 info is not up to date +2024/12/12 19:57:12 Sent new info to client 5 +2024/12/12 19:57:13 Got new message from client 5: +2024/12/12 19:57:13 Sent new info to client 5 +2024/12/12 19:57:14 Got new message from client 5: +2024/12/12 19:57:15 Got new message from client 5: +2024/12/12 19:57:15 Client 5 info is not up to date +2024/12/12 19:57:15 Sent new info to client 5 +2024/12/12 19:57:16 Got new message from client 5: +2024/12/12 19:57:16 Sent new info to client 5 +2024/12/12 19:57:17 Got new message from client 5: +2024/12/12 19:57:18 Got new message from client 5: +2024/12/12 19:57:18 Sent new info to client 5 +2024/12/12 19:57:19 Got new message from client 5: +2024/12/12 19:57:19 Client 5 info is not up to date +2024/12/12 19:57:19 Sent new info to client 5 +2024/12/12 19:57:20 Got new message from client 5: +2024/12/12 19:57:20 Actual info already on client 5 +2024/12/12 19:57:21 Got new message from client 5: +2024/12/12 19:57:21 Sent new info to client 5 +2024/12/12 19:57:22 Got new message from client 5: +2024/12/12 19:57:23 Got new message from client 5: +2024/12/12 19:57:24 Got new message from client 5: +2024/12/12 19:57:25 Got new message from client 5: +2024/12/12 19:57:26 Got new message from client 5: +2024/12/12 19:57:27 Got new message from client 5: +2024/12/12 19:57:27 Client 5 info is not up to dateSent new info to client 5 +2024/12/12 19:57:28 Got new message from client 5: +2024/12/12 19:57:28 Sent new info to client 5 +2024/12/12 19:57:29 Got new message from client 5: +2024/12/12 19:57:29 Actual info already on client 5 +2024/12/12 19:57:30 Got new message from client 5: +2024/12/12 19:57:30 Client 5 info is not up to date +2024/12/12 19:57:30 Sent new info to client 5 +2024/12/12 19:57:31 Got new message from client 5: +2024/12/12 19:57:31 Client 5 info is not up to dateSent new info to client 5 +2024/12/12 19:57:32 Got new message from client 5: +2024/12/12 19:57:33 Got new message from client 5: +2024/12/12 19:57:33 Client 5 info is not up to date +2024/12/12 19:57:33 Sent new info to client 5 +2024/12/12 19:57:34 Got new message from client 5: +2024/12/12 19:57:34 Client 5 info is not up to dateSent new info to client 5 +2024/12/12 19:57:35 Got new message from client 5: +2024/12/12 19:57:36 Got new message from client 5: +2024/12/12 19:57:36 Sent new info to client 5 +2024/12/12 19:57:37 Got new message from client 5: +2024/12/12 19:57:37 Sent new info to client 5 +2024/12/12 19:57:38 Got new message from client 5: +2024/12/12 19:57:38 Actual info already on client 5 +2024/12/12 19:57:39 Got new message from client 5: +2024/12/12 19:57:39 Sent new info to client 5 +2024/12/12 19:57:40 Got new message from client 5: +2024/12/12 19:57:40 Sent new info to client 5 +2024/12/12 19:57:41 Got new message from client 5: +2024/12/12 19:57:41 Actual info already on client 5 +2024/12/12 19:57:42 Got new message from client 5: +2024/12/12 19:57:42 Sent new info to client 5 +2024/12/12 19:57:42 Connection with client 5 closed +2024/12/12 20:00:42 Socket created +2024/12/12 20:00:49 New thread created for socket 5 +2024/12/12 20:00:55 Got new message from client 5 +2024/12/12 20:00:55 Sent new info to client +2024/12/12 20:00:58 Got new message from client 5 +2024/12/12 20:00:58 Sent new info to client +2024/12/12 20:01:00 Got new message from client 5 +2024/12/12 20:01:03 Got new message from client 5 +2024/12/12 20:01:03 Sent new info to client +2024/12/12 20:01:04 Got new message from client 5 +2024/12/12 20:01:04 Sent new info to client +2024/12/12 20:01:05 Got new message from client 5 +2024/12/12 20:01:06 Got new message from client 5 +2024/12/12 20:01:07 Got new message from client 5 +2024/12/12 20:01:07 Sent new info to client +2024/12/12 20:01:08 Got new message from client 5 +2024/12/12 20:01:08 Actual info already on client +2024/12/12 20:01:09 Got new message from client 5 +2024/12/12 20:01:09 Sent new info to client +2024/12/12 20:01:10 Got new message from client 5 +2024/12/12 20:01:10 Sent new info to client +2024/12/12 20:01:11 Got new message from client 5 +2024/12/12 20:01:11 Actual info already on client +2024/12/12 20:01:12 Got new message from client 5 +2024/12/12 20:01:13 Got new message from client 5 +2024/12/12 20:01:14 Got new message from client 5 +2024/12/12 20:01:14 Actual info already on client +2024/12/12 20:01:21 Connection with client 5 closed +2024/12/12 20:14:22 Socket created +2024/12/12 20:14:31 New thread created for socket 4 +2024/12/12 20:14:49 Got new message from client 4 +2024/12/12 20:14:54 Connection with client 4 closed +2024/12/12 20:22:52 New thread created for socket 4 diff --git a/server1/server b/server1/server index 69d8afe..d866659 100755 Binary files a/server1/server and b/server1/server differ diff --git a/server1/server.cpp b/server1/server.cpp index 4a61762..e310cd9 100644 --- a/server1/server.cpp +++ b/server1/server.cpp @@ -24,7 +24,7 @@ /* Configuration */ // Name of the pipe used to transfer logs to the log server -std::string LOG_FIFO_NAME = "./tmp/log"; +std::string LOG_FIFO_NAME = "./../tmp/log"; // Type of the server: server1 - last error & cursor position, server2 - mem % & vmem % std::string SERVER_TYPE = "server1"; @@ -53,6 +53,8 @@ int active_connections = 0; // Total amount of handled requests int handndled_requests = 0; +// Log pipe file descriptor +int is_log_ok = false; // Initializes ncurses lib void init_ncurses() { @@ -191,23 +193,24 @@ void log(std::string msg) { std::strftime(timestamp, 32, "%H:%M:%S ", &tstruct); last_log_messages[0] = timestamp + std::string(" ") + msg; - // Write message to the logging pipe TODO - // int fd = open(LOG_FIFO_NAME.c_str(), O_WRONLY); - // if(fd != -1) { - // write(fd, msg.c_str(), strlen(msg.c_str()) + 1); - // close(fd); - // } + // Write message to the logging pipe + int log_fd = open(LOG_FIFO_NAME.c_str(), O_WRONLY); + if(log_fd != -1) { + is_log_ok = true; + write(log_fd, msg.c_str(), msg.length()); + close(log_fd); + } else { + is_log_ok = false; + } } -// Opens file descriptor for logging pipe TODO -void init_log_pipe() { - // mkfifo(LOG_FIFO_NAME.c_str(), 0666); -} +// // Opens file descriptor for logging pipe TODO +// void init_log_pipe() { +// log_fd = open(LOG_FIFO_NAME.c_str(), O_WRONLY); +// } // Returns file descriptor for server socket int start_socket() { - log("Creating socket..."); - int server_socket = socket(AF_INET, SOCK_STREAM, 0); sockaddr_in server_address; server_address.sin_family = AF_INET; @@ -221,8 +224,6 @@ int start_socket() { return -1; } - log("Socket created"); - int status = listen(server_socket, MAX_CONNECTIONS); if (status != 0) { @@ -230,8 +231,6 @@ int start_socket() { return -1; } - log("Listening for connections..."); - return server_socket; } @@ -243,7 +242,7 @@ void* handle_connection(void* arg) { std::string info = ""; - log("New thread created for socket " + std::to_string(client_socket)); + log("New thread created for socket " + std::to_string(client_socket) + '\0'); while (true) { // Recieve message from client @@ -258,16 +257,15 @@ void* handle_connection(void* arg) { } if (bytes_recieved == -1) { - log("Error while recieving message: " + std::to_string(errno) + " - " + std::strerror(errno)); + log("Error while recieving message: " + std::to_string(errno) + " - " + std::strerror(errno) + '\0'); break; } - log("Got new message from client " + std::to_string(client_socket) + ": " + buffer); + log("Got new message from client " + std::to_string(client_socket) + '\0'); std::string new_info = get_sysinfo(); if (info != new_info) { - log("Client " + std::to_string(client_socket) + " info is not up to date"); info = new_info; time_t now = time(0); struct tm tstruct; @@ -276,10 +274,10 @@ void* handle_connection(void* arg) { std::strftime(timestamp, 32, "%Y/%m/%d %H:%M:%S ", &tstruct); std::string msg = timestamp + info; send(client_socket, msg.c_str(), strlen(msg.c_str()) + 1, 0); - log("Sent new info to client " + std::to_string(client_socket)); + log("Sent new info to client\0"); handndled_requests++; } else { - log("Actual info already on client " + std::to_string(client_socket)); + log("Actual info already on client\0"); } } @@ -302,6 +300,12 @@ void* draw(void* arg) { LOG_FIFO_NAME.c_str(), SERVER_TYPE.c_str(), PORT, MAX_CONNECTIONS, CMD_LOG_BUFFER, CMD_REFRESH_TIME ); + if (is_log_ok) { + printw("Log server connected\n\n"); + } else { + printw("Log server is not connected\n\n"); + } + // Print statistics printw("Active connections: %i\nHandled messages: %i\n", active_connections, handndled_requests); @@ -337,7 +341,6 @@ void* commands(void* arg) { int main() { init_ncurses(); - init_log_pipe(); // Create thread to redraw console output pthread_t tid; @@ -350,6 +353,8 @@ int main() { int server_socket = start_socket(); + log("Socket created\0"); + // Create thread to read commands pthread_t tcid; int* server_socket_copy = new int(server_socket); @@ -367,14 +372,12 @@ int main() { continue; } - log("New connection accepted"); active_connections++; // Create thread for each connection int* client_socket_copy = new int(client_socket); pthread_t tid; if (pthread_create(&tid, nullptr, &handle_connection, client_socket_copy) != 0) { - log("Failed to create thread for the connection"); delete client_socket_copy; } pthread_detach(tid); diff --git a/server2/server.cpp b/server2/server.cpp index f181cf0..ea86c4c 100644 --- a/server2/server.cpp +++ b/server2/server.cpp @@ -24,7 +24,7 @@ /* Configuration */ // Name of the pipe used to transfer logs to the log server -std::string LOG_FIFO_NAME = "./tmp/log"; +std::string LOG_FIFO_NAME = "./../tmp/log"; // Type of the server: server1 - last error & cursor position, server2 - mem % & vmem % std::string SERVER_TYPE = "server2"; @@ -53,6 +53,8 @@ int active_connections = 0; // Total amount of handled requests int handndled_requests = 0; +// Log pipe file descriptor +int is_log_ok = false; // Initializes ncurses lib void init_ncurses() { @@ -191,23 +193,24 @@ void log(std::string msg) { std::strftime(timestamp, 32, "%H:%M:%S ", &tstruct); last_log_messages[0] = timestamp + std::string(" ") + msg; - // Write message to the logging pipe TODO - // int fd = open(LOG_FIFO_NAME.c_str(), O_WRONLY); - // if(fd != -1) { - // write(fd, msg.c_str(), strlen(msg.c_str()) + 1); - // close(fd); - // } + // Write message to the logging pipe + int log_fd = open(LOG_FIFO_NAME.c_str(), O_WRONLY); + if(log_fd != -1) { + is_log_ok = true; + write(log_fd, msg.c_str(), msg.length()); + close(log_fd); + } else { + is_log_ok = false; + } } -// Opens file descriptor for logging pipe TODO -void init_log_pipe() { - // mkfifo(LOG_FIFO_NAME.c_str(), 0666); -} +// // Opens file descriptor for logging pipe TODO +// void init_log_pipe() { +// log_fd = open(LOG_FIFO_NAME.c_str(), O_WRONLY); +// } // Returns file descriptor for server socket int start_socket() { - log("Creating socket..."); - int server_socket = socket(AF_INET, SOCK_STREAM, 0); sockaddr_in server_address; server_address.sin_family = AF_INET; @@ -221,8 +224,6 @@ int start_socket() { return -1; } - log("Socket created"); - int status = listen(server_socket, MAX_CONNECTIONS); if (status != 0) { @@ -230,8 +231,6 @@ int start_socket() { return -1; } - log("Listening for connections..."); - return server_socket; } @@ -243,7 +242,7 @@ void* handle_connection(void* arg) { std::string info = ""; - log("New thread created for socket " + std::to_string(client_socket)); + log("New thread created for socket " + std::to_string(client_socket) + '\0'); while (true) { // Recieve message from client @@ -258,16 +257,15 @@ void* handle_connection(void* arg) { } if (bytes_recieved == -1) { - log("Error while recieving message: " + std::to_string(errno) + " - " + std::strerror(errno)); + log("Error while recieving message: " + std::to_string(errno) + " - " + std::strerror(errno) + '\0'); break; } - log("Got new message from client " + std::to_string(client_socket) + ": " + buffer); + log("Got new message from client " + std::to_string(client_socket) + '\0'); std::string new_info = get_sysinfo(); if (info != new_info) { - log("Client " + std::to_string(client_socket) + " info is not up to date"); info = new_info; time_t now = time(0); struct tm tstruct; @@ -276,10 +274,10 @@ void* handle_connection(void* arg) { std::strftime(timestamp, 32, "%Y/%m/%d %H:%M:%S ", &tstruct); std::string msg = timestamp + info; send(client_socket, msg.c_str(), strlen(msg.c_str()) + 1, 0); - log("Sent new info to client " + std::to_string(client_socket)); + log("Sent new info to client\0"); handndled_requests++; } else { - log("Actual info already on client " + std::to_string(client_socket)); + log("Actual info already on client\0"); } } @@ -302,6 +300,12 @@ void* draw(void* arg) { LOG_FIFO_NAME.c_str(), SERVER_TYPE.c_str(), PORT, MAX_CONNECTIONS, CMD_LOG_BUFFER, CMD_REFRESH_TIME ); + if (is_log_ok) { + printw("Log server connected\n\n"); + } else { + printw("Log server is not connected\n\n"); + } + // Print statistics printw("Active connections: %i\nHandled messages: %i\n", active_connections, handndled_requests); @@ -337,7 +341,6 @@ void* commands(void* arg) { int main() { init_ncurses(); - init_log_pipe(); // Create thread to redraw console output pthread_t tid; @@ -350,6 +353,8 @@ int main() { int server_socket = start_socket(); + log("Socket created\0"); + // Create thread to read commands pthread_t tcid; int* server_socket_copy = new int(server_socket); @@ -367,14 +372,12 @@ int main() { continue; } - log("New connection accepted"); active_connections++; // Create thread for each connection int* client_socket_copy = new int(client_socket); pthread_t tid; if (pthread_create(&tid, nullptr, &handle_connection, client_socket_copy) != 0) { - log("Failed to create thread for the connection"); delete client_socket_copy; } pthread_detach(tid);