Atlas - server.cpp
Home / usr / net / Pulse / src Lines: 2 | Size: 4494 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1#include <iostream> 2#include <fstream> 3#include <string> 4#include <sstream> 5#include <netinet/in.h> 6#include <unistd.h> 7#include <arpa/inet.h> 8#include <map> 9#include <string.h> 10 11#include "time.h" 12#include "global.h" 13std::map<std::string, std::string> last_machine_status; 14 15std::string outputDir = ""; 16bool separateLogs; 17int port; 18 19void log_to_file(const std::string& machine, const std::string& line, const std::string mode) { 20 if (separateLogs) { 21 std::string filename = outputDir + machine + ".csv"; 22 } 23 24 std::ofstream file(filename, std::ios::app); 25 if (!file.is_open()) { 26 std::cerr << "[PulseMSG] ERROR: Can't write to " << filename << std::endl; 27 return; 28 } 29 file << line << "\n"; 30} 31 32void process_log(const std::string& machine, std::stringstream& ss) { 33 std::string level, module, msg; 34 35 std::getline(ss, level, DELIMITER); 36 std::getline(ss, module, DELIMITER); 37 std::getline(ss, msg); 38 39 std::string timestamp = get_current_timestamp(); 40 41 std::string log_line = "LOG" + std::string(1, DELIMITER) + machine + std::string(1, DELIMITER) + 42 timestamp + std::string(1, DELIMITER) + level + std::string(1, DELIMITER) + 43 module + std::string(1, DELIMITER) + msg; 44 45 log_to_file(machine, log_line); 46 std::cout << "[PulseLog] Received LOG from " << machine << ": " << log_line << std::endl; 47} 48 49void process_ping(const std::string& machine, const int client_socket) { 50 std::string timestamp = get_current_timestamp(); 51 const std::string status = "OK"; 52 53 std::string log_line = "PING" + std::string(1, DELIMITER) + machine + std::string(1, DELIMITER) + 54 timestamp + std::string(1, DELIMITER) + status; 55 56 log_to_file(machine, log_line); 57 std::cout << "[PulsePing] Received PING from " << machine << ". Logged: " << log_line << std::endl; 58 59 const char* response = "PONG"; 60 send(client_socket, response, strlen(response), 0); 61} 62 63void process_status(const std::string& machine, std::stringstream& ss) { 64 std::string update_id, status_message; 65 66 std::getline(ss, update_id, DELIMITER); 67 std::getline(ss, status_message); 68 69 std::string current_status_key = update_id + DELIMITER + status_message; 70 71 if (last_machine_status.count(machine) && last_machine_status[machine] == current_status_key) { 72 std::cout << "[PulseStatus] Status from " << machine << " is unchanged. Ignoring." << std::endl; 73 return; 74 } 75 76 last_machine_status[machine] = current_status_key; 77 78 std::string timestamp = get_current_timestamp(); 79 80 std::string log_line = "STATUS" + std::string(1, DELIMITER) + machine + std::string(1, DELIMITER) + 81 timestamp + std::string(1, DELIMITER) + update_id + std::string(1, DELIMITER) + 82 status_message; 83 84 log_to_file(machine, log_line); 85 std::cout << "[PulseStatus] NEW Status received and recorded for " << machine << ": " << log_line << std::endl; 86} 87 88 89int main(int argc, char *argv[]) { 90 91 int server_fd, new_socket; 92 struct sockaddr_in address; 93 int opt = 1; 94 int addrlen = sizeof(address); 95 const int PORT = std::stoi(argv[1]); 96 97 server_fd = socket(AF_INET, SOCK_STREAM, 0); 98 setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)); 99 100 address.sin_family = AF_INET; 101 address.sin_addr.s_addr = INADDR_ANY; 102 address.sin_port = htons(PORT); 103 104 bind(server_fd, (struct sockaddr *)&address, sizeof(address)); 105 listen(server_fd, 3); 106 std::cout << "Pulse Server listening on port " << PORT << std::endl; 107 108 while (true) { 109 new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen); 110 111 char buffer[2048] = {0}; 112 int valread = read(new_socket, buffer, 2048); 113 114 if (valread > 0) { 115 std::string data(buffer); 116 std::stringstream ss(data); 117 std::string type, machine; 118 119 std::getline(ss, type, DELIMITER); 120 std::getline(ss, machine, DELIMITER); 121 122 if (type == "LOG") { 123 process_log(machine, ss); 124 } else if (type == "PING") { 125 process_ping(machine, new_socket); 126 } else if (type == "STATUS") { 127 process_status(machine, ss); 128 } else { 129 std::cerr << "[PulseMSG] WARNING: Unknown message type: " << type << std::endl; 130 } 131 } 132 close(new_socket); 133 } 134} 135[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.