Permalink
Browse files

MB-3575 - Handle error result from new_config() callback

Change-Id: I135f42976e637b20218f2b8affca72f05d05642f
Reviewed-on: http://review.membase.org/6343
Tested-by: Steve Yen <steve.yen@gmail.com>
Reviewed-by: Bin Cui <bin.cui@gmail.com>
  • Loading branch information...
1 parent a3d7ce4 commit fee8f9463b61931f303a1ee2727016b73a5fa37e @steveyen steveyen committed with bcui6611 May 18, 2011
Showing with 43 additions and 13 deletions.
  1. +14 −1 conflate.h
  2. +1 −1 conflate_internal.h
  3. +3 −1 examples/bot.c
  4. +22 −9 rest.c
  5. +3 −1 tests/check_xmpp.c
View
@@ -367,14 +367,25 @@ void conflate_stderr_logger(void *, enum conflate_log_level,
*/
/**
+ * Conflate result codes.
+ */
+typedef enum {
+ CONFLATE_SUCCESS,
+ CONFLATE_ERROR,
+ CONFLATE_ERROR_BAD_SOURCE
+} conflate_result;
+
+/**
* Configuration for a conflatee.
*/
typedef struct {
/** The XMPP JID (typically excludes a resource definition) */
char *jid;
+
/** The XMPP Password */
char *pass;
+
/**
* The XMPP server address (may be NULL).
*
@@ -428,8 +439,10 @@ typedef struct {
*
* The new config *may* be the same as the previous config. It's
* up to the client to detect and decide what to do in this case.
+ *
+ * The callback should return CONFLATE_SUCCESS on success.
*/
- void (*new_config)(void*, kvpair_t*);
+ conflate_result (*new_config)(void*, kvpair_t*);
/** \private */
void *initialization_marker;
View
@@ -16,7 +16,7 @@ struct _conflate_handle {
conflate_config_t *conf;
- alarm_queue_t *alarms;
+ alarm_queue_t *alarms;
pthread_t thread;
View
@@ -34,12 +34,14 @@ static bool config_visitor(void *opaque, const char *key, const char **values)
/*
* Example callback to handle a newly receive configuration.
*/
-static void display_config(void* userdata, kvpair_t* conf)
+static conflate_result display_config(void* userdata, kvpair_t* conf)
{
printf("Hey. I received a new config (userdata: %s):\n",
(char*)userdata);
walk_kvpair(conf, NULL, config_visitor);
+
+ return CONFLATE_SUCCESS;
}
/*
View
31 rest.c
@@ -124,7 +124,7 @@ static bool pattern_ends_with(const char *pattern, const char *target, size_t ta
return memcmp(&target[target_size - pattern_size], pattern, pattern_size) == 0;
}
-static void process_new_config(conflate_handle_t *conf_handle) {
+static conflate_result process_new_config(conflate_handle_t *conf_handle) {
g_tot_process_new_configs++;
//construct the new config from its components
@@ -138,7 +138,7 @@ static void process_new_config(conflate_handle_t *conf_handle) {
if (values[0] == NULL) {
fprintf(stderr, "ERROR: invalid response from REST server\n");
- return;
+ return CONFLATE_ERROR;
}
kvpair_t *kv = mk_kvpair(CONFIG_KEY, values);
@@ -151,15 +151,18 @@ static void process_new_config(conflate_handle_t *conf_handle) {
}
//execute the provided call back
- void (*call_back)(void *, kvpair_t *) = conf_handle->conf->new_config;
- call_back(conf_handle->conf->userdata, kv);
+ conflate_result (*call_back)(void *, kvpair_t *) = conf_handle->conf->new_config;
+
+ conflate_result r = call_back(conf_handle->conf->userdata, kv);
//clean up
free_kvpair(kv);
free(values[0]);
response_buffer_head = mk_response_buffer(RESPONSE_BUFFER_SIZE);
cur_response_buffer = response_buffer_head;
+
+ return r;
}
static size_t handle_response(void *data, size_t s, size_t num, void *cb) {
@@ -271,7 +274,7 @@ void *run_rest_conflate(void *arg) {
char *userpass = NULL;
if (handle->conf->jid && strlen(handle->conf->jid)) {
size_t buff_size = strlen(handle->conf->jid) + strlen(handle->conf->pass) + 2;
- userpass = (char *)malloc(buff_size);
+ userpass = (char *) malloc(buff_size);
assert(userpass);
snprintf(userpass, buff_size, "%s:%s", handle->conf->jid, handle->conf->pass);
userpass[buff_size - 1] = '\0';
@@ -291,11 +294,21 @@ void *run_rest_conflate(void *arg) {
/* We reach here if the REST server didn't provide a
streaming JSON response and so we need to process
the just-one-JSON response */
- process_new_config(handle);
- succeeding = true;
- next = NULL; // Restart at the beginning of the urls list.
+ conflate_result r = process_new_config(handle);
+ if (r == CONFLATE_SUCCESS ||
+ r == CONFLATE_ERROR) {
+ // Restart at the beginning of the urls list
+ // on either a success or a 'local' error.
+ // In contrast, if the callback returned a
+ // value of CONFLATE_ERROR_BAD_SOURCE, then
+ // we should try the next url on the list.
+ //
+ succeeding = true;
+ next = NULL;
+ }
} else {
- fprintf(stderr, "WARNING: curl error: %s\n", curl_error_string);
+ fprintf(stderr, "WARNING: curl error: %s from: %s\n",
+ curl_error_string, url);
}
}
View
@@ -6,8 +6,10 @@
#include <conflate.h>
-static void conf_cb(__attribute__((unused))void* userdata, kvpair_t* __attribute__((unused))conf)
+static conflate_result conf_cb(__attribute__((unused))void* userdata,
+ kvpair_t* __attribute__((unused))conf)
{
+ return CONFLATE_SUCCESS;
}
static void log_cb(__attribute__((unused))void *userdata,

0 comments on commit fee8f94

Please sign in to comment.