Esempio n. 1
0
void vz::api::Volkszaehler::send()
{
	CURLresponse response;
	json_object *json_obj;

	const char *json_str;
	long int http_code;
	CURLcode curl_code;

	/* initialize response */
	response.data = NULL;
	response.size = 0;

	json_obj = api_json_tuples(channel()->buffer());
	json_str = json_object_to_json_string(json_obj);
	if (json_str == NULL || strcmp(json_str, "null")==0) {
		print(log_debug, "JSON request body is null. Nothing to send now.", channel()->name());
		return;
	}

	print(log_debug, "JSON request body: %s", channel()->name(), json_str);

	curl_easy_setopt(curl(), CURLOPT_POSTFIELDS, json_str);
	curl_easy_setopt(curl(), CURLOPT_WRITEFUNCTION, curl_custom_write_callback);
	curl_easy_setopt(curl(), CURLOPT_WRITEDATA, (void *) &response);

	curl_code = curl_easy_perform(curl());
	curl_easy_getinfo(curl(), CURLINFO_RESPONSE_CODE, &http_code);

	/* check response */
	if (curl_code == CURLE_OK && http_code == 200) { /* everything is ok */
		print(log_debug, "CURL Request succeeded with code: %i", channel()->name(), http_code);
		_values.clear();
		//clear buffer-readings
//channel()->buffer.sent = last->next;
	}
	else { /* error */
		if (curl_code != CURLE_OK) {
			print(log_error, "CURL: %s", channel()->name(), curl_easy_strerror(curl_code));
		}
		else if (http_code != 200) {
			char err[255];
			api_parse_exception(response, err, 255);
			print(log_error, "CURL Error from middleware: %s", channel()->name(), err);
		}
	}

	/* householding */
	free(response.data);
	json_object_put(json_obj);

	if (options.daemon() && (curl_code != CURLE_OK || http_code != 200)) {
		print(log_info, "Waiting %i secs for next request due to previous failure",
					channel()->name(), options.retry_pause());
		sleep(options.retry_pause());
	}
}
Esempio n. 2
0
int handle_request(void *cls, struct MHD_Connection *connection, const char *url, const char *method,
			const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) {
	const char * json_str;
	int ret;
	int num_chans = *(int *) cls;
	print(2, "Local request received: %s %s %s", NULL, version, method, url);
	
	struct MHD_Response *response;
	
	struct json_object *json_obj = json_object_new_object();
	struct json_object *json_data = json_object_new_object();

	for (int i = 0; i < num_chans; i++) {
		channel_t *ch = &chans[i];
		reading_t rd;
		
		if (strcmp(url, "/") == 0 || strcmp(ch->uuid, url + 1) == 0) {
			pthread_mutex_lock(&ch->mutex);
				/* wait for new data comet-like blocking of HTTP response */
				pthread_cond_wait(&ch->condition, &ch->mutex); // TODO use pthread_cond_timedwait()
			pthread_mutex_unlock(&ch->mutex);
		
			struct json_object *json_tuples = api_json_tuples(ch, TRUE);

			json_object_object_add(json_data, "uuid", json_object_new_string(ch->uuid));
			json_object_object_add(json_data, "interval", json_object_new_int(ch->interval));
			json_object_object_add(json_data, "tuples", json_tuples);
		}
	}
	
	json_object_object_add(json_obj, "version", json_object_new_string(VZ_VERSION));
	json_object_object_add(json_obj, "generator", json_object_new_string("vzlogger"));
	json_object_object_add(json_obj, "data", json_data);
	json_str = json_object_to_json_string(json_obj);
	
	response = MHD_create_response_from_data(strlen(json_str), (void *) json_str, FALSE, TRUE);
	
	MHD_add_response_header(response, "Content-type", "application/json");
	
	ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
	
	MHD_destroy_response (response);

	return ret;
}