home · contact · privacy
Re-wrote large parts of the server client architecture. No more fifo.
[plomrogue] / src / server / main.c
1 /* src/server/main.c */
2
3 #include <errno.h> /* global errno */
4 #include <stdio.h> /* printf(), fflush() */
5 #include <stdlib.h> /* exit() */
6 #include <string.h> /* strlen() */
7 #include <sys/stat.h> /* mkfifo(), mkdir() */
8 #include <sys/types.h> /* defines pid_t, time_t */
9 #include <time.h> /* #time() */
10 #include <unistd.h> /* access(), getpid(), unlink() */
11 #include "../common/err_try_fgets.h" /* set_err_try_fgets_delim() */
12 #include "../common/readwrite.h" /* try_fopen(), try_fwrite(), try_fclose() */
13 #include "../common/rexit.h" /* exit_err, set_cleanup_func() */
14 #include "../common/try_malloc.h" /* try_malloc() */
15 #include "cleanup.h" /* set_cleanup_flag(), cleanup() */
16 #include "init.h" /* run_game(), obey_argv() */
17 #include "map_object_actions.h" /* init_map_object_actions() */
18 #include "map_objects.h" /* init_map_object_defs() */
19 #include "run.h" /* obey_argv(), run_game() */
20 #include "world.h" /* struct World */
21
22
23
24 struct World world;
25
26
27
28 int main(int argc, char ** argv)
29 {
30     char * f_name = "main()";
31
32     /* So error exits also go through the server's cleanup() function. */
33     set_cleanup_func(cleanup);
34
35     /* Init settings from command line / hard-coded values. Print start info. */
36     obey_argv(argc, argv);
37     if (world.is_verbose)
38     {
39         char * printf_err = "Trouble in main() with printf().";
40         int test = printf("Starting plomrogue-server.\n");
41         exit_err(-1 == test, printf_err);
42         if (world.replay)
43         {
44             test = printf("Replay mode. Auto-replaying up to turn %d.\n",
45                          world.replay);
46             exit_err(-1 == test, printf_err);
47         }
48     }
49     world.path_map_obj_defs = "confserver/defs";
50     world.path_map_obj_acts = "confserver/map_object_actions";
51     world.path_worldstate   = "server/worldstate";
52     world.path_out          = "server/out";
53     world.path_in           = "server/in";
54     world.path_record       = "record";
55     world.tmp_suffix        = "_tmp";
56     set_err_try_fgets_delim("%%\n");
57
58     /* Set map geometry. */
59     world.map.size.x = 64;
60     world.map.size.y = 64;
61     world.map.dist_orthogonal = 5;
62     world.map.dist_diagonal   = 7;
63
64     /* Check existence of config files. */
65     char * err_mod = "No map object definitions file.";
66     char * err_moa = "No map object actions file.";
67     exit_err(access(world.path_map_obj_defs, F_OK), err_mod);
68     exit_err(access(world.path_map_obj_acts, F_OK), err_moa);
69
70     /* Init from config files map object (action) definitions. */
71     init_map_object_defs();
72     init_map_object_actions();
73
74     /* Create server directory if it does not exist yet. */
75     int test = mkdir("server", 0700);
76     exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
77
78     /* Create server out file and start it with server process test string. */
79     world.file_out = try_fopen(world.path_out, "w", f_name);
80     world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name);
81     sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
82     try_fwrite(world.server_test, strlen(world.server_test), 1,
83                world.file_out, f_name);
84     fflush(world.file_out);
85     set_cleanup_flag(CLEANUP_OUT);
86
87     /* Create server in file, switch to reading it. */
88     if (!access(world.path_in, F_OK))  /* This keeps out input from old input */
89     {                                  /* file streams of clients             */
90         unlink(world.path_in);         /* communicating with server processes */
91     }                                  /* superseded by this current one.     */
92     world.file_in = try_fopen(world.path_in, "w", f_name);
93     try_fclose(world.file_in, f_name);
94     world.file_in = try_fopen(world.path_in, "r", f_name);
95     set_cleanup_flag(CLEANUP_IN);
96
97     /* Enter play or replay mode loops, then leave properly. */
98     run_game();
99     cleanup();
100     exit(EXIT_SUCCESS);
101 }