diff options
author | David Härdeman <david@hardeman.nu> | 2020-06-20 11:23:11 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2020-06-20 11:23:11 +0200 |
commit | fc25e880dfb1f804742006bcdd15ac70d18b4144 (patch) | |
tree | 0119f355c85db79c63844841ac01966d1cab0ce7 | |
parent | c405a7d93341d2c13167e8ab834c68f7b035a8bb (diff) |
Add some basic server states
-rw-r--r-- | idle.c | 3 | ||||
-rw-r--r-- | server.c | 47 | ||||
-rw-r--r-- | server.h | 10 |
3 files changed, 50 insertions, 10 deletions
@@ -252,6 +252,8 @@ idle_check_handshake_reply(struct cfg *cfg, struct uring_task *task, int res) if (player_count < 0) goto out; + idle->server->state = SERVER_STATE_RUNNING; + debug(DBG_IDLE, "%s: currently %i active players\n", idle->server->name, player_count); @@ -301,6 +303,7 @@ idle_check_connected_cb(struct cfg *cfg, struct connection *conn, bool connected "idle check connection to remote server (%s) failed\n", idle->server->name); idle->server->idle_count = 0; + idle->server->state = SERVER_STATE_STOPPED; return; } @@ -291,6 +291,7 @@ server_exec_free(struct uring_task *task) #define P_PIDFD 3 #endif +/* FIXME: update states */ static void server_exec_done(struct cfg *cfg, struct uring_task *task, int res) { @@ -363,6 +364,24 @@ server_exec(struct cfg *cfg, struct server *scfg, const char *cmd) return true; } +static bool +server_check_running(struct cfg *cfg, struct server *scfg) +{ + /* FIXME: other methods, rcon? */ + if (scfg->systemd_service) { + verbose("%s: checking if systemd service is running\n", scfg->name); + if (systemd_service_running(cfg, scfg)) { + scfg->state = SERVER_STATE_RUNNING; + return true; + } else { + scfg->state = SERVER_STATE_STOPPED; + return false; + } + } + + return false; +} + bool server_start(struct cfg *cfg, struct server *scfg) { @@ -381,7 +400,12 @@ server_start(struct cfg *cfg, struct server *scfg) case SERVER_START_METHOD_SYSTEMD: verbose("Starting server %s via systemd (%s)\n", scfg->name, scfg->systemd_service); - return systemd_service_start(cfg, scfg); + + if (systemd_service_start(cfg, scfg)) { + scfg->state = SERVER_STATE_RUNNING; + return true; + } else + return server_check_running(cfg, scfg); case SERVER_START_METHOD_UNDEFINED: default: @@ -409,7 +433,11 @@ server_stop(struct cfg *cfg, struct server *scfg) case SERVER_STOP_METHOD_SYSTEMD: verbose("Stopping server %s via systemd (%s)\n", scfg->name, scfg->systemd_service); - return systemd_service_stop(cfg, scfg); + if (systemd_service_stop(cfg, scfg)) { + scfg->state = SERVER_STATE_STOPPED; + return true; + } else + return server_check_running(cfg, scfg); case SERVER_STOP_METHOD_RCON: verbose("Stopping server %s via rcon\n", scfg->name); @@ -438,6 +466,11 @@ server_commit(struct cfg *cfg, struct server *scfg) if (scfg->task.dead) return false; + if (scfg->state != SERVER_STATE_INIT) { + error("called in wrong state\n"); + return false; + } + if (!list_empty(&scfg->proxys)) { error("%s: proxys not empty?\n", scfg->name); return false; @@ -448,8 +481,6 @@ server_commit(struct cfg *cfg, struct server *scfg) return true; } - /* FIXME: running? */ - if (scfg->stop_method == SERVER_STOP_METHOD_RCON && list_empty(&scfg->rcons)) { error("%s: rcon stop method missing rcon address\n", @@ -580,11 +611,9 @@ server_commit(struct cfg *cfg, struct server *scfg) } idle_init(cfg, scfg); + scfg->state = SERVER_STATE_CFG_OK; - if (scfg->systemd_service) { - verbose("%s: checking if systemd service is running\n", scfg->name); - systemd_service_running(cfg, scfg); - } + server_check_running(cfg, scfg); debug(DBG_SRV, "success\n"); return true; @@ -781,10 +810,10 @@ server_new(struct cfg *cfg, const char *name) return NULL; } + scfg->state = SERVER_STATE_INIT; scfg->cfg = cfg; scfg->type = SERVER_TYPE_UNDEFINED; scfg->name = xstrdup(name); - scfg->running = false; scfg->stop_method = SERVER_STOP_METHOD_UNDEFINED; scfg->start_method = SERVER_START_METHOD_UNDEFINED; uring_task_init(&scfg->task, "scfg", uring_parent(cfg), server_free); @@ -1,6 +1,14 @@ #ifndef fooserverhfoo #define fooserverhfoo +enum server_state { + SERVER_STATE_INIT, + SERVER_STATE_CFG_OK, + SERVER_STATE_RUNNING, + SERVER_STATE_STOPPED, + SERVER_STATE_DEAD, +}; + enum server_type { SERVER_TYPE_UNDEFINED, SERVER_TYPE_ANNOUNCE, @@ -30,7 +38,7 @@ struct server { struct list_head proxys; struct list_head rcons; struct list_head dnslookups; - bool running; + enum server_state state; struct cfg *cfg; enum server_stop_method stop_method; |