osm-rvb/api-ulfius/src/main.c

1130 lines
36 KiB
C

/*to create API*/
#include <ulfius.h>
/*to connect to Postgresql*/
#include <libpq-fe.h>
/* to use JSON in response body*/
#include <jansson.h>
/*to use strcat*/
#include <string.h>
/*to use inet_ntoa*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*to use print*/
#include <stdio.h>
/*to use sleep*/
#include <unistd.h>
/*to use struct pq_confo*/
#include "pqconfo.h"
/*to use file2char*/
#include "file2char.h"
/* to use pthread_mutex_t type*/
#include <pthread.h>
#define ROUTE_HELLO "/hello"
#define ROUTE_BIKE_RIDE "/bike-ride"
#define ROUTE_BUS_STOP "/bus-stop"
#define ROUTE_TAXI "/taxi"
#define ROUTE_TICKET_OFFICE "/ticket-office"
#define ROUTE_TICKET_MACHINE "/ticket-machine"
#define ROUTE_TRAIN_STATION "/train-station"
#define ROUTE_PARK_RIDE "/park-ride"
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
/**
* callback functions declaration
*/
int callback_get_hello (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_default (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_bus_stop_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_bike_ride_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_bike_ride_info (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_taxi_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_taxi_info (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_ticket_office_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_ticket_office_info (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_ticket_machine_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_ticket_machine_info (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_train_station_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_train_station_info (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_park_ride_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_park_ride_info (const struct _u_request * request, struct _u_response * response, void * user_data);
/**
* decode a u_map into a string
*/
char * print_map(const struct _u_map * map) {
/*declaration*/
char * line, * to_return = NULL;
const char **keys, * value;
int len, i;
if (map != NULL) {
keys = u_map_enum_keys(map);
for (i=0; keys[i] != NULL; i++) {
value = u_map_get(map, keys[i]);
len = snprintf(NULL, 0, "key is %s, value is %s", keys[i], value);
line = malloc((size_t)(len+1));
snprintf(line, (size_t)(len+1), "key is %s, value is %s", keys[i], value);
if (to_return != NULL) {
len = (int)(strlen(to_return) + strlen(line) + 1);
to_return = realloc(to_return, (size_t)(len+1));
if (strlen(to_return) > 0) {
//concatenate strings
strcat(to_return, "\n");
}
} else {
to_return = malloc((strlen(line) + 1));
to_return[0] = 0;
}
strcat(to_return, line);
free(line);
}
return to_return;
} else {
return NULL;
}
}
int main(int argc, char *argv[]) {
/*declaration*/
struct _u_instance instance;
int frameworkRet, apiPort, apiPortRet, cx;
/*TODO How is const defined?*/
const int pqConInfoSize = 100;
char pqConInfo[pqConInfoSize];
int pqConfoRet;
const struct pq_confo * pqConfo;
printf("main() Started...\n");
if (argc != 3) {
fprintf(stderr, "Usage: ./main <API port> <path to config file>\n");
return 1;
}
printf("main() argv[1]: %s\n", argv[1]);
printf("main() argv[2]: %s\n", argv[2]);
/*get Postgresql connection info from config*/
/*TODO Validate argv!*/
pqConfoRet=get_pq_confo(argv[2],&pqConfo);
if(pqConfoRet){
fprintf(stderr, "Postgresql connection information are NOT valid\n");
return 2;
}else{
fprintf(stdout,"pqConfoRet: %d\n",pqConfoRet);
}
/*create pq connection info*/
cx = snprintf(pqConInfo,pqConInfoSize,"postgresql://%s:%s@%s:%d/%s",pqConfo->user,pqConfo->secret,pqConfo->host,pqConfo->port,pqConfo->db);
if(0>cx && cx>=pqConInfoSize){
fprintf(stderr,"main() error creating pqConInfo\n");
/*free Postgresql connection info*/
pqConfoRet=free_pq_confo(&pqConfo);
if(pqConfoRet){
fprintf(stderr, "Postgresql connection information are NOT free\n");
return 3;
}
return 4;
}
/*free Postgresql connection info*/
pqConfoRet=free_pq_confo(&pqConfo);
if(pqConfoRet){
fprintf(stderr, "Postgresql connection information are NOT free\n");
return 5;
}
/*store command line argument in variable*/
/*validate user input*/
/*omit injection*/
/*stream:argv*/
apiPortRet = sscanf(argv[1], "%d", &apiPort);
/*valid user input:1 successfully filled item*/
if (apiPortRet != 1) {
fprintf(stderr, "The argument must be an integer\n");
return 6;
}
if (apiPort < 0) {
fprintf(stderr, "Error passing a negative port\n");
exit(1);
}
printf("main() port: %d\n", apiPort);
/*TODO Is mem leaked when connection to pq failes?*/
if (ulfius_init_instance(&instance, apiPort, NULL, NULL) != U_OK) {
printf("main() Error ulfius_init_instance, abort");
return 7;
}
/*connect to database*/
//TODO This is a synchronous call! Shall we block the rest?
PGconn *conn = PQconnectdb(pqConInfo);
if (PQstatus(conn) == CONNECTION_BAD) {
fprintf(stderr, "Connection to database failed: %s\n",
PQerrorMessage(conn));
PQfinish(conn);
return 8;
}else if(PQstatus(conn)==CONNECTION_OK){
printf("main() connected to database\n");
}else{
printf("main() connection status NOT known\n");
return 9;
}
//TODO Why?
u_map_put(instance.default_headers, "Access-Control-Allow-Origin", "*");
// Maximum body size sent by the client is 1 Kb
instance.max_post_body_size = 1024;
// Endpoint list declaration
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_HELLO, NULL, 0, &callback_get_hello, NULL);
//user_data: pass pq connection information to callback
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_BIKE_RIDE, "/count", 0, &callback_get_bike_ride_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_BIKE_RIDE, "/info", 0, &callback_get_bike_ride_info, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_BUS_STOP, "/count", 0, &callback_get_bus_stop_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TAXI, "/count", 0, &callback_get_taxi_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TAXI, "/info", 0, &callback_get_taxi_info, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TICKET_OFFICE, "/count", 0, &callback_get_ticket_office_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TICKET_OFFICE, "/info", 0, &callback_get_ticket_office_info, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TICKET_MACHINE, "/count", 0, &callback_get_ticket_machine_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TICKET_MACHINE, "/info", 0, &callback_get_ticket_machine_info, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TRAIN_STATION, "/count", 0, &callback_get_train_station_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TRAIN_STATION, "/info", 0, &callback_get_train_station_info, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_PARK_RIDE, "/count", 0, &callback_get_park_ride_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_PARK_RIDE, "/info", 0, &callback_get_park_ride_info, (void *)conn);
// default_endpoint declaration
ulfius_set_default_endpoint(&instance, &callback_default, NULL);
// Start the framework
// Open an http connection
frameworkRet = ulfius_start_framework(&instance);
if (frameworkRet == U_OK) {
printf("main() Start framework on port %d\n", instance.port);
/*wait*/
while(1){
sleep(3600);
}
} else {
printf("main() Error starting framework; frameworkRet: %d\n",frameworkRet);
}
printf("main() End framework\n");
/*clean up API*/
ulfius_stop_framework(&instance);
ulfius_clean_instance(&instance);
/*clean up Postgresql connection*/
PQfinish(conn);
printf("main() Done.\n");
return 0;
}
/**
* Callback function that put a "Hello World!" string in the response
*/
int callback_get_hello (const struct _u_request * request, struct _u_response * response, void * user_data) {
printf("callback_get_hello() Started...\n");
sleep(1);
ulfius_set_string_body_response(response, 200, "Hello World!");
printf("callback_get_hello() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
* Default callback function called if no endpoint has a match
*/
int callback_default (const struct _u_request * request, struct _u_response * response, void * user_data) {
printf("callback_default() Started...\n");
ulfius_set_string_body_response(response, 404, "Page not found, do what you want");
printf("callback_default() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
* Callback function to reply number of all bus stops
*/
int callback_get_bus_stop_count (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_bus_stop_count() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_bus_stop;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_bus_stop_count() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_bus_stop_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_bus_stop_count() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_bus_stop_count() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_bus_stop_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_train_station_count (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_train_station_count() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_railway;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_train_station_count() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_train_station_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_train_station_count() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_train_station_count() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_train_station_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_park_ride_count (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_park_ride_count() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_park_ride;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_park_ride_count() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_park_ride_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_park_ride_count() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_park_ride_count() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_park_ride_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
* Callback function to reply all train stations
*/
int callback_get_train_station_info (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_train_station_info() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT osm_id AS id, railway AS tag, name, st_x AS lon, st_y AS lat FROM rvb.tbl_railway;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_train_station_info() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_train_station_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_train_station_info() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_train_station_info() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_train_station_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_park_ride_info (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_park_ride_info() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT osm_id AS id, park_ride AS tag, name, st_x AS lon, st_y AS lat FROM rvb.tbl_park_ride;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_park_ride_info() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_park_ride_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_park_ride_info() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_park_ride_info() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_park_ride_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_bike_ride_info (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_bike_ride_info() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT osm_id AS id, bike_ride AS tag, name, st_x AS lon, st_y AS lat FROM rvb.tbl_bike_ride;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_bike_ride_info() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_bike_ride_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_bike_ride_info() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_bike_ride_info() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_bike_ride_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_bike_ride_count (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_bike_ride_count() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_bike_ride;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_bike_ride_count() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_bike_ride_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_bike_ride_count() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_bike_ride_count() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_bike_ride_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_taxi_info (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_taxi_info() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT osm_id AS id, amenity AS tag, name, st_x AS lon, st_y AS lat FROM rvb.tbl_taxi;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_taxi_info() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_taxi_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_taxi_info() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_taxi_info() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_taxi_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_ticket_office_count (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_ticket_office_count() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_shop_ticket;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_ticket_office_count() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_ticket_office_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_ticket_office_count() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_ticket_office_count() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_ticket_office_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_ticket_machine_count (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_ticket_machine_count() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_vending_machine;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_ticket_machine_count() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_ticket_machine_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_ticket_machine_count() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_ticket_machine_count() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_ticket_machine_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_ticket_office_info (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_ticket_office_info() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT osm_id AS id, shop AS tag, name, st_x AS lon, st_y AS lat FROM rvb.tbl_shop_ticket;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_ticket_office_info() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_ticket_office_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_ticket_office_info() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_ticket_office_info() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_ticket_office_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_ticket_machine_info (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_ticket_machine_info() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT osm_id AS id, amenity AS tag, name, st_x AS lon, st_y AS lat FROM rvb.tbl_vending_machine;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_ticket_machine_info() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_ticket_machine_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_ticket_machine_info() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_ticket_machine_info() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_ticket_machine_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_taxi_count (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_taxi_count() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_taxi;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_taxi_count() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_taxi_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_taxi_count() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_taxi_count() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_taxi_count() Done.\n");
return U_CALLBACK_CONTINUE;
}