X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=testing%2Ffastcgi-approach%2Fmain.c;h=5076f455e8be60795720c35fa0e0a0af65f326f4;hb=a671f27ecb900f91bf32b8b13edb678009e319c1;hp=f5f7ae39e3846563b2ca6d308214aa88d9b70511;hpb=a6ae915f96e9769fc6f67deb4ddfaa7e6efcf464;p=matches%2FMCTX3420.git diff --git a/testing/fastcgi-approach/main.c b/testing/fastcgi-approach/main.c index f5f7ae3..5076f45 100644 --- a/testing/fastcgi-approach/main.c +++ b/testing/fastcgi-approach/main.c @@ -1,6 +1,6 @@ #define _BSD_SOURCE -#include "common.h" #include "fastcgi.h" +#include "common.h" #include typedef struct Data { @@ -9,42 +9,39 @@ typedef struct Data { volatile int sensor_value; } Data; -void Handler_Sensors(void *arg, char *params) +Data data; + +void Handler_Sensors(FCGIContext *context, char *params) { const char *key, *value; - char buf[5]; - Data *data = (Data*) arg; - + //Begin a request only when you know the final result //E.g whether OK or not. - FCGI_BeginJSON(STATUS_OK, "sensors"); + FCGI_BeginJSON(context, STATUS_OK); while ((params = FCGI_KeyPair(params, &key, &value))) { - FCGI_BuildJSON(key, value); + FCGI_JSONPair(key, value); } - pthread_mutex_lock(&(data->mutex)); - snprintf(buf, 128, "%d", data->sensor_value); - FCGI_BuildJSON("sensor_value", buf); - pthread_mutex_unlock(&(data->mutex)); + pthread_mutex_lock(&(data.mutex)); + FCGI_JSONLong("sensor_value", data.sensor_value); + pthread_mutex_unlock(&(data.mutex)); FCGI_EndJSON(); } void *SensorsLoop(void *arg) { - Data *data = (Data*) arg; srand(time(NULL)); //Csection while(1) { - pthread_mutex_lock(&(data->mutex)); - data->sensor_value = rand() % 1024; - pthread_mutex_unlock(&(data->mutex)); + pthread_mutex_lock(&(data.mutex)); + data.sensor_value = rand() % 1024; + pthread_mutex_unlock(&(data.mutex)); usleep(200*1000); //200ms } } int main(int argc, char *argv[]) { - Data data = {0}; pthread_mutex_init(&(data.mutex), NULL); //data.mutex = PTHREAD_MUTEX_INITIALIZER; - -// pthread_create(&data.sensors, NULL, SensorsLoop, &data); + + pthread_create(&data.sensors, NULL, SensorsLoop, &data); FCGI_RequestLoop(&data); }