Fix id bounds check for actuator + update graph code
[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 bool PathExists(const char *path) 
19 {
20         FILE *fp = fopen(path, "r");
21         if (fp) {
22                 fclose(fp);
23                 return true;
24         }
25         return false;
26 }
27
28 /**
29  * System control handler. This covers high-level control, including
30  * admin related functions and starting/stopping experiments..
31  * @param context The context to work in
32  * @param params The input parameters
33  */
34 void Control_Handler(FCGIContext *context, char *params) {
35         const char *action = "";
36         const char *name = "";
37         bool force = false;
38         ControlModes desired_mode;
39
40
41         // Login/auth now handled entirely in fastcgi.c and login.c
42         //TODO: Need to not have the ability for any user to stop someone else' experiment...
43         // (achieve by storing the username of the person running the current experiment, even when they log out?)
44         // (Our program should only realisitically support a single experiment at a time, so that should be sufficient)
45         FCGIValue values[3] = {
46                 {"action", &action, FCGI_REQUIRED(FCGI_STRING_T)},
47                 {"force", &force, FCGI_BOOL_T},
48                 {"name", &name, FCGI_STRING_T}
49         };
50
51         if (!FCGI_ParseRequest(context, params, values, 3))
52                 return;
53         
54         if (!strcmp(action, "emergency")) {
55                 desired_mode = CONTROL_EMERGENCY;
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         
69         void *arg = NULL;
70         if (desired_mode == CONTROL_START) {
71                 if (PathExists(name) && !force) {
72                         FCGI_RejectJSON(context, "An experiment with that name already exists.");
73                         return;
74                 }
75
76                 arg = (void*)name;
77         }
78
79         const char *ret;
80         if ((ret = Control_SetMode(desired_mode, arg)) != NULL) {
81                 FCGI_RejectJSON(context, ret);
82         } else {
83                 FCGI_BeginJSON(context, STATUS_OK);
84                 FCGI_JSONPair("description", "ok");
85                 FCGI_EndJSON();
86         }
87 }
88
89 /**
90  * Sets the mode to the desired mode, if possible.
91  * @param desired_mode The mode to be set to
92  * @param arg An argument specific to the mode to be set. 
93  * @return NULL on success, an error message on failure.
94  */
95 const char* Control_SetMode(ControlModes desired_mode, void * arg)
96 {
97         const char *ret = NULL;
98
99         pthread_mutex_lock(&(g_controls.mutex));
100         if (g_controls.current_mode == desired_mode)
101                 ret = "Already in the desired mode.";
102         else if (g_controls.current_mode == CONTROL_EMERGENCY && desired_mode != CONTROL_STOP)
103                 ret = "In emergency mode. You must stop before continuing.";
104         else switch (desired_mode) {
105                 case CONTROL_START:
106                         if (g_controls.current_mode == CONTROL_STOP) {
107                                 const char * name = arg;
108                                 if (!*name)
109                                         ret = "An experiment name must be specified";
110                                 else if (strpbrk(name, INVALID_CHARACTERS))
111                                         ret = "The experiment name must not contain: " INVALID_CHARACTERS_JSON;
112                                 else {
113                                         FILE *fp = fopen((const char*) arg, "a");
114                                         if (fp) {
115                                                 fclose(fp);
116                                                 gettimeofday(&(g_controls.start_time), NULL);
117                                         } else
118                                                 ret = "Cannot open experiment name marker";
119                                 }
120                         } else 
121                                 ret = "Cannot start when not in a stopped state.";
122                 break;
123                 case CONTROL_PAUSE:
124                         if (g_controls.current_mode != CONTROL_START)
125                                 ret = "Cannot pause when not in a running state.";
126                 break;
127                 case CONTROL_RESUME:
128                         if (g_controls.current_mode != CONTROL_PAUSE)
129                                 ret = "Cannot resume when not in a paused state.";
130                 break;
131                 case CONTROL_EMERGENCY:
132                         if (g_controls.current_mode != CONTROL_START) //pfft
133                                 ret = "Not running so how can there be an emergency.";
134                 break;
135                 default:
136                 break;
137         }
138         
139         if (ret == NULL) {
140                 Actuator_SetModeAll(desired_mode, arg);
141                 Sensor_SetModeAll(desired_mode, arg);
142                 if (desired_mode != CONTROL_RESUME)
143                         g_controls.current_mode = desired_mode;
144                 else
145                         g_controls.current_mode = CONTROL_START;
146         }
147         pthread_mutex_unlock(&(g_controls.mutex));
148         return ret;
149 }
150
151 /**
152  * Gets a string representation of the current mode
153  * @param mode The mode to get a string representation of
154  * @return The string representation of the mode
155  */
156 const char * Control_GetModeName() {
157         const char * ret = "Unknown";
158
159         switch (g_controls.current_mode) {
160                 case CONTROL_START: ret = "Running"; break;
161                 case CONTROL_PAUSE: ret = "Paused"; break;
162                 case CONTROL_RESUME: ret = "Resumed"; break;
163                 case CONTROL_STOP: ret = "Stopped"; break;
164                 case CONTROL_EMERGENCY: ret = "Emergency mode"; break;
165         }
166         return ret;
167 }
168
169 /**
170  * Gets the start time for the current experiment
171  * @return the start time
172  */
173 const struct timeval* Control_GetStartTime() {
174         return &g_controls.start_time;
175 }

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