Minor mods to control code
[matches/MCTX3420.git] / server / main.c
1 /**
2  * @file main.c
3  * @brief main and its helper functions, signal handling and cleanup functions
4  */
5
6 // --- Custom headers --- //
7 #include "common.h"
8 #include "options.h"
9 #include "sensor.h"
10 #include "actuator.h"
11 #include "control.h"
12
13 // --- Standard headers --- //
14 #include <signal.h> // for signal handling
15
16 // --- Variable definitions --- //
17 Options g_options; // options passed to program through command line arguments
18
19 // --- Function definitions --- //
20
21 /**
22  * Parse command line arguments, initialise g_options
23  * @param argc - Number of arguments
24  * @param argv - Array of argument strings
25  */
26 void ParseArguments(int argc, char ** argv)
27 {
28         g_options.program = argv[0]; // program name
29         g_options.verbosity = LOGDEBUG; // default log level
30         gettimeofday(&(g_options.start_time), NULL); // Start time
31         Log(LOGDEBUG, "Called as %s with %d arguments.", g_options.program, argc);
32 }
33
34 /**
35  * Handle a signal
36  * @param signal - The signal number
37  */
38 //TODO: Something that gets massively annoying with threads is that you can't predict which one gets the signal
39 // There are ways to deal with this, but I can't remember them
40 // Probably sufficient to just call Thread_QuitProgram here
41 void SignalHandler(int signal)
42 {
43         // At the moment just always exit.
44         // Call `exit` so that Cleanup will be called to... clean up.
45         Log(LOGWARN, "Got signal %d (%s). Exiting.", signal, strsignal(signal));
46
47         //exit(signal);
48 }
49
50 /**
51  * Cleanup before the program exits
52  */
53 void Cleanup()
54 {
55         Log(LOGDEBUG, "Begin cleanup.");
56         Log(LOGDEBUG, "Finish cleanup.");
57
58 }
59
60 /**
61  * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
62  * @param argc - Num args
63  * @param argv - Args
64  * @returns 0 on success, error code on failure
65  * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
66  */
67 int main(int argc, char ** argv)
68 {
69         ParseArguments(argc, argv);
70
71         // signal handler
72         //TODO: Make this work
73         /*
74         int signals[] = {SIGINT, SIGSEGV, SIGTERM};
75         for (int i = 0; i < sizeof(signals)/sizeof(int); ++i)
76         {
77                 signal(signals[i], SignalHandler);
78         }
79         */
80         Sensor_Init();
81         Actuator_Init();
82         //Sensor_StartAll("test");
83         //Actuator_StartAll("test");
84         const char *ret;
85         if ((ret = Control_SetMode(CONTROL_START, "test")) != NULL)
86                 Fatal("Control_SetMode failed with '%s'", ret);
87
88         // run request thread in the main thread
89         FCGI_RequestLoop(NULL);
90
91         if ((ret = Control_SetMode(CONTROL_STOP, "test")) != NULL)
92                 Fatal("Control_SetMode failed with '%s'", ret);
93         //Sensor_StopAll();
94         //Actuator_StopAll();
95
96         Cleanup();
97         return 0;
98 }
99
100

UCC git Repository :: git.ucc.asn.au