Merge branch 'master' of https://github.com/szmoore/MCTX3420.git
[matches/MCTX3420.git] / server / control.c
1 /**
2  * @file control.c
3  * @brief Handles all client control requests (admin related)
4  */
5 #include "common.h"
6 #include "control.h"
7 #include "sensor.h"
8 #include "actuator.h"
9
10 typedef struct ControlData {
11         ControlModes current_mode;
12         pthread_mutex_t mutex;
13         struct timeval start_time;
14 } ControlData;
15
16 ControlData g_controls = {CONTROL_STOP, PTHREAD_MUTEX_INITIALIZER, {0}};
17
18 static bool PathExists(const char *path) {
19         FILE *fp = fopen(path, "r");
20         if (fp) {
21                 fclose(fp);
22                 return true;
23         }
24         return false;
25 }
26
27 /**
28  * System control handler. This covers high-level control, including
29  * admin related functions and starting/stopping experiments..
30  * @param context The context to work in
31  * @param params The input parameters
32  */
33 void Control_Handler(FCGIContext *context, char *params) {
34         const char *action, *key = "", *name = "";
35         bool force = false;
36         ControlModes desired_mode;
37
38         FCGIValue values[4] = {
39                 {"action", &action, FCGI_REQUIRED(FCGI_STRING_T)},
40                 {"key", &key, FCGI_STRING_T},
41                 {"force", &force, FCGI_BOOL_T},
42                 {"name", &name, FCGI_STRING_T}
43         };
44
45         if (!FCGI_ParseRequest(context, params, values, 4))
46                 return;
47         
48         if (!strcmp(action, "lock")) {
49                 FCGI_LockControl(context, force);
50                 return;
51         } else if (!strcmp(action, "emergency")) {
52                 desired_mode = CONTROL_EMERGENCY;
53         } else if (FCGI_HasControl(context, key)) {
54                 if (!strcmp(action, "release")) {
55                         FCGI_ReleaseControl(context);
56                 } else if (!strcmp(action, "start")) {
57                         desired_mode = CONTROL_START;
58                 } else if (!strcmp(action, "pause")) {
59                         desired_mode = CONTROL_PAUSE;
60                 } else if (!strcmp(action, "resume")) {
61                         desired_mode = CONTROL_RESUME;
62                 } else if (!strcmp(action, "stop")) {
63                         desired_mode = CONTROL_STOP;
64                 } else {
65                         FCGI_RejectJSON(context, "Unknown action specified.");
66                         return;
67                 }
68         } else {
69                 FCGI_RejectJSONEx(context, STATUS_UNAUTHORIZED, 
70                         "Invalid control key specified.");
71                 return;
72         }
73
74         void *arg = NULL;
75         if (desired_mode == CONTROL_START) {
76                 if (PathExists(name) && !force) {
77                         FCGI_RejectJSON(context, "An experiment with that name already exists.");
78                         return;
79                 }
80
81                 arg = (void*)name;
82         }
83
84         const char *ret;
85         if ((ret = Control_SetMode(desired_mode, arg)) != NULL) {
86                 FCGI_RejectJSON(context, ret);
87         } else {
88                 FCGI_BeginJSON(context, STATUS_OK);
89                 FCGI_JSONPair("description", "ok");
90                 FCGI_EndJSON();
91         }
92 }
93
94 /**
95  * Sets the mode to the desired mode, if possible.
96  * @param desired_mode The mode to be set to
97  * @param arg An argument specific to the mode to be set. 
98  * @return NULL on success, an error message on failure.
99  */
100 const char* Control_SetMode(ControlModes desired_mode, void * arg)
101 {
102         const char *ret = NULL;
103
104         pthread_mutex_lock(&(g_controls.mutex));
105         if (g_controls.current_mode == desired_mode)
106                 ret = "Already in the desired mode.";
107         else if (g_controls.current_mode == CONTROL_EMERGENCY && desired_mode != CONTROL_STOP)
108                 ret = "In emergency mode. You must stop before continuing.";
109         else switch (desired_mode) {
110                 case CONTROL_START:
111                         if (g_controls.current_mode == CONTROL_STOP) {
112                                 const char * name = arg;
113                                 if (!*name)
114                                         ret = "An experiment name must be specified";
115                                 else if (strpbrk(name, INVALID_CHARACTERS))
116                                         ret = "The experiment name must not contain: " INVALID_CHARACTERS_JSON;
117                                 else {
118                                         FILE *fp = fopen((const char*) arg, "a");
119                                         if (fp) {
120                                                 fclose(fp);
121                                                 gettimeofday(&(g_controls.start_time), NULL);
122                                         } else
123                                                 ret = "Cannot open experiment name marker";
124                                 }
125                         } else 
126                                 ret = "Cannot start when not in a stopped state.";
127                 break;
128                 case CONTROL_PAUSE:
129                         if (g_controls.current_mode != CONTROL_START)
130                                 ret = "Cannot pause when not in a running state.";
131                 break;
132                 case CONTROL_RESUME:
133                         if (g_controls.current_mode != CONTROL_PAUSE)
134                                 ret = "Cannot resume when not in a paused state.";
135                 break;
136                 case CONTROL_EMERGENCY:
137                         if (g_controls.current_mode != CONTROL_START) //pfft
138                                 ret = "Not running so how can there be an emergency.";
139                 break;
140                 default:
141                 break;
142         }
143         
144         if (ret == NULL) {
145                 Actuator_SetModeAll(desired_mode, arg);
146                 Sensor_SetModeAll(desired_mode, arg);
147                 if (desired_mode != CONTROL_RESUME)
148                         g_controls.current_mode = desired_mode;
149                 else
150                         g_controls.current_mode = CONTROL_START;
151         }
152         pthread_mutex_unlock(&(g_controls.mutex));
153         return ret;
154 }
155
156 /**
157  * Gets a string representation of the current mode
158  * @param mode The mode to get a string representation of
159  * @return The string representation of the mode
160  */
161 const char * Control_GetModeName() {
162         const char * ret = "Unknown";
163
164         switch (g_controls.current_mode) {
165                 case CONTROL_START: ret = "Running"; break;
166                 case CONTROL_PAUSE: ret = "Paused"; break;
167                 case CONTROL_RESUME: ret = "Resumed"; break;
168                 case CONTROL_STOP: ret = "Stopped"; break;
169                 case CONTROL_EMERGENCY: ret = "Emergency mode"; break;
170         }
171         return ret;
172 }
173
174 /**
175  * Gets the start time for the current experiment
176  * @return the start time
177  */
178 const struct timeval* Control_GetStartTime() {
179         return &g_controls.start_time;
180 }

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