added cleanup routines

This commit is contained in:
Steffen Vogel 2011-08-12 22:13:56 +02:00
parent ca0c497835
commit 17ce30ef58
2 changed files with 25 additions and 22 deletions

View file

@ -165,6 +165,7 @@ void api_parse_exception(CURLresponse response, char *err) {
strcpy(err, json_tokener_errors[json_tok->err]);
}
json_object_put(json_obj);
json_tokener_free(json_tok);
}
@ -184,8 +185,9 @@ void * api_thread(void *arg) {
do { /* start thread mainloop */
CURLresponse response;
long int http_code, curl_code;
json_object *json_obj;
char *json_str;
long int http_code, curl_code;
/* initialize response */
response.data = NULL;
@ -196,9 +198,11 @@ void * api_thread(void *arg) {
pthread_cond_wait(&ch->condition, &ch->mutex); /* sleep until new data has been read */
}
pthread_mutex_unlock(&ch->mutex);
json_str = json_object_to_json_string(api_json_tuples(ch, FALSE));
print(1, "JSON request body: %s", ch, json_str);
json_obj = api_json_tuples(ch, FALSE);
json_str = json_object_to_json_string(json_obj);
print(8, "JSON request body: %s", ch, json_str);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_str);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_custom_write_callback);
@ -208,7 +212,7 @@ void * api_thread(void *arg) {
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if (curl_code == CURLE_OK && http_code == 200) { /* everything is ok */
print(1, "Request succeeded with code: %i", ch, http_code);
print(3, "Request succeeded with code: %i", ch, http_code);
queue_clear(&ch->queue);
}
else { /* error */
@ -226,9 +230,8 @@ void * api_thread(void *arg) {
}
/* householding */
free(json_str);
// TODO free json objects
free(response.data);
json_object_put(json_obj);
pthread_testcancel(); /* test for cancelation request */
} while (opts.daemon);

View file

@ -34,8 +34,8 @@
#include <getopt.h>
#ifdef LOCAL
#include <microhttpd.h>
#include "local.h"
#include <microhttpd.h>
#include "local.h"
#endif
#include "main.h"
@ -52,12 +52,12 @@
* incl. function pointers
*/
static protocol_t protocols[] = {
{"1wire", "Dallas 1-Wire sensors (via OWFS)", onewire_get, onewire_init, onewire_close, MODE_SENSOR},
// {"obis", "Plaintext OBIS", obis_get, obis_init, obis_close, MODE_SENSOR},
{"random", "Random walk", random_get, random_init, random_close, MODE_SENSOR},
{"rawS0", "S0 on RS232", rawS0_get, rawS0_init, rawS0_close, MODE_METER},
// {"sml", "Smart Meter Language", sml_get, sml_init, sml_close, MODE_SENSOR},
// {"fluksousb", "FluksoUSB board", flukso_get, flukso_init, flukso_close, MODE_SENSOR},
{"1wire", "Dallas 1-Wire sensors (via OWFS)", onewire_get, onewire_init, onewire_close, SENSOR},
// {"obis", "Plaintext OBIS", obis_get, obis_init, obis_close, SENSOR},
{"random", "Random walk", random_get, random_init, random_close, SENSOR},
{"rawS0", "S0 on RS232", rawS0_get, rawS0_init, rawS0_close, METER},
// {"sml", "Smart Meter Language", sml_get, sml_init, sml_close, SENSOR},
// {"fluksousb", "FluksoUSB board", flukso_get, flukso_init, flukso_close, SENSOR},
{NULL} /* stop condition for iterator */
};
@ -107,7 +107,7 @@ options_t opts = { /* setting default options */
};
/**
* Print availble options and some other usefull information
* Print available options and some other usefull information
*/
void usage(char * argv[]) {
char ** desc = long_options_descs;
@ -242,13 +242,13 @@ void parse_options(int argc, char * argv[], options_t * opts) {
}
int parse_channels(char *filename, channel_t *chans) {
if (filename == NULL) {
fprintf(stderr, "No config file found! Please specify with --config!\n");
FILE *file = fopen(filename, "r"); /* open configuration */
if (!filename) { /* nothing found */
print(-1, "No config file found! Please specify with --config!\n", NULL);
exit(EXIT_FAILURE);
}
FILE *file = fopen(filename, "r"); /* open configuration */
if (file == NULL) {
perror(filename); /* why didn't the file open? */
exit(EXIT_FAILURE);
@ -354,7 +354,7 @@ void *read_thread(void *arg) {
do {
/**
* Aquire reading,
* may be blocking if mode == MODE_METER
* may be blocking if interpreter == METER
*/
reading_t rd = ch->prot->read_func(ch->handle);
@ -374,7 +374,7 @@ void *read_thread(void *arg) {
free(queue_str);
}
if (ch->prot->mode != MODE_METER) { /* for meters, the read_func call is blocking */
if (ch->prot->interpreter != METER) { /* for meters, the read_func call is blocking */
print(5, "Next reading in %i seconds", ch, ch->interval);
sleep(ch->interval); /* else sleep and restart aquisition */
}