Refactor Actuators
[matches/MCTX3420.git] / server / control.c
index 2c0fd79..820c37c 100644 (file)
@@ -15,7 +15,8 @@ typedef struct ControlData {
 
 ControlData g_controls = {CONTROL_STOP, PTHREAD_MUTEX_INITIALIZER, {0}};
 
-static bool PathExists(const char *path) {
+bool PathExists(const char *path) 
+{
        FILE *fp = fopen(path, "r");
        if (fp) {
                fclose(fp);
@@ -31,13 +32,19 @@ static bool PathExists(const char *path) {
  * @param params The input parameters
  */
 void Control_Handler(FCGIContext *context, char *params) {
-       const char *action, *key = "", *name = "";
+       const char *action = "";
+       const char *name = "";
        bool force = false;
        ControlModes desired_mode;
 
+
+
+       // Login/auth now handled entirely in fastcgi.c and login.c
+       //TODO: Need to not have the ability for any user to stop someone else' experiment...
+       // (achieve by storing the username of the person running the current experiment, even when they log out?)
+       // (Our program should only realisitically support a single experiment at a time, so that should be sufficient)
        FCGIValue values[4] = {
                {"action", &action, FCGI_REQUIRED(FCGI_STRING_T)},
-               {"key", &key, FCGI_STRING_T},
                {"force", &force, FCGI_BOOL_T},
                {"name", &name, FCGI_STRING_T}
        };
@@ -50,39 +57,25 @@ void Control_Handler(FCGIContext *context, char *params) {
                return;
        } else if (!strcmp(action, "emergency")) {
                desired_mode = CONTROL_EMERGENCY;
-       } else if (!strcmp(action, "query")) {
-               FCGI_BeginJSON(context, STATUS_OK);
-               FCGI_JSONPair("state", Control_GetModeName(Control_GetMode()));
-               FCGI_EndJSON();
-               return;
-       } else if (FCGI_HasControl(context, key)) {
-               if (!strcmp(action, "release")) {
-                       FCGI_ReleaseControl(context);
-               } else if (!strcmp(action, "start")) {
-                       desired_mode = CONTROL_START;
-               } else if (!strcmp(action, "pause")) {
-                       desired_mode = CONTROL_PAUSE;
-               } else if (!strcmp(action, "resume")) {
-                       desired_mode = CONTROL_RESUME;
-               } else if (!strcmp(action, "stop")) {
-                       desired_mode = CONTROL_STOP;
-               } else {
-                       FCGI_RejectJSON(context, "Unknown action specified.");
-                       return;
-               }
+       }
+       else if (!strcmp(action, "release")) {
+               FCGI_ReleaseControl(context);
+       } else if (!strcmp(action, "start")) {
+               desired_mode = CONTROL_START;
+       } else if (!strcmp(action, "pause")) {
+               desired_mode = CONTROL_PAUSE;
+       } else if (!strcmp(action, "resume")) {
+               desired_mode = CONTROL_RESUME;
+       } else if (!strcmp(action, "stop")) {
+               desired_mode = CONTROL_STOP; 
        } else {
-               FCGI_RejectJSONEx(context, STATUS_UNAUTHORIZED, 
-                       "Invalid control key specified.");
+               FCGI_RejectJSON(context, "Unknown action specified.");
                return;
        }
-
+       
        void *arg = NULL;
        if (desired_mode == CONTROL_START) {
-               int len = strlen(name);
-               if (len <= 0) {
-                       FCGI_RejectJSON(context, "An experiment name must be specified.");
-                       return;
-               } else if (PathExists(name) && !force) {
+               if (PathExists(name) && !force) {
                        FCGI_RejectJSON(context, "An experiment with that name already exists.");
                        return;
                }
@@ -111,26 +104,43 @@ const char* Control_SetMode(ControlModes desired_mode, void * arg)
        const char *ret = NULL;
 
        pthread_mutex_lock(&(g_controls.mutex));
-       if (g_controls.current_mode == CONTROL_EMERGENCY && desired_mode != CONTROL_STOP) {
-               ret = "In emergency mode. Stop before doing anything else.";
-       } else if (g_controls.current_mode == desired_mode) {
-               ret = "Already in desired mode.";
-       } else if (desired_mode == CONTROL_START) {
-               if (g_controls.current_mode == CONTROL_STOP) {
-                       //TODO Sanitise name (ensure it contains no special chars eg \ / .. . 
-                       FILE *fp = fopen((const char*) arg, "a");
-                       if (fp) {
-                               fclose(fp);
-                               gettimeofday(&(g_controls.start_time), NULL);
-                       } else {
-                               ret = "Cannot open experiment name marker";
-                       }
-               } else {
-                       ret = "Cannot start when not in a stopped state.";
-               }
-       } else if (desired_mode == CONTROL_RESUME) {
-               if (g_controls.current_mode != CONTROL_PAUSE)
-                       ret = "Cannot resume when not in a paused state.";
+       if (g_controls.current_mode == desired_mode)
+               ret = "Already in the desired mode.";
+       else if (g_controls.current_mode == CONTROL_EMERGENCY && desired_mode != CONTROL_STOP)
+               ret = "In emergency mode. You must stop before continuing.";
+       else switch (desired_mode) {
+               case CONTROL_START:
+                       if (g_controls.current_mode == CONTROL_STOP) {
+                               const char * name = arg;
+                               if (!*name)
+                                       ret = "An experiment name must be specified";
+                               else if (strpbrk(name, INVALID_CHARACTERS))
+                                       ret = "The experiment name must not contain: " INVALID_CHARACTERS_JSON;
+                               else {
+                                       FILE *fp = fopen((const char*) arg, "a");
+                                       if (fp) {
+                                               fclose(fp);
+                                               gettimeofday(&(g_controls.start_time), NULL);
+                                       } else
+                                               ret = "Cannot open experiment name marker";
+                               }
+                       } else 
+                               ret = "Cannot start when not in a stopped state.";
+               break;
+               case CONTROL_PAUSE:
+                       if (g_controls.current_mode != CONTROL_START)
+                               ret = "Cannot pause when not in a running state.";
+               break;
+               case CONTROL_RESUME:
+                       if (g_controls.current_mode != CONTROL_PAUSE)
+                               ret = "Cannot resume when not in a paused state.";
+               break;
+               case CONTROL_EMERGENCY:
+                       if (g_controls.current_mode != CONTROL_START) //pfft
+                               ret = "Not running so how can there be an emergency.";
+               break;
+               default:
+               break;
        }
        
        if (ret == NULL) {
@@ -146,22 +156,14 @@ const char* Control_SetMode(ControlModes desired_mode, void * arg)
 }
 
 /**
- * Gets the current mode.
- * @return The current mode
- */
-ControlModes Control_GetMode() {
-       return g_controls.current_mode;
-}
-
-/**
- * Gets a string representation of a mode
+ * Gets a string representation of the current mode
  * @param mode The mode to get a string representation of
  * @return The string representation of the mode
  */
-const char * Control_GetModeName(ControlModes mode) {
+const char * Control_GetModeName() {
        const char * ret = "Unknown";
 
-       switch (mode) {
+       switch (g_controls.current_mode) {
                case CONTROL_START: ret = "Running"; break;
                case CONTROL_PAUSE: ret = "Paused"; break;
                case CONTROL_RESUME: ret = "Resumed"; break;
@@ -171,23 +173,10 @@ const char * Control_GetModeName(ControlModes mode) {
        return ret;
 }
 
-/*
-bool Control_Lock() {
-       pthread_mutex_lock(&(g_controls.mutex));
-       if (g_controls.state == STATE_RUNNING || g_controls.state == STATE_PAUSED)
-               return true;
-       pthread_mutex_unlock(&(g_controls.mutex));
-       return false;
-}
-
-void Control_Unlock() {
-       pthread_mutex_unlock(&(g_controls.mutex));
-}*/
-
 /**
  * Gets the start time for the current experiment
  * @return the start time
  */
 const struct timeval* Control_GetStartTime() {
        return &g_controls.start_time;
-}
\ No newline at end of file
+}

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