X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Fcontrol.c;h=5c8a4103feceb3672c79aa0cde7b1298973854f4;hb=9fd77a6c5fdf277d6d5c17e3ced66f65f94a36f4;hp=7b07ea33557822b7b82ceb1f9fc50f56ba314e73;hpb=3a85bddfca280f46d675f4987615c65089c80f44;p=matches%2FMCTX3420.git diff --git a/server/control.c b/server/control.c index 7b07ea3..5c8a410 100644 --- a/server/control.c +++ b/server/control.c @@ -15,12 +15,13 @@ typedef struct ControlData { ControlData g_controls = {CONTROL_STOP, PTHREAD_MUTEX_INITIALIZER, {0}}; -static bool ExperimentExists(const char *experiment_name) { - FILE *fp = fopen(experiment_name, "r"); - if (!fp) - return false; - fclose(fp); - return true; +static bool PathExists(const char *path) { + FILE *fp = fopen(path, "r"); + if (fp) { + fclose(fp); + return true; + } + return false; } /** @@ -72,14 +73,11 @@ void Control_Handler(FCGIContext *context, char *params) { 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 (ExperimentExists(name) && !force) { + if (PathExists(name) && !force) { FCGI_RejectJSON(context, "An experiment with that name already exists."); return; } + arg = (void*)name; } @@ -104,61 +102,75 @@ 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) { - ret = "In emergency mode. Restart software."; - } 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) { - FILE *fp = fopen((const char*) arg, "a"); - if (fp) { - fclose(fp); - gettimeofday(&(g_controls.start_time), NULL); - } else { - ret = "Failed to create experiment placeholder"; - } - } 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) { Actuator_SetModeAll(desired_mode, arg); Sensor_SetModeAll(desired_mode, arg); - g_controls.current_mode = desired_mode; + if (desired_mode != CONTROL_RESUME) + g_controls.current_mode = desired_mode; + else + g_controls.current_mode = CONTROL_START; } pthread_mutex_unlock(&(g_controls.mutex)); return ret; } /** - * Gets the current mode. - * @return The current 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 */ -ControlModes Control_GetMode() { - ControlModes ret; - pthread_mutex_lock(&(g_controls.mutex)); - ret = g_controls.current_mode; - pthread_mutex_unlock(&(g_controls.mutex)); - return ret; -} +const char * Control_GetModeName() { + const char * ret = "Unknown"; -/* -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; + switch (g_controls.current_mode) { + case CONTROL_START: ret = "Running"; break; + case CONTROL_PAUSE: ret = "Paused"; break; + case CONTROL_RESUME: ret = "Resumed"; break; + case CONTROL_STOP: ret = "Stopped"; break; + case CONTROL_EMERGENCY: ret = "Emergency mode"; break; + } + return ret; } -void Control_Unlock() { - pthread_mutex_unlock(&(g_controls.mutex)); -}*/ - /** * Gets the start time for the current experiment * @return the start time