#include "control.h"
/**Converts a timeval to a double**/
-#define TIMEVAL_TO_DOUBLE(tv) ((tv).tv_sec + 1e-6 * ((tv).tv_usec))
+#define TIMEVAL_TO_DOUBLE(tv) ((tv).tv_sec + 1e-9 * ((tv).tv_nsec))
/**Takes the tv1-tv2 between two timevals and returns the result as a double*/
-#define TIMEVAL_DIFF(tv1, tv2) ((tv1).tv_sec - (tv2).tv_sec + 1e-6 * ((tv1).tv_usec - (tv2).tv_usec))
-
+#define TIMEVAL_DIFF(tv1, tv2) ((tv1).tv_sec - (tv2).tv_sec + 1e-9 * ((tv1).tv_nsec - (tv2).tv_nsec))
+/** Converts a double time value (in seconds) to a timespec **/
+#define DOUBLE_TO_TIMEVAL(value, tv) { \
+ (tv)->tv_sec = (int)(value); \
+ (tv)->tv_nsec = ((value) - (int)(value))*1e9; \
+ }
extern bool PathExists(const char * path);
+ extern bool DirExists(const char * path);
typedef struct ControlData {
ControlModes current_mode;
pthread_mutex_t mutex;
- struct timeval start_time;
+ struct timespec start_time;
char user_name[31]; // The user who owns the currently running experiment
+ char experiment_dir[BUFSIZ]; //Directory for experiment
+ char experiment_name[BUFSIZ];
} ControlData;
ControlData g_controls = {CONTROL_STOP, PTHREAD_MUTEX_INITIALIZER, {0}};
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);
- clock_gettime(CLOCK_MONOTONIC, &(g_controls.start_time));
- } else
- ret = "Cannot open experiment name marker";
+ const char * path = arg;
+ if (mkdir(path, 0777) != 0 && errno != EEXIST) {
+ Log(LOGERR, "Couldn't create experiment directory %s - %s",
+ path, strerror(errno));
+ ret = "Couldn't create experiment directory.";
+ } else {
- gettimeofday(&(g_controls.start_time), NULL);
++ clock_gettime(CLOCK_MONOTONIC, &(g_controls.start_time));
}
} else
ret = "Cannot start when not in a stopped state.";
if (module_handler)
{
- if (module_handler != Login_Handler && module_handler != IdentifyHandler && module_handler)
- //if (false) // Testing
+ //if (module_handler != Login_Handler && module_handler != IdentifyHandler && module_handler)
+ if (false) // Testing
{
- if (!FCGI_HasControl(&context, cookie))
+ if (!FCGI_HasControl(&context, control_key))
{
FCGI_RejectJSON(&context, "Please login. Invalid control key.");
continue;
g_options.program = argv[0]; // program name
g_options.verbosity = LOGDEBUG; // default log level
// Set the main directory
- if (getcwd(g_options.root_dir, sizeof(g_options.root_dir)) == NULL)
- Fatal("Couldn't get current working directory - %s", strerror(errno));
+ //if (getcwd(g_options.root_dir, sizeof(g_options.root_dir)) == NULL)
+ // Fatal("Couldn't get current working directory - %s", strerror(errno));
- gettimeofday(&(g_options.start_time), NULL); // Start time
+ clock_gettime(CLOCK_MONOTONIC, &(g_options.start_time)); // Start time
g_options.auth_method = AUTH_NONE; // Don't use authentication
*/
void Sensor_Handler(FCGIContext *context, char * params)
{
- struct timeval now;
- gettimeofday(&now, NULL);
+ struct timespec now;
+ clock_gettime(CLOCK_MONOTONIC, &now);
double current_time = TIMEVAL_DIFF(now, *Control_GetStartTime());
-
int id = 0;
const char * name = "";
double start_time = 0;