//服务器端执行入口函数 int main(int argc, char*argv[]){ struct sigaction new_action, old_action; message_data message_request; new_action.sa_handler=catch_signals; sigemptyset(&new_action.sa_mask); new_action.sa_flags=0; if ((sigaction(SIGINT, &new_action, &old_action) != 0) || (sigaction(SIGHUP, &new_action, &old_action) != 0) || (sigaction(SIGTERM, &new_action, &old_action) != 0)) { fprintf(stderr, "Server startup error, signal catching failed\n"); exit(EXIT_FAILURE); } char* server_host="localhost"; char* sql_username="******"; char* sql_password="******"; char* db_name="railwaysystem"; if(!database_init(server_host, sql_username, sql_password, db_name)){ fprintf(stderr, "Server error: could not init database. \n"); sys_log("数据库连接失败."); exit(EXIT_FAILURE); } sys_log("数据库连接成功."); if(!server_start()){ sys_log("服务器启动失败."); exit(EXIT_FAILURE); } sys_log("服务器启动成功."); while(server_state){ if(read_request_from_client(&message_request)){ exec_request(message_request); }else{ if(server_state){ fprintf(stderr, "Server ended, cannot read request from message queue. \n"); sys_log("Server ended, cannot read request from message queue."); } server_state=0; } } database_close(); server_end(); sys_log("服务器关闭."); exit(EXIT_SUCCESS); }
/* The main function loops over reading from the server mqueue (which is * actually done in the read_request_from_client function). Each time it * gets a request, it hands the request to process_command, which handles * - figuring out what operation the client wanted (the same switch * statement that is in main of app_ui.c, in effect) * - delegating to some frunction from cd_dbm.c * - coppying some of the data from the message_db_t struct coming in from * the client to the response, and then filling out other data depending * on the action and the database results * - sending the response message_db_t struct back to the client * * The functions that interact directly with the mqueue all live in mqueue_imp.c; * this module provides the main loop and the "glue" between the server-side * mqueue handling and the server-side data api. * * Note that the actual calls to the server-side data api from here wind up * being identical to the actual calls to the client-side data api (the proxy) * made from app_ui.c. */ int main(int argc, char *argv[]) { struct sigaction new_action, old_action; message_db_t mess_command; int database_init_type = 0; new_action.sa_handler = catch_signals; sigemptyset(&new_action.sa_mask); new_action.sa_flags = 0; if ((sigaction(SIGINT, &new_action, &old_action) != 0) || (sigaction(SIGHUP, &new_action, &old_action) != 0) || (sigaction(SIGTERM, &new_action, &old_action) != 0)) { fprintf(stderr, "Server startup error, signal catching failed\n"); exit(EXIT_FAILURE); } if (argc > 1) { argv++; if (strncmp("-i", *argv, 2) == 0) database_init_type = 1; } if (!database_initialize(database_init_type)) { fprintf(stderr, "Server error: could not initialize database\n"); exit(EXIT_FAILURE); } if (!server_starting()) exit(EXIT_FAILURE); while(server_running) { if (read_request_from_client(&mess_command)) { process_command(mess_command); } else { if(server_running) fprintf(stderr, "Server ended - can not \ read mqueue\n"); server_running = 0; } } /* while */ server_ending(); exit(EXIT_SUCCESS); }