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

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