// Handle server initialization void server_run(char *address, uint32_t port) { struct sockaddr_in addr, inc_addr; struct timeval tv = { .tv_sec = TIMEO_SEC, .tv_usec = 0 }; int client, optval = 1, *m_client; pthread_t thread; socklen_t sockaddr_len = sizeof(struct sockaddr); if ((server_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR) { err_exit("[SERVER] Error creating socket\n"); } setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int)); initialize_sockaddr(&addr, address, port); if (bind(server_socket, (struct sockaddr *)&addr, sockaddr_len) == SOCKET_ERROR) { err_exit("[SERVER] Error binding address to socket\n"); } if (listen(server_socket, LISTEN_BACKLOG) == SOCKET_ERROR) { err_exit("[SERVER] Error creating passive socket\n"); } log_msg(LOG_OK, "[SERVER] Server initialized, listening on %s:%i\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); while (TRUE) { if ((client = accept(server_socket, (struct sockaddr *)&inc_addr, &sockaddr_len)) == SOCKET_ERROR) { log_msg(LOG_ERROR, "[SERVER] Error accepting a new connection\n"); continue; } // Update statistics stats_add_new_connection(); log_msg(LOG_OK, "[SERVER] Accepting new connection from %s:%i, assigning id: %i\n", inet_ntoa(inc_addr.sin_addr), ntohs(inc_addr.sin_port), client); // Set timeout on client's socket setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)); // Alloc params for new client m_client = malloc(sizeof(int)); *m_client = client; // Start new thread for client pthread_create(&thread, NULL, handle_client, (void *)m_client); pthread_detach(thread); } }
BankConfig get_bank_config(Node *conf) { BankConfig config; apply_config_i(&config.bank_port, conf, BANK_SECTION, "port", DEFAULT_BANK_PORT); apply_config_s(&config.bank_health_check, conf, BANK_SECTION, "health-check", CONF_FALSE); apply_config_i(&config.bank_max_msg_length, conf, BANK_SECTION, "max-message-length", DEFAULT_BANK_MAX_MSG_LENGTH); apply_config_i(&config.bank_queue_limit, conf, BANK_SECTION, "queue-limit", DEFAULT_BANK_QUEUE_LIMIT); apply_config_d(&config.bank_send_interval, conf, BANK_SECTION, "send-interval", DEFAULT_BANK_SEND_INTERVAL); apply_config_s(&config.bank_pid_file, conf, BANK_SECTION, "pid-file", DEFAULT_BANK_PID_FILE); apply_config_s(&config.logging_error_log, conf, LOGGING_SECTION, "error-log", DEFAULT_LOGGING_ERROR_LOG); apply_config_log(&config.logging_log_level, conf, LOGGING_SECTION, "log-level", DEFAULT_LOGGING_LEVEL); apply_config_ss(&config.destiny_hosts, &config.destiny_host_count, ",", conf, DESTINY_SECTION, "hosts", ""); apply_config_s(&config.destiny_health_check, conf, DESTINY_SECTION, "health-check", CONF_FALSE); apply_config_s(&config.destiny_health_check_msg, conf, DESTINY_SECTION, "health-check-msg", DEFAULT_HEALTH_CHECK_MSG); apply_config_d(&config.destiny_health_check_interval, conf, DESTINY_SECTION, "health-check-interval", DEFAULT_DESTINY_HEALTH_CHECK_INTERVAL); apply_config_s(&config.destiny_consistent_hashing, conf, DESTINY_SECTION, "consistent-hashing", CONF_FALSE); apply_config_i(&config.downstream_consistent_hash_replica, conf, DESTINY_SECTION, "consistent-hashing-replica", DEFAULT_DESTINY_CONSISTENT_HASHING_REPLICA); config.downstream_sockaddr = malloc( sizeof(struct sockaddr_in*) * config.destiny_host_count); if (initialize_sockaddr(config.destiny_host_count, config.destiny_hosts, config.downstream_sockaddr) != 0) { log_critical("No downstream sockaddr successfully initialized."); exit(1); } config.sockaddr_len = sizeof(*(config.downstream_sockaddr[0])); if (strcmp(config.destiny_health_check, CONF_TRUE) == 0) { config._destiny_health_check = 1; config.downstream_error_count = malloc( sizeof(int) * config.destiny_host_count); memset(config.downstream_error_count, (char )0, sizeof(int) * config.destiny_host_count); if (strcmp(config.destiny_consistent_hashing, CONF_TRUE) == 0) { config._downstream_consistent_hashing = 1; generate_hash_ring(&(config.downstream_hash_ring), config.destiny_host_count, config.downstream_consistent_hash_replica); config.downstream_hash_ring_length = config.destiny_host_count * config.downstream_consistent_hash_replica; } else { config._downstream_consistent_hashing = 0; config.downstream_hash_ring = NULL; config.downstream_hash_ring_length = 0; } } else { config._destiny_health_check = 0; config.downstream_error_count = NULL; config._downstream_consistent_hashing = 0; config.downstream_hash_ring = NULL; config.downstream_hash_ring_length = 0; } return config; }