From: Christian Heller Date: Sun, 3 Aug 2014 01:56:09 +0000 (+0200) Subject: Server: Only write record and save file if 15 seconds have passed. X-Git-Tag: tce~678 X-Git-Url: https://plomlompom.com/repos//%22https:/validator.w3.org/check?a=commitdiff_plain;h=c31d3a8d3f993a2cfcc8c5e438283e3a693d5b27;p=plomrogue Server: Only write record and save file if 15 seconds have passed. --- diff --git a/TODO b/TODO index c8b6681..e79d141 100644 --- a/TODO +++ b/TODO @@ -4,10 +4,6 @@ IN GENERAL: - expand use of hardcoded_strings module(s) -SERVER: - -- check whether saving every player turn slows down gameplay - BOTH SERVER/CLIENT: - make server and client communicate by specific world state info requests diff --git a/src/common/readwrite.c b/src/common/readwrite.c index f05c128..ac7b56a 100644 --- a/src/common/readwrite.c +++ b/src/common/readwrite.c @@ -14,23 +14,6 @@ -/* Return "path" + suffix "_tmp". Value is malloc'd, must be free externally. */ -static char * build_temp_path(char * path); - - - -static char * build_temp_path(char * path) -{ - char * suffix_tmp = "_tmp"; - uint16_t size = strlen(path) + strlen(suffix_tmp) + 1; - char * path_tmp = try_malloc(size, __func__); - int test = sprintf(path_tmp, "%s%s", path, suffix_tmp); - exit_trouble(test < 0, __func__, "sprintf"); - return path_tmp; -} - - - extern FILE * try_fopen(char * path, char * mode, const char * f) { char * msg1 = "Trouble in "; @@ -91,6 +74,18 @@ extern char * try_fgets(char * line, int linemax, FILE * file, const char * f) +extern char * build_temp_path(char * path) +{ + char * suffix_tmp = "_tmp"; + uint16_t size = strlen(path) + strlen(suffix_tmp) + 1; + char * path_tmp = try_malloc(size, __func__); + int test = sprintf(path_tmp, "%s%s", path, suffix_tmp); + exit_trouble(test < 0, __func__, "sprintf"); + return path_tmp; +} + + + extern FILE * atomic_write_start(char * path, char ** path_tmp) { *path_tmp = build_temp_path(path); diff --git a/src/common/readwrite.h b/src/common/readwrite.h index cdbd386..b5b2f1c 100644 --- a/src/common/readwrite.h +++ b/src/common/readwrite.h @@ -29,6 +29,9 @@ extern void try_fputc(uint8_t c, FILE * file, const char * f); extern int try_fgetc(FILE * file, const char * f); extern char * try_fgets(char * line, int size, FILE * file, const char * f); +/* Return "path" + suffix "_tmp". Value is malloc'd, must be freed externally.*/ +extern char * build_temp_path(char * path); + /* Write to "path_tmp" "path" + "_tmp" and return a new file at that "path_tmp" * open for writing. "path_tmp" is malloc()'d, must be freed externally. */ diff --git a/src/server/init.c b/src/server/init.c index 42ec699..49ba22a 100644 --- a/src/server/init.c +++ b/src/server/init.c @@ -26,7 +26,7 @@ #include "things.h" /* Thing, ThingType, free_things(), add_things(), * get_thing_id_action_id_by_name() */ -#include "run.h" /* obey_msg(), io_loop() */ +#include "run.h" /* obey_msg(), io_loop(), record() */ #include "world.h" /* global world */ @@ -257,4 +257,5 @@ extern void run_game() } err_line_zero(); io_loop(); + record(NULL, 1); } diff --git a/src/server/run.c b/src/server/run.c index ecfae2b..3ae7175 100644 --- a/src/server/run.c +++ b/src/server/run.c @@ -7,12 +7,14 @@ #include /* FILE, printf(), fflush() */ #include /* free() */ #include /* strlen(), strcmp(), strncmp(), strdup() */ +#include /* time_t, time() */ #include /* access() */ #include "../common/parse_file.h" /* set_err_line_options(), token_from_line(), * err_line(), err_line_inc(), parse_val() */ #include "../common/readwrite.h" /* try_fopen(), try_fcose(), try_fwrite(), - * try_fgets(), textfile_width(), try_fputc() + * try_fgets(), textfile_width(), try_fputc(), + * atomic_write_finish(), build_temp_path() */ #include "../common/rexit.h" /* exit_trouble(), exit_err() */ #include "../common/try_malloc.h" /* try_malloc() */ @@ -56,7 +58,6 @@ static void turn_over(); - static uint8_t set_char_by_string_comparison(char * string, char * comparand, char * c_to_set, char value) { @@ -221,25 +222,40 @@ static void turn_over() -static void record_msg(char * msg) +extern void record(char * msg, uint8_t force) { - char * path_tmp; - FILE * file_tmp = atomic_write_start(s[S_PATH_RECORD], &path_tmp); - if (!access(s[S_PATH_RECORD], F_OK)) + static FILE * file_tmp = NULL; + static time_t save_wait = 0; + static char * path_tmp; + if (!file_tmp) { - FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", __func__); - uint32_t linemax = textfile_width(file_read); - char * line = try_malloc(linemax + 1, __func__); - while (try_fgets(line, linemax + 1, file_read, __func__)) + path_tmp = build_temp_path(s[S_PATH_RECORD]); + file_tmp = try_fopen(path_tmp, "w", __func__); + if (!access(s[S_PATH_RECORD], F_OK)) { - try_fwrite(line, strlen(line), 1, file_tmp, __func__); + FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", __func__); + uint32_t linemax = textfile_width(file_read); + char * line = try_malloc(linemax + 1, __func__); + while (try_fgets(line, linemax + 1, file_read, __func__)) + { + try_fwrite(line, strlen(line), 1, file_tmp, __func__); + } + free(line); + try_fclose(file_read, __func__); } - free(line); - try_fclose(file_read, __func__); } - try_fwrite(msg, strlen(msg), 1, file_tmp, __func__); - try_fputc('\n', file_tmp, __func__); - atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp); + if (msg) + { + try_fwrite(msg, strlen(msg), 1, file_tmp, __func__); + try_fputc('\n', file_tmp, __func__); + } + if (force || time(NULL) > save_wait + 15) + { + save_wait = time(NULL); + save_world(); + atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp); + file_tmp = NULL; + } } @@ -264,8 +280,7 @@ extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose) } if (do_record) { - save_world(); - record_msg(msg); + record(msg, 0); } char * tokplus = token_from_line(NULL); err_line(NULL != tokplus, "Too many arguments, ignoring overflow."); @@ -283,8 +298,8 @@ extern uint8_t io_loop() { while (1) { - server_test(); char * msg = io_round(); + server_test(); if (NULL == msg) { continue; diff --git a/src/server/run.h b/src/server/run.h index 400f079..6b823c4 100644 --- a/src/server/run.h +++ b/src/server/run.h @@ -10,9 +10,16 @@ -/* Try parsing "msg" into a command to apply, and apply it. Record commands to - * the file at world.path_record if "do_record" is set, and output them to - * stdout if "do_verbose" and world.is_verbose are set. +/* Record save and record file data. Both are only written if "force" is set, or + * on the first run with unset "force", or if 15 seconds have passed since the + * last file writing. "msg" is appended to the record file if it is set. + */ +extern void record(char * msg, uint8_t force); + +/* Try parsing "msg" into a command to apply, and apply it. Output commands to + * stdout if "do_verbose" and world.is_verbose are set. If "do_record" is set, + * record commands to record file, and run save_world() if the last call to it + * via this function has not happened yet or is at least one minute in the past. */ extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose);