Fix minor typo
[matches/MCTX3420.git] / testing / fastcgi-approach / main.c
1 #define _BSD_SOURCE
2 #include "fastcgi.h"
3 #include "common.h"
4 #include <time.h>
5
6 typedef struct Data {
7         pthread_t sensors;
8         pthread_mutex_t mutex;
9         volatile int sensor_value;
10 } Data;
11
12 Data data;
13
14 void Handler_Sensors(FCGIContext *context, char *params) 
15 {
16         const char *key, *value;
17
18         //Begin a request only when you know the final result
19         //E.g whether OK or not.
20         FCGI_BeginJSON(context, STATUS_OK);
21         while ((params = FCGI_KeyPair(params, &key, &value))) {
22                 FCGI_JSONPair(key, value);
23         }
24         pthread_mutex_lock(&(data.mutex));
25         FCGI_JSONLong("sensor_value", data.sensor_value);
26         pthread_mutex_unlock(&(data.mutex));
27         FCGI_EndJSON();
28 }
29
30 void *SensorsLoop(void *arg) {
31         srand(time(NULL));
32         //Csection
33         while(1) {
34                 pthread_mutex_lock(&(data.mutex));
35                 data.sensor_value = rand() % 1024;
36                 pthread_mutex_unlock(&(data.mutex));
37                 usleep(200*1000); //200ms
38         }
39 }
40
41 int main(int argc, char *argv[]) {
42         pthread_mutex_init(&(data.mutex), NULL);
43         //data.mutex = PTHREAD_MUTEX_INITIALIZER;
44
45         pthread_create(&data.sensors, NULL, SensorsLoop, &data);
46         FCGI_RequestLoop(&data);
47 }

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